Skip to content

Commit 7439ad5

Browse files
authored
Merge pull request #613 from nextcloud/test/housekeeping
test: update phpunit config and add some more tests
2 parents 82d54da + d64f84d commit 7439ad5

9 files changed

Lines changed: 183 additions & 24 deletions

File tree

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,6 @@
33
/vendor
44
/.php-cs-fixer.cache
55
/tests/.phpunit.result.cache
6+
/tests/coverage
67
/build
78
.DS_store

composer.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,15 @@
44
"optimize-autoloader": true,
55
"classmap-authoritative": true,
66
"platform": {
7-
"php": "7.4"
7+
"php": "8.1"
88
}
99
},
1010
"scripts": {
1111
"cs:fix": "php-cs-fixer fix",
1212
"cs:check": "php-cs-fixer fix --dry-run --diff",
1313
"lint": "find . -name \\*.php -not -path './vendor/*' -print0 | xargs -0 -n1 php -l",
14-
"test:unit": "phpunit -c tests/phpunit.xml tests"
14+
"test:unit": "phpunit -c tests/phpunit.xml tests",
15+
"test:unit:coverage": "XDEBUG_MODE=coverage phpunit -c tests/phpunit.xml tests"
1516
},
1617
"require-dev": {
1718
"nextcloud/coding-standard": "^1.0.0",

composer.lock

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
use OCA\PreviewGenerator\Service\ModuloService;
1313
use PHPUnit\Framework\TestCase;
1414

15-
class Test extends TestCase {
15+
class ModuloServiceTest extends TestCase {
1616
public static function moduloDataProvider(): array {
1717
return [
1818
[3, 10, 3],

tests/SizeHelperTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ class SizeHelperTest extends TestCase {
1919
private SizeHelper $sizeHelper;
2020

2121
private IConfig|MockObject $config;
22+
private ConfigService&MockObject $configService;
2223

2324
public function setUp(): void {
2425
parent::setUp();
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
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\Support\PreviewLimiter;
11+
12+
use OCA\PreviewGenerator\Support\PreviewLimiter\CountLimiter;
13+
use PHPUnit\Framework\TestCase;
14+
15+
class CountLimiterTest extends TestCase {
16+
public function testNext(): void {
17+
$limiter = new CountLimiter(3);
18+
19+
$this->assertTrue($limiter->next());
20+
$this->assertTrue($limiter->next());
21+
$this->assertTrue($limiter->next());
22+
$this->assertFalse($limiter->next());
23+
}
24+
25+
public function testNextWithZeroPreviews(): void {
26+
$limiter = new CountLimiter(0);
27+
28+
$this->assertFalse($limiter->next());
29+
}
30+
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
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\Support\PreviewLimiter;
11+
12+
use OC\AppFramework\Utility\TimeFactory;
13+
use OCA\PreviewGenerator\Support\PreviewLimiter\ExecutionTimeLimiter;
14+
use PHPUnit\Framework\MockObject\MockObject;
15+
use PHPUnit\Framework\TestCase;
16+
17+
class ExecutionTimeLimiterTest extends TestCase {
18+
private TimeFactory&MockObject $time;
19+
20+
protected function setUp(): void {
21+
parent::setUp();
22+
23+
$this->time = $this->createMock(TimeFactory::class);
24+
}
25+
26+
public function testNext(): void {
27+
$now = 1000;
28+
29+
$this->time->expects(self::exactly(4))
30+
->method('getTime')
31+
->willReturnCallback(static function () use (&$now) {
32+
return $now;
33+
});
34+
35+
$limiter = new ExecutionTimeLimiter($this->time, 10);
36+
37+
$this->assertTrue($limiter->next());
38+
39+
$now = 1010;
40+
$this->assertFalse($limiter->next());
41+
42+
$now = 1100;
43+
$this->assertFalse($limiter->next());
44+
}
45+
46+
public function testNextWithZeroExecutionTime(): void {
47+
$now = 1000;
48+
49+
$this->time->expects(self::exactly(3))
50+
->method('getTime')
51+
->willReturnCallback(static function () use (&$now) {
52+
return $now;
53+
});
54+
55+
$limiter = new ExecutionTimeLimiter($this->time, 0);
56+
57+
$this->assertFalse($limiter->next());
58+
59+
$now = 1100;
60+
$this->assertFalse($limiter->next());
61+
}
62+
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
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\Support\PreviewLimiter;
11+
12+
use OCA\PreviewGenerator\Support\PreviewLimiter\MultiLimiter;
13+
use OCA\PreviewGenerator\Support\PreviewLimiter\PreviewLimiter;
14+
use PHPUnit\Framework\TestCase;
15+
16+
class MultiLimiterTest extends TestCase {
17+
public function testNext(): void {
18+
$limiter1 = $this->createMock(PreviewLimiter::class);
19+
$limiter2 = $this->createMock(PreviewLimiter::class);
20+
21+
$limiter1Next = true;
22+
$limiter2Next = true;
23+
24+
$limiter1->expects(self::exactly(6))
25+
->method('next')
26+
->willReturnCallback(static function () use (&$limiter1Next) {
27+
return $limiter1Next;
28+
});
29+
$limiter2->expects(self::exactly(4))
30+
->method('next')
31+
->willReturnCallback(static function () use (&$limiter2Next) {
32+
return $limiter2Next;
33+
});
34+
35+
$limiter = new MultiLimiter([$limiter1, $limiter2]);
36+
37+
$this->assertTrue($limiter->next());
38+
$this->assertTrue($limiter->next());
39+
40+
$limiter1Next = false;
41+
$this->assertFalse($limiter->next());
42+
43+
$limiter1Next = true;
44+
$limiter2Next = false;
45+
$this->assertFalse($limiter->next());
46+
47+
$limiter1Next = false;
48+
$limiter2Next = false;
49+
$this->assertFalse($limiter->next());
50+
51+
$limiter1Next = true;
52+
$limiter2Next = true;
53+
$this->assertTrue($limiter->next());
54+
}
55+
56+
public function testNextWithoutLimiters(): void {
57+
$limiter = new MultiLimiter([]);
58+
59+
$this->assertTrue($limiter->next());
60+
$this->assertTrue($limiter->next());
61+
}
62+
}

tests/phpunit.xml

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,30 @@
1-
<?xml version="1.0" encoding="utf-8" ?>
1+
<?xml version="1.0" encoding="utf-8"?>
22
<!--
33
- SPDX-FileCopyrightText: 2016 Nextcloud GmbH and Nextcloud contributors
44
- SPDX-License-Identifier: AGPL-3.0-or-later
55
-->
6-
<phpunit bootstrap="bootstrap.php"
6+
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
7+
bootstrap="bootstrap.php"
78
verbose="true"
89
timeoutForSmallTests="900"
910
timeoutForMediumTests="900"
1011
timeoutForLargeTests="900"
12+
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.6/phpunit.xsd"
1113
>
12-
<testsuite name='Preview generator tests'>
13-
<directory suffix='Test.php'>.</directory>
14-
</testsuite>
15-
<!-- filters for code coverage -->
16-
<filter>
17-
<whitelist>
18-
<directory suffix=".php">../</directory>
19-
<exclude>
20-
<directory suffix=".php">../tests</directory>
21-
</exclude>
22-
</whitelist>
23-
</filter>
24-
<logging>
25-
<!-- and this is where your report will be written -->
26-
<log type="coverage-clover" target="./clover.xml"/>
27-
</logging>
14+
<coverage>
15+
<include>
16+
<directory suffix=".php">../</directory>
17+
</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>
23+
<report>
24+
<html outputDirectory="./coverage"/>
25+
</report>
26+
</coverage>
27+
<testsuite name="Preview generator tests">
28+
<directory suffix="Test.php">.</directory>
29+
</testsuite>
2830
</phpunit>

0 commit comments

Comments
 (0)