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..da19d5c7 100644 --- a/src/Adapter/SynchronousAdapter.php +++ b/src/Adapter/SynchronousAdapter.php @@ -4,6 +4,7 @@ namespace Yiisoft\Queue\Adapter; +use BackedEnum; use InvalidArgumentException; use Yiisoft\Queue\Enum\JobStatus; use Yiisoft\Queue\Message\MessageInterface; @@ -74,8 +75,10 @@ public function subscribe(callable $handlerCallback): void $this->runExisting($handlerCallback); } - public function withChannel(string $channel): self + public function withChannel(string|BackedEnum $channel): self { + $channel = $channel instanceof BackedEnum ? (string) $channel->value : $channel; + if ($channel === $this->channel) { return $this; } diff --git a/stubs/StubAdapter.php b/stubs/StubAdapter.php index 0a8e06b2..3d7c7a9a 100644 --- a/stubs/StubAdapter.php +++ b/stubs/StubAdapter.php @@ -4,6 +4,7 @@ namespace Yiisoft\Queue\Stubs; +use BackedEnum; use Yiisoft\Queue\Adapter\AdapterInterface; use Yiisoft\Queue\Enum\JobStatus; use Yiisoft\Queue\Message\MessageInterface; @@ -36,11 +37,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 = $channel instanceof BackedEnum ? (string) $channel->value : $channel; return $new; } diff --git a/tests/App/FakeAdapter.php b/tests/App/FakeAdapter.php index aec34910..a8933689 100644 --- a/tests/App/FakeAdapter.php +++ b/tests/App/FakeAdapter.php @@ -4,6 +4,7 @@ namespace Yiisoft\Queue\Tests\App; +use BackedEnum; use Yiisoft\Queue\Adapter\AdapterInterface; use Yiisoft\Queue\Enum\JobStatus; use Yiisoft\Queue\Message\MessageInterface; @@ -35,12 +36,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 = $channel instanceof BackedEnum ? (string) $channel->value : $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 77% rename from tests/Unit/SynchronousAdapterTest.php rename to tests/Unit/Adapter/SynchronousAdapterTest.php index 6e913932..619a0288 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,19 @@ public function testStatusNotMessage(): void $this->expectExceptionMessage('There is no message with the given ID.'); $adapter->status('1'); } + + public static function dataWithChannel(): iterable + { + yield 'string' => ['test', 'test']; + yield 'string-enum' => ['red', StringEnum::RED]; + yield 'integer-enum' => ['1', IntEnum::ONE]; + } + + #[DataProvider('dataWithChannel')] + public function testWithChannel(string $expected, mixed $channel): void + { + $adapter = (new SynchronousAdapter(new StubWorker(), new StubQueue()))->withChannel($channel); + + $this->assertSame($expected, $adapter->getChannelName()); + } } diff --git a/tests/Unit/Stubs/StubAdapterTest.php b/tests/Unit/Stubs/StubAdapterTest.php index b7337967..65e36b2a 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,19 @@ public function testBase(): void $adapter->runExisting(static fn() => null); $adapter->subscribe(static fn() => null); } + + public static function dataWithChannel(): iterable + { + yield 'string' => ['test', 'test']; + yield 'string-enum' => ['red', StringEnum::RED]; + yield 'integer-enum' => ['1', IntEnum::ONE]; + } + + #[DataProvider('dataWithChannel')] + public function testWithChannel(string $expected, mixed $channel): void + { + $adapter = (new StubAdapter())->withChannel($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 @@ +