-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathQlessJobTest.php
272 lines (209 loc) · 7.77 KB
/
QlessJobTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
<?php
namespace LaravelQless\Tests\Job;
use Illuminate\Container\Container;
use Illuminate\Contracts\Container\BindingResolutionException;
use LaravelQless\Contracts\JobHandler;
use LaravelQless\Queue\QlessQueue;
use LaravelQless\Tests\Helpers\ModifierTrait;
use LaravelQless\Tests\Stub\JobStub;
use LaravelQless\Tests\Stub\JobWithOutFailedStub;
use Orchestra\Testbench\TestCase;
use LaravelQless\Job\QlessJob;
use Qless\Jobs\BaseJob;
use Qless\Jobs\JobData;
class QlessJobTest extends TestCase
{
public function testGetJobId(): void
{
$jobMock = $this->getJob();
$jobMock->method('getJid')
->willReturn('my-test-jid');
$job = (new QlessJob($this->getContainer(), $this->getQueue(), $this->getJobHandler(), $jobMock, ''));
self::assertEquals($job->getJobId(), 'my-test-jid');
}
public function testGetData(): void
{
$jobMock = $this->getJob();
$jobMock->method('getData')
->willReturn(new JobData(['key' => 'value']));
$job = (new QlessJob($this->getContainer(), $this->getQueue(), $this->getJobHandler(), $jobMock, ''));
self::assertEquals($job->getData(), ['key' => 'value']);
}
public function testPayload(): void
{
$payload = '{"key": "value"}';
$job = (new QlessJob($this->getContainer(), $this->getQueue(), $this->getJobHandler(), $this->getJob(), $payload));
self::assertEquals($job->payload(), ['key' => 'value']);
}
public function testFireSuccess(): void
{
self::markTestSkipped('Refactor class to set `failed` property');
}
public function testFireFailed(): void
{
self::markTestSkipped('Refactor class to set `failed` property');
}
public function testFireFailedWithNotExistentClass(): void
{
$jobMock = $this->getJob();
$jobMock->method('getData')
->willReturn(new JobData(['key' => 'value']));
$jobMock->method('__get')
->with('failed')
->willReturn(true);
$container = $this->getContainer();
$container->method('make')
->willThrowException(new BindingResolutionException());
$payload = json_encode(['job' => 'Foo@fire']);
$job = (new QlessJob($container, $this->getQueue(), $this->getJobHandler(), $jobMock, $payload));
$job->fire();
self::assertNull($job->getResolvedJob());
}
public function testFireFailedWithExistentClass(): void
{
$jobMock = $this->getJob();
$jobMock->method('getData')
->willReturn(new JobData(['key' => 'value']));
$jobMock->method('__get')
->with('failed')
->willReturn(true);
$container = $this->getContainer();
$container->method('make')->with(JobStub::class)->willReturn(new JobStub());
$payload = json_encode(['job' => JobStub::class . '@failed', 'data' => []]);
$job = (new QlessJob($container, $this->getQueue(), $this->getJobHandler(), $jobMock, $payload));
$job->fire();
self::assertNotNull($job->getResolvedJob());
}
public function testFireWithNonFailedClass(): void
{
$jobMock = $this->getJob();
$jobMock->method('getData')
->willReturn(new JobData(['key' => 'value']));
$jobMock->method('__get')
->with('failed')
->willReturn(true);
$container = $this->getContainer();
$container->expects(self::never())->method('make')->with(JobWithOutFailedStub::class)
->willReturn(new JobWithOutFailedStub());
$payload = json_encode(['job' => JobWithOutFailedStub::class . '@failed', 'data' => []]);
$job = (new QlessJob($container, $this->getQueue(), $this->getJobHandler(), $jobMock, $payload));
$job->fire();
self::assertNull($job->getResolvedJob());
}
public function testRelease(): void
{
$queue = $this->getQueue();
$queue->expects(self::once())
->method('later')
->willReturn('job-id');
$jobMock = $this->getJob();
$jobMock->method('getData')
->willReturn(new JobData(['key' => 'value']));
$job = (new QlessJob($this->getContainer(), $queue, $this->getJobHandler(), $jobMock, ''));
self::assertFalse($job->isReleased());
self::assertEquals($job->release(), 'job-id');
self::assertTrue($job->isReleased());
}
public function testDelete(): void
{
$job = $this->getJob();
$job->expects(self::once())
->method('cancel');
$job = (new QlessJob($this->getContainer(), $this->getQueue(), $this->getJobHandler(), $job, ''));
$job->delete();
}
public function testAttempts(): void
{
$job = $this->getJob();
$job->expects(self::once())
->method('getRemaining')
->willReturn(3);
$job = (new QlessJob($this->getContainer(), $this->getQueue(), $this->getJobHandler(), $job, ''));
self::assertEquals($job->attempts(), 3);
}
public function testMaxTries(): void
{
$job = $this->getJob();
$job->expects(self::once())
->method('getRetries')
->willReturn(10);
$job = (new QlessJob($this->getContainer(), $this->getQueue(), $this->getJobHandler(), $job, ''));
self::assertEquals($job->maxTries(), 10);
}
public function testTimeout(): void
{
$job = $this->getJob();
$job->expects(self::once())
->method('ttl')
->willReturn(123.0);
$job = (new QlessJob($this->getContainer(), $this->getQueue(), $this->getJobHandler(), $job, ''));
self::assertEquals($job->timeout(), 123);
}
/**
* @depends testTimeout
*/
public function testTimeoutAt(): void
{
self::markTestSkipped('It is needed to refactor the class');
}
public function testGetName(): void
{
$job = $this->getJob();
$job->expects(self::once())
->method('getKlass')
->willReturn('jobClass');
$job = (new QlessJob($this->getContainer(), $this->getQueue(), $this->getJobHandler(), $job, ''));
self::assertEquals($job->getName(), 'jobClass');
}
public function testGetQueue(): void
{
$job = $this->getJob();
$job->expects(self::once())
->method('getQueue')
->willReturn('jobName');
$job = (new QlessJob($this->getContainer(), $this->getQueue(), $this->getJobHandler(), $job, ''));
self::assertEquals($job->getQueue(), 'jobName');
}
public function testGetRawBody(): void
{
$payload = 'raw body';
$job = (new QlessJob($this->getContainer(), $this->getQueue(), $this->getJobHandler(), $this->getJob(), $payload));
self::assertEquals($job->getRawBody(), 'raw body');
}
/**
* @return Container|\PHPUnit\Framework\MockObject\MockObject
*/
private function getContainer()
{
return $this->getMockBuilder(Container::class)
->disableOriginalConstructor()
->getMock();
}
/**
* @return QlessQueue|\PHPUnit\Framework\MockObject\MockObject
*/
private function getQueue()
{
return $this->getMockBuilder(QlessQueue::class)
->disableOriginalConstructor()
->getMock();
}
/**
* @return JobHandler|\PHPUnit\Framework\MockObject\MockObject
*/
private function getJobHandler()
{
return $this->getMockBuilder(JobHandler::class)
->disableOriginalConstructor()
->getMock();
}
/**
* @return BaseJob|\PHPUnit\Framework\MockObject\MockObject
*/
private function getJob()
{
return $this->getMockBuilder(BaseJob::class)
->disableOriginalConstructor()
->getMock();
}
}