From 891398a413dce07674df5db62c2ae5375c6d7a81 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 | 44 +++++++++++++++++++- 4 files changed, 61 insertions(+), 11 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..e5a12b7ac 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', 'i', 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..0cf3247b2 100644 --- a/Tests/Command/DropDatabaseDoctrineTest.php +++ b/Tests/Command/DropDatabaseDoctrineTest.php @@ -3,6 +3,8 @@ namespace Doctrine\Bundle\DoctrineBundle\Tests\Command; use Doctrine\Bundle\DoctrineBundle\Command\DropDatabaseDoctrineCommand; +use Doctrine\DBAL\DBALException; +use Generator; use PHPUnit\Framework\MockObject\MockObject; use PHPUnit\Framework\TestCase; use Symfony\Component\Console\Application; @@ -10,7 +12,10 @@ class DropDatabaseDoctrineTest extends TestCase { - public function testExecute() : void + /** + * @dataProvider provideForceOption + */ + public function testExecute(array $options) : void { $connectionName = 'default'; $dbName = 'test'; @@ -29,7 +34,7 @@ public function testExecute() : void $commandTester = new CommandTester($command); $commandTester->execute( - array_merge(['command' => $command->getName(), '--force' => true]) + array_merge(['command' => $command->getName()], $options) ); $this->assertContains( @@ -42,6 +47,15 @@ public function testExecute() : void ); } + /** + * @dataProvider provideIncompatibleDriverOptions + */ + public function testItThrowsWhenUsingIfExistsWithAnIncompatibleDriver(array $options) : void + { + static::expectException(DBALException::class); + $this->testExecute($options); + } + public function testExecuteWithoutOptionForceWillFailWithAttentionMessage() : void { $connectionName = 'default'; @@ -74,6 +88,32 @@ public function testExecuteWithoutOptionForceWillFailWithAttentionMessage() : vo $this->assertContains('Please run the operation with --force to execute', $commandTester->getDisplay()); } + public function provideForceOption() : Generator + { + yield 'full name' => [ + ['--force' => true], + ]; + yield 'short name' => [ + ['-f' => true], + ]; + } + + public function provideIncompatibleDriverOptions() : Generator + { + yield 'full name' => [ + [ + '--force' => true, + '--if-exists' => true, + ], + ]; + yield 'short name' => [ + [ + '-f' => true, + '-i' => true, + ], + ]; + } + /** * @param array|null $params Connection parameters */