Completed
Push — master ( bf396f...5d9839 )
by
unknown
92:12 queued 52:15
created

MongoQueueTest::testLater()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 21
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 21
rs 9.3142
c 0
b 0
f 0
cc 1
eloc 13
nc 1
nop 0
1
<?php
2
3
namespace SfCod\QueueBundle\Tests\Queue;
4
5
use Helmich\MongoMock\MockDatabase;
6
use MongoDB\Database;
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
 *
15
 * @author Virchenko Maksim <[email protected]>
16
 *
17
 * @package SfCod\QueueBundle\Tests\Queue
18
 */
19
class MongoQueueTest extends TestCase
20
{
21
    /**
22
     * Test pushing into database
23
     */
24
    public function testPush()
25
    {
26
        $collection = uniqid('collection_');
27
        $jobName = uniqid('job_');
28
        $data = range(1, 10);
29
30
        $database = new MockDatabase();
31
32
        $mongoQueue = $this->mockMongoQueue($database, $collection);
33
34
        $mongoQueue->push($jobName, $data);
35
36
        $this->assertEquals(1, $database->selectCollection($collection)->count());
37
38
        $job = $database->selectCollection($collection)->findOne();
39
40
        $payload = json_decode($job->payload, true);
41
        $this->assertEquals($jobName, $payload['job']);
42
        $this->assertEquals($data, $payload['data']);
43
    }
44
45
    /**
46
     * Test pop from queue
47
     */
48
    public function testPop()
49
    {
50
        $collection = uniqid('collection_');
51
        $jobName = uniqid('job_');
52
        $data = range(1, 10);
53
54
        $database = new MockDatabase();
55
56
        $mongoQueue = $this->mockMongoQueue($database, $collection);
57
58
        $mongoQueue->push($jobName, $data);
59
60
        $job = $mongoQueue->pop();
61
        $this->assertEquals($jobName, $job->getName());
62
        $this->assertEquals($data, $job->payload()['data']);
63
    }
64
65
    /**
66
     * Test if job exists
67
     */
68
    public function testExists()
69
    {
70
        $collection = uniqid('collection_');
71
        $jobName = uniqid('job_');
72
        $data = range(1, 10);
73
74
        $database = new MockDatabase();
75
76
        $mongoQueue = $this->mockMongoQueue($database, $collection);
77
78
        $mongoQueue->push($jobName, $data);
79
80
        $this->assertTrue($mongoQueue->exists($jobName, $data));
81
    }
82
83
    /**
84
     * Test pushing into database
85
     */
86
    public function testPushOn()
87
    {
88
        $collection = uniqid('collection_');
89
        $jobName = uniqid('job_');
90
        $data = range(1, 10);
91
92
        $database = new MockDatabase();
93
94
        $mongoQueue = $this->mockMongoQueue($database, $collection);
95
96
        $mongoQueue->pushOn('default', $jobName, $data);
97
98
        $this->assertEquals(1, $database->selectCollection($collection)->count());
99
100
        $job = $database->selectCollection($collection)->findOne();
101
102
        $payload = json_decode($job->payload, true);
103
        $this->assertEquals($jobName, $payload['job']);
104
        $this->assertEquals($data, $payload['data']);
105
    }
106
107
    /**
108
     * Test pushing into database
109
     */
110
    public function testPushRaw()
111
    {
112
        $collection = uniqid('collection_');
113
        $jobName = uniqid('job_');
114
        $data = range(1, 10);
115
116
        $database = new MockDatabase();
117
118
        $mongoQueue = $this->mockMongoQueue($database, $collection);
119
120
        $mongoQueue->pushRaw(json_encode(['job' => $jobName, 'data' => $data]));
121
122
        $this->assertEquals(1, $database->selectCollection($collection)->count());
123
124
        $job = $database->selectCollection($collection)->findOne();
125
126
        $payload = json_decode($job->payload, true);
127
        $this->assertEquals($jobName, $payload['job']);
128
        $this->assertEquals($data, $payload['data']);
129
    }
130
131
    /**
132
     * Test pushing job for later
133
     */
134
    public function testLater()
135
    {
136
        $collection = uniqid('collection_');
137
        $jobName = uniqid('job_');
138
        $data = range(1, 10);
139
        $delay = rand(60, 3600);
140
141
        $database = new MockDatabase();
142
143
        $mongoQueue = $this->mockMongoQueue($database, $collection);
144
145
        $mongoQueue->later($delay, $jobName, $data);
146
147
        $job = $database->selectCollection($collection)->findOne();
148
149
        $payload = json_decode($job->payload, true);
150
        $this->assertEquals($jobName, $payload['job']);
151
        $this->assertEquals($data, $payload['data']);
152
153
        $this->assertGreaterThan(time() + $delay - 10, $job->available_at);
154
    }
155
156
    /**
157
     * Mock mongo queue
158
     *
159
     * @param Database $database
160
     * @param string $collection
161
     *
162
     * @return MongoQueue
163
     */
164
    private function mockMongoQueue(Database $database, string $collection): MongoQueue
165
    {
166
        $jobResolver = $this->createMock(JobResolverInterface::class);
167
        $mongo = $this->createMock(MongoDriverInterface::class);
168
        $mongo
169
            ->expects($this->any())
170
            ->method('getDatabase')
171
            ->will($this->returnValue($database));
172
173
        $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...
174
175
        return $mongoQueue;
176
    }
177
}
178