Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor commands to avoid wrong output #827

Merged
merged 8 commits into from Jun 27, 2018
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion Command/CreateDatabaseDoctrineCommand.php
Expand Up @@ -8,7 +8,7 @@
use Symfony\Component\Console\Output\OutputInterface;

/**
* Database tool allows you to easily drop and create your configured databases.
* Database tool allows you to easily create your configured databases.
*/
class CreateDatabaseDoctrineCommand extends DoctrineCommand
{
Expand Down
19 changes: 12 additions & 7 deletions Command/DropDatabaseDoctrineCommand.php
Expand Up @@ -8,7 +8,7 @@
use Symfony\Component\Console\Output\OutputInterface;

/**
* Database tool allows you to easily drop and create your configured databases.
* Database tool allows you to easily drop your configured databases.
*/
class DropDatabaseDoctrineCommand extends DoctrineCommand
{
Expand All @@ -24,8 +24,8 @@ protected function configure()
$this
->setName('doctrine:database:drop')
->setDescription('Drops the configured database')
->addOption('connection', null, InputOption::VALUE_OPTIONAL, 'The connection to use for this command')
->addOption('shard', null, InputOption::VALUE_REQUIRED, 'The shard connection to use for this command')
->addOption('connection', null, InputOption::VALUE_OPTIONAL, 'The connection to use for this command')
->addOption('if-exists', null, InputOption::VALUE_NONE, 'Don\'t trigger an error, when the database doesn\'t exist')
->addOption('force', null, InputOption::VALUE_NONE, 'Set this parameter to execute this action')
->setHelp(<<<EOT
Expand All @@ -49,7 +49,12 @@ protected function configure()
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
$connection = $this->getDoctrineConnection($input->getOption('connection'));
$connectionName = $input->getOption('connection');
if (empty($connectionName) === true) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

=== true is useless. empty($connectionName) is already a boolean

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

tbh I just copy pasted this part from the create command

shall I change this?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i changed it 👍

$connectionName = $this->getContainer()->get('doctrine')->getDefaultConnectionName();
}
$connection = $this->getDoctrineConnection($connectionName);

$ifExists = $input->getOption('if-exists');

$params = $connection->getParams();
Expand Down Expand Up @@ -82,7 +87,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
if (! $input->getOption('force')) {
$output->writeln('<error>ATTENTION:</error> This operation should not be executed in a production environment.');
$output->writeln('');
$output->writeln(sprintf('<info>Would drop the database named <comment>%s</comment>.</info>', $name));
$output->writeln(sprintf('<info>Would drop the database <comment>%s</comment> for connection named <comment>%s</comment>.</info>', $name, $connectionName));
$output->writeln('Please run the operation with --force to execute');
$output->writeln('<error>All data will be lost!</error>');

Expand All @@ -103,12 +108,12 @@ protected function execute(InputInterface $input, OutputInterface $output)
try {
if ($shouldDropDatabase) {
$connection->getSchemaManager()->dropDatabase($name);
$output->writeln(sprintf('<info>Dropped database for connection named <comment>%s</comment></info>', $name));
$output->writeln(sprintf('<info>Dropped database <comment>%s</comment> for connection named <comment>%s</comment></info>', $name, $connectionName));
} else {
$output->writeln(sprintf('<info>Database for connection named <comment>%s</comment> doesn\'t exist. Skipped.</info>', $name));
$output->writeln(sprintf('<info>Database <comment>%s</comment> for connection named <comment>%s</comment> doesn\'t exist. Skipped.</info>', $name, $connectionName));
}
} catch (\Exception $e) {
$output->writeln(sprintf('<error>Could not drop database for connection named <comment>%s</comment></error>', $name));
$output->writeln(sprintf('<error>Could not drop database <comment>%s</comment> for connection named <comment>%s</comment></error>', $name, $connectionName));
$output->writeln(sprintf('<error>%s</error>', $e->getMessage()));

return self::RETURN_CODE_NOT_DROP;
Expand Down
18 changes: 16 additions & 2 deletions Tests/Command/DropDatabaseDoctrineTest.php
Expand Up @@ -30,7 +30,14 @@ public function testExecute()
array_merge(['command' => $command->getName(), '--force' => true])
);

$this->assertContains('Dropped database for connection named ' . sys_get_temp_dir() . '/' . $dbName . '', $commandTester->getDisplay());
$this->assertContains(
sprintf(
'Dropped database %s for connection named %s',
sys_get_temp_dir() . '/' . $dbName,
$connectionName
),
$commandTester->getDisplay()
);
}

public function testExecuteWithoutOptionForceWillFailWithAttentionMessage()
Expand All @@ -53,7 +60,14 @@ public function testExecuteWithoutOptionForceWillFailWithAttentionMessage()
array_merge(['command' => $command->getName()])
);

$this->assertContains('Would drop the database named ' . sys_get_temp_dir() . '/' . $dbName . '.', $commandTester->getDisplay());
$this->assertContains(
sprintf(
'Would drop the database %s for connection named %s.',
sys_get_temp_dir() . '/' . $dbName,
$connectionName
),
$commandTester->getDisplay()
);
$this->assertContains('Please run the operation with --force to execute', $commandTester->getDisplay());
}

Expand Down