Skip to content

Commit

Permalink
Add new options to load only schema or fixtures
Browse files Browse the repository at this point in the history
  • Loading branch information
Olivier Dolbeau committed Jun 4, 2019
1 parent 9a8bcf8 commit 566f81c
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 14 deletions.
22 changes: 14 additions & 8 deletions src/Bridge/Symfony/Bundle/Command/DBALLoadCommand.php
Expand Up @@ -34,6 +34,8 @@ protected function configure()
->setName('shapin:datagen:dbal:load')
->setDescription('Load the DBAL schema.')
->addOption('groups', 'g', InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY, 'Which groups should be loaded? (default: all)')
->addOption('schema-only', null, InputOption::VALUE_NONE, 'Load only schema.')
->addOption('fixtures-only', null, InputOption::VALUE_NONE, 'Load only fixtures.')
;
}

Expand All @@ -47,17 +49,21 @@ protected function execute(InputInterface $input, OutputInterface $output)

$groups = $input->getOption('groups', []);

$statements = $this->loader->getSchema($groups)->toSql($this->connection->getDatabasePlatform());
foreach ($statements as $statement) {
$this->connection->query($statement);
if (!$input->getOption('fixtures-only')) {
$statements = $this->loader->getSchema($groups)->toSql($this->connection->getDatabasePlatform());
foreach ($statements as $statement) {
$this->connection->query($statement);
}

$io->success('Schema created successfully.');
}

$io->success('Schema created successfully.');
if (!$input->getOption('schema-only')) {
foreach ($this->loader->getFixtures($groups) as $fixture) {
$this->connection->insert($fixture[0], $fixture[1], $fixture[2]);
}

foreach ($this->loader->getFixtures($groups) as $fixture) {
$this->connection->insert($fixture[0], $fixture[1], $fixture[2]);
$io->success('Fixtures created successfully.');
}

$io->success('Fixtures created successfully.');
}
}
37 changes: 31 additions & 6 deletions tests/Bridge/Symfony/Bundle/CommandsTest.php
Expand Up @@ -12,17 +12,42 @@ class CommandsTest extends KernelTestCase
{
public function testExecute()
{
self::bootKernel();
$tester = $this->getTester();

$application = new Application(static::$kernel);
$application->setCatchExceptions(false);
$application->setAutoExit(false);
$tester->run(['command' => 'shapin:datagen:dbal:load']);
$this->assertSame(0, $tester->getStatusCode());
$this->assertContains('[OK] Schema created successfully.', $tester->getDisplay());
$this->assertContains('[OK] Fixtures created successfully.', $tester->getDisplay());
}

$tester = new ApplicationTester($application);
public function testExecuteSchemaOnly()
{
$tester = $this->getTester();

$tester->run(['command' => 'shapin:datagen:dbal:load']);
$tester->run(['command' => 'shapin:datagen:dbal:load', '--schema-only' => null]);
$this->assertSame(0, $tester->getStatusCode());
$this->assertContains('[OK] Schema created successfully.', $tester->getDisplay());
$this->assertNotContains('[OK] Fixtures created successfully.', $tester->getDisplay());
}

public function testExecuteFixturesOnly()
{
$tester = $this->getTester();

$tester->run(['command' => 'shapin:datagen:dbal:load', '--fixtures-only' => null]);
$this->assertSame(0, $tester->getStatusCode());
$this->assertNotContains('[OK] Schema created successfully.', $tester->getDisplay());
$this->assertContains('[OK] Fixtures created successfully.', $tester->getDisplay());
}

private function getTester(): ApplicationTester
{
self::bootKernel();

$application = new Application(static::$kernel);
$application->setCatchExceptions(false);
$application->setAutoExit(false);

return new ApplicationTester($application);
}
}

0 comments on commit 566f81c

Please sign in to comment.