Skip to content

Commit

Permalink
Enable shortcuts for both Drop & Create commands
Browse files Browse the repository at this point in the history
This feature allow to use shortcuts when possible instead of full InputOption name.
Implement #1072
  • Loading branch information
Guikingone committed Jan 28, 2020
1 parent 5ff92f1 commit 891398a
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 11 deletions.
4 changes: 2 additions & 2 deletions Command/CreateDatabaseDoctrineCommand.php
Expand Up @@ -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(<<<EOT
The <info>%command.name%</info> command creates the default connections database:
Expand Down
8 changes: 4 additions & 4 deletions Command/DropDatabaseDoctrineCommand.php
Expand Up @@ -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(<<<EOT
The <info>%command.name%</info> command drops the default connections database:
Expand Down
16 changes: 13 additions & 3 deletions Tests/Command/CreateDatabaseDoctrineTest.php
Expand Up @@ -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;
Expand Down Expand Up @@ -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 = [
Expand Down Expand Up @@ -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
*/
Expand Down
44 changes: 42 additions & 2 deletions Tests/Command/DropDatabaseDoctrineTest.php
Expand Up @@ -3,14 +3,19 @@
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;
use Symfony\Component\Console\Tester\CommandTester;

class DropDatabaseDoctrineTest extends TestCase
{
public function testExecute() : void
/**
* @dataProvider provideForceOption
*/
public function testExecute(array $options) : void
{
$connectionName = 'default';
$dbName = 'test';
Expand All @@ -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(
Expand All @@ -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';
Expand Down Expand Up @@ -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
*/
Expand Down

0 comments on commit 891398a

Please sign in to comment.