1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Lamoda\QueueBundle\Tests\Unit\Service; |
6
|
|
|
|
7
|
|
|
use DateTime; |
8
|
|
|
use Lamoda\QueueBundle\Entity\QueueRepository; |
9
|
|
|
use Lamoda\QueueBundle\Event\QueueAttemptsReachedEvent; |
10
|
|
|
use Lamoda\QueueBundle\Exception\AttemptsReachedException; |
11
|
|
|
use Lamoda\QueueBundle\Exception\UnexpectedValueException; |
12
|
|
|
use Lamoda\QueueBundle\Factory\EntityFactory; |
13
|
|
|
use Lamoda\QueueBundle\Publisher; |
14
|
|
|
use Lamoda\QueueBundle\Service\DelayService; |
15
|
|
|
use Lamoda\QueueBundle\Service\QueueService; |
16
|
|
|
use Lamoda\QueueBundle\Tests\Unit\Job\StubJob; |
17
|
|
|
use Lamoda\QueueBundle\Tests\Unit\QueueCommonServicesTrait; |
18
|
|
|
use Lamoda\QueueBundle\Tests\Unit\QueueEntity; |
19
|
|
|
use Lamoda\QueueBundle\Tests\Unit\SymfonyMockTrait; |
20
|
|
|
use OldSound\RabbitMqBundle\RabbitMq\Producer; |
21
|
|
|
use PHPUnit_Framework_TestCase; |
22
|
|
|
use Psr\Log\NullLogger; |
23
|
|
|
use Symfony\Component\EventDispatcher\EventDispatcher; |
24
|
|
|
|
25
|
|
|
class QueueServiceTest extends PHPUnit_Framework_TestCase |
|
|
|
|
26
|
|
|
{ |
27
|
|
|
use QueueCommonServicesTrait; |
28
|
|
|
use SymfonyMockTrait; |
29
|
|
|
|
30
|
|
|
/** @var int */ |
31
|
|
|
protected $maxAttempts = 5; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @throws \Doctrine\ORM\TransactionRequiredException |
35
|
|
|
*/ |
36
|
|
|
public function testGetToRestore(): void |
37
|
|
|
{ |
38
|
|
|
$limit = 10; |
39
|
|
|
|
40
|
|
|
$queueRepository = $this->getQueueRepository(); |
41
|
|
|
$queueRepository |
|
|
|
|
42
|
|
|
->expects($this->once()) |
43
|
|
|
->method('getToRestore') |
44
|
|
|
->with($this->maxAttempts, $limit); |
45
|
|
|
|
46
|
|
|
$this->createService($queueRepository)->getToRestore($limit); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @param QueueEntity $queue |
51
|
|
|
* |
52
|
|
|
* @throws \Exception |
53
|
|
|
* |
54
|
|
|
* @dataProvider dataGetToProcess() |
55
|
|
|
*/ |
56
|
|
|
public function testGetToProcess(QueueEntity $queue): void |
57
|
|
|
{ |
58
|
|
|
$queueRepository = $this->getQueueRepository(); |
59
|
|
|
$queueRepository |
|
|
|
|
60
|
|
|
->expects($this->once()) |
61
|
|
|
->method('find') |
62
|
|
|
->willReturn($queue); |
63
|
|
|
$queueRepository |
64
|
|
|
->expects($this->once()) |
65
|
|
|
->method('save') |
66
|
|
|
->with($queue); |
67
|
|
|
|
68
|
|
|
$this->assertEquals($queue, $this->createService($queueRepository)->getToProcess(1)); |
69
|
|
|
$this->assertEquals(QueueEntity::STATUS_IN_PROGRESS_TITLE, $queue->getStatusAsString()); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
public function dataGetToProcess(): array |
73
|
|
|
{ |
74
|
|
|
return [ |
75
|
|
|
'new status' => [(new QueueEntity('queue', 'exchange', 'ClassJob', ['id' => 1]))->setNew()], |
76
|
|
|
'in progress status' => [(new QueueEntity('queue', 'exchange', 'ClassJob', ['id' => 1]))->setInProgress()], |
77
|
|
|
]; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* @param null | QueueEntity $queueEntity |
82
|
|
|
* @param string $expectedExceptionMessage |
83
|
|
|
* |
84
|
|
|
* @throws \Exception |
85
|
|
|
* |
86
|
|
|
* @dataProvider dataGetToProcessQueueNotFound |
87
|
|
|
*/ |
88
|
|
|
public function testGetToProcessQueueNotFound(?QueueEntity $queueEntity, string $expectedExceptionMessage): void |
89
|
|
|
{ |
90
|
|
|
$this->expectException(UnexpectedValueException::class); |
91
|
|
|
$this->expectExceptionMessage($expectedExceptionMessage); |
92
|
|
|
|
93
|
|
|
$queueRepository = $this->getQueueRepository(); |
94
|
|
|
|
95
|
|
|
$queueRepository |
|
|
|
|
96
|
|
|
->expects($this->once()) |
97
|
|
|
->method('find') |
98
|
|
|
->willReturn($queueEntity); |
99
|
|
|
|
100
|
|
|
$this->createService($queueRepository)->getToProcess(1); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
public function dataGetToProcessQueueNotFound(): array |
104
|
|
|
{ |
105
|
|
|
return [ |
106
|
|
|
'Not found' => [ |
107
|
|
|
null, |
108
|
|
|
'The queue with id "1" was not found', |
109
|
|
|
], |
110
|
|
|
'Status not NEW' => [ |
111
|
|
|
new QueueEntity('queue', 'exchange', 'ClassJob', ['id' => 1]), |
112
|
|
|
'The queue "queue" with job "ClassJob" was not found in suitable status. Actual status is "initial"', |
113
|
|
|
], |
114
|
|
|
]; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* @throws \Exception |
119
|
|
|
*/ |
120
|
|
|
public function testGetToProcessAttemptsReached(): void |
121
|
|
|
{ |
122
|
|
|
$this->expectException(AttemptsReachedException::class); |
123
|
|
|
|
124
|
|
|
$queue = new QueueEntity('queue', 'exchange', 'ClassJob', ['id' => 1]); |
125
|
|
|
$queue->setNew(); |
126
|
|
|
|
127
|
|
|
$this->maxAttempts = 0; |
128
|
|
|
|
129
|
|
|
$queueRepository = $this->getQueueRepository(); |
130
|
|
|
$queueRepository |
|
|
|
|
131
|
|
|
->expects($this->once()) |
132
|
|
|
->method('find') |
133
|
|
|
->willReturn($queue); |
134
|
|
|
$queueRepository |
135
|
|
|
->expects($this->once()) |
136
|
|
|
->method('save') |
137
|
|
|
->with($queue); |
138
|
|
|
|
139
|
|
|
$attemptsReachedEvent = new QueueAttemptsReachedEvent($queue); |
140
|
|
|
|
141
|
|
|
$eventDispatcher = $this->getEventDispatcher(); |
142
|
|
|
$eventDispatcher |
|
|
|
|
143
|
|
|
->expects($this->once()) |
144
|
|
|
->method('dispatch') |
145
|
|
|
->with('queue.attempts.reached.event', $attemptsReachedEvent); |
146
|
|
|
|
147
|
|
|
$this->assertEquals( |
148
|
|
|
$queue, |
149
|
|
|
$this->createService($queueRepository, null, $eventDispatcher)->getToProcess(1) |
150
|
|
|
); |
151
|
|
|
$this->assertEquals(QueueEntity::STATUS_ATTEMPTS_REACHED_TITLE, $queue->getStatusAsString()); |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
public function testSave(): void |
155
|
|
|
{ |
156
|
|
|
$queue = new QueueEntity('queue', 'exchange', 'ClassJob', ['id' => 1]); |
157
|
|
|
|
158
|
|
|
$queueRepository = $this->getQueueRepository(); |
159
|
|
|
$queueRepository |
|
|
|
|
160
|
|
|
->expects($this->once()) |
161
|
|
|
->method('save') |
162
|
|
|
->with($queue) |
163
|
|
|
->willReturnArgument(0); |
164
|
|
|
|
165
|
|
|
$this->assertEquals( |
166
|
|
|
$queue, |
167
|
|
|
$this->createService($queueRepository)->save($queue) |
168
|
|
|
); |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
public function testCreateQueue(): void |
172
|
|
|
{ |
173
|
|
|
$job = new StubJob(1); |
174
|
|
|
$queue = new QueueEntity('queue', 'exchange', StubJob::class, ['id' => 1]); |
175
|
|
|
|
176
|
|
|
$entityFactory = $this->getEntityFactory(); |
177
|
|
|
$entityFactory |
|
|
|
|
178
|
|
|
->expects($this->once()) |
179
|
|
|
->method('createQueue') |
180
|
|
|
->with($job) |
181
|
|
|
->willReturn($queue); |
182
|
|
|
|
183
|
|
|
$queueRepository = $this->getQueueRepository(); |
184
|
|
|
|
185
|
|
|
$this->assertEquals( |
186
|
|
|
$queue, |
187
|
|
|
$this->createService($queueRepository, $entityFactory)->createQueue($job) |
188
|
|
|
); |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
public function testSavingScheduledQueue(): void |
192
|
|
|
{ |
193
|
|
|
$dateTime = new DateTime(); |
194
|
|
|
$job = new StubJob(1); |
195
|
|
|
$queue = new QueueEntity('queue', 'exchange', StubJob::class, ['id' => 1]); |
196
|
|
|
$queue->setScheduled($dateTime); |
197
|
|
|
|
198
|
|
|
$queueRepository = $this->getQueueRepository(); |
199
|
|
|
$queueRepository |
|
|
|
|
200
|
|
|
->expects($this->once()) |
201
|
|
|
->method('save') |
202
|
|
|
->with($queue) |
203
|
|
|
->willReturnArgument(0); |
204
|
|
|
|
205
|
|
|
$entityFactory = $this->getEntityFactory(); |
206
|
|
|
$entityFactory |
|
|
|
|
207
|
|
|
->expects($this->once()) #at least once |
208
|
|
|
->method('createQueue') |
209
|
|
|
->willReturn($queue); |
210
|
|
|
|
211
|
|
|
$queueService = $this->createService($queueRepository, $entityFactory); |
212
|
|
|
$publisher = new Publisher( |
213
|
|
|
$this->createMock(Producer::class), |
214
|
|
|
$queueService, |
215
|
|
|
new NullLogger(), |
216
|
|
|
$this->createMock(DelayService::class) |
217
|
|
|
); |
218
|
|
|
|
219
|
|
|
$createdQueue = $queueService->createQueue($job); |
220
|
|
|
$publisher->prepareQueueForPublish($createdQueue); |
221
|
|
|
$publisher->release(); |
222
|
|
|
} |
223
|
|
|
|
224
|
|
|
public function testIsTransactionActive(): void |
225
|
|
|
{ |
226
|
|
|
$expected = true; |
227
|
|
|
|
228
|
|
|
$queueRepository = $this->getQueueRepository(); |
229
|
|
|
$queueRepository |
|
|
|
|
230
|
|
|
->expects($this->once()) |
231
|
|
|
->method('isTransactionActive') |
232
|
|
|
->willReturn($expected); |
233
|
|
|
|
234
|
|
|
$this->assertEquals( |
235
|
|
|
$expected, |
236
|
|
|
$this->createService($queueRepository)->isTransactionActive() |
237
|
|
|
); |
238
|
|
|
} |
239
|
|
|
|
240
|
|
|
private function createService( |
241
|
|
|
?QueueRepository $repository = null, |
242
|
|
|
?EntityFactory $entityFactory = null, |
243
|
|
|
?EventDispatcher $eventDispatcher = null |
244
|
|
|
): QueueService { |
245
|
|
|
return new QueueService( |
246
|
|
|
$repository ?? $this->getQueueRepository(), |
247
|
|
|
$entityFactory ?? $this->getEntityFactory(), |
248
|
|
|
$eventDispatcher ?? $this->getEventDispatcher(), |
249
|
|
|
$this->maxAttempts |
250
|
|
|
); |
251
|
|
|
} |
252
|
|
|
|
253
|
|
|
/** |
254
|
|
|
* @return \PHPUnit_Framework_MockObject_MockObject|QueueRepository |
255
|
|
|
*/ |
256
|
|
|
private function getQueueRepository() |
257
|
|
|
{ |
258
|
|
|
return $this->getMockQueueRepository( |
259
|
|
|
[ |
260
|
|
|
'find', |
261
|
|
|
'getToRestore', |
262
|
|
|
'save', |
263
|
|
|
'isTransactionActive', |
264
|
|
|
] |
265
|
|
|
); |
266
|
|
|
} |
267
|
|
|
|
268
|
|
|
/** |
269
|
|
|
* @return EntityFactory|\PHPUnit_Framework_MockObject_MockObject |
270
|
|
|
*/ |
271
|
|
|
private function getEntityFactory() |
272
|
|
|
{ |
273
|
|
|
return $this->getMockEntityFactory(['createQueue']); |
274
|
|
|
} |
275
|
|
|
|
276
|
|
|
/** |
277
|
|
|
* @return EventDispatcher|\PHPUnit_Framework_MockObject_MockObject |
278
|
|
|
*/ |
279
|
|
|
private function getEventDispatcher() |
280
|
|
|
{ |
281
|
|
|
return $this->getMockEventDispatcher(['dispatch']); |
282
|
|
|
} |
283
|
|
|
} |
284
|
|
|
|