Completed
Push — master ( 5d9839...4760ab )
by
unknown
40:03
created

MongoQueueTest::testBulk()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 21
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 21
rs 9.3142
c 0
b 0
f 0
cc 2
eloc 12
nc 2
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
     * Test pushing bulk
158
     */
159
    public function testBulk()
160
    {
161
        $collection = uniqid('collection_');
162
        $jobName = uniqid('job_');
163
        $data = range(1, 10);
164
        $delay = rand(60, 3600);
0 ignored issues
show
Unused Code introduced by
$delay is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
165
166
        $database = new MockDatabase();
167
168
        $mongoQueue = $this->mockMongoQueue($database, $collection);
169
170
        for ($i = 0; $i < 10; ++$i) {
171
            $jobs[] = $jobName . $i;
0 ignored issues
show
Coding Style Comprehensibility introduced by
$jobs was never initialized. Although not strictly required by PHP, it is generally a good practice to add $jobs = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
172
        }
173
174
        $mongoQueue->bulk($jobs, $data);
0 ignored issues
show
Bug introduced by
The variable $jobs does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
175
176
        $count = $database->selectCollection($collection)->count();
177
178
        $this->assertEquals(10, $count);
179
    }
180
181
    public function testRelease()
182
    {
183
        //@TODO
184
    }
185
186
    /**
187
     * Mock mongo queue
188
     *
189
     * @param Database $database
190
     * @param string $collection
191
     *
192
     * @return MongoQueue
193
     */
194
    private function mockMongoQueue(Database $database, string $collection): MongoQueue
195
    {
196
        $jobResolver = $this->createMock(JobResolverInterface::class);
197
        $mongo = $this->createMock(MongoDriverInterface::class);
198
        $mongo
199
            ->expects($this->any())
200
            ->method('getDatabase')
201
            ->will($this->returnValue($database));
202
203
        $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...
204
205
        return $mongoQueue;
206
    }
207
}
208