Skip to content

Commit 1c449a8

Browse files
authored
Updating the implementation of dependencies. (#7)
1 parent cfa7a08 commit 1c449a8

File tree

8 files changed

+86
-26
lines changed

8 files changed

+86
-26
lines changed

src/Adapter.php

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,23 @@
44

55
namespace Yiisoft\Queue\Redis;
66

7+
use BackedEnum;
78
use Yiisoft\Queue\Adapter\AdapterInterface;
89
use Yiisoft\Queue\Cli\LoopInterface;
9-
use Yiisoft\Queue\Enum\JobStatus;
10+
use Yiisoft\Queue\JobStatus;
1011
use Yiisoft\Queue\Message\IdEnvelope;
1112
use Yiisoft\Queue\Message\MessageInterface;
1213
use Yiisoft\Queue\Message\MessageSerializerInterface;
1314

1415
final class Adapter implements AdapterInterface
1516
{
1617
public function __construct(
17-
private QueueProviderInterface $provider,
18+
private QueueProviderInterface $provider,
1819
private MessageSerializerInterface $serializer,
19-
private LoopInterface $loop,
20-
private int $timeout = 3
21-
) {
20+
private LoopInterface $loop,
21+
private int $timeout = 3
22+
)
23+
{
2224
}
2325

2426
public function runExisting(callable $handlerCallback): void
@@ -45,23 +47,21 @@ public function status(int|string $id): JobStatus
4547
}
4648

4749
if ($this->provider->existInReserved($id)) {
48-
return JobStatus::reserved();
50+
return JobStatus::RESERVED;
4951
}
5052

5153
if ($this->provider->existInWaiting($id)) {
52-
return JobStatus::waiting();
54+
return JobStatus::WAITING;
5355
}
5456

55-
return JobStatus::done();
57+
return JobStatus::DONE;
5658
}
5759

5860
public function push(MessageInterface $message): MessageInterface
5961
{
6062
$payload = $this->serializer->serialize($message);
6163
$id = $this->provider->pushMessage($payload, $message->getMetadata());
62-
$envelope = IdEnvelope::fromMessage($message);
63-
$envelope->setId($id);
64-
return $envelope;
64+
return new IdEnvelope($message, $id);
6565
}
6666

6767
public function subscribe(callable $handlerCallback): void
@@ -79,10 +79,11 @@ public function subscribe(callable $handlerCallback): void
7979
}
8080
}
8181

82-
public function withChannel(string $channel): AdapterInterface
82+
public function withChannel(BackedEnum|string $channel): AdapterInterface
8383
{
8484
$adapter = clone $this;
85-
$adapter->provider = $this->provider->withChannelName($channel);
85+
$channelName = is_string($channel) ? $channel : (string) $channel->value;
86+
$adapter->provider = $this->provider->withChannelName($channelName);
8687
return $adapter;
8788
}
8889

@@ -94,9 +95,11 @@ private function reserve(): ?IdEnvelope
9495
}
9596

9697
$message = $this->serializer->unserialize($reserve->payload);
97-
$envelope = IdEnvelope::fromMessage($message);
98-
$envelope->setId($reserve->id);
98+
return new IdEnvelope($message, $reserve->id);
99+
}
99100

100-
return $envelope;
101+
public function getChannel(): string
102+
{
103+
return $this->provider->getChannelName();
101104
}
102105
}

src/Message/Message.php

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
<?php
2-
32
declare(strict_types=1);
43

54
namespace Yiisoft\Queue\Redis\Message;
@@ -10,10 +9,11 @@ final class Message implements MessageInterface
109
{
1110
public function __construct(
1211
private string $handlerName,
13-
private mixed $data,
14-
private array $metadata,
15-
private int $delay = 0 //delay in seconds
16-
) {
12+
private mixed $data,
13+
private array $metadata,
14+
private int $delay = 0 //delay in seconds
15+
)
16+
{
1717
if ($this->delay > 0) {
1818
$this->metadata['delay'] = $delay;
1919
}
@@ -40,4 +40,9 @@ public function getMetadata(): array
4040
{
4141
return $this->metadata;
4242
}
43+
44+
public static function fromData(string $handlerName, mixed $data, array $metadata = []): self
45+
{
46+
return new self($handlerName, $data, $metadata);
47+
}
4348
}

src/QueueProvider.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,4 +152,9 @@ private function checkConnection(): void
152152
throw new NotConnectedRedisException('Redis is not connected.');
153153
}
154154
}
155+
156+
public function getChannelName(): string
157+
{
158+
return $this->channelName;
159+
}
155160
}

