Skip to content
Open
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
12 changes: 0 additions & 12 deletions config/di.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@
use Yiisoft\Queue\Cli\LoopInterface;
use Yiisoft\Queue\Cli\SignalLoop;
use Yiisoft\Queue\Cli\SimpleLoop;
use Yiisoft\Queue\Command\ListenAllCommand;
use Yiisoft\Queue\Command\RunCommand;
use Yiisoft\Queue\Message\JsonMessageSerializer;
use Yiisoft\Queue\Message\MessageSerializerInterface;
use Yiisoft\Queue\Middleware\Consume\ConsumeMiddlewareDispatcher;
Expand Down Expand Up @@ -59,14 +57,4 @@
'__construct()' => ['middlewareDefinitions' => $params['yiisoft/queue']['middlewares-fail']],
],
MessageSerializerInterface::class => JsonMessageSerializer::class,
RunCommand::class => [
'__construct()' => [
'queues' => array_keys($params['yiisoft/queue']['queues']),
],
],
ListenAllCommand::class => [
'__construct()' => [
'queues' => array_keys($params['yiisoft/queue']['queues']),
],
],
];
3 changes: 1 addition & 2 deletions src/Command/ListenAllCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
public function __construct(
private readonly QueueProviderInterface $queueProvider,
private readonly LoopInterface $loop,
private readonly array $queues,
) {
parent::__construct();
}
Expand All @@ -39,7 +38,7 @@
'queue',
InputArgument::OPTIONAL | InputArgument::IS_ARRAY,
'Queue name list to connect to',
$this->queues,
$this->queueProvider->getNames(),
)
->addOption(
'pause',
Expand Down Expand Up @@ -69,14 +68,14 @@
}

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

Check warning on line 71 in src/Command/ListenAllCommand.php

View workflow job for this annotation

GitHub Actions / mutation / PHP 8.5-ubuntu-latest

Escaped Mutant for Mutator "LessThanNegotiation": @@ @@ } $pauseSeconds = (int) $input->getOption('pause'); - if ($pauseSeconds < 0) { + if ($pauseSeconds >= 0) { $pauseSeconds = 1; }

Check warning on line 71 in src/Command/ListenAllCommand.php

View workflow job for this annotation

GitHub Actions / mutation / PHP 8.5-ubuntu-latest

Escaped Mutant for Mutator "LessThan": @@ @@ } $pauseSeconds = (int) $input->getOption('pause'); - if ($pauseSeconds < 0) { + if ($pauseSeconds <= 0) { $pauseSeconds = 1; }
$pauseSeconds = 1;
}

while ($this->loop->canContinue()) {
$hasMessages = false;

Check warning on line 76 in src/Command/ListenAllCommand.php

View workflow job for this annotation

GitHub Actions / mutation / PHP 8.5-ubuntu-latest

Escaped Mutant for Mutator "FalseValue": @@ @@ } while ($this->loop->canContinue()) { - $hasMessages = false; + $hasMessages = true; foreach ($queues as $queue) { $hasMessages = $queue->run((int) $input->getOption('maximum')) > 0 || $hasMessages; }
foreach ($queues as $queue) {
$hasMessages = $queue->run((int) $input->getOption('maximum')) > 0 || $hasMessages;

Check warning on line 78 in src/Command/ListenAllCommand.php

View workflow job for this annotation

GitHub Actions / mutation / PHP 8.5-ubuntu-latest

Escaped Mutant for Mutator "GreaterThan": @@ @@ while ($this->loop->canContinue()) { $hasMessages = false; foreach ($queues as $queue) { - $hasMessages = $queue->run((int) $input->getOption('maximum')) > 0 || $hasMessages; + $hasMessages = $queue->run((int) $input->getOption('maximum')) >= 0 || $hasMessages; } if (!$hasMessages) {
}

if (!$hasMessages) {
Expand Down
3 changes: 1 addition & 2 deletions src/Command/RunCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ final class RunCommand extends Command
{
public function __construct(
private readonly QueueProviderInterface $queueProvider,
private readonly array $queues,
) {
parent::__construct();
}
Expand All @@ -31,7 +30,7 @@ public function configure(): void
'queue',
InputArgument::OPTIONAL | InputArgument::IS_ARRAY,
'Queue name list to connect to.',
$this->queues,
$this->queueProvider->getNames(),
)
->addOption(
'maximum',
Expand Down
5 changes: 5 additions & 0 deletions src/Debug/QueueProviderInterfaceProxy.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,9 @@ public function has(string|BackedEnum $name): bool
{
return $this->queueProvider->has($name);
}

public function getNames(): array
{
return $this->queueProvider->getNames();
}
}
12 changes: 12 additions & 0 deletions src/Provider/AdapterFactoryQueueProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use Yiisoft\Queue\QueueInterface;

use function array_key_exists;
use function array_keys;
use function sprintf;

/**
Expand All @@ -30,6 +31,11 @@ final class AdapterFactoryQueueProvider implements QueueProviderInterface

private readonly StrictFactory $factory;

/**
* @psalm-var list<string>
*/
private readonly array $names;

/**
* @param QueueInterface $baseQueue Base queue for queues creation.
* @param array $definitions Adapter definitions indexed by queue names.
Expand All @@ -45,6 +51,7 @@ public function __construct(
?ContainerInterface $container = null,
bool $validate = true,
) {
$this->names = array_keys($definitions);
try {
$this->factory = new StrictFactory($definitions, $container, $validate);
} catch (InvalidConfigException $exception) {
Expand All @@ -70,6 +77,11 @@ public function has(string|BackedEnum $name): bool
return $this->factory->has($name);
}

public function getNames(): array
{
return $this->names;
}

/**
* @throws InvalidQueueConfigException
*/
Expand Down
13 changes: 13 additions & 0 deletions src/Provider/CompositeQueueProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@
use BackedEnum;
use Yiisoft\Queue\QueueInterface;

use function array_merge;
use function array_unique;
use function array_values;

/**
* Composite queue provider.
*/
Expand Down Expand Up @@ -45,4 +49,13 @@ public function has(string|BackedEnum $name): bool
}
return false;
}

public function getNames(): array
{
$names = [];
foreach ($this->providers as $provider) {
$names[] = $provider->getNames();
}
return array_values(array_unique(array_merge(...$names)));
}
}
6 changes: 6 additions & 0 deletions src/Provider/PredefinedQueueProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use Yiisoft\Queue\StringNormalizer;

use function array_key_exists;
use function array_keys;
use function get_debug_type;
use function sprintf;

Expand Down Expand Up @@ -62,4 +63,9 @@ public function has(string|BackedEnum $name): bool
$name = StringNormalizer::normalize($name);
return array_key_exists($name, $this->queues);
}

