Completed
Pull Request — master (#1)
by Adam
03:21
created

WorkerTest   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 204
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 4
Bugs 0 Features 4
Metric Value
wmc 10
c 4
b 0
f 4
lcom 1
cbo 5
dl 0
loc 204
rs 10

10 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 7 1
A testGetHandler() 0 12 1
A testGetHandlerNotCallable() 0 15 1
A testGetHandlerNoHandler() 0 5 1
A testTickPacketNull() 0 12 1
A testTickInvalidHandler() 0 17 1
B testTickHandlerException() 0 26 1
B testTickHandlerReturnFalse() 0 25 1
B testTick() 0 29 1
B testConsume() 0 24 1
1
<?php
2
3
namespace Equip\Queue;
4
5
use Equip\Queue\Driver\DriverInterface;
6
use Equip\Queue\Exception\HandlerException;
7
use Equip\Queue\Serializer\JsonSerializer;
8
use Equip\Queue\Serializer\MessageSerializerInterface;
9
use Exception;
10
11
class WorkerTest extends TestCase
12
{
13
    /**
14
     * @var DriverInterface
15
     */
16
    private $driver;
17
18
    /**
19
     * @var Event
20
     */
21
    private $event;
22
23
    /**
24
     * @var MessageSerializerInterface
25
     */
26
    private $serializer;
27
28
    /**
29
     * @var Worker
30
     */
31
    private $worker;
32
33
    protected function setUp()
34
    {
35
        $this->driver = $this->createMock(DriverInterface::class);
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->createMock(\Equip...DriverInterface::class) of type object<PHPUnit_Framework_MockObject_MockObject> is incompatible with the declared type object<Equip\Queue\Driver\DriverInterface> of property $driver.

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...
36
        $this->event = $this->createMock(Event::class);
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->createMock(\Equip\Queue\Event::class) of type object<PHPUnit_Framework_MockObject_MockObject> is incompatible with the declared type object<Equip\Queue\Event> of property $event.

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...
37
        $this->serializer = new JsonSerializer;
38
        $this->worker = new Worker($this->driver, $this->event);
39
    }
40
41
    public function testGetHandler()
42
    {
43
        $name = 'test';
44
        $routes = [
45
            $name => function () {},
46
        ];
47
48
        $method = static::getProtectedMethod($this->worker, 'getHandler');
49
        $result = $method->invoke($this->worker, $name, $routes);
50
51
        $this->assertSame($routes[$name], $result);
52
    }
53
54
    public function testGetHandlerNotCallable()
55
    {
56
        $this->setExpectedExceptionRegExp(
57
            HandlerException::class,
58
            '/The handler for `test` is invalid./'
59
        );
60
61
        $name = 'test';
62
        $routes = [
63
            $name => 'test',
64
        ];
65
66
        $method = static::getProtectedMethod($this->worker, 'getHandler');
67
        $method->invoke($this->worker, $name, $routes);
68
    }
69
70
    public function testGetHandlerNoHandler()
71
    {
72
        $method = static::getProtectedMethod($this->worker, 'getHandler');
73
        $this->assertNull($method->invoke($this->worker, 'test', []));
74
    }
75
76
    public function testTickPacketNull()
77
    {
78
        $queue = 'test-queue';
79
        $this->driver
0 ignored issues
show
Bug introduced by
The method expects() does not seem to exist on object<Equip\Queue\Driver\DriverInterface>.

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...
80
            ->expects($this->once())
81
            ->method('pop')
82
            ->with($queue)
83
            ->willReturn(null);
84
85
        $method = static::getProtectedMethod($this->worker, 'tick');
86
        $this->assertTrue($method->invoke($this->worker, $queue));
87
    }
88
89
    public function testTickInvalidHandler()
90
    {
91
        $message = [
92
            'queue' => 'test-queue',
93
            'handler' => 'foo',
94
            'data' => ['foo' => 'bar'],
95
        ];
96
97
        $this->driver
0 ignored issues
show
Bug introduced by
The method expects() does not seem to exist on object<Equip\Queue\Driver\DriverInterface>.

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...
98
            ->expects($this->once())
99
            ->method('pop')
100
            ->with($message['queue'])
101
            ->willReturn(json_encode($message));
102
103
        $method = static::getProtectedMethod($this->worker, 'tick');
104
        $this->assertTrue($method->invoke($this->worker, $message['queue']));
105
    }
106
107
    public function testTickHandlerException()
108
    {
109
        $message = new Message('queue', 'foo', ['foo' => 'bar']);
110
        $exception = new Exception;
111
112
        $this->driver
0 ignored issues
show
Bug introduced by
The method expects() does not seem to exist on object<Equip\Queue\Driver\DriverInterface>.

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...
113
            ->expects($this->once())
114
            ->method('pop')
115
            ->with($message->queue())
116
            ->willReturn($this->serializer->serialize($message));
117
118
        $this->event
0 ignored issues
show
Bug introduced by
The method expects() does not seem to exist on object<Equip\Queue\Event>.

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...
119
            ->expects($this->once())
120
            ->method('reject')
121
            ->with($message, $exception);
122
123
        $worker = new Worker(
124
            $this->driver,
125
            $this->event,
126
            $this->serializer,
127
            ['foo' => function () use ($exception) { throw $exception; }]
128
        );
129
130
        $method = static::getProtectedMethod($worker, 'tick');
131
        $this->assertTrue($method->invoke($worker, $message->queue()));
132
    }
133
134
    public function testTickHandlerReturnFalse()
135
    {
136
        $message = new Message('queue', 'foo', ['foo' => 'bar']);
137
138
        $this->driver
0 ignored issues
show
Bug introduced by
The method expects() does not seem to exist on object<Equip\Queue\Driver\DriverInterface>.

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...
139
            ->expects($this->once())
140
            ->method('pop')
141
            ->with($message->queue())
142
            ->willReturn($this->serializer->serialize($message));
143
144
        $this->event
0 ignored issues
show
Bug introduced by
The method expects() does not seem to exist on object<Equip\Queue\Event>.

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...
145
            ->expects($this->once())
146
            ->method('acknowledge')
147
            ->with($message);
148
149
        $worker = new Worker(
150
            $this->driver,
151
            $this->event,
152
            $this->serializer,
153
            ['foo' => function () { return false; }]
154
        );
155
156
        $method = static::getProtectedMethod($worker, 'tick');
157
        $this->assertFalse($method->invoke($worker, $message->queue()));
158
    }
159
160
    public function testTick()
161
    {
162
        $message = new Message('queue', 'foo', ['name' => 'foo']);
163
164
        $this->driver
0 ignored issues
show
Bug introduced by
The method expects() does not seem to exist on object<Equip\Queue\Driver\DriverInterface>.

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...
165
            ->expects($this->once())
166
            ->method('pop')
167
            ->with($message->queue())
168
            ->willReturn($this->serializer->serialize($message));
169
170
        $this->event
0 ignored issues
show
Bug introduced by
The method expects() does not seem to exist on object<Equip\Queue\Event>.

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...
171
            ->expects($this->once())
172
            ->method('acknowledge')
173
            ->with($message);
174
175
        $worker = new Worker(
176
            $this->driver,
177
            $this->event,
178
            $this->serializer,
179
            [
180
                'foo' => function ($data) use ($message) {
181
                    $this->assertSame($message->handler(), $data->handler());
182
                }
183
            ]
184
        );
185
186
        $method = static::getProtectedMethod($worker, 'tick');
187
        $this->assertTrue($method->invoke($worker, $message->queue()));
188
    }
189
190
    public function testConsume()
191
    {
192
        $message = new Message('queue', 'foo', ['name' => 'foo']);
193
194
        $this->driver
0 ignored issues
show
Bug introduced by
The method expects() does not seem to exist on object<Equip\Queue\Driver\DriverInterface>.

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...
195
            ->expects($this->once())
196
            ->method('pop')
197
            ->with($message->queue())
198
            ->willReturn($this->serializer->serialize($message));
199
200
        $this->event
0 ignored issues
show
Bug introduced by
The method expects() does not seem to exist on object<Equip\Queue\Event>.

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...
201
            ->expects($this->once())
202
            ->method('acknowledge')
203
            ->with($message);
204
205
        $worker = new Worker(
206
            $this->driver,
207
            $this->event,
208
            $this->serializer,
209
            ['foo' => function () { return false; }]
210
        );
211
212
        $worker->consume($message->queue());
213
    }
214
}
215