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); |
|
|
|
|
36
|
|
|
$this->event = $this->createMock(Event::class); |
|
|
|
|
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->expectException(HandlerException::class); |
57
|
|
|
$this->expectExceptionMessage('The handler for `test` is invalid.'); |
58
|
|
|
|
59
|
|
|
$name = 'test'; |
60
|
|
|
$routes = [ |
61
|
|
|
$name => 'test', |
62
|
|
|
]; |
63
|
|
|
|
64
|
|
|
$method = static::getProtectedMethod($this->worker, 'getHandler'); |
65
|
|
|
$method->invoke($this->worker, $name, $routes); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
public function testGetHandlerNoHandler() |
69
|
|
|
{ |
70
|
|
|
$method = static::getProtectedMethod($this->worker, 'getHandler'); |
71
|
|
|
$this->assertNull($method->invoke($this->worker, 'test', [])); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
public function testTickPacketNull() |
75
|
|
|
{ |
76
|
|
|
$queue = 'test-queue'; |
77
|
|
|
$this->driver |
|
|
|
|
78
|
|
|
->expects($this->once()) |
79
|
|
|
->method('pop') |
80
|
|
|
->with($queue) |
81
|
|
|
->willReturn(null); |
82
|
|
|
|
83
|
|
|
$method = static::getProtectedMethod($this->worker, 'tick'); |
84
|
|
|
$this->assertTrue($method->invoke($this->worker, $queue)); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
public function testTickInvalidHandler() |
88
|
|
|
{ |
89
|
|
|
$message = [ |
90
|
|
|
'queue' => 'test-queue', |
91
|
|
|
'handler' => 'foo', |
92
|
|
|
'data' => ['foo' => 'bar'], |
93
|
|
|
]; |
94
|
|
|
|
95
|
|
|
$this->driver |
|
|
|
|
96
|
|
|
->expects($this->once()) |
97
|
|
|
->method('pop') |
98
|
|
|
->with($message['queue']) |
99
|
|
|
->willReturn(json_encode($message)); |
100
|
|
|
|
101
|
|
|
$method = static::getProtectedMethod($this->worker, 'tick'); |
102
|
|
|
$this->assertTrue($method->invoke($this->worker, $message['queue'])); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
public function testTickHandlerException() |
106
|
|
|
{ |
107
|
|
|
$message = new Message('queue', 'foo', ['foo' => 'bar']); |
108
|
|
|
$exception = new Exception; |
109
|
|
|
|
110
|
|
|
$this->driver |
|
|
|
|
111
|
|
|
->expects($this->once()) |
112
|
|
|
->method('pop') |
113
|
|
|
->with($message->queue()) |
114
|
|
|
->willReturn($this->serializer->serialize($message)); |
115
|
|
|
|
116
|
|
|
$this->event |
|
|
|
|
117
|
|
|
->expects($this->once()) |
118
|
|
|
->method('reject') |
119
|
|
|
->with($message, $exception); |
120
|
|
|
|
121
|
|
|
$worker = new Worker( |
122
|
|
|
$this->driver, |
123
|
|
|
$this->event, |
124
|
|
|
$this->serializer, |
125
|
|
|
['foo' => function () use ($exception) { throw $exception; }] |
126
|
|
|
); |
127
|
|
|
|
128
|
|
|
$method = static::getProtectedMethod($worker, 'tick'); |
129
|
|
|
$this->assertTrue($method->invoke($worker, $message->queue())); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
public function testTickHandlerReturnFalse() |
133
|
|
|
{ |
134
|
|
|
$message = new Message('queue', 'foo', ['foo' => 'bar']); |
135
|
|
|
|
136
|
|
|
$this->driver |
|
|
|
|
137
|
|
|
->expects($this->once()) |
138
|
|
|
->method('pop') |
139
|
|
|
->with($message->queue()) |
140
|
|
|
->willReturn($this->serializer->serialize($message)); |
141
|
|
|
|
142
|
|
|
$this->event |
|
|
|
|
143
|
|
|
->expects($this->once()) |
144
|
|
|
->method('acknowledge') |
145
|
|
|
->with($message); |
146
|
|
|
|
147
|
|
|
$worker = new Worker( |
148
|
|
|
$this->driver, |
149
|
|
|
$this->event, |
150
|
|
|
$this->serializer, |
151
|
|
|
['foo' => function () { return false; }] |
152
|
|
|
); |
153
|
|
|
|
154
|
|
|
$method = static::getProtectedMethod($worker, 'tick'); |
155
|
|
|
$this->assertFalse($method->invoke($worker, $message->queue())); |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
public function testTick() |
159
|
|
|
{ |
160
|
|
|
$message = new Message('queue', 'foo', ['name' => 'foo']); |
161
|
|
|
|
162
|
|
|
$this->driver |
|
|
|
|
163
|
|
|
->expects($this->once()) |
164
|
|
|
->method('pop') |
165
|
|
|
->with($message->queue()) |
166
|
|
|
->willReturn($this->serializer->serialize($message)); |
167
|
|
|
|
168
|
|
|
$this->event |
|
|
|
|
169
|
|
|
->expects($this->once()) |
170
|
|
|
->method('acknowledge') |
171
|
|
|
->with($message); |
172
|
|
|
|
173
|
|
|
$worker = new Worker( |
174
|
|
|
$this->driver, |
175
|
|
|
$this->event, |
176
|
|
|
$this->serializer, |
177
|
|
|
[ |
178
|
|
|
'foo' => function ($data) use ($message) { |
179
|
|
|
$this->assertSame($message->handler(), $data->handler()); |
180
|
|
|
} |
181
|
|
|
] |
182
|
|
|
); |
183
|
|
|
|
184
|
|
|
$method = static::getProtectedMethod($worker, 'tick'); |
185
|
|
|
$this->assertTrue($method->invoke($worker, $message->queue())); |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
public function testConsume() |
189
|
|
|
{ |
190
|
|
|
$message = new Message('queue', 'foo', ['name' => 'foo']); |
191
|
|
|
|
192
|
|
|
$this->driver |
|
|
|
|
193
|
|
|
->expects($this->once()) |
194
|
|
|
->method('pop') |
195
|
|
|
->with($message->queue()) |
196
|
|
|
->willReturn($this->serializer->serialize($message)); |
197
|
|
|
|
198
|
|
|
$this->event |
|
|
|
|
199
|
|
|
->expects($this->once()) |
200
|
|
|
->method('acknowledge') |
201
|
|
|
->with($message); |
202
|
|
|
|
203
|
|
|
$worker = new Worker( |
204
|
|
|
$this->driver, |
205
|
|
|
$this->event, |
206
|
|
|
$this->serializer, |
207
|
|
|
['foo' => function () { return false; }] |
208
|
|
|
); |
209
|
|
|
|
210
|
|
|
$worker->consume($message->queue()); |
211
|
|
|
} |
212
|
|
|
} |
213
|
|
|
|
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..