Completed
Pull Request — master (#60)
by Chad
01:27
created

QueueTest::constructWithNonStringUrl()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
namespace TraderInteractive\Mongo;
4
5
use MongoDB\BSON\UTCDateTime;
6
use PHPUnit\Framework\TestCase;
7
8
/**
9
 * @coversDefaultClass \TraderInteractive\Mongo\Queue
10
 * @covers ::<private>
11
 * @covers ::__construct
12
 */
13
final class QueueTest extends TestCase
14
{
15
    private $collection;
16
    private $mongoUrl;
17
    private $queue;
18
19
    public function setUp()
20
    {
21
        $this->mongoUrl = getenv('TESTING_MONGO_URL') ?: 'mongodb://localhost:27017';
22
        $mongo = new \MongoDB\Client(
23
            $this->mongoUrl,
24
            [],
25
            ['typeMap' => ['root' => 'array', 'document' => 'array', 'array' => 'array']]
26
        );
27
        $this->collection = $mongo->selectDatabase('testing')->selectCollection('messages');
28
        $this->collection->drop();
29
30
        $this->queue = new Queue($this->mongoUrl, 'testing', 'messages');
31
    }
32
33
    /**
34
     * @test
35
     * @covers ::__construct
36
     * @expectedException \InvalidArgumentException
37
     */
38
    public function constructWithNonStringUrl()
39
    {
40
        new Queue(1, 'testing', 'messages');
41
    }
42
43
    /**
44
     * @test
45
     * @covers ::ensureGetIndex
46
     */
47
    public function ensureGetIndex()
48
    {
49
        $this->queue->ensureGetIndex(['type' => 1], ['boo' => -1]);
50
        $this->queue->ensureGetIndex(['another.sub' => 1]);
51
52
        $indexes = iterator_to_array($this->collection->listIndexes());
53
        $this->assertSame(3, count($indexes));
54
55
        $expectedOne = [
56
            'earliestGet' => 1,
57
            'payload.type' => 1,
58
            'priority' => 1,
59
            'created' => 1,
60
            'payload.boo' => -1,
61
        ];
62
        $this->assertSame($expectedOne, $indexes[1]['key']);
63
64
        $expectedTwo = [
65
            'earliestGet' => 1,
66
            'payload.another.sub' => 1,
67
            'priority' => 1,
68
            'created' => 1,
69
        ];
70
        $this->assertSame($expectedTwo, $indexes[2]['key']);
71
    }
72
73
    /**
74
     * @test
75
     * @covers ::ensureGetIndex
76
     * @expectedException \Exception
77
     */
78
    public function ensureGetIndexWithTooLongCollectionName()
79
    {
80
        $collectionName = 'messages012345678901234567890123456789012345678901234567890123456789';
81
        $collectionName .= '012345678901234567890123456789012345678901234567890123456789';//128 chars
82
83
        $queue = new Queue($this->mongoUrl, 'testing', $collectionName);
84
        $queue->ensureGetIndex([]);
85
    }
86
87
    /**
88
     * @test
89
     * @covers ::ensureGetIndex
90
     * @expectedException \InvalidArgumentException
91
     */
92
    public function ensureGetIndexWithNonStringBeforeSortKey()
93
    {
94
        $this->queue->ensureGetIndex([0 => 1]);
95
    }
96
97
    /**
98
     * @test
99
     * @covers ::ensureGetIndex
100
     * @expectedException \InvalidArgumentException
101
     */
102
    public function ensureGetIndexWithNonStringAfterSortKey()
103
    {
104
        $this->queue->ensureGetIndex(['field' => 1], [0 => 1]);
105
    }
106
107
    /**
108
     * @test
109
     * @covers ::ensureGetIndex
110
     * @expectedException \InvalidArgumentException
111
     */
112
    public function ensureGetIndexWithBadBeforeSortValue()
113
    {
114
        $this->queue->ensureGetIndex(['field' => 'NotAnInt']);
115
    }
116
117
    /**
118
     * @test
119
     * @covers ::ensureGetIndex
120
     * @expectedException \InvalidArgumentException
121
     */
122
    public function ensureGetIndexWithBadAfterSortValue()
123
    {
124
        $this->queue->ensureGetIndex([], ['field' => 'NotAnInt']);
125
    }
126
127
    /**
128
     * Verifies the behaviour of the Queue when it cannot create an index after 5 attempts.
129
     *
130
     * @test
131
     * @covers ::ensureGetIndex
132
     * @expectedException \Exception
133
     * @expectedExceptionMessage couldnt create index after 5 attempts
134
     */
135
    public function ensureIndexCannotBeCreatedAfterFiveAttempts()
136
    {
137
        $mockCollection = $this->getMockBuilder('\MongoDB\Collection')->disableOriginalConstructor()->getMock();
138
139
        $mockCollection->method('listIndexes')->willReturn([]);
140
141
        $queue = new Queue($mockCollection);
142
        $queue->ensureCountIndex(['type' => 1], false);
143
    }
144
145
    /**
146
     * @test
147
     * @covers ::ensureCountIndex
148
     */
149
    public function ensureCountIndex()
150
    {
151
        $this->queue->ensureCountIndex(['type' => 1, 'boo' => -1], false);
152
        $this->queue->ensureCountIndex(['another.sub' => 1], true);
153
154
        $indexes = iterator_to_array($this->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()
169
    {
170
        $this->queue->ensureCountIndex(['type' => 1, 'boo' => -1], false);
171
        $this->queue->ensureCountIndex(['type' => 1], false);
172
173
        $indexes = iterator_to_array($this->collection->listIndexes());
174
        $this->assertSame(2, count($indexes));
175
176
        $expected = ['payload.type' => 1, 'payload.boo' => -1];
177
        $this->assertSame($expected, $indexes[1]['key']);
178
    }
179
180
    /**
181
     * @test
182
     * @covers ::ensureCountIndex
183
     * @expectedException \InvalidArgumentException
184
     */
185
    public function ensureCountIndexWithNonStringKey()
186
    {
187
        $this->queue->ensureCountIndex([0 => 1], false);
188
    }
189
190
    /**
191
     * @test
192
     * @covers ::ensureCountIndex
193
     * @expectedException \InvalidArgumentException
194
     */
195
    public function ensureCountIndexWithBadValue()
196
    {
197
        $this->queue->ensureCountIndex(['field' => 'NotAnInt'], false);
198
    }
199
200
    /**
201
     * @test
202
     * @covers ::get
203
     */
204
    public function getByBadQuery()
205
    {
206
        $this->queue->send(['key1' => 0, 'key2' => true]);
207
208
        $result = $this->queue->get(['key3' => 0], PHP_INT_MAX, 0);
209
        $this->assertNull($result);
210
211
        $this->assertSame(1, $this->collection->count());
212
    }
213
214
    /**
215
     * @test
216
     * @covers ::get
217
     */
218
    public function getWithNegativePollDuration()
219
    {
220
        $this->queue->send(['key1' => 0]);
221
        $this->assertNotNull($this->queue->get([], 0, 0, -1));
222
    }
223
224
    /**
225
     * @test
226
     * @covers ::get
227
     * @expectedException \InvalidArgumentException
228
     */
229
    public function getWithNonStringKey()
230
    {
231
        $this->queue->get([0 => 'a value'], 0);
232
    }
233
234
    /**
235
     * @test
236
     * @covers ::get
237
     */
238
    public function getByFullQuery()
239
    {
240
        $messageOne = ['id' => 'SHOULD BE REMOVED', 'key1' => 0, 'key2' => true];
241
242
        $this->queue->send($messageOne);
243
        $this->queue->send(['key' => 'value']);
244
245
        $result = $this->queue->get($messageOne, PHP_INT_MAX, 0);
246
247
        $this->assertNotSame($messageOne['id'], $result['id']);
248
249
        $messageOne['id'] = $result['id'];
250
        $this->assertSame($messageOne, $result);
251
    }
252
253
    /**
254
     * @test
255
     * @covers ::get
256
     */
257
    public function getBySubDocQuery()
258
    {
259
        $messageTwo = [
260
            'one' => [
261
                'two' => [
262
                    'three' => 5,
263
                    'notused' => 'notused',
264
                ],
265
                'notused' => 'notused',
266
            ],
267
            'notused' => 'notused',
268
        ];
269
270
        $this->queue->send(['key1' => 0, 'key2' => true]);
271
        $this->queue->send($messageTwo);
272
273
        $result = $this->queue->get(['one.two.three' => ['$gt' => 4]], PHP_INT_MAX, 0);
274
        $this->assertSame(['id' => $result['id']] + $messageTwo, $result);
275
    }
276
277
    /**
278
     * @test
279
     * @covers ::get
280
     */
281
    public function getBeforeAck()
282
    {
283
        $messageOne = ['key1' => 0, 'key2' => true];
284
285
        $this->queue->send($messageOne);
286
        $this->queue->send(['key' => 'value']);
287
288
        $this->queue->get($messageOne, PHP_INT_MAX, 0);
289
290
        //try get message we already have before ack
291
        $result = $this->queue->get($messageOne, PHP_INT_MAX, 0);
292
        $this->assertNull($result);
293
    }
294
295
    /**
296
     * @test
297
     * @covers ::get
298
     */
299
    public function getWithCustomPriority()
300
    {
301
        $messageOne = ['key' => 0];
302
        $messageTwo = ['key' => 1];
303
        $messageThree = ['key' => 2];
304
305
        $this->queue->send($messageOne, 0, 0.5);
306
        $this->queue->send($messageTwo, 0, 0.4);
307
        $this->queue->send($messageThree, 0, 0.3);
308
309
        $resultOne = $this->queue->get([], PHP_INT_MAX, 0);
310
        $resultTwo = $this->queue->get([], PHP_INT_MAX, 0);
311
        $resultThree = $this->queue->get([], PHP_INT_MAX, 0);
312
313
        $this->assertSame(['id' => $resultOne['id']] + $messageThree, $resultOne);
314
        $this->assertSame(['id' => $resultTwo['id']] + $messageTwo, $resultTwo);
315
        $this->assertSame(['id' => $resultThree['id']] + $messageOne, $resultThree);
316
    }
317
318
    /**
319
     * @test
320
     * @covers ::get
321
     */
322
    public function getWithTimeBasedPriority()
323
    {
324
        $messageOne = ['key' => 0];
325
        $messageTwo = ['key' => 1];
326
        $messageThree = ['key' => 2];
327
328
        $this->queue->send($messageOne);
329
        $this->queue->send($messageTwo);
330
        $this->queue->send($messageThree);
331
332
        $resultOne = $this->queue->get([], PHP_INT_MAX, 0);
333
        $resultTwo = $this->queue->get([], PHP_INT_MAX, 0);
334
        $resultThree = $this->queue->get([], PHP_INT_MAX, 0);
335
336
        $this->assertSame(['id' => $resultOne['id']] + $messageOne, $resultOne);
337
        $this->assertSame(['id' => $resultTwo['id']] + $messageTwo, $resultTwo);
338
        $this->assertSame(['id' => $resultThree['id']] + $messageThree, $resultThree);
339
    }
340
341
    /**
342
     * @test
343
     * @covers ::get
344
     */
345
    public function getWithTimeBasedPriorityWithOldTimestamp()
346
    {
347
        $messageOne = ['key' => 0];
348
        $messageTwo = ['key' => 1];
349
        $messageThree = ['key' => 2];
350
351
        $this->queue->send($messageOne);
352
        $this->queue->send($messageTwo);
353
        $this->queue->send($messageThree);
354
355
        $resultTwo = $this->queue->get([], PHP_INT_MAX, 0);
356
        //ensuring using old timestamp shouldn't affect normal time order of send()s
357
        $this->queue->requeue($resultTwo, 0, 0.0, false);
0 ignored issues
show
Bug introduced by
It seems like $resultTwo defined by $this->queue->get(array(), PHP_INT_MAX, 0) on line 355 can also be of type null; however, TraderInteractive\Mongo\AbstractQueue::requeue() does only seem to accept array, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
358
359
        $resultOne = $this->queue->get([], PHP_INT_MAX, 0);
360
        $resultTwo = $this->queue->get([], PHP_INT_MAX, 0);
361
        $resultThree = $this->queue->get([], PHP_INT_MAX, 0);
362
363
        $this->assertSame(['id' => $resultOne['id']] + $messageOne, $resultOne);
364
        $this->assertSame(['id' => $resultTwo['id']] + $messageTwo, $resultTwo);
365
        $this->assertSame(['id' => $resultThree['id']] + $messageThree, $resultThree);
366
    }
367
368
    /**
369
     * @test
370
     * @covers ::get
371
     */
372
    public function getWait()
373
    {
374
        $start = microtime(true);
375
376
        $this->queue->get([], PHP_INT_MAX, 200);
377
378
        $end = microtime(true);
379
380
        $this->assertTrue($end - $start >= 0.200);
381
        $this->assertTrue($end - $start < 0.300);
382
    }
383
384
    /**
385
     * @test
386
     * @covers ::get
387
     */
388
    public function earliestGet()
389
    {
390
         $messageOne = ['key1' => 0, 'key2' => true];
391
392
         $this->queue->send($messageOne, time() + 1);
393
394
         $this->assertNull($this->queue->get($messageOne, PHP_INT_MAX, 0));
395
396
         sleep(1);
397
398
         $this->assertNotNull($this->queue->get($messageOne, PHP_INT_MAX, 0));
399
    }
400
401
    /**
402
     * @test
403
     * @covers ::get
404
     */
405
    public function resetStuck()
406
    {
407
        $messageOne = ['key' => 0];
408
        $messageTwo = ['key' => 1];
409
410
        $this->queue->send($messageOne);
411
        $this->queue->send($messageTwo);
412
413
        //sets to running
414
        $this->collection->updateOne(
415
            ['payload.key' => 0],
416
            ['$set' => ['earliestGet' => new \MongoDB\BSON\UTCDateTime(time() * 1000)]]
417
        );
418
        $this->collection->updateOne(
419
            ['payload.key' => 1],
420
            ['$set' => ['earliestGet' => new \MongoDB\BSON\UTCDateTime(time() * 1000)]]
421
        );
422
423
        $this->assertSame(
424
            2,
425
            $this->collection->count(
426
                ['earliestGet' => ['$lte' => new \MongoDB\BSON\UTCDateTime((int)(microtime(true) * 1000))]]
427
            )
428
        );
429
430
        //resets and gets messageOne
431
        $this->assertNotNull($this->queue->get($messageOne, PHP_INT_MAX, 0));
432
433
        $this->assertSame(
434
            1,
435
            $this->collection->count(
436
                ['earliestGet' => ['$lte' => new \MongoDB\BSON\UTCDateTime((int)(microtime(true) * 1000))]]
437
            )
438
        );
439
    }
440
441
    /**
442
     * @test
443
     * @covers ::count
444
     * @expectedException \InvalidArgumentException
445
     */
446
    public function countWithNonStringKey()
447
    {
448
        $this->queue->count([0 => 'a value']);
449
    }
450
451
    /**
452
     * @test
453
     * @covers ::count
454
     */
455
    public function testCount()
456
    {
457
        $message = ['boo' => 'scary'];
458
459
        $this->assertSame(0, $this->queue->count($message, true));
460
        $this->assertSame(0, $this->queue->count($message, false));
461
        $this->assertSame(0, $this->queue->count($message));
462
463
        $this->queue->send($message);
464
        $this->assertSame(1, $this->queue->count($message, false));
465
        $this->assertSame(0, $this->queue->count($message, true));
466
        $this->assertSame(1, $this->queue->count($message));
467
468
        $this->queue->get($message, PHP_INT_MAX, 0);
469
        $this->assertSame(0, $this->queue->count($message, false));
470
        $this->assertSame(1, $this->queue->count($message, true));
471
        $this->assertSame(1, $this->queue->count($message));
472
    }
473
474
    /**
475
     * @test
476
     * @covers ::ack
477
     */
478
    public function ack()
479
    {
480
        $messageOne = ['key1' => 0, 'key2' => true];
481
482
        $this->queue->send($messageOne);
483
        $this->queue->send(['key' => 'value']);
484
485
        $result = $this->queue->get($messageOne, PHP_INT_MAX, 0);
486
        $this->assertSame(2, $this->collection->count());
487
488
        $this->queue->ack($result);
0 ignored issues
show
Bug introduced by
It seems like $result defined by $this->queue->get($messageOne, PHP_INT_MAX, 0) on line 485 can also be of type null; however, TraderInteractive\Mongo\AbstractQueue::ack() does only seem to accept array, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
489
        $this->assertSame(1, $this->collection->count());
490
    }
491
492
    /**
493
     * @test
494
     * @covers ::ack
495
     * @expectedException \InvalidArgumentException
496
     */
497
    public function ackBadArg()
498
    {
499
        $this->queue->ack(['id' => new \stdClass()]);
500
    }
501
502
    /**
503
     * @test
504
     * @covers ::ackSend
505
     */
506
    public function ackSend()
507
    {
508
        $messageOne = ['key1' => 0, 'key2' => true];
509
        $messageThree = ['hi' => 'there', 'rawr' => 2];
510
511
        $this->queue->send($messageOne);
512
        $this->queue->send(['key' => 'value']);
513
514
        $resultOne = $this->queue->get($messageOne, PHP_INT_MAX, 0);
515
        $this->assertSame(2, $this->collection->count());
516
517
        $this->queue->ackSend($resultOne, $messageThree);
0 ignored issues
show
Bug introduced by
It seems like $resultOne defined by $this->queue->get($messageOne, PHP_INT_MAX, 0) on line 514 can also be of type null; however, TraderInteractive\Mongo\AbstractQueue::ackSend() does only seem to accept array, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
518
        $this->assertSame(2, $this->collection->count());
519
520
        $actual = $this->queue->get(['hi' => 'there'], PHP_INT_MAX, 0);
521
        $expected = ['id' => $resultOne['id']] + $messageThree;
522
523
        $actual['id'] = $actual['id']->__toString();
524
        $expected['id'] = $expected['id']->__toString();
525
        $this->assertSame($expected, $actual);
526
    }
527
528
    /**
529
     * Verify earliestGet with ackSend.
530
     *
531
     * @test
532
     * @covers ::ackSend
533
     *
534
     * @return void
535
     */
536
    public function ackSendWithEarliestGet()
537
    {
538
        $message = ['key1' => 0, 'key2' => true];
539
        $this->queue->send($message);
540
        $result = $this->queue->get([], PHP_INT_MAX, 0);
541
        $this->assertSame($message['key1'], $result['key1']);
542
        $this->assertSame($message['key2'], $result['key2']);
543
        $this->queue->ackSend($result, ['key1' => 1, 'key2' => 2], strtotime('+ 1 day'));
544
        $actual = $this->queue->get([], PHP_INT_MAX, 0);
545
        $this->assertNull($actual);
546
    }
547
548
    /**
549
     * @test
550
     * @covers ::ackSend
551
     * @expectedException \InvalidArgumentException
552
     */
553
    public function ackSendWithWrongIdType()
554
    {
555
        $this->queue->ackSend(['id' => 5], []);
556
    }
557
558
    /**
559
     * @test
560
     * @covers ::ackSend
561
     * @expectedException \InvalidArgumentException
562
     */
563
    public function ackSendWithNanPriority()
564
    {
565
        $this->queue->ackSend(['id' => new \MongoDB\BSON\ObjectID()], [], 0, NAN);
566
    }
567
568
    /**
569
     * @test
570
     * @covers ::ackSend
571
     */
572
    public function ackSendWithHighEarliestGet()
573
    {
574
        $this->queue->send([]);
575
        $messageToAck = $this->queue->get([], PHP_INT_MAX, 0);
576
577
        $this->queue->ackSend($messageToAck, [], PHP_INT_MAX);
0 ignored issues
show
Bug introduced by
It seems like $messageToAck defined by $this->queue->get(array(), PHP_INT_MAX, 0) on line 575 can also be of type null; however, TraderInteractive\Mongo\AbstractQueue::ackSend() does only seem to accept array, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
578
579
        $this->assertSingleMessage(
580
            [
581
                'payload' => [],
582
                'earliestGet' => (new UTCDateTime(Queue::MONGO_INT32_MAX))->toDateTime()->getTimestamp(),
583
                'priority' => 0.0,
584
            ]
585
        );
586
    }
587
588
    /**
589
     * @covers ::ackSend
590
     */
591
    public function ackSendWithLowEarliestGet()
592
    {
593
        $this->queue->send([]);
594
        $messageToAck = $this->queue->get([], PHP_INT_MAX, 0);
595
596
        $this->queue->ackSend($messageToAck, [], -1);
0 ignored issues
show
Bug introduced by
It seems like $messageToAck defined by $this->queue->get(array(), PHP_INT_MAX, 0) on line 594 can also be of type null; however, TraderInteractive\Mongo\AbstractQueue::ackSend() does only seem to accept array, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
597
598
        $this->assertSingleMessage(
599
            [
600
                'payload' => [],
601
                'earliestGet' => 0,
602
                'priority' => 0.0,
603
            ]
604
        );
605
    }
606
607
    /**
608
     * @test
609
     * @covers ::requeue
610
     */
611
    public function requeue()
612
    {
613
        $messageOne = ['key1' => 0, 'key2' => true];
614
615
        $this->queue->send($messageOne);
616
        $this->queue->send(['key' => 'value']);
617
618
        $resultBeforeRequeue = $this->queue->get($messageOne, PHP_INT_MAX, 0);
619
620
        $this->queue->requeue($resultBeforeRequeue);
0 ignored issues
show
Bug introduced by
It seems like $resultBeforeRequeue defined by $this->queue->get($messageOne, PHP_INT_MAX, 0) on line 618 can also be of type null; however, TraderInteractive\Mongo\AbstractQueue::requeue() does only seem to accept array, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
621
        $this->assertSame(2, $this->collection->count());
622
623
        $resultAfterRequeue = $this->queue->get($messageOne, 0);
624
        $this->assertSame(['id' => $resultAfterRequeue['id']] + $messageOne, $resultAfterRequeue);
625
    }
626
627
    /**
628
     * @test
629
     * @covers ::requeue
630
     * @expectedException \InvalidArgumentException
631
     */
632
    public function requeueBadArg()
633
    {
634
        $this->queue->requeue(['id' => new \stdClass()]);
635
    }
636
637
    /**
638
     * @test
639
     * @covers ::send
640
     */
641 View Code Duplication
    public function send()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
642
    {
643
        $payload = ['key1' => 0, 'key2' => true];
644
        $this->queue->send($payload, 34, 0.8);
645
646
        $this->assertSingleMessage(
647
            [
648
                'payload' => $payload,
649
                'earliestGet' => 34,
650
                'priority' => 0.8,
651
            ]
652
        );
653
    }
654
655
    /**
656
     * @test
657
     * @covers ::send
658
     * @expectedException \InvalidArgumentException
659
     */
660
    public function sendWithNanPriority()
661
    {
662
        $this->queue->send([], 0, NAN);
663
    }
664
665
    /**
666
     * @test
667
     * @covers ::send
668
     */
669
    public function sendWithHighEarliestGet()
670
    {
671
        $this->queue->send([], PHP_INT_MAX);
672
673
        $this->assertSingleMessage(
674
            [
675
                'payload' => [],
676
                'earliestGet' => (new UTCDateTime(Queue::MONGO_INT32_MAX))->toDateTime()->getTimestamp(),
677
                'priority' => 0.0,
678
            ]
679
        );
680
    }
681
682
    /**
683
     * @test
684
     * @covers ::send
685
     */
686
    public function sendWithLowEarliestGet()
687
    {
688
        $this->queue->send([], -1);
689
690
        $this->assertSingleMessage(
691
            [
692
                'payload' => [],
693
                'earliestGet' => 0,
694
                'priority' => 0.0,
695
            ]
696
        );
697
    }
698
699
    /**
700
     * Verify Queue can be constructed with \MongoDB\Collection
701
     *
702
     * @test
703
     * @covers ::__construct
704
     *
705
     * @return void
706
     */
707 View Code Duplication
    public function constructWithCollection()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
708
    {
709
        $queue = new Queue($this->collection);
710
711
        $payload = ['key1' => 0, 'key2' => true];
712
        $queue->send($payload, 34, 0.8);
713
714
        $this->assertSingleMessage(
715
            [
716
                'payload' => $payload,
717
                'earliestGet' => 34,
718
                'priority' => 0.8,
719
            ]
720
        );
721
722
    }
723
724
    private function assertSingleMessage(array $expected)
725
    {
726
        $this->assertSame(1, $this->collection->count());
727
728
        $message = $this->collection->findOne();
729
730
        $this->assertLessThanOrEqual(time(), $message['created']->toDateTime()->getTimestamp());
731
        $this->assertGreaterThan(time() - 10, $message['created']->toDateTime()->getTimestamp());
732
733
        unset($message['_id'], $message['created']);
734
        $message['earliestGet'] = (int)$message['earliestGet']->toDateTime()->getTimestamp();
735
736
        $this->assertSame($expected, $message);
737
    }
738
}
739