1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SfCod\QueueBundle\Tests\Service; |
4
|
|
|
|
5
|
|
|
use PHPUnit\Framework\MockObject\MockObject; |
6
|
|
|
use PHPUnit\Framework\TestCase; |
7
|
|
|
use SfCod\QueueBundle\Queue\QueueInterface; |
8
|
|
|
use SfCod\QueueBundle\Service\JobQueue; |
9
|
|
|
use SfCod\QueueBundle\Service\QueueManager; |
10
|
|
|
use SfCod\QueueBundle\Tests\Data\LoadTrait; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Class JobQueueTest |
14
|
|
|
* |
15
|
|
|
* @author Virchenko Maksim <[email protected]> |
16
|
|
|
* |
17
|
|
|
* @package SfCod\QueueBundle\Tests\Service |
18
|
|
|
*/ |
19
|
|
|
class JobQueueTest extends TestCase |
20
|
|
|
{ |
21
|
|
|
use LoadTrait; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Set up test |
25
|
|
|
*/ |
26
|
|
|
protected function setUp() |
27
|
|
|
{ |
28
|
|
|
$this->configure(); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Test pushing into queue |
33
|
|
|
*/ |
34
|
|
|
public function testPush() |
35
|
|
|
{ |
36
|
|
|
$job = uniqid('job_'); |
37
|
|
|
$data = array_rand(range(0, 100), 10); |
38
|
|
|
$queue = uniqid('queue_'); |
39
|
|
|
$connection = uniqid('connection_'); |
40
|
|
|
|
41
|
|
|
$manager = $this->mockManager(); |
42
|
|
|
$manager |
|
|
|
|
43
|
|
|
->expects(self::once()) |
44
|
|
|
->method('push') |
45
|
|
|
->with($job, $data, $queue, $connection) |
46
|
|
|
->willReturn(true); |
47
|
|
|
|
48
|
|
|
$jobQueue = $this->mockJobQueue($manager); |
|
|
|
|
49
|
|
|
|
50
|
|
|
self::assertTrue($jobQueue->push($job, $data, $queue, $connection)); |
|
|
|
|
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Test push unique job into queue |
55
|
|
|
*/ |
56
|
|
|
public function testPushUnique() |
57
|
|
|
{ |
58
|
|
|
$job = uniqid('job_'); |
59
|
|
|
$data = array_rand(range(0, 100), 10); |
60
|
|
|
$queue = uniqid('queue_'); |
61
|
|
|
$connection = uniqid('connection_'); |
62
|
|
|
|
63
|
|
|
$connectionMock = $this->createMock(QueueInterface::class); |
64
|
|
|
$connectionMock |
65
|
|
|
->expects(self::once()) |
66
|
|
|
->method('exists') |
67
|
|
|
->with(self::equalTo($job), self::equalTo($data), self::equalTo($queue)) |
68
|
|
|
->willReturn(false); |
69
|
|
|
|
70
|
|
|
$manager = $this->mockManager(); |
71
|
|
|
$manager |
|
|
|
|
72
|
|
|
->expects(self::once()) |
73
|
|
|
->method('connection') |
74
|
|
|
->with(self::equalTo($connection)) |
75
|
|
|
->willReturn($connectionMock); |
76
|
|
|
$manager |
77
|
|
|
->expects(self::once()) |
78
|
|
|
->method('push') |
79
|
|
|
->with($job, $data, $queue, $connection) |
80
|
|
|
->willReturn(true); |
81
|
|
|
|
82
|
|
|
$jobQueue = $this->mockJobQueue($manager); |
|
|
|
|
83
|
|
|
|
84
|
|
|
self::assertTrue($jobQueue->pushUnique($job, $data, $queue, $connection)); |
|
|
|
|
85
|
|
|
|
86
|
|
|
$connectionMock = $this->createMock(QueueInterface::class); |
87
|
|
|
$connectionMock |
88
|
|
|
->expects(self::once()) |
89
|
|
|
->method('exists') |
90
|
|
|
->with(self::equalTo($job), self::equalTo($data), self::equalTo($queue)) |
91
|
|
|
->willReturn(true); |
92
|
|
|
|
93
|
|
|
$manager = $this->mockManager(); |
94
|
|
|
$manager |
95
|
|
|
->expects(self::once()) |
96
|
|
|
->method('connection') |
97
|
|
|
->with(self::equalTo($connection)) |
98
|
|
|
->willReturn($connectionMock); |
99
|
|
|
|
100
|
|
|
$jobQueue = $this->mockJobQueue($manager); |
|
|
|
|
101
|
|
|
|
102
|
|
|
self::assertNull($jobQueue->pushUnique($job, $data, $queue, $connection)); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* Test bulk pushing into queue |
107
|
|
|
*/ |
108
|
|
|
public function testBulk() |
109
|
|
|
{ |
110
|
|
|
$jobs = []; |
111
|
|
|
|
112
|
|
|
for ($i = 0; $i < 10; ++$i) { |
113
|
|
|
$jobs[] = uniqid('job_' . $i); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
$data = array_rand(range(0, 100), 10); |
117
|
|
|
$queue = uniqid('queue_'); |
118
|
|
|
$connection = uniqid('connection_'); |
119
|
|
|
|
120
|
|
|
$manager = $this->mockManager(); |
121
|
|
|
$manager |
|
|
|
|
122
|
|
|
->expects(self::once()) |
123
|
|
|
->method('bulk') |
124
|
|
|
->with($jobs, $data, $queue, $connection) |
125
|
|
|
->willReturn(true); |
126
|
|
|
|
127
|
|
|
$jobQueue = $this->mockJobQueue($manager); |
|
|
|
|
128
|
|
|
|
129
|
|
|
self::assertTrue($jobQueue->bulk($jobs, $data, $queue, $connection)); |
|
|
|
|
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* Test pushing job with delay |
134
|
|
|
*/ |
135
|
|
|
public function testLater() |
136
|
|
|
{ |
137
|
|
|
$delay = rand(1, 1000); |
138
|
|
|
$job = uniqid('job_'); |
139
|
|
|
$data = array_rand(range(0, 100), 10); |
140
|
|
|
$queue = uniqid('queue_'); |
141
|
|
|
$connection = uniqid('connection_'); |
142
|
|
|
|
143
|
|
|
$manager = $this->mockManager(); |
144
|
|
|
$manager |
|
|
|
|
145
|
|
|
->expects(self::once()) |
146
|
|
|
->method('later') |
147
|
|
|
->with($delay, $job, $data, $queue, $connection) |
148
|
|
|
->willReturn(true); |
149
|
|
|
|
150
|
|
|
$jobQueue = $this->mockJobQueue($manager); |
|
|
|
|
151
|
|
|
|
152
|
|
|
self::assertTrue($jobQueue->later($delay, $job, $data, $queue, $connection)); |
|
|
|
|
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* Test push unique job with delay into queue |
157
|
|
|
*/ |
158
|
|
|
public function testLaterUnique() |
159
|
|
|
{ |
160
|
|
|
$delay = rand(1, 1000); |
161
|
|
|
$job = uniqid('job_'); |
162
|
|
|
$data = array_rand(range(0, 100), 10); |
163
|
|
|
$queue = uniqid('queue_'); |
164
|
|
|
$connection = uniqid('connection_'); |
165
|
|
|
|
166
|
|
|
$connectionMock = $this->createMock(QueueInterface::class); |
167
|
|
|
$connectionMock |
168
|
|
|
->expects(self::once()) |
169
|
|
|
->method('exists') |
170
|
|
|
->with(self::equalTo($job), self::equalTo($data), self::equalTo($queue)) |
171
|
|
|
->willReturn(false); |
172
|
|
|
|
173
|
|
|
$manager = $this->mockManager(); |
174
|
|
|
$manager |
|
|
|
|
175
|
|
|
->expects(self::once()) |
176
|
|
|
->method('connection') |
177
|
|
|
->with(self::equalTo($connection)) |
178
|
|
|
->willReturn($connectionMock); |
179
|
|
|
$manager |
180
|
|
|
->expects(self::once()) |
181
|
|
|
->method('later') |
182
|
|
|
->with($delay, $job, $data, $queue, $connection) |
183
|
|
|
->willReturn(true); |
184
|
|
|
|
185
|
|
|
$jobQueue = $this->mockJobQueue($manager); |
|
|
|
|
186
|
|
|
|
187
|
|
|
self::assertTrue($jobQueue->laterUnique($delay, $job, $data, $queue, $connection)); |
|
|
|
|
188
|
|
|
|
189
|
|
|
$connectionMock = $this->createMock(QueueInterface::class); |
190
|
|
|
$connectionMock |
191
|
|
|
->expects(self::once()) |
192
|
|
|
->method('exists') |
193
|
|
|
->with(self::equalTo($job), self::equalTo($data), self::equalTo($queue)) |
194
|
|
|
->willReturn(true); |
195
|
|
|
|
196
|
|
|
$manager = $this->mockManager(); |
197
|
|
|
$manager |
198
|
|
|
->expects(self::once()) |
199
|
|
|
->method('connection') |
200
|
|
|
->with(self::equalTo($connection)) |
201
|
|
|
->willReturn($connectionMock); |
202
|
|
|
|
203
|
|
|
$jobQueue = $this->mockJobQueue($manager); |
|
|
|
|
204
|
|
|
|
205
|
|
|
self::assertNull($jobQueue->laterUnique($delay, $job, $data, $queue, $connection)); |
206
|
|
|
} |
207
|
|
|
|
208
|
|
|
/** |
209
|
|
|
* Mock job queue |
210
|
|
|
* |
211
|
|
|
* @param QueueManager $manager |
212
|
|
|
* |
213
|
|
|
* @return JobQueue|MockObject |
214
|
|
|
*/ |
215
|
|
|
private function mockJobQueue(QueueManager $manager): JobQueue |
216
|
|
|
{ |
217
|
|
|
$jobQueue = $this->getMockBuilder(JobQueue::class) |
218
|
|
|
->setConstructorArgs([$manager]) |
219
|
|
|
->setMethods(null) |
220
|
|
|
->getMock(); |
221
|
|
|
|
222
|
|
|
return $jobQueue; |
223
|
|
|
} |
224
|
|
|
|
225
|
|
|
/** |
226
|
|
|
* Mock manager |
227
|
|
|
* |
228
|
|
|
* @return QueueManager|MockObject |
229
|
|
|
*/ |
230
|
|
|
private function mockManager(): QueueManager |
231
|
|
|
{ |
232
|
|
|
$manager = $this->getMockBuilder(QueueManager::class) |
233
|
|
|
->setMethods([ |
234
|
|
|
'push', |
235
|
|
|
'bulk', |
236
|
|
|
'later', |
237
|
|
|
'connection', |
238
|
|
|
]) |
239
|
|
|
->getMock(); |
240
|
|
|
|
241
|
|
|
return $manager; |
242
|
|
|
} |
243
|
|
|
} |
244
|
|
|
|
It seems like the method you are trying to call exists only in some of the possible types.
Let’s take a look at an example:
Available Fixes
Add an additional type-check:
Only allow a single type to be passed if the variable comes from a parameter: