Skip to content

Commit

Permalink
Improve test coverage (#237)
Browse files Browse the repository at this point in the history
  • Loading branch information
viktorprogger authored Jan 27, 2025
1 parent dddf279 commit 9928cca
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 6 deletions.
4 changes: 3 additions & 1 deletion phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@
<source>
<include>
<directory suffix=".php">./src</directory>
<directory suffix=".php">./config</directory>
</include>
<exclude>
<directory suffix=".php">./config</directory>
</exclude>
</source>
</phpunit>
13 changes: 8 additions & 5 deletions src/Command/ListenAllCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ public function __construct(
parent::__construct();
}

/**
* @codeCoverageIgnore
*/
public function configure(): void
{
$this->addArgument(
Expand Down Expand Up @@ -65,18 +68,18 @@ 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) {
$hasMessages = $queue->run((int) $input->getOption('maximum')) > 0 || $hasMessages;
}

if (!$hasMessages) {
$pauseSeconds = (int) $input->getOption('pause');
if ($pauseSeconds < 0) {
$pauseSeconds = 1;
}

/** @psalm-var 0|positive-int $pauseSeconds */
sleep($pauseSeconds);
}
Expand Down
42 changes: 42 additions & 0 deletions tests/Unit/Command/ListenAllCommandTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

declare(strict_types=1);

namespace Yiisoft\Queue\Tests\Unit\Command;

use PHPUnit\Framework\TestCase;
use Symfony\Component\Console\Input\ArrayInput;
use Symfony\Component\Console\Output\OutputInterface;
use Yiisoft\Queue\Cli\LoopInterface;
use Yiisoft\Queue\Command\ListenAllCommand;
use Yiisoft\Queue\Provider\QueueProviderInterface;
use Yiisoft\Queue\QueueInterface;

final class ListenAllCommandTest extends TestCase
{
public function testExecute(): void
{
$queue1 = $this->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);
}
}

0 comments on commit 9928cca

Please sign in to comment.