1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SfCod\QueueBundle\Tests\JobContract; |
4
|
|
|
|
5
|
|
|
use Exception; |
6
|
|
|
use PHPUnit\Framework\TestCase; |
7
|
|
|
use SfCod\QueueBundle\Base\JobInterface; |
8
|
|
|
use SfCod\QueueBundle\Base\JobResolverInterface; |
9
|
|
|
use SfCod\QueueBundle\Entity\Job; |
10
|
|
|
use SfCod\QueueBundle\JobContract\JobContract; |
11
|
|
|
use SfCod\QueueBundle\Queue\QueueInterface; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Class JobContractTest |
15
|
|
|
* |
16
|
|
|
* @author Virchenko Maksim <[email protected]> |
17
|
|
|
* |
18
|
|
|
* @package SfCod\QueueBundle\Tests\JobContract |
19
|
|
|
*/ |
20
|
|
|
class JobContractTest extends TestCase |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* Test job |
24
|
|
|
*/ |
25
|
|
|
public function testJob() |
26
|
|
|
{ |
27
|
|
|
$job = $this->mockJob(); |
28
|
|
|
$contract = $this->mockJobContract($job); |
29
|
|
|
|
30
|
|
|
$this->assertEquals($job->getId(), $contract->getJobId()); |
31
|
|
|
$this->assertEquals($job->getAttempts(), $contract->attempts()); |
32
|
|
|
$this->assertEquals($job->isReserved(), $contract->reserved()); |
33
|
|
|
$this->assertEquals($job->getReservedAt(), $contract->reservedAt()); |
34
|
|
|
$this->assertEquals($job->getPayload(), $contract->payload()); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Test job payload |
39
|
|
|
*/ |
40
|
|
|
public function testJobPayload() |
41
|
|
|
{ |
42
|
|
|
$job = $this->mockJob(); |
43
|
|
|
$contract = $this->mockJobContract($job); |
44
|
|
|
|
45
|
|
|
$this->assertEquals($job->getPayload(), $contract->payload()); |
46
|
|
|
$this->assertEquals($job->getPayload()['job'], $contract->getName()); |
47
|
|
|
$this->assertEquals($job->getPayload()['data'], $contract->getData()); |
48
|
|
|
$this->assertEquals($job->getPayload()['maxTries'], $contract->maxTries()); |
49
|
|
|
$this->assertEquals($job->getPayload()['timeout'], $contract->timeout()); |
50
|
|
|
$this->assertEquals($job->getPayload()['timeoutAt'], $contract->timeoutAt()); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Test contract |
55
|
|
|
*/ |
56
|
|
|
public function testContract() |
57
|
|
|
{ |
58
|
|
|
$job = $this->mockJob(); |
59
|
|
|
$contract = $this->mockJobContract($job); |
60
|
|
|
|
61
|
|
|
$this->assertFalse($contract->isDeleted()); |
62
|
|
|
$this->assertFalse($contract->isDeletedOrReleased()); |
63
|
|
|
|
64
|
|
|
$contract->delete(); |
65
|
|
|
|
66
|
|
|
$this->assertTrue($contract->isDeleted()); |
67
|
|
|
$this->assertTrue($contract->isDeletedOrReleased()); |
68
|
|
|
|
69
|
|
|
$this->assertFalse($contract->isReleased()); |
70
|
|
|
|
71
|
|
|
$contract->release(); |
72
|
|
|
|
73
|
|
|
$this->assertTrue($contract->isReleased()); |
74
|
|
|
|
75
|
|
|
$this->assertFalse($contract->hasFailed()); |
76
|
|
|
|
77
|
|
|
$contract->markAsFailed(); |
78
|
|
|
|
79
|
|
|
$this->assertTrue($contract->hasFailed()); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Test contract main actions |
84
|
|
|
*/ |
85
|
|
|
public function testActions() |
86
|
|
|
{ |
87
|
|
|
$job = $this->mockJob(); |
88
|
|
|
|
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($job->getPayload()['data'])); |
101
|
|
|
$jobInstance |
102
|
|
|
->expects($this->once()) |
103
|
|
|
->method('failed') |
104
|
|
|
->with($this->equalTo($job->getPayload()['data']), $this->equalTo($exception)); |
105
|
|
|
|
106
|
|
|
$contract = $this->mockJobContract($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 Job |
121
|
|
|
*/ |
122
|
|
|
private function mockJob(): Job |
123
|
|
|
{ |
124
|
|
|
$job = new Job(); |
125
|
|
|
$job->setId(new \MongoDB\BSON\ObjectID()); |
126
|
|
|
$job->setQueue(uniqid('queue_')); |
127
|
|
|
$job->setAttempts(rand(1, 10)); |
128
|
|
|
$job->setReserved((bool)rand(0, 1)); |
129
|
|
|
$job->setReservedAt(time()); |
130
|
|
|
$job->setPayload([ |
131
|
|
|
'job' => uniqid('job_'), |
132
|
|
|
'data' => range(1, 10), |
133
|
|
|
'maxTries' => rand(1, 10), |
134
|
|
|
'timeout' => rand(1, 1000), |
135
|
|
|
'timeoutAt' => time() + rand(1, 1000), |
136
|
|
|
]); |
137
|
|
|
|
138
|
|
|
return $job; |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* Mock mongo job contract |
143
|
|
|
* |
144
|
|
|
* @param Job $job |
|
|
|
|
145
|
|
|
* @param JobInterface|null $jobInstance |
146
|
|
|
* |
147
|
|
|
* @return JobContract |
148
|
|
|
*/ |
149
|
|
|
private function mockJobContract(Job $jobData, ?JobInterface $jobInstance = null): JobContract |
150
|
|
|
{ |
151
|
|
|
$queueName = uniqid('queue_name_'); |
152
|
|
|
$queue = $this->createMock(QueueInterface::class); |
153
|
|
|
|
154
|
|
|
if (is_null($jobInstance)) { |
155
|
|
|
$jobInstance = $this->createMock(JobInterface::class); |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
$resolver = $this->createMock(JobResolverInterface::class); |
159
|
|
|
$resolver |
160
|
|
|
->expects($this->any()) |
161
|
|
|
->method('resolve') |
162
|
|
|
->with($this->equalTo($jobData->getPayload()['job'])) |
163
|
|
|
->will($this->returnValue($jobInstance)); |
164
|
|
|
|
165
|
|
|
$contract = $this->getMockBuilder(JobContract::class) |
|
|
|
|
166
|
|
|
->setConstructorArgs([$resolver, $queue, $jobData, $queueName]) |
167
|
|
|
->setMethods(null) |
168
|
|
|
->getMock(); |
169
|
|
|
|
170
|
|
|
return $contract; |
171
|
|
|
} |
172
|
|
|
} |
173
|
|
|
|
This method has been deprecated. The supplier of the class has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.