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
65 changes: 65 additions & 0 deletions src/Provider/PredefinedQueueProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<?php

declare(strict_types=1);

namespace Yiisoft\Queue\Provider;

use BackedEnum;
use Yiisoft\Queue\QueueInterface;
use Yiisoft\Queue\StringNormalizer;

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

/**
* Queue provider that uses a pre-defined map of queue name to queue instance.
*/
final class PredefinedQueueProvider implements QueueProviderInterface
{
/**
* @psalm-var array<string, QueueInterface>
*/
private readonly array $queues;

/**
* @param array $queues Map of queue name to queue instance.
*
* @psalm-param array<string, QueueInterface> $queues
*
* @throws InvalidQueueConfigException If a value in the array is not a {@see QueueInterface} instance.
*/
public function __construct(array $queues)
{
foreach ($queues as $name => $queue) {
if (!$queue instanceof QueueInterface) {
throw new InvalidQueueConfigException(
sprintf(
'Queue must implement "%s". For queue "%s" got "%s" instead.',
QueueInterface::class,
$name,
get_debug_type($queue),
),
);
}
}
$this->queues = $queues;
}

public function get(string|BackedEnum $name): QueueInterface
{
$name = StringNormalizer::normalize($name);

if (!array_key_exists($name, $this->queues)) {
throw new QueueNotFoundException($name);
}

return $this->queues[$name];
}

public function has(string|BackedEnum $name): bool
{
$name = StringNormalizer::normalize($name);
return array_key_exists($name, $this->queues);
}
}
32 changes: 13 additions & 19 deletions tests/Unit/Provider/CompositeQueueProviderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace Yiisoft\Queue\Tests\Unit\Provider;

use Yiisoft\Queue\Provider\AdapterFactoryQueueProvider;
use Yiisoft\Queue\Provider\PredefinedQueueProvider;
use Yiisoft\Queue\Provider\QueueNotFoundException;
use Yiisoft\Queue\Provider\CompositeQueueProvider;
use Yiisoft\Queue\Stubs\StubAdapter;
Expand All @@ -15,33 +15,27 @@ final class CompositeQueueProviderTest extends TestCase
{
public function testBase(): void
{
$queue = new StubQueue(new StubAdapter());
$queue1 = new StubQueue(new StubAdapter());
$queue2 = new StubQueue(new StubAdapter());
$provider = new CompositeQueueProvider(
new AdapterFactoryQueueProvider(
$queue,
['channel1' => new StubAdapter()],
),
new AdapterFactoryQueueProvider(
$queue,
['channel2' => new StubAdapter()],
),
new PredefinedQueueProvider(['queue1' => $queue1]),
new PredefinedQueueProvider(['queue2' => $queue2]),
);

$this->assertTrue($provider->has('channel1'));
$this->assertTrue($provider->has('channel2'));
$this->assertFalse($provider->has('channel3'));
$this->assertTrue($provider->has('queue1'));
$this->assertTrue($provider->has('queue2'));
$this->assertFalse($provider->has('queue3'));

$this->assertSame('channel1', $provider->get('channel1')->getName());
$this->assertSame('channel2', $provider->get('channel2')->getName());
$this->assertSame($queue1, $provider->get('queue1'));
$this->assertSame($queue2, $provider->get('queue2'));
}

public function testNotFound(): void
{
$provider = new CompositeQueueProvider(
new AdapterFactoryQueueProvider(
new StubQueue(new StubAdapter()),
['channel1' => new StubAdapter()],
),
new PredefinedQueueProvider([
'queue1' => new StubQueue(new StubAdapter()),
]),
);

$this->expectException(QueueNotFoundException::class);
Expand Down
91 changes: 91 additions & 0 deletions tests/Unit/Provider/PredefinedQueueProviderTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
<?php

declare(strict_types=1);

namespace Yiisoft\Queue\Tests\Unit\Provider;

use PHPUnit\Framework\TestCase;
use Yiisoft\Queue\Provider\InvalidQueueConfigException;
use Yiisoft\Queue\Provider\QueueNotFoundException;
use Yiisoft\Queue\Provider\PredefinedQueueProvider;
use Yiisoft\Queue\QueueInterface;
use Yiisoft\Queue\Stubs\StubQueue;
use Yiisoft\Queue\Tests\Unit\Support\StringEnum;

use stdClass;

use function sprintf;

final class PredefinedQueueProviderTest extends TestCase
{
public function testBase(): void
{
$queue = new StubQueue();
$provider = new PredefinedQueueProvider([
'queue1' => $queue,
]);

$this->assertSame($queue, $provider->get('queue1'));
$this->assertTrue($provider->has('queue1'));
$this->assertFalse($provider->has('not-exist-queue'));
}

public function testGetTwice(): void
{
$queue = new StubQueue();
$provider = new PredefinedQueueProvider([
'queue1' => $queue,
]);

$queue1 = $provider->get('queue1');
$queue2 = $provider->get('queue1');

$this->assertSame($queue1, $queue2);
}

public function testGetNotExistQueue(): void
{
$provider = new PredefinedQueueProvider([
'queue1' => new StubQueue(),
]);

$this->expectException(QueueNotFoundException::class);
$this->expectExceptionMessage('Queue with name "not-exist-queue" not found.');
$provider->get('not-exist-queue');
}

public function testInvalidQueueConfig(): void
{
$this->expectException(InvalidQueueConfigException::class);
$this->expectExceptionMessage(
sprintf(
'Queue must implement "%s". For queue "%s" got "%s" instead.',
QueueInterface::class,
'queue1',
'stdClass',
),
);
new PredefinedQueueProvider([
'queue1' => new stdClass(),
]);
}

public function testGetHasByStringEnum(): void
{
$queue = new StubQueue();
$provider = new PredefinedQueueProvider([
'red' => $queue,
]);

$this->assertSame($queue, $provider->get(StringEnum::RED));
$this->assertTrue($provider->has(StringEnum::RED));
$this->assertFalse($provider->has(StringEnum::GREEN));
}

public function testEmpty(): void
{
$provider = new PredefinedQueueProvider([]);

$this->assertFalse($provider->has('any'));
}
}
Loading