1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SfCod\QueueBundle\Tests\Job; |
4
|
|
|
|
5
|
|
|
use PHPUnit\Framework\TestCase; |
6
|
|
|
use SfCod\QueueBundle\Base\JobInterface; |
7
|
|
|
use SfCod\QueueBundle\Base\JobResolverInterface; |
8
|
|
|
use SfCod\QueueBundle\Job\MongoJobContract; |
9
|
|
|
use SfCod\QueueBundle\Queue\QueueInterface; |
10
|
|
|
use Exception; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Class MongoJobContractTest |
14
|
|
|
* @author Virchenko Maksim <[email protected]> |
15
|
|
|
* @package SfCod\QueueBundle\Tests\Job |
16
|
|
|
*/ |
17
|
|
|
class MongoJobContractTest extends TestCase |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* Test job |
21
|
|
|
*/ |
22
|
|
|
public function testJob() |
23
|
|
|
{ |
24
|
|
|
$job = $this->mockJob(); |
25
|
|
|
$contract = $this->mockMongoJobContract($job); |
26
|
|
|
|
27
|
|
|
$this->assertEquals($job->_id, $contract->getJobId()); |
28
|
|
|
$this->assertEquals($job->attempts, $contract->attempts()); |
29
|
|
|
$this->assertEquals($job->reserved, $contract->reserved()); |
30
|
|
|
$this->assertEquals($job->reserved_at, $contract->reservedAt()); |
31
|
|
|
$this->assertEquals($job->payload, $contract->getRawBody()); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Test job payload |
36
|
|
|
*/ |
37
|
|
|
public function testJobPayload() |
38
|
|
|
{ |
39
|
|
|
$job = $this->mockJob(); |
40
|
|
|
$contract = $this->mockMongoJobContract($job); |
41
|
|
|
|
42
|
|
|
$payload = json_decode($job->payload, true); |
43
|
|
|
|
44
|
|
|
$this->assertEquals($payload, $contract->payload()); |
45
|
|
|
$this->assertEquals($payload['job'], $contract->getName()); |
46
|
|
|
$this->assertEquals($payload['data'], $contract->getData()); |
47
|
|
|
$this->assertEquals($payload['maxTries'], $contract->maxTries()); |
48
|
|
|
$this->assertEquals($payload['timeout'], $contract->timeout()); |
49
|
|
|
$this->assertEquals($payload['timeoutAt'], $contract->timeoutAt()); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Test contract |
54
|
|
|
*/ |
55
|
|
|
public function testContract() |
56
|
|
|
{ |
57
|
|
|
$job = $this->mockJob(); |
58
|
|
|
$contract = $this->mockMongoJobContract($job); |
59
|
|
|
|
60
|
|
|
$this->assertFalse($contract->isDeleted()); |
61
|
|
|
$this->assertFalse($contract->isDeletedOrReleased()); |
62
|
|
|
|
63
|
|
|
$contract->delete(); |
64
|
|
|
|
65
|
|
|
$this->assertTrue($contract->isDeleted()); |
66
|
|
|
$this->assertTrue($contract->isDeletedOrReleased()); |
67
|
|
|
|
68
|
|
|
$this->assertFalse($contract->isReleased()); |
69
|
|
|
|
70
|
|
|
$contract->release(); |
71
|
|
|
|
72
|
|
|
$this->assertTrue($contract->isReleased()); |
73
|
|
|
|
74
|
|
|
$this->assertFalse($contract->hasFailed()); |
75
|
|
|
|
76
|
|
|
$contract->markAsFailed(); |
77
|
|
|
|
78
|
|
|
$this->assertTrue($contract->hasFailed()); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Test contract main actions |
83
|
|
|
*/ |
84
|
|
|
public function testActions() |
85
|
|
|
{ |
86
|
|
|
$job = $this->mockJob(); |
87
|
|
|
|
88
|
|
|
$payload = json_decode($job->payload, true); |
89
|
|
|
$exception = new Exception(uniqid('message_')); |
90
|
|
|
|
91
|
|
|
$jobInstance = $this->getMockBuilder(JobInterface::class) |
92
|
|
|
->setMethods([ |
93
|
|
|
'fire', |
94
|
|
|
'failed', |
95
|
|
|
]) |
96
|
|
|
->getMock(); |
97
|
|
|
$jobInstance |
98
|
|
|
->expects($this->once()) |
99
|
|
|
->method('fire') |
100
|
|
|
->with($this->anything(), $this->equalTo($payload['data'])); |
101
|
|
|
$jobInstance |
102
|
|
|
->expects($this->once()) |
103
|
|
|
->method('failed') |
104
|
|
|
->with($this->equalTo($payload['data']), $this->equalTo($exception)); |
105
|
|
|
|
106
|
|
|
$contract = $this->mockMongoJobContract($job, $jobInstance); |
|
|
|
|
107
|
|
|
|
108
|
|
|
$contract->fire(); |
109
|
|
|
|
110
|
|
|
$this->assertFalse($contract->hasFailed()); |
111
|
|
|
|
112
|
|
|
$contract->failed($exception); |
113
|
|
|
|
114
|
|
|
$this->assertTrue($contract->hasFailed()); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* Mock job |
119
|
|
|
* |
120
|
|
|
* @return object |
121
|
|
|
*/ |
122
|
|
|
private function mockJob() |
123
|
|
|
{ |
124
|
|
|
$job = [ |
125
|
|
|
'_id' => new \MongoDB\BSON\ObjectID(), |
126
|
|
|
'attempts' => rand(1, 10), |
127
|
|
|
'reserved' => (bool)rand(0, 1), |
128
|
|
|
'reserved_at' => time(), |
129
|
|
|
'payload' => json_encode([ |
130
|
|
|
'job' => uniqid('job_'), |
131
|
|
|
'data' => range(1, 10), |
132
|
|
|
'maxTries' => rand(1, 10), |
133
|
|
|
'timeout' => rand(1, 1000), |
134
|
|
|
'timeoutAt' => time() + rand(1, 1000), |
135
|
|
|
]), |
136
|
|
|
]; |
137
|
|
|
|
138
|
|
|
return (object)$job; |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* Mock mongo job contract |
143
|
|
|
* |
144
|
|
|
* @param $job |
145
|
|
|
* @param JobInterface|null $jobInstance |
146
|
|
|
* |
147
|
|
|
* @return MongoJobContract |
148
|
|
|
*/ |
149
|
|
|
private function mockMongoJobContract($jobData, ?JobInterface $jobInstance = null): MongoJobContract |
150
|
|
|
{ |
151
|
|
|
$queueName = uniqid('queue_name_'); |
152
|
|
|
$payload = json_decode($jobData->payload, true); |
153
|
|
|
|
154
|
|
|
$queue = $this->createMock(QueueInterface::class); |
155
|
|
|
|
156
|
|
|
if (is_null($jobInstance)) { |
157
|
|
|
$jobInstance = $this->createMock(JobInterface::class); |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
$resolver = $this->createMock(JobResolverInterface::class); |
161
|
|
|
$resolver |
162
|
|
|
->expects($this->any()) |
163
|
|
|
->method('resolve') |
164
|
|
|
->with($this->equalTo($payload['job'])) |
165
|
|
|
->will($this->returnValue($jobInstance)); |
166
|
|
|
|
167
|
|
|
$contract = $this->getMockBuilder(MongoJobContract::class) |
168
|
|
|
->setConstructorArgs([$resolver, $queue, $jobData, $queueName]) |
169
|
|
|
->setMethods(null) |
170
|
|
|
->getMock(); |
171
|
|
|
|
172
|
|
|
return $contract; |
173
|
|
|
} |
174
|
|
|
} |
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: