1 | <?php |
||
15 | final class QueueTest extends TestCase |
||
16 | { |
||
17 | private $collection; |
||
18 | private $mongoUrl; |
||
19 | private $queue; |
||
20 | |||
21 | public function setUp() |
||
22 | { |
||
23 | $this->mongoUrl = getenv('TESTING_MONGO_URL') ?: 'mongodb://localhost:27017'; |
||
24 | $mongo = new Client( |
||
25 | $this->mongoUrl, |
||
26 | [], |
||
27 | ['typeMap' => ['root' => 'array', 'document' => 'array', 'array' => 'array']] |
||
28 | ); |
||
29 | $this->collection = $mongo->selectDatabase('testing')->selectCollection('messages'); |
||
30 | $this->collection->deleteMany([]); |
||
31 | |||
32 | $this->queue = new Queue($this->collection); |
||
33 | } |
||
34 | |||
35 | public function tearDown() |
||
39 | |||
40 | /** |
||
41 | * @test |
||
42 | * @covers ::__construct |
||
43 | * @expectedException \InvalidArgumentException |
||
44 | */ |
||
45 | public function constructWithNonStringUrl() |
||
49 | |||
50 | /** |
||
51 | * @test |
||
52 | * @covers ::ensureGetIndex |
||
53 | */ |
||
54 | public function ensureGetIndex() |
||
55 | { |
||
56 | $collection = (new Client($this->mongoUrl))->selectDatabase('testing')->selectCollection(uniqid()); |
||
57 | $queue = new Queue($collection); |
||
58 | $queue->ensureGetIndex(['type' => 1], ['boo' => -1]); |
||
59 | $queue->ensureGetIndex(['another.sub' => 1]); |
||
60 | |||
61 | $indexes = iterator_to_array($collection->listIndexes()); |
||
62 | $this->assertSame(3, count($indexes)); |
||
63 | |||
64 | $expectedOne = [ |
||
65 | 'earliestGet' => 1, |
||
66 | 'payload.type' => 1, |
||
67 | 'priority' => 1, |
||
68 | 'created' => 1, |
||
69 | 'payload.boo' => -1, |
||
70 | ]; |
||
71 | $this->assertSame($expectedOne, $indexes[1]['key']); |
||
72 | |||
73 | $expectedTwo = [ |
||
74 | 'earliestGet' => 1, |
||
75 | 'payload.another.sub' => 1, |
||
76 | 'priority' => 1, |
||
77 | 'created' => 1, |
||
78 | ]; |
||
79 | $this->assertSame($expectedTwo, $indexes[2]['key']); |
||
80 | } |
||
81 | |||
82 | /** |
||
83 | * @test |
||
84 | * @covers ::ensureGetIndex |
||
85 | * @expectedException \Exception |
||
86 | */ |
||
87 | public function ensureGetIndexWithTooLongCollectionName() |
||
95 | |||
96 | /** |
||
97 | * @test |
||
98 | * @covers ::ensureGetIndex |
||
99 | * @expectedException \InvalidArgumentException |
||
100 | */ |
||
101 | public function ensureGetIndexWithNonStringBeforeSortKey() |
||
105 | |||
106 | /** |
||
107 | * @test |
||
108 | * @covers ::ensureGetIndex |
||
109 | * @expectedException \InvalidArgumentException |
||
110 | */ |
||
111 | public function ensureGetIndexWithNonStringAfterSortKey() |
||
115 | |||
116 | /** |
||
117 | * @test |
||
118 | * @covers ::ensureGetIndex |
||
119 | * @expectedException \InvalidArgumentException |
||
120 | */ |
||
121 | public function ensureGetIndexWithBadBeforeSortValue() |
||
125 | |||
126 | /** |
||
127 | * @test |
||
128 | * @covers ::ensureGetIndex |
||
129 | * @expectedException \InvalidArgumentException |
||
130 | */ |
||
131 | public function ensureGetIndexWithBadAfterSortValue() |
||
135 | |||
136 | /** |
||
137 | * Verifies the behaviour of the Queue when it cannot create an index after 5 attempts. |
||
138 | * |
||
139 | * @test |
||
140 | * @covers ::ensureGetIndex |
||
141 | * @expectedException \Exception |
||
142 | * @expectedExceptionMessage couldnt create index after 5 attempts |
||
143 | */ |
||
144 | public function ensureIndexCannotBeCreatedAfterFiveAttempts() |
||
153 | |||
154 | /** |
||
155 | * @test |
||
156 | * @covers ::ensureCountIndex |
||
157 | */ |
||
158 | public function ensureCountIndex() |
||
174 | |||
175 | /** |
||
176 | * @test |
||
177 | * @covers ::ensureCountIndex |
||
178 | */ |
||
179 | public function ensureCountIndexWithPrefixOfPrevious() |
||
190 | |||
191 | /** |
||
192 | * @test |
||
193 | * @covers ::ensureCountIndex |
||
194 | * @expectedException \InvalidArgumentException |
||
195 | */ |
||
196 | public function ensureCountIndexWithNonStringKey() |
||
200 | |||
201 | /** |
||
202 | * @test |
||
203 | * @covers ::ensureCountIndex |
||
204 | * @expectedException \InvalidArgumentException |
||
205 | */ |
||
206 | public function ensureCountIndexWithBadValue() |
||
210 | |||
211 | /** |
||
212 | * @test |
||
213 | * @covers ::get |
||
214 | */ |
||
215 | public function getByBadQuery() |
||
224 | |||
225 | /** |
||
226 | * @test |
||
227 | * @covers ::get |
||
228 | * |
||
229 | * @return void |
||
230 | */ |
||
231 | public function getWithOverflowResetTimestamp() |
||
237 | |||
238 | /** |
||
239 | * @test |
||
240 | * @covers ::get |
||
241 | */ |
||
242 | public function getWithNegativePollDuration() |
||
247 | |||
248 | /** |
||
249 | * @test |
||
250 | * @covers ::get |
||
251 | * @expectedException \InvalidArgumentException |
||
252 | */ |
||
253 | public function getWithNonStringKey() |
||
257 | |||
258 | /** |
||
259 | * @test |
||
260 | * @covers ::get |
||
261 | */ |
||
262 | public function getByFullQuery() |
||
274 | |||
275 | /** |
||
276 | * @test |
||
277 | * @covers ::get |
||
278 | */ |
||
279 | public function getBySubDocQuery() |
||
300 | |||
301 | /** |
||
302 | * @test |
||
303 | * @covers ::get |
||
304 | */ |
||
305 | public function getBeforeAck() |
||
318 | |||
319 | /** |
||
320 | * @test |
||
321 | * @covers ::get |
||
322 | */ |
||
323 | public function getWithCustomPriority() |
||
339 | |||
340 | /** |
||
341 | * @test |
||
342 | * @covers ::get |
||
343 | */ |
||
344 | public function getWithTimeBasedPriority() |
||
360 | |||
361 | /** |
||
362 | * @test |
||
363 | * @covers ::get |
||
364 | */ |
||
365 | public function getWait() |
||
376 | |||
377 | /** |
||
378 | * @test |
||
379 | * @covers ::get |
||
380 | */ |
||
381 | public function earliestGet() |
||
396 | |||
397 | /** |
||
398 | * @test |
||
399 | * @covers ::get |
||
400 | */ |
||
401 | public function resetStuck() |
||
436 | |||
437 | /** |
||
438 | * @test |
||
439 | * @covers ::count |
||
440 | * @expectedException \InvalidArgumentException |
||
441 | */ |
||
442 | public function countWithNonStringKey() |
||
446 | |||
447 | /** |
||
448 | * @test |
||
449 | * @covers ::count |
||
450 | */ |
||
451 | public function testCount() |
||
469 | |||
470 | /** |
||
471 | * @test |
||
472 | * @covers ::ack |
||
473 | */ |
||
474 | public function ack() |
||
487 | |||
488 | /** |
||
489 | * @test |
||
490 | * @covers ::requeue |
||
491 | */ |
||
492 | public function requeue() |
||
518 | |||
519 | /** |
||
520 | * @test |
||
521 | * @covers ::send |
||
522 | */ |
||
523 | public function send() |
||
529 | |||
530 | /** |
||
531 | * @test |
||
532 | * @covers ::send |
||
533 | */ |
||
534 | public function sendWithHighEarliestGet() |
||
540 | |||
541 | /** |
||
542 | * @test |
||
543 | * @covers ::send |
||
544 | */ |
||
545 | public function sendWithLowEarliestGet() |
||
551 | |||
552 | private function assertSameMessage(Message $expected, Message $actual) |
||
558 | |||
559 | private function assertSingleMessage(Message $expected) |
||
573 | |||
574 | private function getMessage(array $payload = [], UTCDateTime $earliestGet = null, float $priority = 0.0) |
||
578 | } |
||
579 |