Skip to content

Commit b484aff

Browse files
authored
Merge pull request #630 from nextcloud/test/restructure-and-more-tests
Restructure unit tests and add some more tests
2 parents 4e4037e + d769d6f commit b484aff

File tree

14 files changed

+168
-37
lines changed

14 files changed

+168
-37
lines changed

.gitignore

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
# SPDX-FileCopyrightText: 2023 Nextcloud GmbH and Nextcloud contributors
22
# SPDX-License-Identifier: AGPL-3.0-or-later
33
/vendor
4-
/.php-cs-fixer.cache
5-
/tests/.phpunit.result.cache
4+
.php-cs-fixer.cache
5+
.phpunit.result.cache
66
/tests/coverage
77
/build
88
.DS_store

.nextcloudignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,4 @@ package-lock.json
2020
screenshots
2121
src
2222
vendor
23+
tests

composer.json

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,18 @@
77
"php": "8.1"
88
}
99
},
10+
"autoload": {
11+
"psr-4": {
12+
"OCA\\PreviewGenerator\\": "lib/",
13+
"OCA\\PreviewGenerator\\Tests\\": "tests/"
14+
}
15+
},
1016
"scripts": {
1117
"cs:fix": "php-cs-fixer fix",
1218
"cs:check": "php-cs-fixer fix --dry-run --diff",
1319
"lint": "find . -name \\*.php -not -path './vendor/*' -print0 | xargs -0 -n1 php -l",
14-
"test:unit": "phpunit -c tests/phpunit.xml tests",
15-
"test:unit:coverage": "XDEBUG_MODE=coverage phpunit -c tests/phpunit.xml tests"
20+
"test:unit": "phpunit -c phpunit.xml tests",
21+
"test:unit:coverage": "XDEBUG_MODE=coverage phpunit -c phpunit.xml tests"
1622
},
1723
"require-dev": {
1824
"nextcloud/coding-standard": "^1.0.0",

lib/BackgroundJob/PreviewJob.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,21 +11,21 @@
1111

1212
use OCA\PreviewGenerator\Service\ConfigService;
1313
use OCA\PreviewGenerator\Service\PreGenerateService;
14-
use OCA\PreviewGenerator\Support\OutputInterfaceLoggerAdapter;
1514
use OCA\PreviewGenerator\Support\PreviewLimiter\CountLimiter;
1615
use OCA\PreviewGenerator\Support\PreviewLimiter\ExecutionTimeLimiter;
1716
use OCA\PreviewGenerator\Support\PreviewLimiter\MultiLimiter;
1817
use OCA\PreviewGenerator\Support\PreviewLimiter\PreviewLimiter;
1918
use OCP\AppFramework\Utility\ITimeFactory;
2019
use OCP\BackgroundJob\TimedJob;
20+
use Psr\Log\LoggerInterface;
2121

2222
class PreviewJob extends TimedJob {
2323
private readonly PreviewLimiter $limiter;
2424

2525
public function __construct(
2626
ITimeFactory $time,
2727
private readonly PreGenerateService $preGenerateService,
28-
private readonly OutputInterfaceLoggerAdapter $outputInterface,
28+
private readonly LoggerInterface $logger,
2929
private readonly ConfigService $configService,
3030
) {
3131
parent::__construct($time);
@@ -53,7 +53,8 @@ protected function run($argument) {
5353
return;
5454
}
5555

56+
$this->preGenerateService->setLogger($this->logger);
5657
$this->preGenerateService->setLimiter($this->limiter);
57-
$this->preGenerateService->preGenerate($this->outputInterface);
58+
$this->preGenerateService->preGenerate();
5859
}
5960
}

tests/phpunit.xml renamed to phpunit.xml

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
- SPDX-License-Identifier: AGPL-3.0-or-later
55
-->
66
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
7-
bootstrap="bootstrap.php"
7+
bootstrap="./tests/Unit/bootstrap.php"
88
verbose="true"
99
timeoutForSmallTests="900"
1010
timeoutForMediumTests="900"
@@ -13,18 +13,13 @@
1313
>
1414
<coverage>
1515
<include>
16-
<directory suffix=".php">../</directory>
16+
<directory suffix=".php">./lib</directory>
1717
</include>
18-
<exclude>
19-
<directory suffix=".php">../tests</directory>
20-
<directory suffix=".php">../vendor</directory>
21-
<file>../.php-cs-fixer.dist.php</file>
22-
</exclude>
2318
<report>
24-
<html outputDirectory="./coverage"/>
19+
<html outputDirectory="./tests/coverage"/>
2520
</report>
2621
</coverage>
27-
<testsuite name="Preview generator tests">
28-
<directory suffix="Test.php">.</directory>
22+
<testsuite name="PreviewGenerator unit tests">
23+
<directory suffix="Test.php">./tests/Unit</directory>
2924
</testsuite>
3025
</phpunit>
Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
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+
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
* SPDX-License-Identifier: AGPL-3.0-or-later
88
*/
99

10-
namespace OCA\PreviewGenerator\Tests\Service;
10+
namespace OCA\PreviewGenerator\Tests\Unit\Service;
1111

1212
use OCA\PreviewGenerator\Service\ModuloService;
1313
use PHPUnit\Framework\TestCase;
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
* SPDX-License-Identifier: AGPL-3.0-or-later
88
*/
99

10-
namespace OCA\PreviewGenerator\Tests\Service;
10+
namespace OCA\PreviewGenerator\Tests\Unit\Service;
1111

1212
use OCA\PreviewGenerator\Service\NoMediaService;
1313
use OCP\Files\File;
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
* SPDX-License-Identifier: AGPL-3.0-or-later
88
*/
99

10-
namespace OCA\PreviewGenerator\Tests;
10+
namespace OCA\PreviewGenerator\Tests\Unit;
1111

1212
use OCA\PreviewGenerator\Service\ConfigService;
1313
use OCA\PreviewGenerator\SizeHelper;

tests/Support/PreviewLimiter/CountLimiterTest.php renamed to tests/Unit/Support/PreviewLimiter/CountLimiterTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
* SPDX-License-Identifier: AGPL-3.0-or-later
88
*/
99

10-
namespace OCA\PreviewGenerator\Tests\Support\PreviewLimiter;
10+
namespace OCA\PreviewGenerator\Tests\Unit\Support\PreviewLimiter;
1111

1212
use OCA\PreviewGenerator\Support\PreviewLimiter\CountLimiter;
1313
use PHPUnit\Framework\TestCase;

0 commit comments

Comments
 (0)