|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +/** |
| 6 | + * SPDX-FileCopyrightText: 2026 Nextcloud GmbH and Nextcloud contributors |
| 7 | + * SPDX-License-Identifier: AGPL-3.0-or-later |
| 8 | + */ |
| 9 | + |
| 10 | +namespace OCA\PreviewGenerator\Tests\Unit\BackgroundJob; |
| 11 | + |
| 12 | +use OCA\PreviewGenerator\BackgroundJob\PreviewJob; |
| 13 | +use OCA\PreviewGenerator\Service\ConfigService; |
| 14 | +use OCA\PreviewGenerator\Service\PreGenerateService; |
| 15 | +use OCA\PreviewGenerator\Support\PreviewLimiter\MultiLimiter; |
| 16 | +use OCA\PreviewGenerator\Support\PreviewLimiter\PreviewLimiter; |
| 17 | +use OCP\AppFramework\Utility\ITimeFactory; |
| 18 | +use OCP\BackgroundJob\IJobList; |
| 19 | +use PHPUnit\Framework\MockObject\MockObject; |
| 20 | +use PHPUnit\Framework\TestCase; |
| 21 | +use Psr\Log\LoggerInterface; |
| 22 | + |
| 23 | +class PreviewJobTest extends TestCase { |
| 24 | + private PreviewJob $previewJob; |
| 25 | + |
| 26 | + private ITimeFactory&MockObject $time; |
| 27 | + private PreGenerateService&MockObject $preGenerateService; |
| 28 | + private LoggerInterface&MockObject $logger; |
| 29 | + private ConfigService&MockObject $configService; |
| 30 | + private IJobList&MockObject $jobList; |
| 31 | + |
| 32 | + protected function setUp(): void { |
| 33 | + parent::setUp(); |
| 34 | + |
| 35 | + $this->time = $this->createMock(ITimeFactory::class); |
| 36 | + $this->preGenerateService = $this->createMock(PreGenerateService::class); |
| 37 | + $this->logger = $this->createMock(LoggerInterface::class); |
| 38 | + $this->configService = $this->createMock(ConfigService::class); |
| 39 | + $this->jobList = $this->createMock(IJobList::class); |
| 40 | + |
| 41 | + $this->time->method('getTime') |
| 42 | + ->willReturnCallback(time(...)); |
| 43 | + |
| 44 | + $this->configService->expects(self::once()) |
| 45 | + ->method('getMaxBackgroundJobPreviews') |
| 46 | + ->willReturn(100); |
| 47 | + $this->configService->expects(self::once()) |
| 48 | + ->method('getMaxBackgroundJobExecutionTime') |
| 49 | + ->willReturn(300); |
| 50 | + |
| 51 | + $this->previewJob = new PreviewJob( |
| 52 | + $this->time, |
| 53 | + $this->preGenerateService, |
| 54 | + $this->logger, |
| 55 | + $this->configService, |
| 56 | + ); |
| 57 | + } |
| 58 | + |
| 59 | + public function testJobSettings(): void { |
| 60 | + $this->assertEquals(300, $this->previewJob->getInterval()); |
| 61 | + $this->assertTrue($this->previewJob->isTimeSensitive()); |
| 62 | + } |
| 63 | + |
| 64 | + public function testRun(): void { |
| 65 | + $this->configService->method('isBackgroundJobDisabled') |
| 66 | + ->willReturn(false); |
| 67 | + $this->configService->method('usesCronDaemon') |
| 68 | + ->willReturn(true); |
| 69 | + |
| 70 | + $this->preGenerateService->expects(self::once()) |
| 71 | + ->method('setLogger') |
| 72 | + ->with($this->logger); |
| 73 | + $this->preGenerateService->expects(self::once()) |
| 74 | + ->method('setLimiter') |
| 75 | + ->willReturnCallback(function (PreviewLimiter $limiter): void { |
| 76 | + $this->assertInstanceOf(MultiLimiter::class, $limiter); |
| 77 | + }); |
| 78 | + |
| 79 | + $this->preGenerateService->expects(self::once()) |
| 80 | + ->method('preGenerate'); |
| 81 | + |
| 82 | + $this->previewJob->start($this->jobList); |
| 83 | + } |
| 84 | + |
| 85 | + /** @dataProvider runSkipsDataProvider */ |
| 86 | + public function testRunSkips(bool $isBackgroundJobDisabled, bool $usesCronDaemon): void { |
| 87 | + $this->configService->method('isBackgroundJobDisabled') |
| 88 | + ->willReturn($isBackgroundJobDisabled); |
| 89 | + $this->configService->method('usesCronDaemon') |
| 90 | + ->willReturn($usesCronDaemon); |
| 91 | + |
| 92 | + $this->preGenerateService->expects(self::never()) |
| 93 | + ->method('setLogger'); |
| 94 | + $this->preGenerateService->expects(self::never()) |
| 95 | + ->method('setLimiter'); |
| 96 | + |
| 97 | + $this->preGenerateService->expects(self::never()) |
| 98 | + ->method('preGenerate'); |
| 99 | + |
| 100 | + $this->previewJob->start($this->jobList); |
| 101 | + } |
| 102 | + |
| 103 | + public static function runSkipsDataProvider(): array { |
| 104 | + return [ |
| 105 | + 'background job is disabled' => [ |
| 106 | + 'isBackgroundJobDisabled' => true, |
| 107 | + 'usesCronDaemon' => true, |
| 108 | + ], |
| 109 | + 'cron daemon is not used' => [ |
| 110 | + 'isBackgroundJobDisabled' => false, |
| 111 | + 'usesCronDaemon' => false, |
| 112 | + ], |
| 113 | + 'background job is disabled and no cron daemon is used' => [ |
| 114 | + 'isBackgroundJobDisabled' => true, |
| 115 | + 'usesCronDaemon' => false, |
| 116 | + ], |
| 117 | + ]; |
| 118 | + } |
| 119 | +} |
0 commit comments