silverstripe /
silverstripe-staticpublishqueue
| 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\StaticPublishQueue\Job; |
||
| 9 | use SilverStripe\StaticPublishQueue\Job\DeleteStaticCacheJob; |
||
| 10 | use SilverStripe\StaticPublishQueue\Job\GenerateStaticCacheJob; |
||
| 11 | use SilverStripe\StaticPublishQueue\Service\UrlBundleService; |
||
| 12 | |||
| 13 | class UrlBundleServiceTest extends SapphireTest |
||
| 14 | { |
||
| 15 | /** |
||
| 16 | * @param string $jobClass |
||
| 17 | * @dataProvider jobClasses |
||
| 18 | */ |
||
| 19 | public function testJobsFromDataDefault(string $jobClass): void |
||
| 20 | { |
||
| 21 | $urls = [ |
||
| 22 | 'http://some-locale/some-page/', |
||
| 23 | 'http://some-locale/some-other-page/', |
||
| 24 | ]; |
||
| 25 | $message = 'Test message'; |
||
| 26 | |||
| 27 | $service = UrlBundleService::create(); |
||
| 28 | $service->addUrls($urls); |
||
| 29 | $jobs = $service->getJobsForUrls($jobClass, $message); |
||
| 30 | |||
| 31 | $this->assertCount(1, $jobs); |
||
| 32 | |||
| 33 | /** @var Job $job */ |
||
| 34 | $job = array_shift($jobs); |
||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
| 35 | |||
| 36 | $this->assertEquals([ |
||
| 37 | 'http://some-locale/some-page/' => 0, |
||
| 38 | 'http://some-locale/some-other-page/' => 1, |
||
| 39 | |||
| 40 | ], $job->URLsToProcess); |
||
| 41 | |||
| 42 | $messages = $job->getJobData()->messages; |
||
| 43 | $this->assertCount(1, $messages); |
||
| 44 | |||
| 45 | $messageData = array_shift($messages); |
||
| 46 | $this->assertContains($message, $messageData); |
||
| 47 | } |
||
| 48 | |||
| 49 | /** |
||
| 50 | * @param string $jobClass |
||
| 51 | * @dataProvider jobClasses |
||
| 52 | */ |
||
| 53 | public function testJobsFromDataExplicitUrlsPerJob(string $jobClass): void |
||
| 54 | { |
||
| 55 | Config::modify()->set($jobClass, 'urls_per_job', 1); |
||
| 56 | $urls = [ |
||
| 57 | 'http://some-locale/some-page/', |
||
| 58 | 'http://some-locale/some-other-page/', |
||
| 59 | ]; |
||
| 60 | |||
| 61 | $service = UrlBundleService::singleton(); |
||
| 62 | $service->addUrls($urls); |
||
| 63 | $jobs = $service->getJobsForUrls($jobClass); |
||
| 64 | |||
| 65 | $this->assertCount(2, $jobs); |
||
| 66 | } |
||
| 67 | |||
| 68 | /** |
||
| 69 | * @param string $jobClass |
||
| 70 | * @param int $urlsPerJob |
||
| 71 | * @dataProvider urlsPerJobCases |
||
| 72 | */ |
||
| 73 | public function testUrlsPerJob(string $jobClass, int $urlsPerJob): void |
||
| 74 | { |
||
| 75 | Config::modify()->set($jobClass, 'urls_per_job', $urlsPerJob); |
||
| 76 | |||
| 77 | /** @var Job $job */ |
||
| 78 | $job = singleton($jobClass); |
||
| 79 | |||
| 80 | $method = new ReflectionMethod(Job::class, 'getUrlsPerJob'); |
||
| 81 | $method->setAccessible(true); |
||
| 82 | $this->assertEquals($urlsPerJob, $method->invoke($job)); |
||
| 83 | } |
||
| 84 | |||
| 85 | /** |
||
| 86 | * @param string $jobClass |
||
| 87 | * @param int $chunkSize |
||
| 88 | * @dataProvider chunkCases |
||
| 89 | */ |
||
| 90 | public function testChunkSize(string $jobClass, int $chunkSize): void |
||
| 91 | { |
||
| 92 | Config::modify()->set($jobClass, 'chunk_size', $chunkSize); |
||
| 93 | |||
| 94 | /** @var Job $job */ |
||
| 95 | $job = singleton($jobClass); |
||
| 96 | |||
| 97 | $method = new ReflectionMethod(Job::class, 'getChunkSize'); |
||
| 98 | $method->setAccessible(true); |
||
| 99 | $this->assertEquals($chunkSize, $method->invoke($job)); |
||
| 100 | } |
||
| 101 | |||
| 102 | /** |
||
| 103 | * @return array |
||
| 104 | */ |
||
| 105 | public function jobClasses(): array |
||
| 106 | { |
||
| 107 | return [ |
||
| 108 | [GenerateStaticCacheJob::class], |
||
| 109 | [DeleteStaticCacheJob::class], |
||
| 110 | ]; |
||
| 111 | } |
||
| 112 | |||
| 113 | /** |
||
| 114 | * @return array |
||
| 115 | */ |
||
| 116 | public function urlsPerJobCases(): array |
||
| 117 | { |
||
| 118 | return [ |
||
| 119 | [ |
||
| 120 | GenerateStaticCacheJob::class, |
||
| 121 | 8, |
||
| 122 | ], |
||
| 123 | [ |
||
| 124 | DeleteStaticCacheJob::class, |
||
| 125 | 9, |
||
| 126 | ], |
||
| 127 | ]; |
||
| 128 | } |
||
| 129 | |||
| 130 | /** |
||
| 131 | * @return array |
||
| 132 | */ |
||
| 133 | public function chunkCases(): array |
||
| 134 | { |
||
| 135 | return [ |
||
| 136 | [ |
||
| 137 | GenerateStaticCacheJob::class, |
||
| 138 | 10, |
||
| 139 | ], |
||
| 140 | [ |
||
| 141 | DeleteStaticCacheJob::class, |
||
| 142 | 15, |
||
| 143 | ], |
||
| 144 | ]; |
||
| 145 | } |
||
| 146 | } |
||
| 147 |