From 2007ee0e6c5eb214717c785804a40847b6f7b782 Mon Sep 17 00:00:00 2001 From: viktorprogger Date: Sun, 26 Jan 2025 23:39:02 +0500 Subject: [PATCH 1/2] Improve test coverage --- phpunit.xml.dist | 4 +- src/Command/ListenAllCommand.php | 15 +++++--- tests/Unit/Command/ListenAllCommandTest.php | 42 +++++++++++++++++++++ 3 files changed, 54 insertions(+), 7 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..0d61d470 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,12 @@ protected function execute(InputInterface $input, OutputInterface $output): int $queues[] = $this->queueProvider->get($channel); } + /** @psalm-var 0|positive-int $pauseSeconds */ + $pauseSeconds = (int) $input->getOption('pause'); + if ($pauseSeconds < 0) { + $pauseSeconds = 1; + } + while ($this->loop->canContinue()) { $hasMessages = false; foreach ($queues as $queue) { @@ -72,12 +81,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); + } +} From f1d0912ac070029f90f68dcef113cdd6b4e9632a Mon Sep 17 00:00:00 2001 From: viktorprogger Date: Sun, 26 Jan 2025 23:44:50 +0500 Subject: [PATCH 2/2] move type clarification --- src/Command/ListenAllCommand.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Command/ListenAllCommand.php b/src/Command/ListenAllCommand.php index 0d61d470..9703ee59 100644 --- a/src/Command/ListenAllCommand.php +++ b/src/Command/ListenAllCommand.php @@ -68,7 +68,6 @@ protected function execute(InputInterface $input, OutputInterface $output): int $queues[] = $this->queueProvider->get($channel); } - /** @psalm-var 0|positive-int $pauseSeconds */ $pauseSeconds = (int) $input->getOption('pause'); if ($pauseSeconds < 0) { $pauseSeconds = 1; @@ -81,6 +80,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int } if (!$hasMessages) { + /** @psalm-var 0|positive-int $pauseSeconds */ sleep($pauseSeconds); } }