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); |
|
|
|
|
165
|
|
|
|
166
|
|
|
$database = new MockDatabase(); |
167
|
|
|
|
168
|
|
|
$mongoQueue = $this->mockMongoQueue($database, $collection); |
169
|
|
|
|
170
|
|
|
for ($i = 0; $i < 10; ++$i) { |
171
|
|
|
$jobs[] = $jobName . $i; |
|
|
|
|
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
$mongoQueue->bulk($jobs, $data); |
|
|
|
|
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); |
|
|
|
|
204
|
|
|
|
205
|
|
|
return $mongoQueue; |
206
|
|
|
} |
207
|
|
|
} |
208
|
|
|
|
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
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.