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 27, 2020
1 parent 5ff92f1 commit 6b7135f
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 9 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', '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(<<<EOT
The <info>%command.name%</info> command drops the default connections database:
Expand Down
19 changes: 16 additions & 3 deletions Tests/Command/CreateDatabaseDoctrineTest.php
Expand Up @@ -41,7 +41,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 +78,26 @@ 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
67 changes: 67 additions & 0 deletions Tests/Command/DropDatabaseDoctrineTest.php
Expand Up @@ -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;
Expand Down Expand Up @@ -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';
Expand Down

0 comments on commit 6b7135f

Please sign in to comment.