diff --git a/src/Adapter.php b/src/Adapter.php index c503559..9978023 100644 --- a/src/Adapter.php +++ b/src/Adapter.php @@ -4,9 +4,10 @@ namespace Yiisoft\Queue\Redis; +use BackedEnum; use Yiisoft\Queue\Adapter\AdapterInterface; use Yiisoft\Queue\Cli\LoopInterface; -use Yiisoft\Queue\Enum\JobStatus; +use Yiisoft\Queue\JobStatus; use Yiisoft\Queue\Message\IdEnvelope; use Yiisoft\Queue\Message\MessageInterface; use Yiisoft\Queue\Message\MessageSerializerInterface; @@ -14,11 +15,12 @@ final class Adapter implements AdapterInterface { public function __construct( - private QueueProviderInterface $provider, + private QueueProviderInterface $provider, private MessageSerializerInterface $serializer, - private LoopInterface $loop, - private int $timeout = 3 - ) { + private LoopInterface $loop, + private int $timeout = 3 + ) + { } public function runExisting(callable $handlerCallback): void @@ -45,23 +47,21 @@ public function status(int|string $id): JobStatus } if ($this->provider->existInReserved($id)) { - return JobStatus::reserved(); + return JobStatus::RESERVED; } if ($this->provider->existInWaiting($id)) { - return JobStatus::waiting(); + return JobStatus::WAITING; } - return JobStatus::done(); + return JobStatus::DONE; } public function push(MessageInterface $message): MessageInterface { $payload = $this->serializer->serialize($message); $id = $this->provider->pushMessage($payload, $message->getMetadata()); - $envelope = IdEnvelope::fromMessage($message); - $envelope->setId($id); - return $envelope; + return new IdEnvelope($message, $id); } public function subscribe(callable $handlerCallback): void @@ -79,10 +79,11 @@ public function subscribe(callable $handlerCallback): void } } - public function withChannel(string $channel): AdapterInterface + public function withChannel(BackedEnum|string $channel): AdapterInterface { $adapter = clone $this; - $adapter->provider = $this->provider->withChannelName($channel); + $channelName = is_string($channel) ? $channel : (string) $channel->value; + $adapter->provider = $this->provider->withChannelName($channelName); return $adapter; } @@ -94,9 +95,11 @@ private function reserve(): ?IdEnvelope } $message = $this->serializer->unserialize($reserve->payload); - $envelope = IdEnvelope::fromMessage($message); - $envelope->setId($reserve->id); + return new IdEnvelope($message, $reserve->id); + } - return $envelope; + public function getChannel(): string + { + return $this->provider->getChannelName(); } } diff --git a/src/Message/Message.php b/src/Message/Message.php index 2c5cc9d..a760ef2 100644 --- a/src/Message/Message.php +++ b/src/Message/Message.php @@ -1,5 +1,4 @@ delay > 0) { $this->metadata['delay'] = $delay; } @@ -40,4 +40,9 @@ public function getMetadata(): array { return $this->metadata; } + + public static function fromData(string $handlerName, mixed $data, array $metadata = []): self + { + return new self($handlerName, $data, $metadata); + } } diff --git a/src/QueueProvider.php b/src/QueueProvider.php index 5ed38ce..a5d6c9b 100644 --- a/src/QueueProvider.php +++ b/src/QueueProvider.php @@ -152,4 +152,9 @@ private function checkConnection(): void throw new NotConnectedRedisException('Redis is not connected.'); } } + + public function getChannelName(): string + { + return $this->channelName; + } } diff --git a/src/QueueProviderInterface.php b/src/QueueProviderInterface.php index 1bce6fc..f59b523 100644 --- a/src/QueueProviderInterface.php +++ b/src/QueueProviderInterface.php @@ -20,4 +20,6 @@ public function existInWaiting(int $id): bool; public function existInReserved(int $id): bool; public function withChannelName(string $channelName): self; + + public function getChannelName(): string; } diff --git a/tests/Integration/QueueTest.php b/tests/Integration/QueueTest.php index 2882e40..196a1b3 100644 --- a/tests/Integration/QueueTest.php +++ b/tests/Integration/QueueTest.php @@ -6,7 +6,7 @@ use Yiisoft\Queue\Adapter\AdapterInterface; use Yiisoft\Queue\Cli\LoopInterface; -use Yiisoft\Queue\Enum\JobStatus; +use Yiisoft\Queue\JobStatus; use Yiisoft\Queue\Message\JsonMessageSerializer; use Yiisoft\Queue\Message\Message; use Yiisoft\Queue\Message\MessageInterface; @@ -53,12 +53,12 @@ public function testStatus(): void ); $status = $adapter->status($message->getId()); - $this->assertEquals(JobStatus::waiting(), $status); + $this->assertEquals(JobStatus::WAITING, $status); $queue->run(); $status = $adapter->status($message->getId()); - $this->assertEquals(JobStatus::done(), $status); + $this->assertEquals(JobStatus::DONE, $status); $mockReserved = $this->createMock(QueueProviderInterface::class); $mockReserved->method('existInReserved')->willReturn(true); @@ -66,7 +66,7 @@ public function testStatus(): void $queue = $this->getDefaultQueue($adapter); $status = $adapter->status('1'); - $this->assertEquals(JobStatus::reserved(), $status); + $this->assertEquals(JobStatus::RESERVED, $status); } public function testListen(): void diff --git a/tests/Unit/Message/MessageTest.php b/tests/Unit/Message/MessageTest.php index d982c14..beca2b2 100644 --- a/tests/Unit/Message/MessageTest.php +++ b/tests/Unit/Message/MessageTest.php @@ -1,5 +1,4 @@ assertNotSame($message, $delayedMessage); $this->assertEquals(5, $delayedMessage->getMetadata()['delay']); } + + public function testFromData(): void + { + $handlerName = 'test-handler'; + $data = ['key' => 'value']; + $metadata = ['custom' => 'metadata']; + + $message = Message::fromData($handlerName, $data, $metadata); + + $this->assertInstanceOf(Message::class, $message); + $this->assertEquals($handlerName, $message->getHandlerName()); + $this->assertEquals($data, $message->getData()); + $this->assertEquals($metadata, $message->getMetadata()); + } } diff --git a/tests/Unit/QueueProviderTest.php b/tests/Unit/QueueProviderTest.php index 8458639..a3bcdf9 100644 --- a/tests/Unit/QueueProviderTest.php +++ b/tests/Unit/QueueProviderTest.php @@ -61,4 +61,21 @@ public function testRedisException(): void $this->expectException(\RuntimeException::class); $provider->getId(); } + + /** + * @throws \PHPUnit\Framework\MockObject\Exception + */ + public function testGetChannelName(): void + { + // Тестируем значение по умолчанию + $redis = $this->createMock(\Redis::class); + $redis->method('isConnected')->willReturn(true); + $provider = new QueueProvider($redis); + $this->assertEquals('yii-queue', $provider->getChannelName()); + + // Тестируем пользовательское имя канала + $customChannelName = 'custom-channel'; + $provider = new QueueProvider($redis, $customChannelName); + $this->assertEquals($customChannelName, $provider->getChannelName()); + } } diff --git a/tests/Unit/QueueTest.php b/tests/Unit/QueueTest.php index 18494e3..abbee71 100644 --- a/tests/Unit/QueueTest.php +++ b/tests/Unit/QueueTest.php @@ -52,4 +52,19 @@ public function testAdapterNullMessage() }); $this->assertTrue($notUseHandler); } + + public function testGetChannel(): void + { + $expectedChannelName = 'test-channel'; + $queueProvider = $this->createMock(QueueProviderInterface::class); + $queueProvider->method('getChannelName')->willReturn($expectedChannelName); + + $adapter = new Adapter( + $queueProvider, + $this->createMock(MessageSerializerInterface::class), + $this->createMock(LoopInterface::class) + ); + + $this->assertEquals($expectedChannelName, $adapter->getChannel()); + } }