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->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 |
|
|
|
|
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 |
|
|
|
|
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 |
|
|
|
|
113
|
|
|
->expects($this->once()) |
114
|
|
|
->method('pop') |
115
|
|
|
->with($message->queue()) |
116
|
|
|
->willReturn($this->serializer->serialize($message)); |
117
|
|
|
|
118
|
|
|
$this->event |
|
|
|
|
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 |
|
|
|
|
139
|
|
|
->expects($this->once()) |
140
|
|
|
->method('pop') |
141
|
|
|
->with($message->queue()) |
142
|
|
|
->willReturn($this->serializer->serialize($message)); |
143
|
|
|
|
144
|
|
|
$this->event |
|
|
|
|
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 |
|
|
|
|
165
|
|
|
->expects($this->once()) |
166
|
|
|
->method('pop') |
167
|
|
|
->with($message->queue()) |
168
|
|
|
->willReturn($this->serializer->serialize($message)); |
169
|
|
|
|
170
|
|
|
$this->event |
|
|
|
|
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 |
|
|
|
|
195
|
|
|
->expects($this->once()) |
196
|
|
|
->method('pop') |
197
|
|
|
->with($message->queue()) |
198
|
|
|
->willReturn($this->serializer->serialize($message)); |
199
|
|
|
|
200
|
|
|
$this->event |
|
|
|
|
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
|
|
|
|
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..