Completed
Push — master ( 31ce3a...bf396f )
by
unknown
59:39 queued 19:43
created

MongoQueueTest::testPop()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 16
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 16
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 10
nc 1
nop 0
1
<?php
2
3
namespace SfCod\QueueBundle\Tests\Queue;
4
5
use MongoDB\Database;
6
use Helmich\MongoMock\MockDatabase;
7
use PHPUnit\Framework\TestCase;
8
use SfCod\QueueBundle\Base\JobResolverInterface;
9
use SfCod\QueueBundle\Base\MongoDriverInterface;
10
use SfCod\QueueBundle\Queue\MongoQueue;
11
12
/**
13
 * Class MongoQueueTest
14
 * @author Virchenko Maksim <[email protected]>
15
 * @package SfCod\QueueBundle\Tests\Queue
16
 */
17
class MongoQueueTest extends TestCase
18
{
19
    /**
20
     * Test pushing into database
21
     */
22
    public function testPush()
23
    {
24
        $collection = uniqid('collection_');
25
        $jobName = uniqid('job_');
26
        $data = range(1, 10);
27
28
        $database = new MockDatabase();
29
30
        $mongoQueue = $this->mockMongoQueue($database, $collection);
31
32
        $mongoQueue->push($jobName, $data);
33
34
        $this->assertEquals(1, $database->selectCollection($collection)->count());
35
36
        $job = $database->selectCollection($collection)->findOne();
37
38
        $payload = json_decode($job->payload, true);
39
        $this->assertEquals($jobName, $payload['job']);
40
        $this->assertEquals($data, $payload['data']);
41
    }
42
43
    /**
44
     * Test pop from queue
45
     */
46
    public function testPop()
47
    {
48
        $collection = uniqid('collection_');
49
        $jobName = uniqid('job_');
50
        $data = range(1, 10);
51
52
        $database = new MockDatabase();
53
54
        $mongoQueue = $this->mockMongoQueue($database, $collection);
55
56
        $mongoQueue->push($jobName, $data);
57
58
        $job = $mongoQueue->pop();
59
        $this->assertEquals($jobName, $job->getName());
60
        $this->assertEquals($data, $job->payload()['data']);
61
    }
62
63
    /**
64
     * Test if job exists
65
     */
66
    public function testExists()
67
    {
68
        $collection = uniqid('collection_');
69
        $jobName = uniqid('job_');
70
        $data = range(1, 10);
71
72
        $database = new MockDatabase();
73
74
        $mongoQueue = $this->mockMongoQueue($database, $collection);
75
76
        $mongoQueue->push($jobName, $data);
77
78
        $this->assertTrue($mongoQueue->exists($jobName, $data));
79
    }
80
81
    public function testPushRaw()
82
    {
83
        // @TODO
84
    }
85
86
    /**
87
     * Mock mongo queue
88
     *
89
     * @param Database $database
90
     * @param string $collection
91
     *
92
     * @return MongoQueue
93
     */
94
    private function mockMongoQueue(Database $database, string $collection): MongoQueue
95
    {
96
        $jobResolver = $this->createMock(JobResolverInterface::class);
97
        $mongo = $this->createMock(MongoDriverInterface::class);
98
        $mongo
99
            ->expects($this->any())
100
            ->method('getDatabase')
101
            ->will($this->returnValue($database));
102
103
        $mongoQueue = new MongoQueue($jobResolver, $mongo, $collection);
0 ignored issues
show
Documentation introduced by
$jobResolver is of type object<PHPUnit\Framework\MockObject\MockObject>, but the function expects a object<SfCod\QueueBundle...e\JobResolverInterface>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
Documentation introduced by
$mongo is of type object<PHPUnit\Framework\MockObject\MockObject>, but the function expects a object<SfCod\QueueBundle...e\MongoDriverInterface>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
104
105
        return $mongoQueue;
106
    }
107
}