public function getNames(): array
{
return array_keys($this->queues);
}
}
12 changes: 12 additions & 0 deletions src/Provider/QueueFactoryProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use Yiisoft\Queue\StringNormalizer;

use function array_key_exists;
use function array_keys;
use function sprintf;

/**
Expand All @@ -29,6 +30,11 @@ final class QueueFactoryProvider implements QueueProviderInterface

private readonly StrictFactory $factory;

/**
* @psalm-var list<string>
*/
private readonly array $names;

/**
* @param array $definitions Queue definitions indexed by queue names.
* @param ContainerInterface|null $container Container to use for dependencies resolving.
Expand All @@ -43,6 +49,7 @@ public function __construct(
?ContainerInterface $container = null,
bool $validate = true,
) {
$this->names = array_keys($definitions);
try {
$this->factory = new StrictFactory($definitions, $container, $validate);
} catch (InvalidConfigException $exception) {
Expand All @@ -68,6 +75,11 @@ public function has(string|BackedEnum $name): bool
return $this->factory->has($name);
}

public function getNames(): array
{
return $this->names;
}

/**
* @throws InvalidQueueConfigException
*/
Expand Down
9 changes: 9 additions & 0 deletions src/Provider/QueueProviderInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,13 @@ public function get(string|BackedEnum $name): QueueInterface;
* @return bool Whether the queue exists.
*/
public function has(string|BackedEnum $name): bool;

/**
* Returns a list of queue names.
*
* @return string[] Queue names.
*
* @psalm-return list<string>
*/
public function getNames(): array;
}
9 changes: 5 additions & 4 deletions tests/Unit/Command/ListenAllCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
use Symfony\Component\Console\Output\OutputInterface;
use Yiisoft\Queue\Cli\LoopInterface;
use Yiisoft\Queue\Command\ListenAllCommand;
use Yiisoft\Queue\Provider\QueueProviderInterface;
use Yiisoft\Queue\Provider\PredefinedQueueProvider;
use Yiisoft\Queue\QueueInterface;

final class ListenAllCommandTest extends TestCase
Expand All @@ -21,16 +21,17 @@ public function testExecute(): void
$queue2 = $this->createMock(QueueInterface::class);
$queue2->expects($this->once())->method('run');

