Passed
Pull Request — master (#132)
by
unknown
02:07
created

JobTest::chunkCases()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 5
nc 1
nop 0
dl 0
loc 10
rs 10
c 1
b 0
f 0
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);
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

55
        $messageData = array_shift(/** @scrutinizer ignore-type */ $savedJobMessages);
Loading history...
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