Skip to content

Commit 30c4178

Browse files
committed
Add command, templates and tests
1 parent 11a9668 commit 30c4178

7 files changed

Lines changed: 89 additions & 12 deletions

File tree

Migrate/Command/AbstractEnvCommand.php

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -69,10 +69,6 @@ protected function init(InputInterface $input, OutputInterface $output, $env = n
6969

7070
$parser = $configLocator->locate($env);
7171

72-
if (is_null($parser)) {
73-
throw new \RuntimeException("Could not locate supported environment file");
74-
}
75-
7672
$conf = $parser->parse();
7773

7874
$this->config = $conf;

Migrate/Command/AddEnvCommand.php

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77

88
namespace Migrate\Command;
99

10+
use Migrate\Config\ConfigLocator;
11+
use Symfony\Component\Console\Input\InputArgument;
1012
use Symfony\Component\Console\Input\InputInterface;
1113
use Symfony\Component\Console\Output\OutputInterface;
1214
use Symfony\Component\Console\Question\ChoiceQuestion;
@@ -19,11 +21,26 @@ protected function configure()
1921
$this
2022
->setName('migrate:addenv')
2123
->setDescription('Initialise an environment to work with php db migrate')
24+
->addArgument(
25+
'format',
26+
InputArgument::OPTIONAL,
27+
'Environment file format: (yml, json or php), default: yml'
28+
)
2229
;
2330
}
2431

2532
protected function execute(InputInterface $input, OutputInterface $output)
2633
{
34+
$format = $input->getArgument('format');
35+
$supportedFormats = array_keys(ConfigLocator::$SUPPORTED_PARSERS);
36+
37+
if (is_null($format)) {
38+
$format = 'yml';
39+
}
40+
41+
if (!in_array($format, $supportedFormats)) {
42+
throw new \RuntimeException(sprintf('Invalid file format: %s', $format));
43+
}
2744

2845
// init directories
2946
if(! file_exists($this->getMainDir())) {
@@ -46,7 +63,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
4663
$envQuestion = new Question("Please enter the name of the new environment <info>(default dev)</info>: ", "dev");
4764
$envName = $questions->ask($input, $output, $envQuestion );
4865

49-
$envConfigFile = $this->getEnvironmentDir() . '/' . $envName . '.yml';
66+
$envConfigFile = $this->getEnvironmentDir() . '/' . $envName . '.' . $format;
5067
if (file_exists($envConfigFile)) {
5168
throw new \InvalidArgumentException("environment [$envName] is already defined!");
5269
}
@@ -75,7 +92,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
7592
$defaultEditorQuestion = new Question("Please enter the text editor to use by default <info>(default vim)</info>: ", "vim");
7693
$defaultEditor = $questions->ask($input, $output, $defaultEditorQuestion);
7794

78-
$confTemplate = file_get_contents(__DIR__ . '/../../templates/env.yml.tpl');
95+
$confTemplate = file_get_contents(__DIR__ . '/../../templates/env.' . $format . '.tpl');
7996
$confTemplate = str_replace('{DRIVER}', $driver, $confTemplate);
8097
$confTemplate = str_replace('{HOST}', $dbHost, $confTemplate);
8198
$confTemplate = str_replace('{PORT}', $dbPort, $confTemplate);

Migrate/Config/ConfigLocator.php

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,11 @@
1010

1111
class ConfigLocator
1212
{
13-
static $SUPPORTED_PARSERS = [
14-
'yaml' => '\Migrate\Config\YamlConfigParser',
13+
public static $SUPPORTED_PARSERS = array(
14+
'yml' => '\Migrate\Config\YamlConfigParser',
1515
'json' => '\Migrate\Config\JsonConfigParser',
1616
'php' => '\Migrate\Config\PhpConfigParser'
17-
];
17+
);
1818

1919
private $configPath;
2020

@@ -32,6 +32,8 @@ public function locate($nameWithoutExt)
3232
return new $parserClass($path);
3333
}
3434
}
35-
return null;
35+
throw new \RuntimeException(
36+
sprintf('Environment file %s does not exist or file not supported', $nameWithoutExt)
37+
);
3638
}
3739
}

templates/env.json.tpl

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"connection": {
3+
"host": "{HOST}",
4+
"driver": "{DRIVER}",
5+
"port": "{PORT}",
6+
"username": "{USERNAME}",
7+
"password": "{PASSWORD}",
8+
"database": "{DATABASE}"
9+
},
10+
"changelog": "{CHANGELOG}",
11+
"default_editor": "{EDITOR}"
12+
}

templates/env.php.tpl

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
3+
return [
4+
"connection" => [
5+
"host" => "{HOST}",
6+
"driver" => "{DRIVER}",
7+
"port" => "{PORT}",
8+
"username" => "{USERNAME}",
9+
"password" => "{PASSWORD}",
10+
"database" => "{DATABASE}"
11+
],
12+
"changelog" => "{CHANGELOG}",
13+
"default_editor" => "{EDITOR}"
14+
];

tests/Command/AbstractCommandTester.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public function cleanEnv()
3434
}
3535
}
3636

37-
public function createEnv()
37+
public function createEnv($format = 'yml')
3838
{
3939
$application = new Application();
4040
$application->add(new AddEnvCommand());
@@ -49,7 +49,7 @@ public function createEnv()
4949
$question = $command->getHelper('question');
5050
$question->setInputStream(InputStreamUtil::type("testing\n$driverKey\ntest.sqlite\n\n\n\n\nchangelog\nvim\n"));
5151

52-
$commandTester->execute(array('command' => $command->getName()));
52+
$commandTester->execute(array('command' => $command->getName(), 'format' => $format));
5353
}
5454

5555
public function initEnv()

tests/Command/EnvFormatsTest.php

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
3+
namespace Migrate\Test\Command;
4+
5+
use SebastianBergmann\GlobalState\RuntimeException;
6+
7+
class EnvFormatsTest extends AbstractCommandTester
8+
{
9+
public function testYamlFormat()
10+
{
11+
$this->createEnv('yml');
12+
$this->cleanEnv();
13+
}
14+
15+
public function testJsonFormat()
16+
{
17+
$this->createEnv('json');
18+
$this->cleanEnv();
19+
}
20+
21+
public function testPhpFormat()
22+
{
23+
$this->createEnv('php');
24+
$this->cleanEnv();
25+
}
26+
27+
/**
28+
* @expectedException \RuntimeException
29+
* @expectedExceptionMessage Invalid file format: xml
30+
*/
31+
public function testUnsupportedXmlFormat()
32+
{
33+
$this->createEnv('xml');
34+
$this->cleanEnv();
35+
}
36+
}

0 commit comments

Comments
 (0)