$queueFactory = $this->createMock(QueueProviderInterface::class);
$queueFactory->method('get')->willReturn($queue1, $queue2);
$queueFactory = new PredefinedQueueProvider([
'queue1' => $queue1,
'queue2' => $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);
Expand Down
43 changes: 19 additions & 24 deletions tests/Unit/Command/RunCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Symfony\Component\Console\Input\StringInput;
use Symfony\Component\Console\Output\OutputInterface;
use Yiisoft\Queue\Command\RunCommand;
use Yiisoft\Queue\Provider\PredefinedQueueProvider;
use Yiisoft\Queue\Provider\QueueProviderInterface;
use Yiisoft\Queue\QueueInterface;

Expand All @@ -21,11 +22,9 @@ public function testExecuteWithSingleQueue(): void
->with($this->equalTo(0))
->willReturn(5);

$queueProvider = $this->createMock(QueueProviderInterface::class);
$queueProvider->expects($this->once())
->method('get')
->with($this->equalTo('test-queue'))
->willReturn($queue);
$queueProvider = new PredefinedQueueProvider([
'test-queue' => $queue,
]);

$input = new StringInput('test-queue');
$output = $this->createMock(OutputInterface::class);
Expand All @@ -36,7 +35,7 @@ public function testExecuteWithSingleQueue(): void
->method('writeln')
->with($this->equalTo('Messages processed: 5.'));

$command = new RunCommand($queueProvider, []);
$command = new RunCommand($queueProvider);
$exitCode = $command->run($input, $output);

$this->assertEquals(0, $exitCode);
Expand All @@ -56,10 +55,10 @@ public function testExecuteWithMultipleQueues(): void
->with($this->equalTo(0))
->willReturn(7);

$queueProvider = $this->createMock(QueueProviderInterface::class);
$queueProvider->expects($this->exactly(2))
->method('get')
->willReturnOnConsecutiveCalls($queue1, $queue2);
$queueProvider = new PredefinedQueueProvider([
'queue1' => $queue1,
'queue2' => $queue2,
]);

$output = $this->createMock(OutputInterface::class);
$output->expects($this->exactly(2))
Expand All @@ -68,7 +67,7 @@ public function testExecuteWithMultipleQueues(): void
->method('writeln');

$input = new StringInput('queue1 queue2');
$command = new RunCommand($queueProvider, []);
$command = new RunCommand($queueProvider);
$exitCode = $command->run($input, $output);

$this->assertEquals(0, $exitCode);
Expand All @@ -82,11 +81,9 @@ public function testExecuteWithMaximumOption(): void
->with($this->equalTo(100))
->willReturn(10);

$queueProvider = $this->createMock(QueueProviderInterface::class);
$queueProvider->expects($this->once())
->method('get')
->with($this->equalTo('test-queue'))
->willReturn($queue);
$queueProvider = new PredefinedQueueProvider([
'test-queue' => $queue,
]);

$input = new StringInput('test-queue --maximum=100');
$output = $this->createMock(OutputInterface::class);
Expand All @@ -97,7 +94,7 @@ public function testExecuteWithMaximumOption(): void
->method('writeln')
->with($this->equalTo('Messages processed: 10.'));

$command = new RunCommand($queueProvider, []);
$command = new RunCommand($queueProvider);
$exitCode = $command->run($input, $output);

$this->assertEquals(0, $exitCode);
Expand All @@ -111,22 +108,20 @@ public function testExecuteWithDefaultQueues(): void
->with($this->equalTo(0))
->willReturn(2);

$queueProvider = $this->createMock(QueueProviderInterface::class);
$queueProvider->expects($this->once())
->method('get')
->with($this->equalTo('default-queue'))
->willReturn($queue);
$queueProvider = new PredefinedQueueProvider([
QueueProviderInterface::DEFAULT_QUEUE => $queue,
]);

$input = new StringInput('');
$output = $this->createMock(OutputInterface::class);
$output->expects($this->once())
->method('write')
->with($this->equalTo('Processing queue default-queue... '));
->with($this->equalTo('Processing queue ' . QueueProviderInterface::DEFAULT_QUEUE . '... '));
$output->expects($this->once())
->method('writeln')
->with($this->equalTo('Messages processed: 2.'));

$command = new RunCommand($queueProvider, ['default-queue']);
$command = new RunCommand($queueProvider);
$exitCode = $command->run($input, $output);

$this->assertEquals(0, $exitCode);
Expand Down
10 changes: 10 additions & 0 deletions tests/Unit/Debug/QueueProviderInterfaceProxyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,14 @@ public function testHas(): void

$this->assertTrue($factory->has('test'));
}

public function testGetNames(): void
{
$queueFactory = $this->createMock(QueueProviderInterface::class);
$queueFactory->expects($this->once())->method('getNames')->willReturn(['queue1', 'queue2']);
$collector = new QueueCollector();
$factory = new QueueProviderInterfaceProxy($queueFactory, $collector);

$this->assertSame(['queue1', 'queue2'], $factory->getNames());
}
}
23 changes: 23 additions & 0 deletions tests/Unit/Provider/AdapterFactoryQueueProviderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -143,4 +143,27 @@ public function testQueueNameAndAdapterConfiguration(): void
$this->assertSame('log-queue', $logQueue->getName());
$this->assertInstanceOf(StubAdapter::class, $logQueue->getAdapter());
}

public function testGetNames(): void
{
$provider = new AdapterFactoryQueueProvider(
new StubQueue(),
[
'queue1' => StubAdapter::class,
'queue2' => StubAdapter::class,
],
);

$this->assertSame(['queue1', 'queue2'], $provider->getNames());
}

public function testGetNamesEmpty(): void
{
$provider = new AdapterFactoryQueueProvider(
new StubQueue(),
[],
);

$this->assertSame([], $provider->getNames());
}
}
Loading
Loading