Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
}
}
Loading