1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SilverStripe\StaticPublishQueue\Test; |
4
|
|
|
|
5
|
|
|
use ReflectionException; |
6
|
|
|
use ReflectionMethod; |
7
|
|
|
use SilverStripe\Core\Config\Config; |
8
|
|
|
use SilverStripe\Dev\SapphireTest; |
9
|
|
|
use SilverStripe\StaticPublishQueue\Job; |
10
|
|
|
use SilverStripe\StaticPublishQueue\Job\DeleteStaticCacheJob; |
11
|
|
|
use SilverStripe\StaticPublishQueue\Job\GenerateStaticCacheJob; |
12
|
|
|
use Symbiote\QueuedJobs\DataObjects\QueuedJobDescriptor; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Class JobTest |
16
|
|
|
* |
17
|
|
|
* @package SilverStripe\StaticPublishQueue\Test |
18
|
|
|
*/ |
19
|
|
|
class JobTest extends SapphireTest |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* @var bool |
23
|
|
|
*/ |
24
|
|
|
protected $usesDatabase = true; |
25
|
|
|
|
26
|
|
|
public function testJobsFromDataDefault() |
27
|
|
|
{ |
28
|
|
|
$urls = [ |
29
|
|
|
'http://some-locale/some-page/', |
30
|
|
|
'http://some-locale/some-other-page/', |
31
|
|
|
]; |
32
|
|
|
|
33
|
|
|
GenerateStaticCacheJob::singleton()->queueJobsFromData($urls); |
34
|
|
|
|
35
|
|
|
$jobs = QueuedJobDescriptor::get()->filter(['Implementation' => GenerateStaticCacheJob::class]); |
36
|
|
|
$this->assertCount(1, $jobs); |
37
|
|
|
|
38
|
|
|
/** @var QueuedJobDescriptor $jobDescriptor */ |
39
|
|
|
$jobDescriptor = $jobs->first(); |
40
|
|
|
$savedJobData = unserialize($jobDescriptor->SavedJobData); |
41
|
|
|
|
42
|
|
|
$this->assertEquals([ |
43
|
|
|
'http://some-locale/some-page/' => 0, |
44
|
|
|
'http://some-locale/some-other-page/' => 1, |
45
|
|
|
|
46
|
|
|
], $savedJobData->URLsToProcess); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @dataProvider jobClasses |
51
|
|
|
*/ |
52
|
|
|
public function testJobsFromDataJobClass($jobClass) |
53
|
|
|
{ |
54
|
|
|
$urls = [ |
55
|
|
|
'http://some-locale/some-page/', |
56
|
|
|
'http://some-locale/some-other-page/', |
57
|
|
|
]; |
58
|
|
|
|
59
|
|
|
GenerateStaticCacheJob::singleton()->queueJobsFromData($urls, '', null, $jobClass); |
60
|
|
|
|
61
|
|
|
$jobs = QueuedJobDescriptor::get()->filter(['Implementation' => $jobClass]); |
62
|
|
|
$this->assertCount(1, $jobs); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
public function testJobsFromDataMessage() |
66
|
|
|
{ |
67
|
|
|
$urls = [ |
68
|
|
|
'http://some-locale/some-page/', |
69
|
|
|
'http://some-locale/some-other-page/', |
70
|
|
|
]; |
71
|
|
|
|
72
|
|
|
$message = 'test message'; |
73
|
|
|
|
74
|
|
|
GenerateStaticCacheJob::singleton()->queueJobsFromData($urls, $message); |
75
|
|
|
|
76
|
|
|
$jobs = QueuedJobDescriptor::get()->filter(['Implementation' => GenerateStaticCacheJob::class]); |
77
|
|
|
$this->assertCount(1, $jobs); |
78
|
|
|
|
79
|
|
|
/** @var QueuedJobDescriptor $jobDescriptor */ |
80
|
|
|
$jobDescriptor = $jobs->first(); |
81
|
|
|
$savedJobMessages = unserialize($jobDescriptor->SavedJobMessages); |
82
|
|
|
$this->assertCount(1, $savedJobMessages); |
83
|
|
|
|
84
|
|
|
$messageData = array_shift($savedJobMessages); |
|
|
|
|
85
|
|
|
$this->assertContains($message, $messageData); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
public function testJobsFromDataExplicitUrlsPerJob() |
89
|
|
|
{ |
90
|
|
|
$urls = [ |
91
|
|
|
'http://some-locale/some-page/', |
92
|
|
|
'http://some-locale/some-other-page/', |
93
|
|
|
]; |
94
|
|
|
|
95
|
|
|
GenerateStaticCacheJob::singleton()->queueJobsFromData($urls, '', 1); |
96
|
|
|
|
97
|
|
|
$jobs = QueuedJobDescriptor::get()->filter(['Implementation' => GenerateStaticCacheJob::class]); |
98
|
|
|
$this->assertCount(2, $jobs); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
public function testJobsFromDataImplicitUrlsPerJob() |
102
|
|
|
{ |
103
|
|
|
Config::modify()->set(GenerateStaticCacheJob::class, 'urls_per_job', 1); |
104
|
|
|
$urls = [ |
105
|
|
|
'http://some-locale/some-page/', |
106
|
|
|
'http://some-locale/some-other-page/', |
107
|
|
|
]; |
108
|
|
|
|
109
|
|
|
GenerateStaticCacheJob::singleton()->queueJobsFromData($urls); |
110
|
|
|
|
111
|
|
|
$jobs = QueuedJobDescriptor::get()->filter(['Implementation' => GenerateStaticCacheJob::class]); |
112
|
|
|
$this->assertCount(2, $jobs); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
public function testJobsFromDataQueueCallback() |
116
|
|
|
{ |
117
|
|
|
$urls = [ |
118
|
|
|
'http://some-locale/some-page/', |
119
|
|
|
'http://some-locale/some-other-page/', |
120
|
|
|
]; |
121
|
|
|
|
122
|
|
|
$jobs = GenerateStaticCacheJob::singleton()->createJobsFromData($urls); |
123
|
|
|
|
124
|
|
|
$this->assertCount(1, $jobs); |
125
|
|
|
|
126
|
|
|
$job = array_shift($jobs); |
|
|
|
|
127
|
|
|
$this->assertInstanceOf(GenerateStaticCacheJob::class, $job); |
128
|
|
|
|
129
|
|
|
$this->assertCount( |
130
|
|
|
0, |
131
|
|
|
QueuedJobDescriptor::get()->filter(['Implementation' => GenerateStaticCacheJob::class]) |
132
|
|
|
); |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* @dataProvider urlsPerJobCases |
137
|
|
|
* @param $jobClass |
138
|
|
|
* @param $urlsPerJob |
139
|
|
|
* @throws ReflectionException |
140
|
|
|
*/ |
141
|
|
|
public function testUrlsPerJob($jobClass, $urlsPerJob) |
142
|
|
|
{ |
143
|
|
|
Config::modify()->set($jobClass, 'urls_per_job', $urlsPerJob); |
144
|
|
|
|
145
|
|
|
/** @var Job $job */ |
146
|
|
|
$job = singleton($jobClass); |
147
|
|
|
|
148
|
|
|
$method = new ReflectionMethod(Job::class, 'getUrlsPerJob'); |
149
|
|
|
$method->setAccessible(true); |
150
|
|
|
$this->assertEquals($urlsPerJob, $method->invoke($job)); |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* @dataProvider chunkCases |
155
|
|
|
* @param $jobClass |
156
|
|
|
* @param $chunkSize |
157
|
|
|
* @throws ReflectionException |
158
|
|
|
*/ |
159
|
|
|
public function testChunkSize($jobClass, $chunkSize) |
160
|
|
|
{ |
161
|
|
|
Config::modify()->set($jobClass, 'chunk_size', $chunkSize); |
162
|
|
|
|
163
|
|
|
/** @var Job $job */ |
164
|
|
|
$job = singleton($jobClass); |
165
|
|
|
|
166
|
|
|
$method = new ReflectionMethod(Job::class, 'getChunkSize'); |
167
|
|
|
$method->setAccessible(true); |
168
|
|
|
$this->assertEquals($chunkSize, $method->invoke($job)); |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
/** |
172
|
|
|
* @return array |
173
|
|
|
*/ |
174
|
|
|
public function jobClasses() |
175
|
|
|
{ |
176
|
|
|
return [ |
177
|
|
|
[GenerateStaticCacheJob::class], |
178
|
|
|
[DeleteStaticCacheJob::class], |
179
|
|
|
]; |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
/** |
183
|
|
|
* @return array |
184
|
|
|
*/ |
185
|
|
|
public function urlsPerJobCases() |
186
|
|
|
{ |
187
|
|
|
return [ |
188
|
|
|
[ |
189
|
|
|
GenerateStaticCacheJob::class, |
190
|
|
|
8, |
191
|
|
|
], |
192
|
|
|
[ |
193
|
|
|
DeleteStaticCacheJob::class, |
194
|
|
|
9, |
195
|
|
|
], |
196
|
|
|
]; |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
/** |
200
|
|
|
* @return array |
201
|
|
|
*/ |
202
|
|
|
public function chunkCases() |
203
|
|
|
{ |
204
|
|
|
return [ |
205
|
|
|
[ |
206
|
|
|
GenerateStaticCacheJob::class, |
207
|
|
|
10, |
208
|
|
|
], |
209
|
|
|
[ |
210
|
|
|
DeleteStaticCacheJob::class, |
211
|
|
|
15, |
212
|
|
|
], |
213
|
|
|
]; |
214
|
|
|
} |
215
|
|
|
} |
216
|
|
|
|