From 7106ce456789b4a777b05f84457e3d7fa54fe785 Mon Sep 17 00:00:00 2001 From: Loulier Guillaume Date: Mon, 27 Jan 2020 09:07:52 +0100 Subject: [PATCH] Enable shortcuts for both Drop & Create commands This feature allow to use shortcuts when possible instead of full InputOption name. Implement #1072 --- Command/CreateDatabaseDoctrineCommand.php | 4 +- Command/DropDatabaseDoctrineCommand.php | 8 +-- Tests/Command/CreateDatabaseDoctrineTest.php | 16 ++++- Tests/Command/DropDatabaseDoctrineTest.php | 67 ++++++++++++++++++++ 4 files changed, 86 insertions(+), 9 deletions(-) diff --git a/Command/CreateDatabaseDoctrineCommand.php b/Command/CreateDatabaseDoctrineCommand.php index a4c645c92..6afac11a6 100644 --- a/Command/CreateDatabaseDoctrineCommand.php +++ b/Command/CreateDatabaseDoctrineCommand.php @@ -24,8 +24,8 @@ protected function configure() $this ->setName('doctrine:database:create') ->setDescription('Creates the configured database') - ->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('shard', 's', InputOption::VALUE_REQUIRED, 'The shard connection to use for this command') + ->addOption('connection', 'c', InputOption::VALUE_OPTIONAL, 'The connection to use for this command') ->addOption('if-not-exists', null, InputOption::VALUE_NONE, 'Don\'t trigger an error, when the database already exists') ->setHelp(<<%command.name% command creates the default connections database: diff --git a/Command/DropDatabaseDoctrineCommand.php b/Command/DropDatabaseDoctrineCommand.php index 13c027299..2ff604345 100644 --- a/Command/DropDatabaseDoctrineCommand.php +++ b/Command/DropDatabaseDoctrineCommand.php @@ -28,10 +28,10 @@ protected function configure() $this ->setName('doctrine:database:drop') ->setDescription('Drops the configured database') - ->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') + ->addOption('shard', 's', InputOption::VALUE_REQUIRED, 'The shard connection to use for this command') + ->addOption('connection', 'c', InputOption::VALUE_OPTIONAL, 'The connection to use for this command') + ->addOption('if-exists', 'e', InputOption::VALUE_NONE, 'Don\'t trigger an error, when the database doesn\'t exist') + ->addOption('force', 'f', InputOption::VALUE_NONE, 'Set this parameter to execute this action') ->setHelp(<<%command.name% command drops the default connections database: diff --git a/Tests/Command/CreateDatabaseDoctrineTest.php b/Tests/Command/CreateDatabaseDoctrineTest.php index b3ea8e586..a90b36555 100644 --- a/Tests/Command/CreateDatabaseDoctrineTest.php +++ b/Tests/Command/CreateDatabaseDoctrineTest.php @@ -3,6 +3,7 @@ namespace Doctrine\Bundle\DoctrineBundle\Tests\Command; use Doctrine\Bundle\DoctrineBundle\Command\CreateDatabaseDoctrineCommand; +use Generator; use PHPUnit\Framework\MockObject\MockObject; use PHPUnit\Framework\TestCase; use Symfony\Component\Console\Application; @@ -41,7 +42,10 @@ public function testExecute() : void $this->assertContains('Created database ' . sys_get_temp_dir() . '/' . $dbName . ' for connection named ' . $connectionName, $commandTester->getDisplay()); } - public function testExecuteWithShardOption() : void + /** + * @dataProvider provideShardOption + */ + public function testExecuteWithShardAlias(string $shardOption) : void { $connectionName = 'default'; $params = [ @@ -75,16 +79,22 @@ public function testExecuteWithShardOption() : void $command = $application->find('doctrine:database:create'); $commandTester = new CommandTester($command); - $commandTester->execute(['command' => $command->getName(), '--shard' => 1]); + $commandTester->execute(['command' => $command->getName(), $shardOption => 1]); $this->assertContains('Created database ' . sys_get_temp_dir() . '/shard_1 for connection named ' . $connectionName, $commandTester->getDisplay()); $commandTester = new CommandTester($command); - $commandTester->execute(['command' => $command->getName(), '--shard' => 2]); + $commandTester->execute(['command' => $command->getName(), $shardOption => 2]); $this->assertContains('Created database ' . sys_get_temp_dir() . '/shard_2 for connection named ' . $connectionName, $commandTester->getDisplay()); } + public function provideShardOption() : Generator + { + yield 'full name' => ['--shard']; + yield 'short name' => ['-s']; + } + /** * @param mixed[]|null $params Connection parameters */ diff --git a/Tests/Command/DropDatabaseDoctrineTest.php b/Tests/Command/DropDatabaseDoctrineTest.php index 010f598d8..796a9686c 100644 --- a/Tests/Command/DropDatabaseDoctrineTest.php +++ b/Tests/Command/DropDatabaseDoctrineTest.php @@ -3,6 +3,7 @@ namespace Doctrine\Bundle\DoctrineBundle\Tests\Command; use Doctrine\Bundle\DoctrineBundle\Command\DropDatabaseDoctrineCommand; +use Doctrine\DBAL\DBALException; use PHPUnit\Framework\MockObject\MockObject; use PHPUnit\Framework\TestCase; use Symfony\Component\Console\Application; @@ -42,6 +43,72 @@ public function testExecute() : void ); } + public function testExecuteWithAlias() : void + { + $connectionName = 'default'; + $dbName = 'test'; + $params = [ + 'url' => 'sqlite:///' . sys_get_temp_dir() . '/test.db', + 'path' => sys_get_temp_dir() . '/' . $dbName, + 'driver' => 'pdo_sqlite', + ]; + + $container = $this->getMockContainer($connectionName, $params); + + $application = new Application(); + $application->add(new DropDatabaseDoctrineCommand($container->get('doctrine'))); + + $command = $application->find('doctrine:database:drop'); + + $commandTester = new CommandTester($command); + $commandTester->execute( + array_merge(['command' => $command->getName(), '-f' => true]) + ); + + $this->assertContains( + sprintf( + 'Dropped database %s for connection named %s', + sys_get_temp_dir() . '/' . $dbName, + $connectionName + ), + $commandTester->getDisplay() + ); + } + + public function testItThrowsWhenUsingIfExistsWithAnIncompatibleDriver() : void + { + $connectionName = 'default'; + $dbName = 'test'; + $params = [ + 'url' => 'sqlite:///' . sys_get_temp_dir() . '/test.db', + 'path' => sys_get_temp_dir() . '/' . $dbName, + 'driver' => 'pdo_sqlite', + ]; + + $container = $this->getMockContainer($connectionName, $params); + + $application = new Application(); + $application->add(new DropDatabaseDoctrineCommand($container->get('doctrine'))); + + $command = $application->find('doctrine:database:drop'); + + $commandTester = new CommandTester($command); + + static::expectException(DBALException::class); + $commandTester->execute( + array_merge(['command' => $command->getName(), '-f' => true, '-e' => true]) + ); + + $this->assertContains( + sprintf( + 'Dropped database %s for connection named %s', + sys_get_temp_dir() . '/' . $dbName, + $connectionName + ), + $commandTester->getDisplay() + ); + } + public function testExecuteWithoutOptionForceWillFailWithAttentionMessage() : void { $connectionName = 'default';