diff --git a/lib/Doctrine/ORM/Tools/Console/Command/EnsureProductionSettingsCommand.php b/lib/Doctrine/ORM/Tools/Console/Command/EnsureProductionSettingsCommand.php index eb0ff17567d..daedd771c4d 100644 --- a/lib/Doctrine/ORM/Tools/Console/Command/EnsureProductionSettingsCommand.php +++ b/lib/Doctrine/ORM/Tools/Console/Command/EnsureProductionSettingsCommand.php @@ -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) { diff --git a/tests/Doctrine/Tests/ORM/Tools/Console/Command/EnsureProductionSettingsCommandTest.php b/tests/Doctrine/Tests/ORM/Tools/Console/Command/EnsureProductionSettingsCommandTest.php new file mode 100644 index 00000000000..a78f45a7666 --- /dev/null +++ b/tests/Doctrine/Tests/ORM/Tools/Console/Command/EnsureProductionSettingsCommandTest.php @@ -0,0 +1,122 @@ +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) + ); + } +}