Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/Propel/Common/Config/PropelConfiguration.php
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,7 @@ protected function addReverseSection(ArrayNodeDefinition $node)
->arrayNode('reverse')
->children()
->scalarNode('connection')->end()
->scalarNode('namespace')->defaultNull()->end()
->scalarNode('parserClass')->end()
->end()
->end() //reverse
Expand Down
55 changes: 48 additions & 7 deletions src/Propel/Generator/Command/DatabaseReverseCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,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'])
Expand All @@ -54,14 +54,14 @@ protected function execute(InputInterface $input, OutputInterface $output): int
{
$configOptions = [];

$connection = (string)$input->getArgument('connection');
if (strpos($connection, ':') === false) {
$connection = (string) $input->getArgument('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(
Expand All @@ -73,9 +73,46 @@ protected function execute(InputInterface $input, OutputInterface $output): int
$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);
Expand All @@ -84,9 +121,13 @@ protected function execute(InputInterface $input, OutputInterface $output): int
$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');

Expand Down
7 changes: 7 additions & 0 deletions tests/Fixtures/bookstore/propel.yaml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ propel:
general:
project: bookstore

paths:
schemaDir: ../../reversecommand

database:
connections:
bookstore:
Expand Down Expand Up @@ -45,6 +48,10 @@ propel:
settings:
charset: utf8

reverse:
connection: bookstore
namespace: \ReverseVendor\ReversePackageWithConfDir

generator:
defaultConnection: bookstore
connections:
Expand Down
95 changes: 89 additions & 6 deletions tests/Propel/Tests/Generator/Command/DatabaseReverseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,19 @@
*/
class DatabaseReverseTest extends TestCaseFixturesDatabase
{
protected $configDir;
protected $outputDir;

/**
* @return void
*/
public function setUp()
{
parent::setUp();
$this->configDir = __DIR__ . '/../../../../Fixtures/bookstore';
$this->outputDir = __DIR__ . '/../../../../reversecommand';
}

/**
* @return void
*/
Expand All @@ -31,14 +44,13 @@ public function testCommandWithoutNamespace()
$app->add($command);

$currentDir = getcwd();
$outputDir = __DIR__ . '/../../../../reversecommand';

chdir(__DIR__ . '/../../../../Fixtures/bookstore');

$input = new 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),
Expand All @@ -56,7 +68,7 @@ public function testCommandWithoutNamespace()
}
$this->assertSame(AbstractCommand::CODE_SUCCESS, $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'));
Expand All @@ -79,15 +91,14 @@ public function testCommandWithNamespace()
$app->add($command);

$currentDir = getcwd();
$outputDir = __DIR__ . '/../../../../reversecommand';
$testNamespace = '\ReverseVendor\ReversePackage';

chdir(__DIR__ . '/../../../../Fixtures/bookstore');

$input = new ArrayInput([
'command' => 'database:reverse',
'--database-name' => 'reverse-test',
'--output-dir' => $outputDir,
'--output-dir' => $this->outputDir,
'--verbose' => true,
'--platform' => ucfirst($this->getDriver()) . 'Platform',
'--namespace' => $testNamespace,
Expand All @@ -106,7 +117,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']);
}
}