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

Commands throw exception if ran twice. #1195

Open
wants to merge 1 commit into
base: 3.0.x
Choose a base branch
from
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,16 @@
namespace Doctrine\Migrations\Tests\Tools\Console\Command;

use Doctrine\Migrations\Configuration\Configuration;
use Doctrine\Migrations\Configuration\Connection\ConnectionLoader;
use Doctrine\Migrations\Configuration\Connection\ExistingConnection;
use Doctrine\Migrations\Configuration\Migration\ConfigurationLoader;
use Doctrine\Migrations\Configuration\Migration\ExistingConfiguration;
use Doctrine\Migrations\DependencyFactory;
use Doctrine\Migrations\Tests\MigrationTestCase;
use Doctrine\Migrations\Tools\Console\Command\DoctrineCommand;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Tester\CommandTester;

use function sys_get_temp_dir;

class DoctrineCommandTest extends MigrationTestCase
Expand All @@ -29,8 +30,7 @@ public function testCommandFreezes(): void
->expects(self::once())
->method('freeze');

$command = new class ($dependencyFactory) extends DoctrineCommand
{
$command = new class ($dependencyFactory) extends DoctrineCommand {
protected function execute(InputInterface $input, OutputInterface $output): int
{
return 0;
Expand All @@ -44,6 +44,39 @@ protected function execute(InputInterface $input, OutputInterface $output): int
);
}

public function testCommandNotThrowingFrozenException()
{
$configurationLoader = $this->createMock(ConfigurationLoader::class);
$configurationLoader->method('getConfiguration')->willReturn(new Configuration());

$dependencyFactory = DependencyFactory::fromConnection(
$configurationLoader,
$this->createMock(ConnectionLoader::class)
);

$command = new class ($dependencyFactory) extends DoctrineCommand {
protected function execute(InputInterface $input, OutputInterface $output): int
{
return 0;
}
};
$commandTester = new CommandTester($command);

// execute once, this will freeze the dependencies.
$commandTester->execute(
Copy link
Member

Choose a reason for hiding this comment

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

im not sure if this test makes sense... is it possible to execute two times the same command ? ( i guess that you are using it as a service, not CLI, right?)

Copy link
Author

@Warxcell Warxcell Aug 24, 2021

Choose a reason for hiding this comment

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

Yes, commands are ran from PHPUnit without going a loop throught CLI and bootstraping the project again. Basically services are instantiated, and second ran of command uses same services, and one of them (DependencyFactory) is frozen and throws exception on second ran.

Copy link
Author

Choose a reason for hiding this comment

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

['--configuration' => __DIR__ . '/_files/config.yml'],
['interactive' => false,]
);

// execute one more time, this will throw exception.
$commandTester->execute(
['--configuration' => __DIR__ . '/_files/config.yml'],
['interactive' => false,]
);

$this->expectNotToPerformAssertions();
}

public function testCustomConfiguration(): void
{
$configuration = new Configuration();
Expand All @@ -56,8 +89,7 @@ public function testCustomConfiguration(): void
new ExistingConnection($conn)
);

$command = new class ($dependencyFactory) extends DoctrineCommand
{
$command = new class ($dependencyFactory) extends DoctrineCommand {
protected function execute(InputInterface $input, OutputInterface $output): int
{
$migrationDirectories = $this->getDependencyFactory()->getConfiguration()->getMigrationDirectories();
Expand All @@ -77,8 +109,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
public function testDependencyFactoryIsSetFirst(): void
{
$dependencyFactory = $this->createMock(DependencyFactory::class);
$command = new class ($dependencyFactory) extends DoctrineCommand
{
$command = new class ($dependencyFactory) extends DoctrineCommand {
protected function configure(): void
{
$this->getDependencyFactory();
Expand Down