diff --git a/packages/application/Tests/Command/CronDumpToFileCommandTest.php b/packages/application/Tests/Command/CronDumpToFileCommandTest.php index 15477181f..a859a3edb 100644 --- a/packages/application/Tests/Command/CronDumpToFileCommandTest.php +++ b/packages/application/Tests/Command/CronDumpToFileCommandTest.php @@ -9,7 +9,6 @@ use PHPUnit\Framework\Attributes\CoversClass; use PHPUnit\Framework\MockObject\MockObject; use PHPUnit\Framework\TestCase; -use Symfony\Component\Console\Command\Command; use Symfony\Component\Console\Input\InputArgument; use Symfony\Component\Console\Input\InputOption; @@ -20,9 +19,9 @@ class CronDumpToFileCommandTest extends TestCase private CronManager&MockObject $cronManager; - public function createCommand(): Command + protected function setUp(): void { - return new CronDumpToFileCommand( + $this->command = new CronDumpToFileCommand( $this->cronManager = $this->createMock(CronManager::class) ); } diff --git a/packages/application/Tests/Versioning/Command/UpdateDeployedVersionCommandTest.php b/packages/application/Tests/Versioning/Command/UpdateDeployedVersionCommandTest.php index a76b0d505..6d17454f7 100644 --- a/packages/application/Tests/Versioning/Command/UpdateDeployedVersionCommandTest.php +++ b/packages/application/Tests/Versioning/Command/UpdateDeployedVersionCommandTest.php @@ -9,7 +9,6 @@ use PHPUnit\Framework\Attributes\CoversClass; use PHPUnit\Framework\MockObject\MockObject; use PHPUnit\Framework\TestCase; -use Symfony\Component\Console\Command\Command; #[CoversClass(UpdateDeployedVersionCommand::class)] class UpdateDeployedVersionCommandTest extends TestCase @@ -18,9 +17,9 @@ class UpdateDeployedVersionCommandTest extends TestCase private VersionManager&MockObject $versionManager; - public function createCommand(): Command + protected function setUp(): void { - return new UpdateDeployedVersionCommand( + $this->command = new UpdateDeployedVersionCommand( $this->versionManager = $this->createMock(VersionManager::class) ); } diff --git a/packages/aws-tool-kit/Tests/Command/CloudWatchLogsDownloadCommandTest.php b/packages/aws-tool-kit/Tests/Command/CloudWatchLogsDownloadCommandTest.php index 90b46f32d..58b45397c 100644 --- a/packages/aws-tool-kit/Tests/Command/CloudWatchLogsDownloadCommandTest.php +++ b/packages/aws-tool-kit/Tests/Command/CloudWatchLogsDownloadCommandTest.php @@ -11,7 +11,6 @@ use PHPUnit\Framework\Attributes\CoversClass; use PHPUnit\Framework\MockObject\MockObject; use PHPUnit\Framework\TestCase; -use Symfony\Component\Console\Command\Command; use Symfony\Component\Console\Input\InputArgument; use Symfony\Component\Console\Input\InputOption; @@ -23,7 +22,7 @@ class CloudWatchLogsDownloadCommandTest extends TestCase private CloudWatchLogsClient&MockObject $cloudWatchLogsClient; - public function createCommand(): Command + protected function setUp(): void { $this->cloudWatchLogsClient = $this ->getMockBuilder(CloudWatchLogsClient::class) @@ -34,7 +33,7 @@ public function createCommand(): Command ->addMethods(['getLogEvents']) ->getMock(); - return new CloudWatchLogsDownloadCommand($this->cloudWatchLogsClient); + $this->command = new CloudWatchLogsDownloadCommand($this->cloudWatchLogsClient); } public function getCommandName(): string diff --git a/packages/console/Tests/Command/PurgeExecutionCommandTest.php b/packages/console/Tests/Command/PurgeExecutionCommandTest.php index 2e990d9ad..5abffc088 100644 --- a/packages/console/Tests/Command/PurgeExecutionCommandTest.php +++ b/packages/console/Tests/Command/PurgeExecutionCommandTest.php @@ -11,7 +11,6 @@ use PHPUnit\Framework\MockObject\MockObject; use PHPUnit\Framework\TestCase; use Psr\Log\LoggerInterface; -use Symfony\Component\Console\Command\Command; use Symfony\Component\Console\Input\InputOption; #[CoversClass(PurgeExecutionCommand::class)] @@ -24,12 +23,12 @@ class PurgeExecutionCommandTest extends TestCase private LoggerInterface&MockObject $logger; - public function createCommand(): Command + protected function setUp(): void { $this->connection = $this->createMock(Connection::class); $this->logger = $this->createMock(LoggerInterface::class); - return new PurgeExecutionCommand($this->connection, $this->logger); + $this->command = new PurgeExecutionCommand($this->connection, $this->logger); } public function getCommandName(): string diff --git a/packages/cron-job/Tests/Command/QueueCronJobByNameCommandTest.php b/packages/cron-job/Tests/Command/QueueCronJobByNameCommandTest.php index fcf4b2c26..a60dcf989 100644 --- a/packages/cron-job/Tests/Command/QueueCronJobByNameCommandTest.php +++ b/packages/cron-job/Tests/Command/QueueCronJobByNameCommandTest.php @@ -28,7 +28,7 @@ class QueueCronJobByNameCommandTest extends TestCase private EntityRepository&MockObject $repository; - public function createCommand(): Command + protected function setUp(): void { $this->managerRegistry = $this->createMock(ManagerRegistry::class); $this->managerRegistry @@ -37,7 +37,7 @@ public function createCommand(): Command ->with(CronJob::class) ->willReturn($this->repository = $this->createMock(EntityRepository::class)); - return new QueueCronJobByNameCommand( + $this->command = new QueueCronJobByNameCommand( $this->managerRegistry, $this->cronJobProcessor = $this->createMock(CronJobProcessor::class) ); diff --git a/packages/cron-job/Tests/Command/QueueDueCronJobsCommandTest.php b/packages/cron-job/Tests/Command/QueueDueCronJobsCommandTest.php index abce12b38..e0edacc83 100644 --- a/packages/cron-job/Tests/Command/QueueDueCronJobsCommandTest.php +++ b/packages/cron-job/Tests/Command/QueueDueCronJobsCommandTest.php @@ -28,9 +28,9 @@ class QueueDueCronJobsCommandTest extends TestCase private CronJobProcessor&MockObject $cronJobProcessor; - public function createCommand(): Command + protected function setUp(): void { - return new QueueDueCronJobsCommand( + $this->command = new QueueDueCronJobsCommand( $this->managerRegistry = $this->createMock(ManagerRegistry::class), $this->cronJobProcessor = $this->createMock(CronJobProcessor::class) ); diff --git a/packages/mailer/Tests/Command/SendTestEmailCommandTest.php b/packages/mailer/Tests/Command/SendTestEmailCommandTest.php index e1b48b203..d9e04d02b 100644 --- a/packages/mailer/Tests/Command/SendTestEmailCommandTest.php +++ b/packages/mailer/Tests/Command/SendTestEmailCommandTest.php @@ -8,7 +8,6 @@ use PHPUnit\Framework\Attributes\CoversClass; use PHPUnit\Framework\MockObject\MockObject; use PHPUnit\Framework\TestCase; -use Symfony\Component\Console\Command\Command; use Symfony\Component\Console\Input\InputArgument; use Symfony\Component\Mailer\MailerInterface; use Symfony\Component\Mime\Email; @@ -20,9 +19,9 @@ class SendTestEmailCommandTest extends TestCase private MailerInterface&MockObject $mailer; - public function createCommand(): Command + protected function setUp(): void { - return new SendTestEmailCommand( + $this->command = new SendTestEmailCommand( $this->mailer = $this->createMock(MailerInterface::class) ); } diff --git a/packages/messenger/Tests/Broker/Command/StartMessengerBrokerCommandTest.php b/packages/messenger/Tests/Broker/Command/StartMessengerBrokerCommandTest.php index 6aed749fa..2c50e60db 100644 --- a/packages/messenger/Tests/Broker/Command/StartMessengerBrokerCommandTest.php +++ b/packages/messenger/Tests/Broker/Command/StartMessengerBrokerCommandTest.php @@ -12,7 +12,6 @@ use PHPUnit\Framework\Attributes\CoversClass; use PHPUnit\Framework\MockObject\MockObject; use PHPUnit\Framework\TestCase; -use Symfony\Component\Console\Command\Command; use Symfony\Component\Console\Exception\InvalidOptionException; use Symfony\Component\Console\Input\InputOption; use Symfony\Component\EventDispatcher\EventDispatcher; @@ -30,9 +29,9 @@ class StartMessengerBrokerCommandTest extends TestCase private string $consolePath; - public function createCommand(): Command + protected function setUp(): void { - return new StartMessengerBrokerCommand( + $this->command = new StartMessengerBrokerCommand( $this->consolePath = uniqid('console-path-'), $this->processFactory = $this->createMock(ProcessFactoryInterface::class), $this->eventDispatcher = new EventDispatcher(), diff --git a/packages/messenger/Tests/Expirable/Command/PurgeExpiredMessageCommandTest.php b/packages/messenger/Tests/Expirable/Command/PurgeExpiredMessageCommandTest.php index e305c9c12..95999ef25 100644 --- a/packages/messenger/Tests/Expirable/Command/PurgeExpiredMessageCommandTest.php +++ b/packages/messenger/Tests/Expirable/Command/PurgeExpiredMessageCommandTest.php @@ -11,7 +11,6 @@ use PHPUnit\Framework\Attributes\CoversClass; use PHPUnit\Framework\MockObject\MockObject; use PHPUnit\Framework\TestCase; -use Symfony\Component\Console\Command\Command; use Symfony\Component\Console\Input\InputArgument; use Symfony\Component\Console\Input\InputOption; use Symfony\Component\Messenger\Transport\TransportInterface; @@ -24,9 +23,9 @@ class PurgeExpiredMessageCommandTest extends TestCase private TransportRepository&MockObject $transportRepository; - public function createCommand(): Command + protected function setUp(): void { - return new PurgeExpiredMessageCommand( + $this->command = new PurgeExpiredMessageCommand( $this->transportRepository = $this->createMock(TransportRepository::class), ); } diff --git a/packages/open-api/Tests/Command/InstallSandboxCommandTest.php b/packages/open-api/Tests/Command/InstallSandboxCommandTest.php index 44e4e518e..7dd26fd81 100644 --- a/packages/open-api/Tests/Command/InstallSandboxCommandTest.php +++ b/packages/open-api/Tests/Command/InstallSandboxCommandTest.php @@ -8,7 +8,6 @@ use Draw\Component\Tester\MockTrait; use PHPUnit\Framework\Attributes\CoversClass; use PHPUnit\Framework\TestCase; -use Symfony\Component\Console\Command\Command; use Symfony\Component\Console\Input\InputArgument; use Symfony\Component\Console\Input\InputOption; use Symfony\Component\Filesystem\Filesystem; @@ -19,9 +18,9 @@ class InstallSandboxCommandTest extends TestCase use CommandTestTrait; use MockTrait; - public function createCommand(): Command + protected function setUp(): void { - return new InstallSandboxCommand(); + $this->command = new InstallSandboxCommand(); } public function getCommandName(): string diff --git a/packages/tester/Application/CommandTestTrait.php b/packages/tester/Application/CommandTestTrait.php index aa060f42f..169feb70a 100644 --- a/packages/tester/Application/CommandTestTrait.php +++ b/packages/tester/Application/CommandTestTrait.php @@ -14,11 +14,37 @@ trait CommandTestTrait { private static int $argumentsCount; - protected CommandTester $commandTester; + protected ?CommandTester $commandTester = null; - protected Command $command; + protected ?Command $command = null; - abstract public function createCommand(): Command; + private ?Application $application = null; + + protected function createCommand(): Command + { + if (null === $this->command) { + throw new \RuntimeException('You must override createCommand method or set the command property.'); + } + + return $this->command; + } + + final protected function loadCommand(): Command + { + $command = $this->command ??= $this->createCommand(); + + if (null === $this->application) { + $this->application = new Application(); + $this->application->add($command); + } + + return $command; + } + + final protected function loadCommandTester(): CommandTester + { + return $this->commandTester ??= new CommandTester($this->loadCommand()); + } abstract public function getCommandName(): string; @@ -31,14 +57,6 @@ public static function setUpBeforeClass(): void self::$argumentsCount = 0; } - public function setUp(): void - { - $application = new Application(); - $application->add($this->createCommand()); - $this->command = $application->find($this->getCommandName()); - $this->commandTester = new CommandTester($this->command); - } - protected function getMinimumCommandDescriptionStringLength(): int { return 10; @@ -49,22 +67,34 @@ protected function getMinimumInputDescriptionStringLength(): int return 10; } + public function testGetCommandName(): void + { + $command = $this->loadCommand(); + + TestCase::assertSame( + $command, + $this->application->find($this->getCommandName()), + 'Cannot find the command by name' + ); + } + public function testGetDescription(): void { TestCase::assertGreaterThanOrEqual( $this->getMinimumCommandDescriptionStringLength(), - mb_strlen($this->command->getDescription()), + mb_strlen($this->loadCommand()->getDescription()), 'Command description is too short' ); } public function testArguments(): void { - $definition = $this->command->getDefinition(); + $command = $this->loadCommand(); + $definition = $command->getDefinition(); $position = 0; foreach ($this->provideTestArgument() as $argument) { - array_unshift($argument, $this->command, $position); + array_unshift($argument, $command, $position); \call_user_func_array([$this, 'assertArgument'], $argument); ++$position; } @@ -118,7 +148,8 @@ public function assertArgument( public function testOptions(): void { $count = 0; - $definition = $this->command->getDefinition(); + $command = $this->loadCommand(); + $definition = $command->getDefinition(); $realCommandOptions = []; foreach ($definition->getOptions() as $option) { $realCommandOptions[$option->getName()] = $option; @@ -127,7 +158,7 @@ public function testOptions(): void foreach ($this->provideTestOption() as $optionConfiguration) { unset($realCommandOptions[$optionConfiguration[0]]); ++$count; - array_unshift($optionConfiguration, $this->command); + array_unshift($optionConfiguration, $command); \call_user_func_array([$this, 'assertOption'], $optionConfiguration); } @@ -207,12 +238,13 @@ public function assertOption( */ public function execute(array $input, array $options = []): DataTester { + $commandTester = $this->loadCommandTester(); $columns = getenv('COLUMNS'); putenv('COLUMNS=120'); $options += ['capture_stderr_separately' => true]; - $this->commandTester->execute($input, $options); + $commandTester->execute($input, $options); putenv('COLUMNS='.$columns); - return new DataTester($this->commandTester); + return new DataTester($commandTester); } } diff --git a/tests/Command/NullCommandTest.php b/tests/Command/NullCommandTest.php index 52c8a01d9..0f0139a53 100644 --- a/tests/Command/NullCommandTest.php +++ b/tests/Command/NullCommandTest.php @@ -4,22 +4,19 @@ use App\Command\NullCommand; use App\Tests\TestCase; +use Draw\Bundle\TesterBundle\PHPUnit\Extension\SetUpAutowire\AutowiredInterface; +use Draw\Bundle\TesterBundle\PHPUnit\Extension\SetUpAutowire\AutowireService; use Draw\Component\Tester\Application\CommandDataTester; use Draw\Component\Tester\Application\CommandTestTrait; use Symfony\Component\Console\Command\Command; use Symfony\Component\Console\Input\InputOption; -/** - * @covers \App\Command\NullCommand - */ -class NullCommandTest extends TestCase +class NullCommandTest extends TestCase implements AutowiredInterface { use CommandTestTrait; - public function createCommand(): Command - { - return static::getService(NullCommand::class); - } + #[AutowireService(NullCommand::class)] + protected ?Command $command = null; public function getCommandName(): string { diff --git a/tests/Console/Command/GenerateDocumentationCommandTest.php b/tests/Console/Command/GenerateDocumentationCommandTest.php index cb8fe53db..114dcb835 100644 --- a/tests/Console/Command/GenerateDocumentationCommandTest.php +++ b/tests/Console/Command/GenerateDocumentationCommandTest.php @@ -4,6 +4,8 @@ use App\Tests\FilteredCommandTestTrait; use App\Tests\TestCase; +use Draw\Bundle\TesterBundle\PHPUnit\Extension\SetUpAutowire\AutowiredInterface; +use Draw\Bundle\TesterBundle\PHPUnit\Extension\SetUpAutowire\AutowireService; use Draw\Component\Console\Command\GenerateDocumentationCommand; use Draw\Component\Tester\Application\CommandDataTester; use Symfony\Component\Console\Command\Command; @@ -13,16 +15,14 @@ /** * @covers \Draw\Component\Console\Command\GenerateDocumentationCommand */ -class GenerateDocumentationCommandTest extends TestCase +class GenerateDocumentationCommandTest extends TestCase implements AutowiredInterface { use FilteredCommandTestTrait; private bool $writeFile = false; - public function createCommand(): Command - { - return static::getContainer()->get(GenerateDocumentationCommand::class); - } + #[AutowireService(GenerateDocumentationCommand::class)] + protected ?Command $command = null; public function getCommandName(): string {