From 9928ccad3b13d1c5fc4ca398e18792b0876dbb2f Mon Sep 17 00:00:00 2001 From: Viktor Babanov Date: Mon, 27 Jan 2025 11:27:35 +0500 Subject: [PATCH] Improve test coverage (#237) --- phpunit.xml.dist | 4 +- src/Command/ListenAllCommand.php | 13 ++++--- tests/Unit/Command/ListenAllCommandTest.php | 42 +++++++++++++++++++++ 3 files changed, 53 insertions(+), 6 deletions(-) create mode 100644 tests/Unit/Command/ListenAllCommandTest.php diff --git a/phpunit.xml.dist b/phpunit.xml.dist index 1968c822..bde3a3bc 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -28,7 +28,9 @@ ./src - ./config + + ./config + diff --git a/src/Command/ListenAllCommand.php b/src/Command/ListenAllCommand.php index da531ad1..9703ee59 100644 --- a/src/Command/ListenAllCommand.php +++ b/src/Command/ListenAllCommand.php @@ -30,6 +30,9 @@ public function __construct( parent::__construct(); } + /** + * @codeCoverageIgnore + */ public function configure(): void { $this->addArgument( @@ -65,6 +68,11 @@ protected function execute(InputInterface $input, OutputInterface $output): int $queues[] = $this->queueProvider->get($channel); } + $pauseSeconds = (int) $input->getOption('pause'); + if ($pauseSeconds < 0) { + $pauseSeconds = 1; + } + while ($this->loop->canContinue()) { $hasMessages = false; foreach ($queues as $queue) { @@ -72,11 +80,6 @@ protected function execute(InputInterface $input, OutputInterface $output): int } if (!$hasMessages) { - $pauseSeconds = (int) $input->getOption('pause'); - if ($pauseSeconds < 0) { - $pauseSeconds = 1; - } - /** @psalm-var 0|positive-int $pauseSeconds */ sleep($pauseSeconds); } diff --git a/tests/Unit/Command/ListenAllCommandTest.php b/tests/Unit/Command/ListenAllCommandTest.php new file mode 100644 index 00000000..bef271e5 --- /dev/null +++ b/tests/Unit/Command/ListenAllCommandTest.php @@ -0,0 +1,42 @@ +createMock(QueueInterface::class); + $queue1->expects($this->once())->method('run'); + $queue2 = $this->createMock(QueueInterface::class); + $queue2->expects($this->once())->method('run'); + + $queueFactory = $this->createMock(QueueProviderInterface::class); + $queueFactory->method('get')->willReturn($queue1, $queue2); + + $loop = $this->createMock(LoopInterface::class); + $loop->method('canContinue')->willReturn(true, false); + + + $command = new ListenAllCommand( + $queueFactory, + $loop, + ['channel1', 'channel2'], + ); + $input = new ArrayInput([], $command->getNativeDefinition()); + $input->setOption('pause', 0); + $exitCode = $command->run($input, $this->createMock(OutputInterface::class)); + + $this->assertEquals(0, $exitCode); + } +}