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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Commands shortcuts #1132

Merged
merged 1 commit into from Jan 31, 2020
Merged
Show file tree
Hide file tree
Changes from all 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
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
6 changes: 3 additions & 3 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('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', 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('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
Copy link
Member

Choose a reason for hiding this comment

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

I've never tried this myself, but I think using variadics here could help simplify the data providers

{
$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,
'--if-exists' => true,
],
];
}

/**
* @param array|null $params Connection parameters
*/
Expand Down