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 Symfony\Component\EventDispatcher\EventDispatcherInterface; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Description of RabbitMqWorkerTest |
25
|
|
|
* |
26
|
|
|
* @author Tomasz Struczyński <[email protected]> |
27
|
|
|
*/ |
28
|
|
|
class RabbitMqWorkerTest extends PHPUnit_Framework_TestCase |
29
|
|
|
{ |
30
|
|
|
public function testCorrectPass() |
31
|
|
|
{ |
32
|
|
|
$command = $this->getCommand(); |
33
|
|
|
$processor = $this->getProcessor(); |
34
|
|
|
$processorFactory = $this->getProcessorFactory(); |
35
|
|
|
$serializer = $this->getSerializer(); |
36
|
|
|
$eventDispatcher = $this->getEventDispatcher(); |
37
|
|
|
$rescheduleProducer = $this->getRescheduleProducer(); |
38
|
|
|
|
39
|
|
|
$msg = new AMQPMessage("Test", array( |
40
|
|
|
'application_headers' => new AMQPTable( |
41
|
|
|
array( |
42
|
|
|
'x-class-name' => get_class($command), |
43
|
|
|
) |
44
|
|
|
), |
45
|
|
|
)); |
46
|
|
|
$msg->delivery_info = array( |
|
|
|
|
47
|
|
|
'channel' => null, |
48
|
|
|
'consumer_tag' => 't', |
49
|
|
|
'delivery_tag' => 't', |
50
|
|
|
'redelivered' => true, |
51
|
|
|
'exchange' => 't', |
52
|
|
|
'routing_key' => 't' |
53
|
|
|
); |
54
|
|
|
|
55
|
|
|
$eventDispatcher->expects($this->exactly(4)) |
|
|
|
|
56
|
|
|
->method('dispatch') |
57
|
|
|
->withConsecutive( |
58
|
|
|
array($this->equalTo(QueueEvents::WORKER_RUN_BEFORE_TRANSLATE)), |
59
|
|
|
array($this->equalTo(QueueEvents::WORKER_RUN_BEFORE_GET_PROCESSOR)), |
60
|
|
|
array($this->equalTo(QueueEvents::WORKER_RUN_BEFORE_PROCESS)), |
61
|
|
|
array($this->equalTo(QueueEvents::WORKER_RUN_AFTER_PROCESS)) |
62
|
|
|
); |
63
|
|
|
|
64
|
|
|
$serializer->expects($this->once())->method('deserialize') |
|
|
|
|
65
|
|
|
->with("Test", get_class($command), 'json') |
66
|
|
|
->will($this->returnValue($command)); |
67
|
|
|
|
68
|
|
|
$processorFactory->expects($this->once()) |
|
|
|
|
69
|
|
|
->method('getProcessor') |
70
|
|
|
->will($this->returnValue($processor)); |
71
|
|
|
|
72
|
|
|
$processor->expects($this->once()) |
|
|
|
|
73
|
|
|
->method('process') |
74
|
|
|
->with($command) |
75
|
|
|
; |
76
|
|
|
|
77
|
|
|
$worker = new RabbitMqWorker($eventDispatcher, $processorFactory, $serializer, $rescheduleProducer); |
78
|
|
|
|
79
|
|
|
$worker->execute($msg); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
public function testTranslateError() |
83
|
|
|
{ |
84
|
|
|
$command = $this->getCommand(); |
85
|
|
|
$processorFactory = $this->getProcessorFactory(); |
86
|
|
|
$serializer = $this->getSerializer(); |
87
|
|
|
$eventDispatcher = $this->getEventDispatcher(); |
88
|
|
|
$rescheduleProducer = $this->getRescheduleProducer(); |
89
|
|
|
|
90
|
|
|
$msg = new AMQPMessage("Test", array( |
91
|
|
|
'application_headers' => new AMQPTable( |
92
|
|
|
array( |
93
|
|
|
) |
94
|
|
|
), |
95
|
|
|
)); |
96
|
|
|
$msg->delivery_info = array( |
|
|
|
|
97
|
|
|
'channel' => null, |
98
|
|
|
'consumer_tag' => 't', |
99
|
|
|
'delivery_tag' => 't', |
100
|
|
|
'redelivered' => true, |
101
|
|
|
'exchange' => 't', |
102
|
|
|
'routing_key' => 't' |
103
|
|
|
); |
104
|
|
|
|
105
|
|
|
$eventDispatcher->expects($this->exactly(1)) |
|
|
|
|
106
|
|
|
->method('dispatch') |
107
|
|
|
->withConsecutive( |
108
|
|
|
array($this->equalTo(QueueEvents::WORKER_RUN_BEFORE_TRANSLATE)) |
109
|
|
|
); |
110
|
|
|
|
111
|
|
|
$serializer->expects($this->never())->method('deserialize') |
|
|
|
|
112
|
|
|
->with("Test", get_class($command), 'json') |
113
|
|
|
->will($this->returnValue($command)); |
114
|
|
|
$rescheduleProducer |
|
|
|
|
115
|
|
|
->expects($this->once()) |
116
|
|
|
->method('publish'); |
117
|
|
|
|
118
|
|
|
$worker = new RabbitMqWorker($eventDispatcher, $processorFactory, $serializer, $rescheduleProducer); |
119
|
|
|
|
120
|
|
|
$this->assertEquals(ConsumerInterface::MSG_REJECT, $worker->execute($msg)); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
public function testTranslateErrorNoRepublish() |
124
|
|
|
{ |
125
|
|
|
$command = $this->getCommand(); |
126
|
|
|
$processorFactory = $this->getProcessorFactory(); |
127
|
|
|
$serializer = $this->getSerializer(); |
128
|
|
|
$eventDispatcher = $this->getEventDispatcher(); |
129
|
|
|
$rescheduleProducer = $this->getRescheduleProducer(); |
130
|
|
|
|
131
|
|
|
$msg = new AMQPMessage("Test", array( |
132
|
|
|
'application_headers' => new AMQPTable( |
133
|
|
|
array( |
134
|
|
|
'x-death' => array(array('count' => 10)), |
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($command)); |
156
|
|
|
$rescheduleProducer |
|
|
|
|
157
|
|
|
->expects($this->never()) |
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 testGetProcessorError() |
166
|
|
|
{ |
167
|
|
|
$command = $this->getCommand(); |
168
|
|
|
$processor = $this->getProcessor(); |
169
|
|
|
$processorFactory = $this->getProcessorFactory(); |
170
|
|
|
$serializer = $this->getSerializer(); |
171
|
|
|
$eventDispatcher = $this->getEventDispatcher(); |
172
|
|
|
$rescheduleProducer = $this->getRescheduleProducer(); |
173
|
|
|
|
174
|
|
|
$msg = new AMQPMessage("Test", array( |
175
|
|
|
'application_headers' => new AMQPTable( |
176
|
|
|
array( |
177
|
|
|
'x-class-name' => get_class($command), |
178
|
|
|
) |
179
|
|
|
), |
180
|
|
|
)); |
181
|
|
|
$msg->delivery_info = array( |
|
|
|
|
182
|
|
|
'channel' => null, |
183
|
|
|
'consumer_tag' => 't', |
184
|
|
|
'delivery_tag' => 't', |
185
|
|
|
'redelivered' => true, |
186
|
|
|
'exchange' => 't', |
187
|
|
|
'routing_key' => 't' |
188
|
|
|
); |
189
|
|
|
|
190
|
|
|
$eventDispatcher->expects($this->exactly(2)) |
|
|
|
|
191
|
|
|
->method('dispatch') |
192
|
|
|
->withConsecutive( |
193
|
|
|
array($this->equalTo(QueueEvents::WORKER_RUN_BEFORE_TRANSLATE)), |
194
|
|
|
array($this->equalTo(QueueEvents::WORKER_RUN_BEFORE_GET_PROCESSOR)) |
195
|
|
|
); |
196
|
|
|
|
197
|
|
|
$serializer->expects($this->once())->method('deserialize') |
|
|
|
|
198
|
|
|
->with("Test", get_class($command), 'json') |
199
|
|
|
->will($this->returnValue($command)); |
200
|
|
|
|
201
|
|
|
$processorFactory->expects($this->once()) |
|
|
|
|
202
|
|
|
->method('getProcessor') |
203
|
|
|
->will($this->throwException(new ProcessorNotFoundException("ProcessorNotFound"))); |
204
|
|
|
|
205
|
|
|
$processor->expects($this->never()) |
|
|
|
|
206
|
|
|
->method('process') |
207
|
|
|
->with($command) |
208
|
|
|
; |
209
|
|
|
|
210
|
|
|
$rescheduleProducer |
|
|
|
|
211
|
|
|
->expects($this->once()) |
212
|
|
|
->method('publish'); |
213
|
|
|
|
214
|
|
|
$worker = new RabbitMqWorker($eventDispatcher, $processorFactory, $serializer, $rescheduleProducer); |
215
|
|
|
|
216
|
|
|
$this->assertEquals(ConsumerInterface::MSG_REJECT, $worker->execute($msg)); |
217
|
|
|
} |
218
|
|
|
|
219
|
|
|
public function testProcessError() |
220
|
|
|
{ |
221
|
|
|
$command = $this->getCommand(); |
222
|
|
|
$processor = $this->getProcessor(); |
223
|
|
|
$processorFactory = $this->getProcessorFactory(); |
224
|
|
|
$serializer = $this->getSerializer(); |
225
|
|
|
$eventDispatcher = $this->getEventDispatcher(); |
226
|
|
|
$rescheduleProducer = $this->getRescheduleProducer(); |
227
|
|
|
|
228
|
|
|
$msg = new AMQPMessage("Test", array( |
229
|
|
|
'application_headers' => new AMQPTable( |
230
|
|
|
array( |
231
|
|
|
'x-class-name' => get_class($command), |
232
|
|
|
) |
233
|
|
|
), |
234
|
|
|
)); |
235
|
|
|
$msg->delivery_info = array( |
|
|
|
|
236
|
|
|
'channel' => null, |
237
|
|
|
'consumer_tag' => 't', |
238
|
|
|
'delivery_tag' => 't', |
239
|
|
|
'redelivered' => true, |
240
|
|
|
'exchange' => 't', |
241
|
|
|
'routing_key' => 't' |
242
|
|
|
); |
243
|
|
|
|
244
|
|
|
$eventDispatcher->expects($this->exactly(4)) |
|
|
|
|
245
|
|
|
->method('dispatch') |
246
|
|
|
->withConsecutive( |
247
|
|
|
array($this->equalTo(QueueEvents::WORKER_RUN_BEFORE_TRANSLATE)), |
248
|
|
|
array($this->equalTo(QueueEvents::WORKER_RUN_BEFORE_GET_PROCESSOR)), |
249
|
|
|
array($this->equalTo(QueueEvents::WORKER_RUN_BEFORE_PROCESS)), |
250
|
|
|
array($this->equalTo(QueueEvents::WORKER_RUN_PROCESSOR_ERROR), $this->callback(function(QueueProcessErrorEvent $event) { |
251
|
|
|
return $event->getException()->getMessage() == "DummyException"; |
252
|
|
|
})) |
253
|
|
|
); |
254
|
|
|
|
255
|
|
|
$serializer->expects($this->once())->method('deserialize') |
|
|
|
|
256
|
|
|
->with("Test", get_class($command), 'json') |
257
|
|
|
->will($this->returnValue($command)); |
258
|
|
|
|
259
|
|
|
$processorFactory->expects($this->once()) |
|
|
|
|
260
|
|
|
->method('getProcessor') |
261
|
|
|
->will($this->returnValue($processor)); |
262
|
|
|
|
263
|
|
|
$processor->expects($this->once()) |
|
|
|
|
264
|
|
|
->method('process') |
265
|
|
|
->with($command) |
266
|
|
|
->will($this->throwException(new Exception("DummyException"))) |
267
|
|
|
; |
268
|
|
|
|
269
|
|
|
$rescheduleProducer |
|
|
|
|
270
|
|
|
->expects($this->once()) |
271
|
|
|
->method('publish'); |
272
|
|
|
|
273
|
|
|
$worker = new RabbitMqWorker($eventDispatcher, $processorFactory, $serializer, $rescheduleProducer); |
274
|
|
|
|
275
|
|
|
$this->assertEquals(ConsumerInterface::MSG_REJECT, $worker->execute($msg)); |
276
|
|
|
} |
277
|
|
|
|
278
|
|
|
/** |
279
|
|
|
* |
280
|
|
|
* @return $processorFactory PHPUnit_Framework_MockObject_MockObject|PHPUnit_Framework_MockObject_Generator|ProcessorFactoryInterface |
|
|
|
|
281
|
|
|
*/ |
282
|
|
|
private function getProcessorFactory() |
283
|
|
|
{ |
284
|
|
|
/* @var $processorFactory PHPUnit_Framework_MockObject_MockObject|PHPUnit_Framework_MockObject_Generator|ProcessorFactoryInterface */ |
285
|
|
|
$processorFactory = $this->getMockBuilder(ProcessorFactoryInterface::class)->getMock(); |
286
|
|
|
return $processorFactory; |
287
|
|
|
} |
288
|
|
|
|
289
|
|
|
/** |
290
|
|
|
* |
291
|
|
|
* @return PHPUnit_Framework_MockObject_Generator|PHPUnit_Framework_MockObject_MockObject|CommandProcessorInterface |
292
|
|
|
*/ |
293
|
|
|
private function getProcessor() |
294
|
|
|
{ |
295
|
|
|
return $this->getMockBuilder(CommandProcessorInterface::class)->getMock(); |
296
|
|
|
} |
297
|
|
|
|
298
|
|
|
/** |
299
|
|
|
* |
300
|
|
|
* @return PHPUnit_Framework_MockObject_Generator|PHPUnit_Framework_MockObject_MockObject|CommandInterface |
301
|
|
|
*/ |
302
|
|
|
private function getCommand() |
303
|
|
|
{ |
304
|
|
|
return $this->getMockBuilder(CommandInterface::class)->getMock(); |
305
|
|
|
} |
306
|
|
|
|
307
|
|
|
/** |
308
|
|
|
* @return PHPUnit_Framework_MockObject_Generator|PHPUnit_Framework_MockObject_MockObject|Serializer |
309
|
|
|
*/ |
310
|
|
|
private function getSerializer() |
311
|
|
|
{ |
312
|
|
|
return $this->getMockBuilder(Serializer::class)->disableOriginalConstructor()->getMock(); |
313
|
|
|
} |
314
|
|
|
|
315
|
|
|
/** |
316
|
|
|
* |
317
|
|
|
* @return PHPUnit_Framework_MockObject_Generator|PHPUnit_Framework_MockObject_MockObject|EventDispatcherInterface |
318
|
|
|
*/ |
319
|
|
|
private function getEventDispatcher() |
320
|
|
|
{ |
321
|
|
|
return $this->getMockBuilder(EventDispatcherInterface::class)->getMock(); |
322
|
|
|
} |
323
|
|
|
|
324
|
|
|
/** |
325
|
|
|
* |
326
|
|
|
* @return PHPUnit_Framework_MockObject_Generator|PHPUnit_Framework_MockObject_MockObject|ProducerInterface |
327
|
|
|
*/ |
328
|
|
|
private function getRescheduleProducer() |
329
|
|
|
{ |
330
|
|
|
return $this->getMockBuilder(ProducerInterface::class)->getMock(); |
331
|
|
|
} |
332
|
|
|
|
333
|
|
|
} |
334
|
|
|
|
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..