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’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix --complete flag in orm:ensure-production-settings command #8426

Merged
merged 7 commits into from
Feb 1, 2021
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
try {
$em->getConfiguration()->ensureProductionSettings();

if ($input->getOption('complete') !== null) {
if ($input->getOption('complete') === true) {
$em->getConnection()->connect();
}
} catch (Throwable $e) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
<?php

declare(strict_types=1);

namespace Doctrine\Tests\ORM\Tools\Console\Command;

use Doctrine\DBAL\Connection;
use Doctrine\ORM\Configuration;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\Tools\Console\Command\EnsureProductionSettingsCommand;
use Doctrine\ORM\Tools\Console\Helper\EntityManagerHelper;
use Doctrine\Tests\DoctrineTestCase;
use RuntimeException;
use Symfony\Component\Console\Application;
use Symfony\Component\Console\Helper\HelperSet;
use Symfony\Component\Console\Tester\CommandTester;

use function array_merge;

class EnsureProductionSettingsCommandTest extends DoctrineTestCase
{
public function testExecute(): void
{
$em = $this->createMock(EntityManagerInterface::class);

$configuration = $this->createMock(Configuration::class);
$configuration->expects($this->once())
->method('ensureProductionSettings');

$em->method('getConfiguration')
->willReturn($configuration);

$em->expects($this->never())
->method('getConnection');

$this->assertSame(0, $this->executeCommand($em));
}

public function testExecuteFailed(): void
{
$em = $this->createMock(EntityManagerInterface::class);

$configuration = $this->createMock(Configuration::class);
$configuration->expects($this->once())
->method('ensureProductionSettings')
->willThrowException(new RuntimeException());

$em->method('getConfiguration')
->willReturn($configuration);

$em->expects($this->never())
->method('getConnection');

$this->assertSame(1, $this->executeCommand($em));
}

public function testExecuteWithComplete(): void
{
$em = $this->createMock(EntityManagerInterface::class);

$configuration = $this->createMock(Configuration::class);
$configuration->expects($this->once())
->method('ensureProductionSettings');

$em->method('getConfiguration')
->willReturn($configuration);

$connection = $this->createMock(Connection::class);

$connection->expects($this->once())
->method('connect');

$em->method('getConnection')
->willReturn($connection);

$this->assertSame(0, $this->executeCommand($em, ['--complete' => true]));
}

public function testExecuteWithCompleteFailed(): void
{
$em = $this->createMock(EntityManagerInterface::class);

$configuration = $this->createMock(Configuration::class);
$configuration->expects($this->once())
->method('ensureProductionSettings');

$em->method('getConfiguration')
->willReturn($configuration);

$connection = $this->createMock(Connection::class);

$connection->expects($this->once())
->method('connect')
->willThrowException(new RuntimeException());

$em->method('getConnection')
->willReturn($connection);

$this->assertSame(1, $this->executeCommand($em, ['--complete' => true]));
}

/**
* @param mixed[] $options
*/
private function executeCommand(
EntityManagerInterface $em,
array $input = []
): int {
$application = new Application();
$application->setHelperSet(new HelperSet(['em' => new EntityManagerHelper($em)]));
$application->add(new EnsureProductionSettingsCommand());

$command = $application->find('orm:ensure-production-settings');
$tester = new CommandTester($command);

return $tester->execute(
array_merge([
'command' => $command->getName(),
], $input)
);
}
}