CommandTest.php 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. <?php
  2. namespace Test\Michel\PQueueBundle;
  3. use Michel\PQueueBundle\Command\PQueueWorkerRunCommand;
  4. use Symfony\Bundle\FrameworkBundle\Console\Application;
  5. use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
  6. use Symfony\Component\Console\Tester\CommandTester;
  7. use Test\Michel\PQueueBundle\Fixtures\TestKernel;
  8. class CommandTest extends KernelTestCase
  9. {
  10. protected static function getKernelClass(): string
  11. {
  12. return TestKernel::class;
  13. }
  14. protected function setUp(): void
  15. {
  16. self::bootKernel();
  17. }
  18. public function testPQueueWorkerRunCommand(): void
  19. {
  20. $application = new Application(self::$kernel);
  21. /**
  22. * @var PQueueWorkerRunCommand $command
  23. */
  24. $command = self::$kernel->getContainer()->get(PQueueWorkerRunCommand::class);
  25. $application->add($command);
  26. $commandTester = new CommandTester($application->find('pqueue:worker:run'));
  27. $exitCode = $commandTester->execute([
  28. '--stop-when-empty' => true,
  29. '--memory-limit' => '64'
  30. ]);
  31. $this->assertSame(0, $exitCode, 'Command should return success code 0');
  32. $display = trim($commandTester->getDisplay());
  33. $display = preg_replace('/\e\[[0-9;]*m/', '', $display);
  34. $this->assertStringContainsStringIgnoringCase(
  35. 'Worker started.',
  36. $display
  37. );
  38. $optionsDisplay = [
  39. 'Stop when empty : Yes',
  40. ' Idle sleep : 10 s',
  41. ' Retry delay : 60 s'
  42. ];
  43. foreach ($optionsDisplay as $expectedLine) {
  44. $this->assertStringContainsString($expectedLine, $display, "Expected line '{$expectedLine}' not found in command output.");
  45. }
  46. }
  47. }