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() |
||
36 | { |
||
37 | (new Client($this->mongoUrl))->dropDatabase('testing'); |
||
38 | } |
||
39 | |||
40 | /** |
||
41 | * @test |
||
42 | * @covers ::__construct |
||
43 | * @expectedException \InvalidArgumentException |
||
44 | */ |
||
45 | public function constructWithNonStringUrl() |
||
46 | { |
||
47 | new Queue(1, 'testing', 'messages'); |
||
48 | } |
||
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 = ['earliestGet' => 1, 'payload.type' => 1, 'priority' => 1, 'created' => 1, 'payload.boo' => -1]; |
||
65 | $this->assertSame($expectedOne, $indexes[1]['key']); |
||
66 | |||
67 | $expectedTwo = ['earliestGet' => 1, 'payload.another.sub' => 1, 'priority' => 1, 'created' => 1]; |
||
68 | $this->assertSame($expectedTwo, $indexes[2]['key']); |
||
69 | } |
||
70 | |||
71 | /** |
||
72 | * @test |
||
73 | * @covers ::ensureGetIndex |
||
74 | * @expectedException \Exception |
||
75 | */ |
||
76 | public function ensureGetIndexWithTooLongCollectionName() |
||
77 | { |
||
78 | $collectionName = 'messages012345678901234567890123456789012345678901234567890123456789'; |
||
79 | $collectionName .= '012345678901234567890123456789012345678901234567890123456789';//128 chars |
||
80 | |||
81 | $queue = new Queue($this->mongoUrl, 'testing', $collectionName); |
||
82 | $queue->ensureGetIndex([]); |
||
83 | } |
||
84 | |||
85 | /** |
||
86 | * @test |
||
87 | * @covers ::ensureGetIndex |
||
88 | * @expectedException \InvalidArgumentException |
||
89 | */ |
||
90 | public function ensureGetIndexWithNonStringBeforeSortKey() |
||
91 | { |
||
92 | $this->queue->ensureGetIndex([0 => 1]); |
||
93 | } |
||
94 | |||
95 | /** |
||
96 | * @test |
||
97 | * @covers ::ensureGetIndex |
||
98 | * @expectedException \InvalidArgumentException |
||
99 | */ |
||
100 | public function ensureGetIndexWithNonStringAfterSortKey() |
||
101 | { |
||
102 | $this->queue->ensureGetIndex(['field' => 1], [0 => 1]); |
||
103 | } |
||
104 | |||
105 | /** |
||
106 | * @test |
||
107 | * @covers ::ensureGetIndex |
||
108 | * @expectedException \InvalidArgumentException |
||
109 | */ |
||
110 | public function ensureGetIndexWithBadBeforeSortValue() |
||
111 | { |
||
112 | $this->queue->ensureGetIndex(['field' => 'NotAnInt']); |
||
113 | } |
||
114 | |||
115 | /** |
||
116 | * @test |
||
117 | * @covers ::ensureGetIndex |
||
118 | * @expectedException \InvalidArgumentException |
||
119 | */ |
||
120 | public function ensureGetIndexWithBadAfterSortValue() |
||
121 | { |
||
122 | $this->queue->ensureGetIndex([], ['field' => 'NotAnInt']); |
||
123 | } |
||
124 | |||
125 | /** |
||
126 | * Verifies the behaviour of the Queue when it cannot create an index after 5 attempts. |
||
127 | * |
||
128 | * @test |
||
129 | * @covers ::ensureGetIndex |
||
130 | * @expectedException \Exception |
||
131 | * @expectedExceptionMessage couldnt create index after 5 attempts |
||
132 | */ |
||
133 | public function ensureIndexCannotBeCreatedAfterFiveAttempts() |
||
134 | { |
||
135 | $mockCollection = $this->getMockBuilder('\MongoDB\Collection')->disableOriginalConstructor()->getMock(); |
||
136 | |||
137 | $mockCollection->method('listIndexes')->willReturn([]); |
||
138 | |||
139 | $queue = new Queue($mockCollection); |
||
140 | $queue->ensureCountIndex(['type' => 1], false); |
||
141 | } |
||
142 | |||
143 | /** |
||
144 | * @test |
||
145 | * @covers ::ensureCountIndex |
||
146 | */ |
||
147 | public function ensureCountIndex() |
||
148 | { |
||
149 | $collection = (new Client($this->mongoUrl))->selectDatabase('testing')->selectCollection(uniqid()); |
||
150 | $queue = new Queue($collection); |
||
151 | $queue->ensureCountIndex(['type' => 1, 'boo' => -1], false); |
||
152 | $queue->ensureCountIndex(['another.sub' => 1], true); |
||
153 | |||
154 | $indexes = iterator_to_array($collection->listIndexes()); |
||
155 | $this->assertSame(3, count($indexes)); |
||
156 | |||
157 | $expectedOne = ['payload.type' => 1, 'payload.boo' => -1]; |
||
158 | $this->assertSame($expectedOne, $indexes[1]['key']); |
||
159 | |||
160 | $expectedTwo = ['earliestGet' => 1, 'payload.another.sub' => 1]; |
||
161 | $this->assertSame($expectedTwo, $indexes[2]['key']); |
||
162 | } |
||
163 | |||
164 | /** |
||
165 | * @test |
||
166 | * @covers ::ensureCountIndex |
||
167 | */ |
||
168 | public function ensureCountIndexWithPrefixOfPrevious() |
||
179 | |||
180 | /** |
||
181 | * @test |
||
182 | * @covers ::ensureCountIndex |
||
183 | * @expectedException \InvalidArgumentException |
||
184 | */ |
||
185 | public function ensureCountIndexWithNonStringKey() |
||
189 | |||
190 | /** |
||
191 | * @test |
||
192 | * @covers ::ensureCountIndex |
||
193 | * @expectedException \InvalidArgumentException |
||
194 | */ |
||
195 | public function ensureCountIndexWithBadValue() |
||
199 | |||
200 | /** |
||
201 | * @test |
||
202 | * @covers ::get |
||
203 | */ |
||
204 | public function getByBadQuery() |
||
213 | |||
214 | /** |
||
215 | * @test |
||
216 | * @covers ::get |
||
217 | * |
||
218 | * @return void |
||
219 | */ |
||
220 | public function getWithOverflowResetTimestamp() |
||
221 | { |
||
222 | $this->queue->send($this->getMessage()); |
||
223 | $message = $this->queue->get([], ['runningResetDuration' => PHP_INT_MAX])[0]; |
||
224 | $this->assertEquals(new UTCDateTime(PHP_INT_MAX), $message->getEarliestGet()); |
||
226 | |||
227 | /** |
||
228 | * @test |
||
229 | * @covers ::get |
||
230 | */ |
||
231 | public function getWithNegativePollDuration() |
||
236 | |||
237 | /** |
||
238 | * @test |
||
239 | * @covers ::get |
||
240 | * @expectedException \InvalidArgumentException |
||
241 | */ |
||
242 | public function getWithNonStringKey() |
||
246 | |||
247 | /** |
||
248 | * @test |
||
249 | * @covers ::get |
||
250 | */ |
||
251 | public function getByFullQuery() |
||
263 | |||
264 | /** |
||
265 | * @test |
||
266 | * @covers ::get |
||
267 | */ |
||
268 | public function getBySubDocQuery() |
||
289 | |||
290 | /** |
||
291 | * @test |
||
292 | * @covers ::get |
||
293 | */ |
||
294 | public function getBeforeAck() |
||
307 | |||
308 | /** |
||
309 | * @test |
||
310 | * @covers ::get |
||
311 | */ |
||
312 | public function getWithCustomPriority() |
||
328 | |||
329 | /** |
||
330 | * @test |
||
331 | * @covers ::get |
||
332 | */ |
||
333 | public function getWithTimeBasedPriority() |
||
349 | |||
350 | /** |
||
351 | * @test |
||
352 | * @covers ::get |
||
353 | */ |
||
354 | public function getWait() |
||
365 | |||
366 | /** |
||
367 | * @test |
||
368 | * @covers ::get |
||
369 | */ |
||
370 | public function earliestGet() |
||
385 | |||
386 | /** |
||
387 | * @test |
||
388 | * @covers ::count |
||
389 | * @expectedException \InvalidArgumentException |
||
390 | */ |
||
391 | public function countWithNonStringKey() |
||
395 | |||
396 | /** |
||
397 | * @test |
||
398 | * @covers ::count |
||
399 | */ |
||
400 | public function testCount() |
||
418 | |||
419 | /** |
||
420 | * @test |
||
421 | * @covers ::ack |
||
422 | */ |
||
423 | public function ack() |
||
434 | |||
435 | /** |
||
436 | * @test |
||
437 | * @covers ::requeue |
||
438 | */ |
||
439 | public function requeue() |
||
461 | |||
462 | /** |
||
463 | * @test |
||
464 | * @covers ::send |
||
465 | */ |
||
466 | public function send() |
||
472 | |||
473 | /** |
||
474 | * @test |
||
475 | * @covers ::send |
||
476 | */ |
||
477 | public function sendWithHighEarliestGet() |
||
483 | |||
484 | /** |
||
485 | * @test |
||
486 | * @covers ::send |
||
487 | */ |
||
488 | public function sendWithLowEarliestGet() |
||
494 | |||
495 | private function assertSameMessage(Message $expected, Message $actual) |
||
501 | |||
502 | private function assertSingleMessage(Message $expected) |
||
516 | |||
517 | private function getMessage(array $payload = [], UTCDateTime $earliestGet = null, float $priority = 0.0) |
||
526 | |||
527 | private function sendAllMessages(array $messages) |
||
533 | } |
||
534 |