1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SilverStripe\StaticPublishQueue\Test; |
4
|
|
|
|
5
|
|
|
use ReflectionMethod; |
6
|
|
|
use SilverStripe\Core\Config\Config; |
7
|
|
|
use SilverStripe\Dev\SapphireTest; |
8
|
|
|
use SilverStripe\ORM\ValidationException; |
9
|
|
|
use SilverStripe\StaticPublishQueue\Job; |
10
|
|
|
use SilverStripe\StaticPublishQueue\Job\DeleteStaticCacheJob; |
11
|
|
|
use SilverStripe\StaticPublishQueue\Job\GenerateStaticCacheJob; |
12
|
|
|
use SilverStripe\StaticPublishQueue\Service\UrlBundleService; |
13
|
|
|
use Symbiote\QueuedJobs\DataObjects\QueuedJobDescriptor; |
14
|
|
|
|
15
|
|
|
class JobTest extends SapphireTest |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* @var bool |
19
|
|
|
*/ |
20
|
|
|
protected $usesDatabase = true; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @param string $jobClass |
24
|
|
|
* @throws ValidationException |
25
|
|
|
* @dataProvider jobClasses |
26
|
|
|
*/ |
27
|
|
|
public function testJobsFromDataDefault(string $jobClass): void |
28
|
|
|
{ |
29
|
|
|
$urls = [ |
30
|
|
|
'http://some-locale/some-page/', |
31
|
|
|
'http://some-locale/some-other-page/', |
32
|
|
|
]; |
33
|
|
|
$message = 'Test message'; |
34
|
|
|
|
35
|
|
|
$service = UrlBundleService::singleton(); |
36
|
|
|
$service->addUrls($urls); |
37
|
|
|
$service->queueJobsForUrls($jobClass, $message); |
38
|
|
|
|
39
|
|
|
$jobs = QueuedJobDescriptor::get()->filter(['Implementation' => $jobClass]); |
40
|
|
|
$this->assertCount(1, $jobs); |
41
|
|
|
|
42
|
|
|
/** @var QueuedJobDescriptor $jobDescriptor */ |
43
|
|
|
$jobDescriptor = $jobs->first(); |
44
|
|
|
$savedJobData = unserialize($jobDescriptor->SavedJobData); |
45
|
|
|
|
46
|
|
|
$this->assertEquals([ |
47
|
|
|
'http://some-locale/some-page/' => 0, |
48
|
|
|
'http://some-locale/some-other-page/' => 1, |
49
|
|
|
|
50
|
|
|
], $savedJobData->URLsToProcess); |
51
|
|
|
|
52
|
|
|
$savedJobMessages = unserialize($jobDescriptor->SavedJobMessages); |
53
|
|
|
$this->assertCount(1, $savedJobMessages); |
54
|
|
|
|
55
|
|
|
$messageData = array_shift($savedJobMessages); |
|
|
|
|
56
|
|
|
$this->assertContains($message, $messageData); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @param string $jobClass |
61
|
|
|
* @throws ValidationException |
62
|
|
|
* @dataProvider jobClasses |
63
|
|
|
*/ |
64
|
|
|
public function testJobsFromDataExplicitUrlsPerJob(string $jobClass): void |
65
|
|
|
{ |
66
|
|
|
Config::modify()->set($jobClass, 'urls_per_job', 1); |
67
|
|
|
$urls = [ |
68
|
|
|
'http://some-locale/some-page/', |
69
|
|
|
'http://some-locale/some-other-page/', |
70
|
|
|
]; |
71
|
|
|
|
72
|
|
|
$service = UrlBundleService::singleton(); |
73
|
|
|
$service->addUrls($urls); |
74
|
|
|
$service->queueJobsForUrls($jobClass); |
75
|
|
|
|
76
|
|
|
$jobs = QueuedJobDescriptor::get()->filter(['Implementation' => $jobClass]); |
77
|
|
|
$this->assertCount(2, $jobs); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* @param string $jobClass |
82
|
|
|
* @param int $urlsPerJob |
83
|
|
|
* @dataProvider urlsPerJobCases |
84
|
|
|
*/ |
85
|
|
|
public function testUrlsPerJob(string $jobClass, int $urlsPerJob): void |
86
|
|
|
{ |
87
|
|
|
Config::modify()->set($jobClass, 'urls_per_job', $urlsPerJob); |
88
|
|
|
|
89
|
|
|
/** @var Job $job */ |
90
|
|
|
$job = singleton($jobClass); |
91
|
|
|
|
92
|
|
|
$method = new ReflectionMethod(Job::class, 'getUrlsPerJob'); |
93
|
|
|
$method->setAccessible(true); |
94
|
|
|
$this->assertEquals($urlsPerJob, $method->invoke($job)); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* @param string $jobClass |
99
|
|
|
* @param int $chunkSize |
100
|
|
|
* @dataProvider chunkCases |
101
|
|
|
*/ |
102
|
|
|
public function testChunkSize(string $jobClass, int $chunkSize): void |
103
|
|
|
{ |
104
|
|
|
Config::modify()->set($jobClass, 'chunk_size', $chunkSize); |
105
|
|
|
|
106
|
|
|
/** @var Job $job */ |
107
|
|
|
$job = singleton($jobClass); |
108
|
|
|
|
109
|
|
|
$method = new ReflectionMethod(Job::class, 'getChunkSize'); |
110
|
|
|
$method->setAccessible(true); |
111
|
|
|
$this->assertEquals($chunkSize, $method->invoke($job)); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* @return array |
116
|
|
|
*/ |
117
|
|
|
public function jobClasses(): array |
118
|
|
|
{ |
119
|
|
|
return [ |
120
|
|
|
[GenerateStaticCacheJob::class], |
121
|
|
|
[DeleteStaticCacheJob::class], |
122
|
|
|
]; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* @return array |
127
|
|
|
*/ |
128
|
|
|
public function urlsPerJobCases(): array |
129
|
|
|
{ |
130
|
|
|
return [ |
131
|
|
|
[ |
132
|
|
|
GenerateStaticCacheJob::class, |
133
|
|
|
8, |
134
|
|
|
], |
135
|
|
|
[ |
136
|
|
|
DeleteStaticCacheJob::class, |
137
|
|
|
9, |
138
|
|
|
], |
139
|
|
|
]; |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* @return array |
144
|
|
|
*/ |
145
|
|
|
public function chunkCases(): array |
146
|
|
|
{ |
147
|
|
|
return [ |
148
|
|
|
[ |
149
|
|
|
GenerateStaticCacheJob::class, |
150
|
|
|
10, |
151
|
|
|
], |
152
|
|
|
[ |
153
|
|
|
DeleteStaticCacheJob::class, |
154
|
|
|
15, |
155
|
|
|
], |
156
|
|
|
]; |
157
|
|
|
} |
158
|
|
|
} |
159
|
|
|
|