|
3 | 3 | * This file is part of Berlioz framework. |
4 | 4 | * |
5 | 5 | * @license https://opensource.org/licenses/MIT MIT License |
6 | | - * @copyright 2025 Ronan GIRON |
| 6 | + * @copyright 2024 Ronan GIRON |
7 | 7 | * @author Ronan GIRON <https://github.com/ElGigi> |
8 | 8 | * |
9 | 9 | * For the full copyright and license information, please view the LICENSE |
|
12 | 12 |
|
13 | 13 | namespace Berlioz\QueueManager\Tests\Queue; |
14 | 14 |
|
15 | | -use Berlioz\QueueManager\Job\JobDescriptorInterface; |
16 | | -use Berlioz\QueueManager\Job\RedisJob; |
| 15 | +use Berlioz\QueueManager\Queue\QueueInterface; |
17 | 16 | use Berlioz\QueueManager\Queue\RedisQueue; |
18 | | -use PHPUnit\Framework\TestCase; |
| 17 | +use PHPUnit\Framework\Attributes\RequiresPhpExtension; |
19 | 18 | use Redis; |
| 19 | +use RedisException; |
20 | 20 |
|
21 | | -class RedisQueueTest extends TestCase |
| 21 | +#[RequiresPhpExtension('redis')] |
| 22 | +class RedisQueueTest extends QueueTestCase |
22 | 23 | { |
23 | | - private Redis $redisMock; |
24 | | - private RedisQueue $queue; |
| 24 | + private static ?Redis $redis = null; |
25 | 25 |
|
26 | | - protected function setUp(): void |
27 | | - { |
28 | | - $this->redisMock = $this->createMock(Redis::class); |
29 | | - $this->queue = new RedisQueue($this->redisMock, 'testQueue'); |
30 | | - } |
31 | | - |
32 | | - public function testGetName() |
| 26 | + public static function setUpBeforeClass(): void |
33 | 27 | { |
34 | | - $this->assertSame('testQueue', $this->queue->getName()); |
| 28 | + try { |
| 29 | + self::$redis = new Redis(); |
| 30 | + self::$redis->connect('127.0.0.1', 6379, 1); |
| 31 | + } catch (RedisException) { |
| 32 | + self::markTestSkipped('Redis is not available on 127.0.0.1:6379.'); |
| 33 | + } |
35 | 34 | } |
36 | 35 |
|
37 | | - public function testSize() |
38 | | - { |
39 | | - $this->redisMock |
40 | | - ->method('llen') |
41 | | - ->with('testQueue') |
42 | | - ->willReturn(5); |
43 | | - |
44 | | - $this->assertSame(5, $this->queue->size()); |
45 | | - } |
46 | | - |
47 | | - public function testConsumeReturnsJob() |
48 | | - { |
49 | | - $jobData = json_encode(['jobId' => '123', 'payload' => '{"key":"value"}', 'attempts' => 0]); |
50 | | - |
51 | | - $this->redisMock |
52 | | - ->method('lpop') |
53 | | - ->with('testQueue') |
54 | | - ->willReturn($jobData); |
55 | | - |
56 | | - $job = $this->queue->consume(); |
57 | | - $this->assertInstanceOf(RedisJob::class, $job); |
58 | | - $this->assertSame('123', $job->getId()); |
59 | | - } |
60 | | - |
61 | | - public function testConsumeReturnsNullWhenEmpty() |
62 | | - { |
63 | | - $this->redisMock |
64 | | - ->method('lpop') |
65 | | - ->with('testQueue') |
66 | | - ->willReturn(false); |
67 | | - |
68 | | - $this->assertNull($this->queue->consume()); |
69 | | - } |
70 | | - |
71 | | - public function testPushReturnsJobId() |
| 36 | + protected function setUp(): void |
72 | 37 | { |
73 | | - $jobDescriptorMock = $this->createMock(JobDescriptorInterface::class); |
74 | | - $jobDescriptorMock->method('jsonSerialize')->willReturn(['key' => 'value']); |
75 | | - |
76 | | - $this->redisMock |
77 | | - ->expects($this->once()) |
78 | | - ->method('rPush') |
79 | | - ->with( |
80 | | - 'testQueue', |
81 | | - $this->callback(function ($jobData) { |
82 | | - $decoded = json_decode($jobData, true); |
83 | | - return isset($decoded['jobId'], $decoded['payload']) && $decoded['payload'] === '{"key":"value"}'; |
84 | | - }) |
85 | | - ); |
86 | | - |
87 | | - $jobId = $this->queue->push($jobDescriptorMock); |
88 | | - $this->assertNotEmpty($jobId); |
| 38 | + self::$redis->flushAll(); |
89 | 39 | } |
90 | 40 |
|
91 | | - public function testPushWithDelayAddsToDelayedQueue() |
| 41 | + public static function newQueue(): QueueInterface |
92 | 42 | { |
93 | | - $jobDescriptorMock = $this->createMock(JobDescriptorInterface::class); |
94 | | - $jobDescriptorMock->method('jsonSerialize')->willReturn(['key' => 'value']); |
95 | | - |
96 | | - $this->redisMock |
97 | | - ->expects($this->once()) |
98 | | - ->method('zadd') |
99 | | - ->with('testQueue:delayed', ['NX'], $this->greaterThan(time()), $this->callback(function ($jobData) { |
100 | | - $decoded = json_decode($jobData, true); |
101 | | - return isset($decoded['jobId'], $decoded['payload']) && $decoded['payload'] === '{"key":"value"}'; |
102 | | - })); |
103 | | - |
104 | | - $jobId = $this->queue->push($jobDescriptorMock, 10); |
105 | | - $this->assertNotEmpty($jobId); |
| 43 | + return new RedisQueue(redis: self::$redis, name: 'default'); |
106 | 44 | } |
107 | 45 |
|
108 | 46 | public function testFreeDelayedJobs() |
109 | 47 | { |
| 48 | + $redisMock = $this->createMock(Redis::class); |
| 49 | + $queue = new RedisQueue($redisMock, 'testQueue'); |
110 | 50 | $jobData = json_encode(['jobId' => '123', 'payload' => '{"key":"value"}', 'attempts' => 0]); |
111 | 51 |
|
112 | | - $this->redisMock |
| 52 | + $redisMock |
113 | 53 | ->expects($this->once()) |
114 | 54 | ->method('set') |
115 | 55 | ->with('testQueue:delayed:lock', '1', ['nx', 'ex' => 10]) |
116 | 56 | ->willReturn(true); |
117 | 57 |
|
118 | | - $this->redisMock |
| 58 | + $redisMock |
119 | 59 | ->expects($this->once()) |
120 | 60 | ->method('zrangebyscore') |
121 | 61 | ->with('testQueue:delayed', '-inf', (string)time()) |
122 | 62 | ->willReturn([$jobData]); |
123 | 63 |
|
124 | | - $this->redisMock |
| 64 | + $redisMock |
125 | 65 | ->expects($this->once()) |
126 | 66 | ->method('zrem') |
127 | 67 | ->with('testQueue:delayed', $jobData); |
128 | 68 |
|
129 | | - $this->redisMock |
| 69 | + $redisMock |
130 | 70 | ->expects($this->once()) |
131 | 71 | ->method('rpush') |
132 | 72 | ->with('testQueue', $jobData); |
133 | 73 |
|
134 | | - $this->queue->freeDelayedJobs(); |
| 74 | + $queue->freeDelayedJobs(); |
135 | 75 | } |
136 | 76 | } |
0 commit comments