1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Gendoria\CommandQueueRabbitMqDriverBundle\Tests\Worker; |
4
|
|
|
|
5
|
|
|
use Exception; |
6
|
|
|
use Gendoria\CommandQueue\Command\CommandInterface; |
7
|
|
|
use Gendoria\CommandQueue\CommandProcessor\CommandProcessorInterface; |
8
|
|
|
use Gendoria\CommandQueue\ProcessorFactoryInterface; |
9
|
|
|
use Gendoria\CommandQueue\ProcessorNotFoundException; |
10
|
|
|
use Gendoria\CommandQueueBundle\Event\QueueEvents; |
11
|
|
|
use Gendoria\CommandQueueBundle\Event\QueueProcessErrorEvent; |
12
|
|
|
use Gendoria\CommandQueueRabbitMqDriverBundle\Worker\RabbitMqWorker; |
13
|
|
|
use JMS\Serializer\Serializer; |
14
|
|
|
use OldSound\RabbitMqBundle\RabbitMq\ConsumerInterface; |
15
|
|
|
use OldSound\RabbitMqBundle\RabbitMq\ProducerInterface; |
16
|
|
|
use PhpAmqpLib\Message\AMQPMessage; |
17
|
|
|
use PhpAmqpLib\Wire\AMQPTable; |
18
|
|
|
use PHPUnit_Framework_MockObject_Generator; |
19
|
|
|
use PHPUnit_Framework_MockObject_MockObject; |
20
|
|
|
use PHPUnit_Framework_TestCase; |
21
|
|
|
use stdClass; |
22
|
|
|
use Symfony\Component\EventDispatcher\EventDispatcherInterface; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Description of RabbitMqWorkerTest |
26
|
|
|
* |
27
|
|
|
* @author Tomasz Struczyński <[email protected]> |
28
|
|
|
*/ |
29
|
|
|
class RabbitMqWorkerTest extends PHPUnit_Framework_TestCase |
30
|
|
|
{ |
31
|
|
|
public function testCorrectPass() |
32
|
|
|
{ |
33
|
|
|
$command = $this->getCommand(); |
34
|
|
|
$processor = $this->getProcessor(); |
35
|
|
|
$processorFactory = $this->getProcessorFactory(); |
36
|
|
|
$serializer = $this->getSerializer(); |
37
|
|
|
$eventDispatcher = $this->getEventDispatcher(); |
38
|
|
|
$rescheduleProducer = $this->getRescheduleProducer(); |
39
|
|
|
|
40
|
|
|
$msg = new AMQPMessage("Test", array( |
41
|
|
|
'application_headers' => new AMQPTable( |
42
|
|
|
array( |
43
|
|
|
'x-class-name' => get_class($command), |
44
|
|
|
) |
45
|
|
|
), |
46
|
|
|
)); |
47
|
|
|
$msg->delivery_info = array( |
|
|
|
|
48
|
|
|
'channel' => null, |
49
|
|
|
'consumer_tag' => 't', |
50
|
|
|
'delivery_tag' => 't', |
51
|
|
|
'redelivered' => true, |
52
|
|
|
'exchange' => 't', |
53
|
|
|
'routing_key' => 't' |
54
|
|
|
); |
55
|
|
|
|
56
|
|
|
$eventDispatcher->expects($this->exactly(4)) |
|
|
|
|
57
|
|
|
->method('dispatch') |
58
|
|
|
->withConsecutive( |
59
|
|
|
array($this->equalTo(QueueEvents::WORKER_RUN_BEFORE_TRANSLATE)), |
60
|
|
|
array($this->equalTo(QueueEvents::WORKER_RUN_BEFORE_GET_PROCESSOR)), |
61
|
|
|
array($this->equalTo(QueueEvents::WORKER_RUN_BEFORE_PROCESS)), |
62
|
|
|
array($this->equalTo(QueueEvents::WORKER_RUN_AFTER_PROCESS)) |
63
|
|
|
); |
64
|
|
|
|
65
|
|
|
$serializer->expects($this->once())->method('deserialize') |
|
|
|
|
66
|
|
|
->with("Test", get_class($command), 'json') |
67
|
|
|
->will($this->returnValue($command)); |
68
|
|
|
|
69
|
|
|
$processorFactory->expects($this->once()) |
|
|
|
|
70
|
|
|
->method('getProcessor') |
71
|
|
|
->will($this->returnValue($processor)); |
72
|
|
|
|
73
|
|
|
$processor->expects($this->once()) |
|
|
|
|
74
|
|
|
->method('process') |
75
|
|
|
->with($command) |
76
|
|
|
; |
77
|
|
|
|
78
|
|
|
$worker = new RabbitMqWorker($eventDispatcher, $processorFactory, $serializer, $rescheduleProducer); |
79
|
|
|
|
80
|
|
|
$worker->execute($msg); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
public function testTranslateError() |
84
|
|
|
{ |
85
|
|
|
$command = $this->getCommand(); |
86
|
|
|
$processorFactory = $this->getProcessorFactory(); |
87
|
|
|
$serializer = $this->getSerializer(); |
88
|
|
|
$eventDispatcher = $this->getEventDispatcher(); |
89
|
|
|
$rescheduleProducer = $this->getRescheduleProducer(); |
90
|
|
|
|
91
|
|
|
$msg = new AMQPMessage("Test", array( |
92
|
|
|
'application_headers' => new AMQPTable( |
93
|
|
|
array( |
94
|
|
|
) |
95
|
|
|
), |
96
|
|
|
)); |
97
|
|
|
$msg->delivery_info = array( |
|
|
|
|
98
|
|
|
'channel' => null, |
99
|
|
|
'consumer_tag' => 't', |
100
|
|
|
'delivery_tag' => 't', |
101
|
|
|
'redelivered' => true, |
102
|
|
|
'exchange' => 't', |
103
|
|
|
'routing_key' => 't' |
104
|
|
|
); |
105
|
|
|
|
106
|
|
|
$eventDispatcher->expects($this->exactly(1)) |
|
|
|
|
107
|
|
|
->method('dispatch') |
108
|
|
|
->withConsecutive( |
109
|
|
|
array($this->equalTo(QueueEvents::WORKER_RUN_BEFORE_TRANSLATE)) |
110
|
|
|
); |
111
|
|
|
|
112
|
|
|
$serializer->expects($this->never())->method('deserialize') |
|
|
|
|
113
|
|
|
->with("Test", get_class($command), 'json') |
114
|
|
|
->will($this->returnValue($command)); |
115
|
|
|
$rescheduleProducer |
|
|
|
|
116
|
|
|
->expects($this->once()) |
117
|
|
|
->method('publish'); |
118
|
|
|
|
119
|
|
|
$worker = new RabbitMqWorker($eventDispatcher, $processorFactory, $serializer, $rescheduleProducer); |
120
|
|
|
|
121
|
|
|
$this->assertEquals(ConsumerInterface::MSG_REJECT, $worker->execute($msg)); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
public function testTranslateErrorNotAnObject() |
125
|
|
|
{ |
126
|
|
|
$command = $this->getCommand(); |
127
|
|
|
$processorFactory = $this->getProcessorFactory(); |
128
|
|
|
$serializer = $this->getSerializer(); |
129
|
|
|
$eventDispatcher = $this->getEventDispatcher(); |
130
|
|
|
$rescheduleProducer = $this->getRescheduleProducer(); |
131
|
|
|
|
132
|
|
|
$msg = new AMQPMessage("Test", array( |
133
|
|
|
'application_headers' => new AMQPTable( |
134
|
|
|
array( |
135
|
|
|
) |
136
|
|
|
), |
137
|
|
|
)); |
138
|
|
|
$msg->delivery_info = array( |
|
|
|
|
139
|
|
|
'channel' => null, |
140
|
|
|
'consumer_tag' => 't', |
141
|
|
|
'delivery_tag' => 't', |
142
|
|
|
'redelivered' => true, |
143
|
|
|
'exchange' => 't', |
144
|
|
|
'routing_key' => 't' |
145
|
|
|
); |
146
|
|
|
|
147
|
|
|
$eventDispatcher->expects($this->exactly(1)) |
|
|
|
|
148
|
|
|
->method('dispatch') |
149
|
|
|
->withConsecutive( |
150
|
|
|
array($this->equalTo(QueueEvents::WORKER_RUN_BEFORE_TRANSLATE)) |
151
|
|
|
); |
152
|
|
|
|
153
|
|
|
$serializer->expects($this->never())->method('deserialize') |
|
|
|
|
154
|
|
|
->with("Test", get_class($command), 'json') |
155
|
|
|
->will($this->returnValue("notanobject")); |
156
|
|
|
$rescheduleProducer |
|
|
|
|
157
|
|
|
->expects($this->once()) |
158
|
|
|
->method('publish'); |
159
|
|
|
|
160
|
|
|
$worker = new RabbitMqWorker($eventDispatcher, $processorFactory, $serializer, $rescheduleProducer); |
161
|
|
|
|
162
|
|
|
$this->assertEquals(ConsumerInterface::MSG_REJECT, $worker->execute($msg)); |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
public function testTranslateErrorNotACommand() |
166
|
|
|
{ |
167
|
|
|
$command = $this->getCommand(); |
168
|
|
|
$processorFactory = $this->getProcessorFactory(); |
169
|
|
|
$serializer = $this->getSerializer(); |
170
|
|
|
$eventDispatcher = $this->getEventDispatcher(); |
171
|
|
|
$rescheduleProducer = $this->getRescheduleProducer(); |
172
|
|
|
|
173
|
|
|
$msg = new AMQPMessage("Test", array( |
174
|
|
|
'application_headers' => new AMQPTable( |
175
|
|
|
array( |
176
|
|
|
) |
177
|
|
|
), |
178
|
|
|
)); |
179
|
|
|
$msg->delivery_info = array( |
|
|
|
|
180
|
|
|
'channel' => null, |
181
|
|
|
'consumer_tag' => 't', |
182
|
|
|
'delivery_tag' => 't', |
183
|
|
|
'redelivered' => true, |
184
|
|
|
'exchange' => 't', |
185
|
|
|
'routing_key' => 't' |
186
|
|
|
); |
187
|
|
|
|
188
|
|
|
$eventDispatcher->expects($this->exactly(1)) |
|
|
|
|
189
|
|
|
->method('dispatch') |
190
|
|
|
->withConsecutive( |
191
|
|
|
array($this->equalTo(QueueEvents::WORKER_RUN_BEFORE_TRANSLATE)) |
192
|
|
|
); |
193
|
|
|
|
194
|
|
|
$serializer->expects($this->never())->method('deserialize') |
|
|
|
|
195
|
|
|
->with("Test", get_class($command), 'json') |
196
|
|
|
->will($this->returnValue(new stdClass())); |
197
|
|
|
$rescheduleProducer |
|
|
|
|
198
|
|
|
->expects($this->once()) |
199
|
|
|
->method('publish'); |
200
|
|
|
|
201
|
|
|
$worker = new RabbitMqWorker($eventDispatcher, $processorFactory, $serializer, $rescheduleProducer); |
202
|
|
|
|
203
|
|
|
$this->assertEquals(ConsumerInterface::MSG_REJECT, $worker->execute($msg)); |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
public function testTranslateErrorNoRepublish() |
207
|
|
|
{ |
208
|
|
|
$command = $this->getCommand(); |
209
|
|
|
$processorFactory = $this->getProcessorFactory(); |
210
|
|
|
$serializer = $this->getSerializer(); |
211
|
|
|
$eventDispatcher = $this->getEventDispatcher(); |
212
|
|
|
$rescheduleProducer = $this->getRescheduleProducer(); |
213
|
|
|
|
214
|
|
|
$msg = new AMQPMessage("Test", array( |
215
|
|
|
'application_headers' => new AMQPTable( |
216
|
|
|
array( |
217
|
|
|
'x-death' => array(array('count' => 10)), |
218
|
|
|
) |
219
|
|
|
), |
220
|
|
|
)); |
221
|
|
|
$msg->delivery_info = array( |
|
|
|
|
222
|
|
|
'channel' => null, |
223
|
|
|
'consumer_tag' => 't', |
224
|
|
|
'delivery_tag' => 't', |
225
|
|
|
'redelivered' => true, |
226
|
|
|
'exchange' => 't', |
227
|
|
|
'routing_key' => 't' |
228
|
|
|
); |
229
|
|
|
|
230
|
|
|
$eventDispatcher->expects($this->exactly(1)) |
|
|
|
|
231
|
|
|
->method('dispatch') |
232
|
|
|
->withConsecutive( |
233
|
|
|
array($this->equalTo(QueueEvents::WORKER_RUN_BEFORE_TRANSLATE)) |
234
|
|
|
); |
235
|
|
|
|
236
|
|
|
$serializer->expects($this->never())->method('deserialize') |
|
|
|
|
237
|
|
|
->with("Test", get_class($command), 'json') |
238
|
|
|
->will($this->returnValue($command)); |
239
|
|
|
$rescheduleProducer |
|
|
|
|
240
|
|
|
->expects($this->never()) |
241
|
|
|
->method('publish'); |
242
|
|
|
|
243
|
|
|
$worker = new RabbitMqWorker($eventDispatcher, $processorFactory, $serializer, $rescheduleProducer); |
244
|
|
|
|
245
|
|
|
$this->assertEquals(ConsumerInterface::MSG_REJECT, $worker->execute($msg)); |
246
|
|
|
} |
247
|
|
|
|
248
|
|
|
public function testGetProcessorError() |
249
|
|
|
{ |
250
|
|
|
$command = $this->getCommand(); |
251
|
|
|
$processor = $this->getProcessor(); |
252
|
|
|
$processorFactory = $this->getProcessorFactory(); |
253
|
|
|
$serializer = $this->getSerializer(); |
254
|
|
|
$eventDispatcher = $this->getEventDispatcher(); |
255
|
|
|
$rescheduleProducer = $this->getRescheduleProducer(); |
256
|
|
|
|
257
|
|
|
$msg = new AMQPMessage("Test", array( |
258
|
|
|
'application_headers' => new AMQPTable( |
259
|
|
|
array( |
260
|
|
|
'x-class-name' => get_class($command), |
261
|
|
|
) |
262
|
|
|
), |
263
|
|
|
)); |
264
|
|
|
$msg->delivery_info = array( |
|
|
|
|
265
|
|
|
'channel' => null, |
266
|
|
|
'consumer_tag' => 't', |
267
|
|
|
'delivery_tag' => 't', |
268
|
|
|
'redelivered' => true, |
269
|
|
|
'exchange' => 't', |
270
|
|
|
'routing_key' => 't' |
271
|
|
|
); |
272
|
|
|
|
273
|
|
|
$eventDispatcher->expects($this->exactly(2)) |
|
|
|
|
274
|
|
|
->method('dispatch') |
275
|
|
|
->withConsecutive( |
276
|
|
|
array($this->equalTo(QueueEvents::WORKER_RUN_BEFORE_TRANSLATE)), |
277
|
|
|
array($this->equalTo(QueueEvents::WORKER_RUN_BEFORE_GET_PROCESSOR)) |
278
|
|
|
); |
279
|
|
|
|
280
|
|
|
$serializer->expects($this->once())->method('deserialize') |
|
|
|
|
281
|
|
|
->with("Test", get_class($command), 'json') |
282
|
|
|
->will($this->returnValue($command)); |
283
|
|
|
|
284
|
|
|
$processorFactory->expects($this->once()) |
|
|
|
|
285
|
|
|
->method('getProcessor') |
286
|
|
|
->will($this->throwException(new ProcessorNotFoundException("ProcessorNotFound"))); |
287
|
|
|
|
288
|
|
|
$processor->expects($this->never()) |
|
|
|
|
289
|
|
|
->method('process') |
290
|
|
|
->with($command) |
291
|
|
|
; |
292
|
|
|
|
293
|
|
|
$rescheduleProducer |
|
|
|
|
294
|
|
|
->expects($this->once()) |
295
|
|
|
->method('publish'); |
296
|
|
|
|
297
|
|
|
$worker = new RabbitMqWorker($eventDispatcher, $processorFactory, $serializer, $rescheduleProducer); |
298
|
|
|
|
299
|
|
|
$this->assertEquals(ConsumerInterface::MSG_REJECT, $worker->execute($msg)); |
300
|
|
|
} |
301
|
|
|
|
302
|
|
|
public function testProcessError() |
303
|
|
|
{ |
304
|
|
|
$command = $this->getCommand(); |
305
|
|
|
$processor = $this->getProcessor(); |
306
|
|
|
$processorFactory = $this->getProcessorFactory(); |
307
|
|
|
$serializer = $this->getSerializer(); |
308
|
|
|
$eventDispatcher = $this->getEventDispatcher(); |
309
|
|
|
$rescheduleProducer = $this->getRescheduleProducer(); |
310
|
|
|
|
311
|
|
|
$msg = new AMQPMessage("Test", array( |
312
|
|
|
'application_headers' => new AMQPTable( |
313
|
|
|
array( |
314
|
|
|
'x-class-name' => get_class($command), |
315
|
|
|
) |
316
|
|
|
), |
317
|
|
|
)); |
318
|
|
|
$msg->delivery_info = array( |
|
|
|
|
319
|
|
|
'channel' => null, |
320
|
|
|
'consumer_tag' => 't', |
321
|
|
|
'delivery_tag' => 't', |
322
|
|
|
'redelivered' => true, |
323
|
|
|
'exchange' => 't', |
324
|
|
|
'routing_key' => 't' |
325
|
|
|
); |
326
|
|
|
|
327
|
|
|
$eventDispatcher->expects($this->exactly(4)) |
|
|
|
|
328
|
|
|
->method('dispatch') |
329
|
|
|
->withConsecutive( |
330
|
|
|
array($this->equalTo(QueueEvents::WORKER_RUN_BEFORE_TRANSLATE)), |
331
|
|
|
array($this->equalTo(QueueEvents::WORKER_RUN_BEFORE_GET_PROCESSOR)), |
332
|
|
|
array($this->equalTo(QueueEvents::WORKER_RUN_BEFORE_PROCESS)), |
333
|
|
|
array($this->equalTo(QueueEvents::WORKER_RUN_PROCESSOR_ERROR), $this->callback(function(QueueProcessErrorEvent $event) { |
334
|
|
|
return $event->getException()->getMessage() == "DummyException"; |
335
|
|
|
})) |
336
|
|
|
); |
337
|
|
|
|
338
|
|
|
$serializer->expects($this->once())->method('deserialize') |
|
|
|
|
339
|
|
|
->with("Test", get_class($command), 'json') |
340
|
|
|
->will($this->returnValue($command)); |
341
|
|
|
|
342
|
|
|
$processorFactory->expects($this->once()) |
|
|
|
|
343
|
|
|
->method('getProcessor') |
344
|
|
|
->will($this->returnValue($processor)); |
345
|
|
|
|
346
|
|
|
$processor->expects($this->once()) |
|
|
|
|
347
|
|
|
->method('process') |
348
|
|
|
->with($command) |
349
|
|
|
->will($this->throwException(new Exception("DummyException"))) |
350
|
|
|
; |
351
|
|
|
|
352
|
|
|
$rescheduleProducer |
|
|
|
|
353
|
|
|
->expects($this->once()) |
354
|
|
|
->method('publish'); |
355
|
|
|
|
356
|
|
|
$worker = new RabbitMqWorker($eventDispatcher, $processorFactory, $serializer, $rescheduleProducer); |
357
|
|
|
|
358
|
|
|
$this->assertEquals(ConsumerInterface::MSG_REJECT, $worker->execute($msg)); |
359
|
|
|
} |
360
|
|
|
|
361
|
|
|
/** |
362
|
|
|
* |
363
|
|
|
* @return $processorFactory PHPUnit_Framework_MockObject_MockObject|PHPUnit_Framework_MockObject_Generator|ProcessorFactoryInterface |
|
|
|
|
364
|
|
|
*/ |
365
|
|
|
private function getProcessorFactory() |
366
|
|
|
{ |
367
|
|
|
/* @var $processorFactory PHPUnit_Framework_MockObject_MockObject|PHPUnit_Framework_MockObject_Generator|ProcessorFactoryInterface */ |
368
|
|
|
$processorFactory = $this->getMockBuilder(ProcessorFactoryInterface::class)->getMock(); |
369
|
|
|
return $processorFactory; |
370
|
|
|
} |
371
|
|
|
|
372
|
|
|
/** |
373
|
|
|
* |
374
|
|
|
* @return PHPUnit_Framework_MockObject_Generator|PHPUnit_Framework_MockObject_MockObject|CommandProcessorInterface |
375
|
|
|
*/ |
376
|
|
|
private function getProcessor() |
377
|
|
|
{ |
378
|
|
|
return $this->getMockBuilder(CommandProcessorInterface::class)->getMock(); |
379
|
|
|
} |
380
|
|
|
|
381
|
|
|
/** |
382
|
|
|
* |
383
|
|
|
* @return PHPUnit_Framework_MockObject_Generator|PHPUnit_Framework_MockObject_MockObject|CommandInterface |
384
|
|
|
*/ |
385
|
|
|
private function getCommand() |
386
|
|
|
{ |
387
|
|
|
return $this->getMockBuilder(CommandInterface::class)->getMock(); |
388
|
|
|
} |
389
|
|
|
|
390
|
|
|
/** |
391
|
|
|
* @return PHPUnit_Framework_MockObject_Generator|PHPUnit_Framework_MockObject_MockObject|Serializer |
392
|
|
|
*/ |
393
|
|
|
private function getSerializer() |
394
|
|
|
{ |
395
|
|
|
return $this->getMockBuilder(Serializer::class)->disableOriginalConstructor()->getMock(); |
396
|
|
|
} |
397
|
|
|
|
398
|
|
|
/** |
399
|
|
|
* |
400
|
|
|
* @return PHPUnit_Framework_MockObject_Generator|PHPUnit_Framework_MockObject_MockObject|EventDispatcherInterface |
401
|
|
|
*/ |
402
|
|
|
private function getEventDispatcher() |
403
|
|
|
{ |
404
|
|
|
return $this->getMockBuilder(EventDispatcherInterface::class)->getMock(); |
405
|
|
|
} |
406
|
|
|
|
407
|
|
|
/** |
408
|
|
|
* |
409
|
|
|
* @return PHPUnit_Framework_MockObject_Generator|PHPUnit_Framework_MockObject_MockObject|ProducerInterface |
410
|
|
|
*/ |
411
|
|
|
private function getRescheduleProducer() |
412
|
|
|
{ |
413
|
|
|
return $this->getMockBuilder(ProducerInterface::class)->getMock(); |
414
|
|
|
} |
415
|
|
|
|
416
|
|
|
} |
417
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..