From dba95ebc4ececb9df0535c5e9ac7504c0a59d744 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Gomes?= Date: Mon, 15 Jan 2018 18:33:46 +0000 Subject: [PATCH] Allow Database Reverse command to receive only a --config-dir parameter and read all the needed parameters from those configuration files. Making all command parameters optional. You can still replace configuration file settings by passing their related command parameters. You may need to change the documentation reflect this changes. --- .../Common/Config/PropelConfiguration.php | 1 + .../Command/DatabaseReverseCommand.php | 55 +++++++++-- tests/Fixtures/bookstore/propel.yaml.dist | 7 ++ .../Generator/Command/DatabaseReverseTest.php | 92 +++++++++++++++++-- 4 files changed, 141 insertions(+), 14 deletions(-) diff --git a/src/Propel/Common/Config/PropelConfiguration.php b/src/Propel/Common/Config/PropelConfiguration.php index 50841fc826..4539cd4271 100644 --- a/src/Propel/Common/Config/PropelConfiguration.php +++ b/src/Propel/Common/Config/PropelConfiguration.php @@ -212,6 +212,7 @@ protected function addReverseSection(ArrayNodeDefinition $node) ->arrayNode('reverse') ->children() ->scalarNode('connection')->end() + ->scalarNode('namespace')->defaultNull()->end() ->scalarNode('parserClass')->end() ->end() ->end() //reverse diff --git a/src/Propel/Generator/Command/DatabaseReverseCommand.php b/src/Propel/Generator/Command/DatabaseReverseCommand.php index 513922dc76..7c2b10448f 100644 --- a/src/Propel/Generator/Command/DatabaseReverseCommand.php +++ b/src/Propel/Generator/Command/DatabaseReverseCommand.php @@ -42,7 +42,7 @@ protected function configure() 'connection', InputArgument::OPTIONAL, 'Connection name or dsn to use. Example: \'mysql:host=127.0.0.1;dbname=test;user=root;password=foobar\' (don\'t forget the quote for dsn)', - 'default' + NULL ) ->setName('database:reverse') ->setAliases(['reverse']) @@ -58,13 +58,13 @@ protected function execute(InputInterface $input, OutputInterface $output) $configOptions = []; $connection = $input->getArgument('connection'); - if (false === strpos($connection, ':')) { + if ($connection !== NULL && false === strpos($connection, ':')) { //treat it as connection name $configOptions['propel']['reverse']['connection'] = $connection; if (!$input->getOption('database-name')) { $input->setOption('database-name', $connection); } - } else { + } else if ($connection !== NULL) { //probably a dsn $configOptions += $this->connectionToProperties('reverseconnection=' . $connection, 'reverse'); $configOptions['propel']['reverse']['parserClass'] = sprintf( @@ -76,9 +76,46 @@ protected function execute(InputInterface $input, OutputInterface $output) $input->setOption('database-name', self::DEFAULT_DATABASE_NAME); } } + + if ($input->getOption('namespace')) { + $configOptions['propel']['reverse']['namespace'] = $input->getOption('namespace'); + } + if ($input->getOption('output-dir') !== self::DEFAULT_OUTPUT_DIRECTORY) { + $configOptions['propel']['paths']['schemaDir'] = $input->getOption('output-dir'); + } + + if ($input->getOption('schema-name') !== self::DEFAULT_SCHEMA_NAME) { + $configOptions['propel']['generator']['schema']['basename'] = $input->getOption('schema-name'); + } + $generatorConfig = $this->getGeneratorConfig($configOptions, $input); - $this->createDirectory($input->getOption('output-dir')); + $outputDir = $generatorConfig->getConfigProperty('paths.schemaDir'); + $shemaName = $generatorConfig->getConfigProperty('generator.schema.basename'); + if (empty($outputDir) || empty($shemaName)) { + // if output dir or shema name is still empty, we have to set them + // to his default values, in order to keep the previous + // behaviour of the script + if (empty($outputDir)) { + $configOptions['propel']['paths']['schemaDir'] = $input->getOption('output-dir'); + } + if (empty($shemaName)) { + $configOptions['propel']['generator']['schema']['basename'] = $input->getOption('schema-name'); + } + + // regenerate config with the new options + $generatorConfig = $this->getGeneratorConfig($configOptions, $input);exit(1); + } + + if (!$connection) { + $connection = $generatorConfig->getConfigProperty('reverse.connection'); + if ($connection && !$input->getOption('database-name')) { + $input->setOption('database-name', $connection); + } + } + + $this->createDirectory( + $generatorConfig->getConfigProperty('paths.schemaDir')); $manager = new ReverseManager(new XmlDumper()); $manager->setGeneratorConfig($generatorConfig); @@ -87,12 +124,14 @@ protected function execute(InputInterface $input, OutputInterface $output) $output->writeln($message); } }); - $manager->setWorkingDirectory($input->getOption('output-dir')); + $manager->setWorkingDirectory( + $generatorConfig->getConfigProperty('paths.schemaDir')); $manager->setDatabaseName($input->getOption('database-name')); - $manager->setSchemaName($input->getOption('schema-name')); + $manager->setSchemaName( + $generatorConfig->getConfigProperty('generator.schema.basename')); + + $namespace = $generatorConfig->getConfigProperty('reverse.namespace'); - $namespace = $input->getOption('namespace'); - if ($namespace) { $manager->setNamespace($namespace); } diff --git a/tests/Fixtures/bookstore/propel.yaml.dist b/tests/Fixtures/bookstore/propel.yaml.dist index 5c371f8445..b24e0b003b 100644 --- a/tests/Fixtures/bookstore/propel.yaml.dist +++ b/tests/Fixtures/bookstore/propel.yaml.dist @@ -3,6 +3,9 @@ propel: general: project: bookstore + paths: + schemaDir: ../../reversecommand + database: connections: bookstore: @@ -45,6 +48,10 @@ propel: settings: charset: utf8 + reverse: + connection: bookstore + namespace: \ReverseVendor\ReversePackageWithConfDir + generator: defaultConnection: bookstore connections: diff --git a/tests/Propel/Tests/Generator/Command/DatabaseReverseTest.php b/tests/Propel/Tests/Generator/Command/DatabaseReverseTest.php index 91cbf55dc7..159025fedc 100644 --- a/tests/Propel/Tests/Generator/Command/DatabaseReverseTest.php +++ b/tests/Propel/Tests/Generator/Command/DatabaseReverseTest.php @@ -12,6 +12,16 @@ */ class DatabaseReverseTest extends TestCaseFixturesDatabase { + protected $configDir; + protected $outputDir; + + public function setUp() + { + parent::setUp(); + $this->configDir = __DIR__ . '/../../../../Fixtures/bookstore'; + $this->outputDir = __DIR__ . '/../../../../reversecommand'; + } + public function testCommandWithoutNamespace() { $app = new Application('Propel', Propel::VERSION); @@ -19,14 +29,13 @@ public function testCommandWithoutNamespace() $app->add($command); $currentDir = getcwd(); - $outputDir = __DIR__ . '/../../../../reversecommand'; chdir(__DIR__ . '/../../../../Fixtures/bookstore'); $input = new \Symfony\Component\Console\Input\ArrayInput([ 'command' => 'database:reverse', '--database-name' => 'reverse-test', - '--output-dir' => $outputDir, + '--output-dir' => $this->outputDir, '--verbose' => true, '--platform' => ucfirst($this->getDriver()) . 'Platform', 'connection' => $this->getConnectionDsn('bookstore-schemas', true) @@ -44,7 +53,7 @@ public function testCommandWithoutNamespace() } $this->assertEquals(0, $result, 'database:reverse tests exited successfully'); - $databaseXml = simplexml_load_file($outputDir . '/schema.xml'); + $databaseXml = simplexml_load_file($this->outputDir . '/schema.xml'); $this->assertEquals('reverse-test', $databaseXml['name']); $this->assertGreaterThan(20, $databaseXml->xpath("table")); @@ -64,7 +73,6 @@ public function testCommandWithNamespace() $app->add($command); $currentDir = getcwd(); - $outputDir = __DIR__ . '/../../../../reversecommand'; $testNamespace = '\ReverseVendor\ReversePackage'; chdir(__DIR__ . '/../../../../Fixtures/bookstore'); @@ -72,7 +80,7 @@ public function testCommandWithNamespace() $input = new \Symfony\Component\Console\Input\ArrayInput([ 'command' => 'database:reverse', '--database-name' => 'reverse-test', - '--output-dir' => $outputDir, + '--output-dir' => $this->outputDir, '--verbose' => true, '--platform' => ucfirst($this->getDriver()) . 'Platform', '--namespace' => $testNamespace, @@ -91,7 +99,79 @@ public function testCommandWithNamespace() } $this->assertEquals(0, $result, 'database:reverse tests exited successfully'); - $databaseXml = simplexml_load_file($outputDir . '/schema.xml'); + $databaseXml = simplexml_load_file($this->outputDir . '/schema.xml'); + $this->assertEquals($testNamespace, $databaseXml['namespace']); + } + + public function testCommandWithConfigDirAndAllParams() + { + $app = new Application('Propel', Propel::VERSION); + $command = new DatabaseReverseCommand(); + $app->add($command); + + $currentDir = getcwd(); + $testNamespace = '\ReverseVendor\ReversePackage'; + + chdir(__DIR__ . '/../../../../Fixtures/bookstore'); + + $input = new \Symfony\Component\Console\Input\ArrayInput([ + 'command' => 'database:reverse', + '--config-dir' => $this->configDir, + '--database-name' => 'reverse-test', + '--output-dir' => $this->outputDir, + '--verbose' => true, + '--platform' => ucfirst($this->getDriver()) . 'Platform', + '--namespace' => $testNamespace, + 'connection' => $this->getConnectionDsn('bookstore-schemas', true) + ]); + + $output = new \Symfony\Component\Console\Output\StreamOutput(fopen("php://temp", 'r+')); + $app->setAutoExit(false); + $result = $app->run($input, $output); + + chdir($currentDir); + + if (0 !== $result) { + rewind($output->getStream()); + echo stream_get_contents($output->getStream()); + } + $this->assertEquals(0, $result, 'database:reverse tests exited successfully'); + + $databaseXml = simplexml_load_file($this->outputDir . '/schema.xml'); + $this->assertEquals($testNamespace, $databaseXml['namespace']); + } + + public function testCommandWithConfigDirAndNoParams() + { + $app = new Application('Propel', Propel::VERSION); + $command = new DatabaseReverseCommand(); + $app->add($command); + + $currentDir = getcwd(); + $testNamespace = '\ReverseVendor\ReversePackageWithConfDir'; + + chdir(__DIR__ . '/../../../../Fixtures/bookstore'); + + $input = new \Symfony\Component\Console\Input\ArrayInput([ + 'command' => 'database:reverse', + '--config-dir' => $this->configDir, + '--verbose' => true, + '--platform' => ucfirst($this->getDriver()) . 'Platform' + ]); + + $output = new \Symfony\Component\Console\Output\StreamOutput(fopen("php://temp", 'r+')); + $app->setAutoExit(false); + $result = $app->run($input, $output); + + chdir($currentDir); + + if (0 !== $result) { + rewind($output->getStream()); + echo stream_get_contents($output->getStream()); + } + $this->assertEquals(0, $result, 'database:reverse tests exited successfully'); + + $databaseXml = simplexml_load_file($this->outputDir . '/schema.xml'); $this->assertEquals($testNamespace, $databaseXml['namespace']); }