Completed
Push — master ( 59049f...fab486 )
by Tomasz
12:46 queued 02:08
created

RabbitMqWorkerTest::testProcessError()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 58
Code Lines 41

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 58
rs 9.639
c 0
b 0
f 0
cc 1
eloc 41
nc 1
nop 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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(
0 ignored issues
show
Documentation Bug introduced by
It seems like array('channel' => null,..., 'routing_key' => 't') of type array<string,null|string...routing_key":"string"}> is incompatible with the declared type array<integer,object<Php...b\Channel\AMQPChannel>> of property $delivery_info.

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..

Loading history...
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))
0 ignored issues
show
Bug introduced by
The method expects() does not seem to exist on object<Symfony\Component...entDispatcherInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
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')
0 ignored issues
show
Bug introduced by
The method expects() does not seem to exist on object<JMS\Serializer\Serializer>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
65
            ->with("Test", get_class($command), 'json')
66
            ->will($this->returnValue($command));
67
68
        $processorFactory->expects($this->once())
0 ignored issues
show
Bug introduced by
The method expects() does not seem to exist on object<Gendoria\CommandQ...cessorFactoryInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
69
            ->method('getProcessor')
70
            ->will($this->returnValue($processor));
71
        
72
        $processor->expects($this->once())
0 ignored issues
show
Bug introduced by
The method expects() does not seem to exist on object<Gendoria\CommandQ...mandProcessorInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
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(
0 ignored issues
show
Documentation Bug introduced by
It seems like array('channel' => null,..., 'routing_key' => 't') of type array<string,null|string...routing_key":"string"}> is incompatible with the declared type array<integer,object<Php...b\Channel\AMQPChannel>> of property $delivery_info.

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..

Loading history...
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))
0 ignored issues
show
Bug introduced by
The method expects() does not seem to exist on object<Symfony\Component...entDispatcherInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
106
            ->method('dispatch')
107
            ->withConsecutive(
108
                array($this->equalTo(QueueEvents::WORKER_RUN_BEFORE_TRANSLATE))
109
            );
110
111
        $serializer->expects($this->never())->method('deserialize')
0 ignored issues
show
Bug introduced by
The method expects() does not seem to exist on object<JMS\Serializer\Serializer>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
112
            ->with("Test", get_class($command), 'json')
113
            ->will($this->returnValue($command));
114
        $rescheduleProducer
0 ignored issues
show
Bug introduced by
The method expects() does not seem to exist on object<OldSound\RabbitMq...itMq\ProducerInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
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(
0 ignored issues
show
Documentation Bug introduced by
It seems like array('channel' => null,..., 'routing_key' => 't') of type array<string,null|string...routing_key":"string"}> is incompatible with the declared type array<integer,object<Php...b\Channel\AMQPChannel>> of property $delivery_info.

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..

Loading history...
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))
0 ignored issues
show
Bug introduced by
The method expects() does not seem to exist on object<Symfony\Component...entDispatcherInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
148
            ->method('dispatch')
149
            ->withConsecutive(
150
                array($this->equalTo(QueueEvents::WORKER_RUN_BEFORE_TRANSLATE))
151
            );
152
153
        $serializer->expects($this->never())->method('deserialize')
0 ignored issues
show
Bug introduced by
The method expects() does not seem to exist on object<JMS\Serializer\Serializer>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
154
            ->with("Test", get_class($command), 'json')
155
            ->will($this->returnValue($command));
156
        $rescheduleProducer
0 ignored issues
show
Bug introduced by
The method expects() does not seem to exist on object<OldSound\RabbitMq...itMq\ProducerInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
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(
0 ignored issues
show
Documentation Bug introduced by
It seems like array('channel' => null,..., 'routing_key' => 't') of type array<string,null|string...routing_key":"string"}> is incompatible with the declared type array<integer,object<Php...b\Channel\AMQPChannel>> of property $delivery_info.

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..

Loading history...
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))
0 ignored issues
show
Bug introduced by
The method expects() does not seem to exist on object<Symfony\Component...entDispatcherInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
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')
0 ignored issues
show
Bug introduced by
The method expects() does not seem to exist on object<JMS\Serializer\Serializer>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
198
            ->with("Test", get_class($command), 'json')
199
            ->will($this->returnValue($command));
200
201
        $processorFactory->expects($this->once())
0 ignored issues
show
Bug introduced by
The method expects() does not seem to exist on object<Gendoria\CommandQ...cessorFactoryInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
202
            ->method('getProcessor')
203
            ->will($this->throwException(new ProcessorNotFoundException("ProcessorNotFound")));
204
        
205
        $processor->expects($this->never())
0 ignored issues
show
Bug introduced by
The method expects() does not seem to exist on object<Gendoria\CommandQ...mandProcessorInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
206
            ->method('process')
207
            ->with($command)
208
            ;
209
        
210
        $rescheduleProducer
0 ignored issues
show
Bug introduced by
The method expects() does not seem to exist on object<OldSound\RabbitMq...itMq\ProducerInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
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(
0 ignored issues
show
Documentation Bug introduced by
It seems like array('channel' => null,..., 'routing_key' => 't') of type array<string,null|string...routing_key":"string"}> is incompatible with the declared type array<integer,object<Php...b\Channel\AMQPChannel>> of property $delivery_info.

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..

Loading history...
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))
0 ignored issues
show
Bug introduced by
The method expects() does not seem to exist on object<Symfony\Component...entDispatcherInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
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')
0 ignored issues
show
Bug introduced by
The method expects() does not seem to exist on object<JMS\Serializer\Serializer>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
256
            ->with("Test", get_class($command), 'json')
257
            ->will($this->returnValue($command));
258
259
        $processorFactory->expects($this->once())
0 ignored issues
show
Bug introduced by
The method expects() does not seem to exist on object<Gendoria\CommandQ...cessorFactoryInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
260
            ->method('getProcessor')
261
            ->will($this->returnValue($processor));
262
        
263
        $processor->expects($this->once())
0 ignored issues
show
Bug introduced by
The method expects() does not seem to exist on object<Gendoria\CommandQ...mandProcessorInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
264
            ->method('process')
265
            ->with($command)
266
            ->will($this->throwException(new Exception("DummyException")))
267
            ;
268
        
269
        $rescheduleProducer
0 ignored issues
show
Bug introduced by
The method expects() does not seem to exist on object<OldSound\RabbitMq...itMq\ProducerInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
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
0 ignored issues
show
Documentation introduced by
The doc-type $processorFactory could not be parsed: Unknown type name "$processorFactory" at position 0. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
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