src/QueueProviderInterface.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,6 @@ public function existInWaiting(int $id): bool;
2020
public function existInReserved(int $id): bool;
2121

2222
public function withChannelName(string $channelName): self;
23+
24+
public function getChannelName(): string;
2325
}

tests/Integration/QueueTest.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
use Yiisoft\Queue\Adapter\AdapterInterface;
88
use Yiisoft\Queue\Cli\LoopInterface;
9-
use Yiisoft\Queue\Enum\JobStatus;
9+
use Yiisoft\Queue\JobStatus;
1010
use Yiisoft\Queue\Message\JsonMessageSerializer;
1111
use Yiisoft\Queue\Message\Message;
1212
use Yiisoft\Queue\Message\MessageInterface;
@@ -53,20 +53,20 @@ public function testStatus(): void
5353
);
5454

5555
$status = $adapter->status($message->getId());
56-
$this->assertEquals(JobStatus::waiting(), $status);
56+
$this->assertEquals(JobStatus::WAITING, $status);
5757

5858
$queue->run();
5959

6060
$status = $adapter->status($message->getId());
61-
$this->assertEquals(JobStatus::done(), $status);
61+
$this->assertEquals(JobStatus::DONE, $status);
6262

6363
$mockReserved = $this->createMock(QueueProviderInterface::class);
6464
$mockReserved->method('existInReserved')->willReturn(true);
6565
$adapter = new Adapter($mockReserved, new JsonMessageSerializer(), $this->getLoop());
6666
$queue = $this->getDefaultQueue($adapter);
6767

6868
$status = $adapter->status('1');
69-
$this->assertEquals(JobStatus::reserved(), $status);
69+
$this->assertEquals(JobStatus::RESERVED, $status);
7070
}
7171

7272
public function testListen(): void

tests/Unit/Message/MessageTest.php

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
<?php
2-
32
declare(strict_types=1);
43

54
namespace Unit\Message;
@@ -40,4 +39,18 @@ public function testWithDelay(): void
4039
$this->assertNotSame($message, $delayedMessage);
4140
$this->assertEquals(5, $delayedMessage->getMetadata()['delay']);
4241
}
42+
43+
public function testFromData(): void
44+
{
45+
$handlerName = 'test-handler';
46+
$data = ['key' => 'value'];
47+
$metadata = ['custom' => 'metadata'];
48+
49+
$message = Message::fromData($handlerName, $data, $metadata);
50+
51+
$this->assertInstanceOf(Message::class, $message);
52+
$this->assertEquals($handlerName, $message->getHandlerName());
53+
$this->assertEquals($data, $message->getData());
54+
$this->assertEquals($metadata, $message->getMetadata());
55+
}
4356
}

tests/Unit/QueueProviderTest.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,4 +61,21 @@ public function testRedisException(): void
6161
$this->expectException(\RuntimeException::class);
6262
$provider->getId();
6363
}
64+
65+
/**
66+
* @throws \PHPUnit\Framework\MockObject\Exception
67+
*/
68+
public function testGetChannelName(): void
69+
{
70+
// Тестируем значение по умолчанию
71+
$redis = $this->createMock(\Redis::class);
72+
$redis->method('isConnected')->willReturn(true);
73+
$provider = new QueueProvider($redis);
74+
$this->assertEquals('yii-queue', $provider->getChannelName());
75+
76+
// Тестируем пользовательское имя канала
77+
$customChannelName = 'custom-channel';
78+
$provider = new QueueProvider($redis, $customChannelName);
79+
$this->assertEquals($customChannelName, $provider->getChannelName());
80+
}
6481
}

tests/Unit/QueueTest.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,4 +52,19 @@ public function testAdapterNullMessage()
5252
});
5353
$this->assertTrue($notUseHandler);
5454
}
55+
56+
public function testGetChannel(): void
57+
{
58+
$expectedChannelName = 'test-channel';
59+
$queueProvider = $this->createMock(QueueProviderInterface::class);
60+
$queueProvider->method('getChannelName')->willReturn($expectedChannelName);
61+
62+
$adapter = new Adapter(
63+
$queueProvider,
64+
$this->createMock(MessageSerializerInterface::class),
65+
$this->createMock(LoopInterface::class)
66+
);
67+
68+
$this->assertEquals($expectedChannelName, $adapter->getChannel());
69+
}
5570
}

0 commit comments

Comments
 (0)