diff --git a/composer.json b/composer.json index 9812ea48..3a570e00 100644 --- a/composer.json +++ b/composer.json @@ -40,7 +40,7 @@ "psr/container": "^1.0|^2.0", "psr/log": "^2.0|^3.0", "symfony/console": "^5.4|^6.0", - "yiisoft/definitions": "^1.0|^2.0|^3.0", + "yiisoft/definitions": "^3.3.1", "yiisoft/factory": "^1.3", "yiisoft/friendly-exception": "^1.0", "yiisoft/injector": "^1.0" diff --git a/src/Adapter/AdapterInterface.php b/src/Adapter/AdapterInterface.php index 0d8ceeda..fd6ca02a 100644 --- a/src/Adapter/AdapterInterface.php +++ b/src/Adapter/AdapterInterface.php @@ -4,6 +4,7 @@ namespace Yiisoft\Queue\Adapter; +use BackedEnum; use InvalidArgumentException; use Yiisoft\Queue\Enum\JobStatus; use Yiisoft\Queue\Message\MessageInterface; @@ -40,7 +41,7 @@ public function push(MessageInterface $message): MessageInterface; */ public function subscribe(callable $handlerCallback): void; - public function withChannel(string $channel): self; + public function withChannel(string|BackedEnum $channel): self; public function getChannelName(): string; } diff --git a/src/Adapter/SynchronousAdapter.php b/src/Adapter/SynchronousAdapter.php index 84148941..82ebde2a 100644 --- a/src/Adapter/SynchronousAdapter.php +++ b/src/Adapter/SynchronousAdapter.php @@ -4,7 +4,9 @@ namespace Yiisoft\Queue\Adapter; +use BackedEnum; use InvalidArgumentException; +use Yiisoft\Queue\ChannelNormalizer; use Yiisoft\Queue\Enum\JobStatus; use Yiisoft\Queue\Message\MessageInterface; use Yiisoft\Queue\QueueInterface; @@ -15,12 +17,14 @@ final class SynchronousAdapter implements AdapterInterface { private array $messages = []; private int $current = 0; + private string $channel; public function __construct( private WorkerInterface $worker, private QueueInterface $queue, - private string $channel = QueueInterface::DEFAULT_CHANNEL_NAME, + string|BackedEnum $channel = QueueInterface::DEFAULT_CHANNEL_NAME, ) { + $this->channel = ChannelNormalizer::normalize($channel); } public function __destruct() @@ -74,8 +78,10 @@ public function subscribe(callable $handlerCallback): void $this->runExisting($handlerCallback); } - public function withChannel(string $channel): self + public function withChannel(string|BackedEnum $channel): self { + $channel = ChannelNormalizer::normalize($channel); + if ($channel === $this->channel) { return $this; } diff --git a/src/ChannelNormalizer.php b/src/ChannelNormalizer.php new file mode 100644 index 00000000..1b178188 --- /dev/null +++ b/src/ChannelNormalizer.php @@ -0,0 +1,18 @@ +value : $channel; + } +} diff --git a/src/Debug/QueueProviderInterfaceProxy.php b/src/Debug/QueueProviderInterfaceProxy.php index 5f00096e..22ce1307 100644 --- a/src/Debug/QueueProviderInterfaceProxy.php +++ b/src/Debug/QueueProviderInterfaceProxy.php @@ -4,6 +4,7 @@ namespace Yiisoft\Queue\Debug; +use BackedEnum; use Yiisoft\Queue\Provider\QueueProviderInterface; use Yiisoft\Queue\QueueInterface; @@ -15,13 +16,13 @@ public function __construct( ) { } - public function get(string $channel): QueueInterface + public function get(string|BackedEnum $channel): QueueInterface { $queue = $this->queueProvider->get($channel); return new QueueDecorator($queue, $this->collector); } - public function has(string $channel): bool + public function has(string|BackedEnum $channel): bool { return $this->queueProvider->has($channel); } diff --git a/src/Provider/AdapterFactoryQueueProvider.php b/src/Provider/AdapterFactoryQueueProvider.php index 7f9cca30..36c2a193 100644 --- a/src/Provider/AdapterFactoryQueueProvider.php +++ b/src/Provider/AdapterFactoryQueueProvider.php @@ -4,10 +4,12 @@ namespace Yiisoft\Queue\Provider; +use BackedEnum; use Psr\Container\ContainerInterface; use Yiisoft\Definitions\Exception\InvalidConfigException; use Yiisoft\Factory\StrictFactory; use Yiisoft\Queue\Adapter\AdapterInterface; +use Yiisoft\Queue\ChannelNormalizer; use Yiisoft\Queue\QueueInterface; use function array_key_exists; @@ -50,17 +52,21 @@ public function __construct( } } - public function get(string $channel): QueueInterface + public function get(string|BackedEnum $channel): QueueInterface { + $channel = ChannelNormalizer::normalize($channel); + $queue = $this->getOrTryToCreate($channel); if ($queue === null) { throw new ChannelNotFoundException($channel); } + return $queue; } - public function has(string $channel): bool + public function has(string|BackedEnum $channel): bool { + $channel = ChannelNormalizer::normalize($channel); return $this->factory->has($channel); } diff --git a/src/Provider/ChannelNotFoundException.php b/src/Provider/ChannelNotFoundException.php index 2538cda7..ef9b3ace 100644 --- a/src/Provider/ChannelNotFoundException.php +++ b/src/Provider/ChannelNotFoundException.php @@ -4,8 +4,10 @@ namespace Yiisoft\Queue\Provider; +use BackedEnum; use LogicException; use Throwable; +use Yiisoft\Queue\ChannelNormalizer; use function sprintf; @@ -14,10 +16,10 @@ */ final class ChannelNotFoundException extends LogicException implements QueueProviderException { - public function __construct(string $channel, int $code = 0, ?Throwable $previous = null) + public function __construct(string|BackedEnum $channel, int $code = 0, ?Throwable $previous = null) { parent::__construct( - sprintf('Channel "%s" not found.', $channel), + sprintf('Channel "%s" not found.', ChannelNormalizer::normalize($channel)), $code, $previous, ); diff --git a/src/Provider/CompositeQueueProvider.php b/src/Provider/CompositeQueueProvider.php index cd7f36ca..f699d256 100644 --- a/src/Provider/CompositeQueueProvider.php +++ b/src/Provider/CompositeQueueProvider.php @@ -4,6 +4,7 @@ namespace Yiisoft\Queue\Provider; +use BackedEnum; use Yiisoft\Queue\QueueInterface; /** @@ -25,7 +26,7 @@ public function __construct( $this->providers = $providers; } - public function get(string $channel): QueueInterface + public function get(string|BackedEnum $channel): QueueInterface { foreach ($this->providers as $provider) { if ($provider->has($channel)) { @@ -35,7 +36,7 @@ public function get(string $channel): QueueInterface throw new ChannelNotFoundException($channel); } - public function has(string $channel): bool + public function has(string|BackedEnum $channel): bool { foreach ($this->providers as $provider) { if ($provider->has($channel)) { diff --git a/src/Provider/PrototypeQueueProvider.php b/src/Provider/PrototypeQueueProvider.php index 4b7a2d4b..929d124a 100644 --- a/src/Provider/PrototypeQueueProvider.php +++ b/src/Provider/PrototypeQueueProvider.php @@ -4,6 +4,7 @@ namespace Yiisoft\Queue\Provider; +use BackedEnum; use Yiisoft\Queue\Adapter\AdapterInterface; use Yiisoft\Queue\QueueInterface; @@ -22,12 +23,12 @@ public function __construct( ) { } - public function get(string $channel): QueueInterface + public function get(string|BackedEnum $channel): QueueInterface { return $this->baseQueue->withAdapter($this->baseAdapter->withChannel($channel)); } - public function has(string $channel): bool + public function has(string|BackedEnum $channel): bool { return true; } diff --git a/src/Provider/QueueProviderInterface.php b/src/Provider/QueueProviderInterface.php index 7d3501b7..d833a84b 100644 --- a/src/Provider/QueueProviderInterface.php +++ b/src/Provider/QueueProviderInterface.php @@ -4,6 +4,7 @@ namespace Yiisoft\Queue\Provider; +use BackedEnum; use Yiisoft\Queue\QueueInterface; /** @@ -14,21 +15,21 @@ interface QueueProviderInterface /** * Find a queue by channel name and returns it. * - * @param string $channel Channel name. + * @param BackedEnum|string $channel Channel name. * * @throws InvalidQueueConfigException If the queue configuration is invalid. * @throws ChannelNotFoundException If the channel is not found. * @throws QueueProviderException If the queue provider fails to provide a queue. * @return QueueInterface Queue instance. */ - public function get(string $channel): QueueInterface; + public function get(string|BackedEnum $channel): QueueInterface; /** * Check if a queue with the specified channel name exists. * - * @param string $channel Channel name. + * @param BackedEnum|string $channel Channel name. * * @return bool Whether the queue exists. */ - public function has(string $channel): bool; + public function has(string|BackedEnum $channel): bool; } diff --git a/stubs/StubAdapter.php b/stubs/StubAdapter.php index 0a8e06b2..1142f173 100644 --- a/stubs/StubAdapter.php +++ b/stubs/StubAdapter.php @@ -4,7 +4,9 @@ namespace Yiisoft\Queue\Stubs; +use BackedEnum; use Yiisoft\Queue\Adapter\AdapterInterface; +use Yiisoft\Queue\ChannelNormalizer; use Yiisoft\Queue\Enum\JobStatus; use Yiisoft\Queue\Message\MessageInterface; use Yiisoft\Queue\QueueInterface; @@ -14,8 +16,12 @@ */ final class StubAdapter implements AdapterInterface { - public function __construct(private string $channelName = QueueInterface::DEFAULT_CHANNEL_NAME) - { + private string $channelName; + + public function __construct( + string|BackedEnum $channelName = QueueInterface::DEFAULT_CHANNEL_NAME + ) { + $this->channelName = ChannelNormalizer::normalize($channelName); } public function runExisting(callable $handlerCallback): void @@ -36,11 +42,10 @@ public function subscribe(callable $handlerCallback): void { } - public function withChannel(string $channel): AdapterInterface + public function withChannel(string|BackedEnum $channel): AdapterInterface { $new = clone $this; - $new->channelName = $channel; - + $new->channelName = ChannelNormalizer::normalize($channel); return $new; } diff --git a/tests/App/FakeAdapter.php b/tests/App/FakeAdapter.php index aec34910..237fef6d 100644 --- a/tests/App/FakeAdapter.php +++ b/tests/App/FakeAdapter.php @@ -4,7 +4,9 @@ namespace Yiisoft\Queue\Tests\App; +use BackedEnum; use Yiisoft\Queue\Adapter\AdapterInterface; +use Yiisoft\Queue\ChannelNormalizer; use Yiisoft\Queue\Enum\JobStatus; use Yiisoft\Queue\Message\MessageInterface; @@ -35,12 +37,11 @@ public function subscribe(callable $handlerCallback): void //skip } - public function withChannel(string $channel): AdapterInterface + public function withChannel(string|BackedEnum $channel): AdapterInterface { $instance = clone $this; $instance->pushMessages = []; - $instance->channel = $channel; - + $instance->channel = ChannelNormalizer::normalize($channel); return $instance; } diff --git a/tests/Benchmark/Support/VoidAdapter.php b/tests/Benchmark/Support/VoidAdapter.php index b1a42966..6d074811 100644 --- a/tests/Benchmark/Support/VoidAdapter.php +++ b/tests/Benchmark/Support/VoidAdapter.php @@ -4,6 +4,7 @@ namespace Yiisoft\Queue\Tests\Benchmark\Support; +use BackedEnum; use InvalidArgumentException; use RuntimeException; use Yiisoft\Queue\Adapter\AdapterInterface; @@ -45,7 +46,7 @@ public function subscribe(callable $handlerCallback): void throw new RuntimeException('Method is not implemented'); } - public function withChannel(string $channel): AdapterInterface + public function withChannel(string|BackedEnum $channel): AdapterInterface { throw new RuntimeException('Method is not implemented'); } diff --git a/tests/Unit/SynchronousAdapterTest.php b/tests/Unit/Adapter/SynchronousAdapterTest.php similarity index 71% rename from tests/Unit/SynchronousAdapterTest.php rename to tests/Unit/Adapter/SynchronousAdapterTest.php index 6e913932..35e0f0a5 100644 --- a/tests/Unit/SynchronousAdapterTest.php +++ b/tests/Unit/Adapter/SynchronousAdapterTest.php @@ -2,13 +2,19 @@ declare(strict_types=1); -namespace Yiisoft\Queue\Tests\Unit; +namespace Yiisoft\Queue\Tests\Unit\Adapter; +use PHPUnit\Framework\Attributes\DataProvider; +use Yiisoft\Queue\Adapter\SynchronousAdapter; use Yiisoft\Queue\Enum\JobStatus; +use Yiisoft\Queue\Message\IdEnvelope; use Yiisoft\Queue\Message\Message; use Yiisoft\Queue\QueueInterface; +use Yiisoft\Queue\Stubs\StubQueue; +use Yiisoft\Queue\Stubs\StubWorker; use Yiisoft\Queue\Tests\TestCase; -use Yiisoft\Queue\Message\IdEnvelope; +use Yiisoft\Queue\Tests\Unit\Support\IntEnum; +use Yiisoft\Queue\Tests\Unit\Support\StringEnum; final class SynchronousAdapterTest extends TestCase { @@ -92,4 +98,27 @@ public function testStatusNotMessage(): void $this->expectExceptionMessage('There is no message with the given ID.'); $adapter->status('1'); } + + public static function dataChannels(): iterable + { + yield 'string' => ['test', 'test']; + yield 'string-enum' => ['red', StringEnum::RED]; + yield 'integer-enum' => ['1', IntEnum::ONE]; + } + + #[DataProvider('dataChannels')] + public function testWithChannel(string $expected, mixed $channel): void + { + $adapter = (new SynchronousAdapter(new StubWorker(), new StubQueue()))->withChannel($channel); + + $this->assertSame($expected, $adapter->getChannelName()); + } + + #[DataProvider('dataChannels')] + public function testChannelInConstructor(string $expected, mixed $channel): void + { + $adapter = new SynchronousAdapter(new StubWorker(), new StubQueue(), $channel); + + $this->assertSame($expected, $adapter->getChannelName()); + } } diff --git a/tests/Unit/Provider/AdapterFactoryQueueProviderTest.php b/tests/Unit/Provider/AdapterFactoryQueueProviderTest.php index 24ca9096..94900a66 100644 --- a/tests/Unit/Provider/AdapterFactoryQueueProviderTest.php +++ b/tests/Unit/Provider/AdapterFactoryQueueProviderTest.php @@ -12,6 +12,7 @@ use Yiisoft\Queue\Provider\InvalidQueueConfigException; use Yiisoft\Queue\Stubs\StubQueue; use Yiisoft\Queue\Stubs\StubAdapter; +use Yiisoft\Queue\Tests\Unit\Support\StringEnum; use function sprintf; @@ -100,4 +101,20 @@ public function testInvalidQueueConfigOnGet(): void ); $provider->get('channel1'); } + + public function testGetHasByStringEnum(): void + { + $provider = new AdapterFactoryQueueProvider( + new StubQueue(), + [ + 'red' => StubAdapter::class, + ], + ); + + $queue = $provider->get(StringEnum::RED); + + $this->assertSame('red', $queue->getChannelName()); + $this->assertTrue($provider->has(StringEnum::RED)); + $this->assertFalse($provider->has(StringEnum::GREEN)); + } } diff --git a/tests/Unit/Provider/ChannelNotFoundExceptionTest.php b/tests/Unit/Provider/ChannelNotFoundExceptionTest.php new file mode 100644 index 00000000..699a94b2 --- /dev/null +++ b/tests/Unit/Provider/ChannelNotFoundExceptionTest.php @@ -0,0 +1,30 @@ + ['channel1', 'channel1']; + yield 'string-enum' => ['red', StringEnum::RED]; + } + + #[DataProvider('dataBase')] + public function testBase(string $expectedChannelName, mixed $channel): void + { + $exception = new ChannelNotFoundException($channel); + + $this->assertSame( + 'Channel "' . $expectedChannelName . '" not found.', + $exception->getMessage(), + ); + } +} diff --git a/tests/Unit/Stubs/StubAdapterTest.php b/tests/Unit/Stubs/StubAdapterTest.php index b7337967..aad5c799 100644 --- a/tests/Unit/Stubs/StubAdapterTest.php +++ b/tests/Unit/Stubs/StubAdapterTest.php @@ -4,9 +4,12 @@ namespace Yiisoft\Queue\Tests\Unit\Stubs; +use PHPUnit\Framework\Attributes\DataProvider; use PHPUnit\Framework\TestCase; use Yiisoft\Queue\Message\Message; use Yiisoft\Queue\Stubs\StubAdapter; +use Yiisoft\Queue\Tests\Unit\Support\IntEnum; +use Yiisoft\Queue\Tests\Unit\Support\StringEnum; final class StubAdapterTest extends TestCase { @@ -21,4 +24,27 @@ public function testBase(): void $adapter->runExisting(static fn() => null); $adapter->subscribe(static fn() => null); } + + public static function dataChannels(): iterable + { + yield 'string' => ['test', 'test']; + yield 'string-enum' => ['red', StringEnum::RED]; + yield 'integer-enum' => ['1', IntEnum::ONE]; + } + + #[DataProvider('dataChannels')] + public function testWithChannel(string $expected, mixed $channel): void + { + $adapter = (new StubAdapter())->withChannel($channel); + + $this->assertSame($expected, $adapter->getChannelName()); + } + + #[DataProvider('dataChannels')] + public function testChannelInConstructor(string $expected, mixed $channel): void + { + $adapter = new StubAdapter($channel); + + $this->assertSame($expected, $adapter->getChannelName()); + } } diff --git a/tests/Unit/Support/IntEnum.php b/tests/Unit/Support/IntEnum.php new file mode 100644 index 00000000..9f8cdb85 --- /dev/null +++ b/tests/Unit/Support/IntEnum.php @@ -0,0 +1,11 @@ +