Passed
Pull Request — master (#104)
by
unknown
02:52
created

JobTest::testJobsFromDataDefault()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 21
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 12
nc 1
nop 0
dl 0
loc 21
rs 9.8666
c 1
b 0
f 0
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(): void
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
     * @param string $jobClass
51
     * @dataProvider jobClasses
52
     */
53
    public function testJobsFromDataJobClass(string $jobClass): void
54
    {
55
        $urls = [
56
            'http://some-locale/some-page/',
57
            'http://some-locale/some-other-page/',
58
        ];
59
60
        GenerateStaticCacheJob::singleton()->queueJobsFromData($urls, '', null, $jobClass);
61
62
        $jobs = QueuedJobDescriptor::get()->filter(['Implementation' => $jobClass]);
63
        $this->assertCount(1, $jobs);
64
    }
65
66
    public function testJobsFromDataMessage(): void
67
    {
68
        $urls = [
69
            'http://some-locale/some-page/',
70
            'http://some-locale/some-other-page/',
71
        ];
72
73
        $message = 'test message';
74
75
        GenerateStaticCacheJob::singleton()->queueJobsFromData($urls, $message);
76
77
        $jobs = QueuedJobDescriptor::get()->filter(['Implementation' => GenerateStaticCacheJob::class]);
78
        $this->assertCount(1, $jobs);
79
80
        /** @var QueuedJobDescriptor $jobDescriptor */
81
        $jobDescriptor = $jobs->first();
82
        $savedJobMessages = unserialize($jobDescriptor->SavedJobMessages);
83
        $this->assertCount(1, $savedJobMessages);
84
85
        $messageData = array_shift($savedJobMessages);
0 ignored issues
show
Bug introduced by
It seems like $savedJobMessages can also be of type Countable and Traversable; however, parameter $array of array_shift() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

85
        $messageData = array_shift(/** @scrutinizer ignore-type */ $savedJobMessages);
Loading history...
86
        $this->assertContains($message, $messageData);
87
    }
88
89
    public function testJobsFromDataExplicitUrlsPerJob(): void
90
    {
91
        $urls = [
92
            'http://some-locale/some-page/',
93
            'http://some-locale/some-other-page/',
94
        ];
95
96
        GenerateStaticCacheJob::singleton()->queueJobsFromData($urls, '', 1);
97
98
        $jobs = QueuedJobDescriptor::get()->filter(['Implementation' => GenerateStaticCacheJob::class]);
99
        $this->assertCount(2, $jobs);
100
    }
101
102
    public function testJobsFromDataImplicitUrlsPerJob(): void
103
    {
104
        Config::modify()->set(GenerateStaticCacheJob::class, 'urls_per_job', 1);
105
        $urls = [
106
            'http://some-locale/some-page/',
107
            'http://some-locale/some-other-page/',
108
        ];
109
110
        GenerateStaticCacheJob::singleton()->queueJobsFromData($urls);
111
112
        $jobs = QueuedJobDescriptor::get()->filter(['Implementation' => GenerateStaticCacheJob::class]);
113
        $this->assertCount(2, $jobs);
114
    }
115
116
    public function testJobsFromDataQueueCallback(): void
117
    {
118
        $urls = [
119
            'http://some-locale/some-page/',
120
            'http://some-locale/some-other-page/',
121
        ];
122
123
        $jobs = GenerateStaticCacheJob::singleton()->createJobsFromData($urls);
124
125
        $this->assertCount(1, $jobs);
126
127
        $job = array_shift($jobs);
0 ignored issues
show
Bug introduced by
It seems like $jobs can also be of type Countable and Traversable; however, parameter $array of array_shift() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

127
        $job = array_shift(/** @scrutinizer ignore-type */ $jobs);
Loading history...
128
        $this->assertInstanceOf(GenerateStaticCacheJob::class, $job);
129
130
        $this->assertCount(
131
            0,
132
            QueuedJobDescriptor::get()->filter(['Implementation' => GenerateStaticCacheJob::class])
133
        );
134
    }
135
136
    /**
137
     * @param string $jobClass
138
     * @param int $urlsPerJob
139
     * @throws ReflectionException
140
     * @dataProvider urlsPerJobCases
141
     */
142
    public function testUrlsPerJob(string $jobClass, int $urlsPerJob): void
143
    {
144
        Config::modify()->set($jobClass, 'urls_per_job', $urlsPerJob);
145
146
        /** @var Job $job */
147
        $job = singleton($jobClass);
148
149
        $method = new ReflectionMethod(Job::class, 'getUrlsPerJob');
150
        $method->setAccessible(true);
151
        $this->assertEquals($urlsPerJob, $method->invoke($job));
152
    }
153
154
    /**
155
     * @param string $jobClass
156
     * @param int $chunkSize
157
     * @throws ReflectionException
158
     * @dataProvider chunkCases
159
     */
160
    public function testChunkSize(string $jobClass, int $chunkSize): void
161
    {
162
        Config::modify()->set($jobClass, 'chunk_size', $chunkSize);
163
164
        /** @var Job $job */
165
        $job = singleton($jobClass);
166
167
        $method = new ReflectionMethod(Job::class, 'getChunkSize');
168
        $method->setAccessible(true);
169
        $this->assertEquals($chunkSize, $method->invoke($job));
170
    }
171
172
    /**
173
     * @return array
174
     */
175
    public function jobClasses(): array
176
    {
177
        return [
178
            [GenerateStaticCacheJob::class],
179
            [DeleteStaticCacheJob::class],
180
        ];
181
    }
182
183
    /**
184
     * @return array
185
     */
186
    public function urlsPerJobCases(): array
187
    {
188
        return [
189
            [
190
                GenerateStaticCacheJob::class,
191
                8,
192
            ],
193
            [
194
                DeleteStaticCacheJob::class,
195
                9,
196
            ],
197
        ];
198
    }
199
200
    /**
201
     * @return array
202
     */
203
    public function chunkCases(): array
204
    {
205
        return [
206
            [
207
                GenerateStaticCacheJob::class,
208
                10,
209
            ],
210
            [
211
                DeleteStaticCacheJob::class,
212
                15,
213
            ],
214
        ];
215
    }
216
}
217