Skip to content

Commit 6bb2fc6

Browse files
committed
test: Migrate files_versions to PHPUnit 10
Signed-off-by: Joas Schilling <[email protected]>
1 parent 68b2a62 commit 6bb2fc6

File tree

8 files changed

+63
-103
lines changed

8 files changed

+63
-103
lines changed

apps/files_versions/tests/BackgroundJob/ExpireVersionsTest.php

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
<?php
2+
3+
declare(strict_types=1);
24
/**
35
* SPDX-FileCopyrightText: 2021 Nextcloud GmbH and Nextcloud contributors
46
* SPDX-License-Identifier: AGPL-3.0-or-later
@@ -16,17 +18,10 @@
1618
use Test\TestCase;
1719

1820
class ExpireVersionsTest extends TestCase {
19-
/** @var IConfig|MockObject */
20-
private $config;
21-
22-
/** @var IUserManager|MockObject */
23-
private $userManager;
24-
25-
/** @var Expiration|MockObject */
26-
private $expiration;
27-
28-
/** @var IJobList|MockObject */
29-
private $jobList;
21+
private IConfig&MockObject $config;
22+
private IUserManager&MockObject $userManager;
23+
private Expiration&MockObject $expiration;
24+
private IJobList&MockObject $jobList;
3025

3126
protected function setUp(): void {
3227
parent::setUp();

apps/files_versions/tests/Command/CleanupTest.php

Lines changed: 21 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
<?php
2+
3+
declare(strict_types=1);
24
/**
35
* SPDX-FileCopyrightText: 2017-2024 Nextcloud GmbH and Nextcloud contributors
46
* SPDX-FileCopyrightText: 2016 ownCloud, Inc.
@@ -8,11 +10,13 @@
810

911
use OC\User\Manager;
1012
use OCA\Files_Versions\Command\CleanUp;
13+
use OCA\Files_Versions\Db\VersionsMapper;
1114
use OCP\Files\Cache\ICache;
1215
use OCP\Files\Folder;
1316
use OCP\Files\IRootFolder;
1417
use OCP\Files\Storage\IStorage;
1518
use OCP\UserInterface;
19+
use PHPUnit\Framework\MockObject\MockObject;
1620
use Test\TestCase;
1721

1822
/**
@@ -23,28 +27,17 @@
2327
* @package OCA\Files_Versions\Tests\Command
2428
*/
2529
class CleanupTest extends TestCase {
26-
27-
/** @var CleanUp */
28-
protected $cleanup;
29-
30-
/** @var \PHPUnit\Framework\MockObject\MockObject | Manager */
31-
protected $userManager;
32-
33-
/** @var \PHPUnit\Framework\MockObject\MockObject | IRootFolder */
34-
protected $rootFolder;
35-
36-
/** @var \PHPUnit\Framework\MockObject\MockObject | VersionsMapper */
37-
protected $versionMapper;
30+
protected Manager&MockObject $userManager;
31+
protected IRootFolder&MockObject $rootFolder;
32+
protected VersionsMapper&MockObject $versionMapper;
33+
protected CleanUp $cleanup;
3834

3935
protected function setUp(): void {
4036
parent::setUp();
4137

42-
$this->rootFolder = $this->getMockBuilder('OCP\Files\IRootFolder')
43-
->disableOriginalConstructor()->getMock();
44-
$this->userManager = $this->getMockBuilder('OC\User\Manager')
45-
->disableOriginalConstructor()->getMock();
46-
$this->versionMapper = $this->getMockBuilder('OCA\Files_Versions\Db\VersionsMapper')
47-
->disableOriginalConstructor()->getMock();
38+
$this->rootFolder = $this->createMock(IRootFolder::class);
39+
$this->userManager = $this->createMock(Manager::class);
40+
$this->versionMapper = $this->createMock(VersionsMapper::class);
4841

4942
$this->cleanup = new CleanUp($this->rootFolder, $this->userManager, $this->versionMapper);
5043
}
@@ -53,7 +46,7 @@ protected function setUp(): void {
5346
* @dataProvider dataTestDeleteVersions
5447
* @param boolean $nodeExists
5548
*/
56-
public function testDeleteVersions($nodeExists): void {
49+
public function testDeleteVersions(bool $nodeExists): void {
5750
$this->rootFolder->expects($this->once())
5851
->method('nodeExists')
5952
->with('/testUser/files_versions')
@@ -92,7 +85,7 @@ public function testDeleteVersions($nodeExists): void {
9285
$this->invokePrivate($this->cleanup, 'deleteVersions', ['testUser']);
9386
}
9487

95-
public function dataTestDeleteVersions() {
88+
public static function dataTestDeleteVersions(): array {
9689
return [
9790
[true],
9891
[false]
@@ -106,8 +99,8 @@ public function dataTestDeleteVersions() {
10699
public function testExecuteDeleteListOfUsers(): void {
107100
$userIds = ['user1', 'user2', 'user3'];
108101

109-
$instance = $this->getMockBuilder('OCA\Files_Versions\Command\CleanUp')
110-
->setMethods(['deleteVersions'])
102+
$instance = $this->getMockBuilder(CleanUp::class)
103+
->onlyMethods(['deleteVersions'])
111104
->setConstructorArgs([$this->rootFolder, $this->userManager, $this->versionMapper])
112105
->getMock();
113106
$instance->expects($this->exactly(count($userIds)))
@@ -119,14 +112,12 @@ public function testExecuteDeleteListOfUsers(): void {
119112
$this->userManager->expects($this->exactly(count($userIds)))
120113
->method('userExists')->willReturn(true);
121114

122-
$inputInterface = $this->getMockBuilder('\Symfony\Component\Console\Input\InputInterface')
123-
->disableOriginalConstructor()->getMock();
115+
$inputInterface = $this->createMock(\Symfony\Component\Console\Input\InputInterface::class);
124116
$inputInterface->expects($this->once())->method('getArgument')
125117
->with('user_id')
126118
->willReturn($userIds);
127119

128-
$outputInterface = $this->getMockBuilder('\Symfony\Component\Console\Output\OutputInterface')
129-
->disableOriginalConstructor()->getMock();
120+
$outputInterface = $this->createMock(\Symfony\Component\Console\Output\OutputInterface::class);
130121

131122
$this->invokePrivate($instance, 'execute', [$inputInterface, $outputInterface]);
132123
}
@@ -138,8 +129,8 @@ public function testExecuteAllUsers(): void {
138129
$userIds = [];
139130
$backendUsers = ['user1', 'user2'];
140131

141-
$instance = $this->getMockBuilder('OCA\Files_Versions\Command\CleanUp')
142-
->setMethods(['deleteVersions'])
132+
$instance = $this->getMockBuilder(CleanUp::class)
133+
->onlyMethods(['deleteVersions'])
143134
->setConstructorArgs([$this->rootFolder, $this->userManager, $this->versionMapper])
144135
->getMock();
145136

@@ -155,14 +146,12 @@ public function testExecuteAllUsers(): void {
155146
$this->assertTrue(in_array($user, $backendUsers));
156147
});
157148

158-
$inputInterface = $this->getMockBuilder('\Symfony\Component\Console\Input\InputInterface')
159-
->disableOriginalConstructor()->getMock();
149+
$inputInterface = $this->createMock(\Symfony\Component\Console\Input\InputInterface::class);
160150
$inputInterface->expects($this->once())->method('getArgument')
161151
->with('user_id')
162152
->willReturn($userIds);
163153

164-
$outputInterface = $this->getMockBuilder('\Symfony\Component\Console\Output\OutputInterface')
165-
->disableOriginalConstructor()->getMock();
154+
$outputInterface = $this->createMock(\Symfony\Component\Console\Output\OutputInterface::class);
166155

167156
$this->userManager->expects($this->once())
168157
->method('getBackends')

apps/files_versions/tests/Command/ExpireTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
<?php
2+
3+
declare(strict_types=1);
24
/**
35
* SPDX-FileCopyrightText: 2018 Nextcloud GmbH and Nextcloud contributors
46
* SPDX-FileCopyrightText: 2016 ownCloud, Inc.

apps/files_versions/tests/Controller/PreviewControllerTest.php

Lines changed: 8 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
<?php
2+
3+
declare(strict_types=1);
24
/**
35
* SPDX-FileCopyrightText: 2016 Nextcloud GmbH and Nextcloud contributors
46
* SPDX-License-Identifier: AGPL-3.0-or-later
@@ -12,7 +14,6 @@
1214
use OCP\AppFramework\Http\DataResponse;
1315
use OCP\Files\File;
1416
use OCP\Files\Folder;
15-
use OCP\Files\IMimeTypeDetector;
1617
use OCP\Files\IRootFolder;
1718
use OCP\Files\NotFoundException;
1819
use OCP\Files\SimpleFS\ISimpleFile;
@@ -25,29 +26,14 @@
2526
use Test\TestCase;
2627

2728
class PreviewControllerTest extends TestCase {
28-
29-
/** @var IRootFolder|\PHPUnit\Framework\MockObject\MockObject */
30-
private $rootFolder;
31-
32-
/** @var string */
33-
private $userId;
34-
35-
/** @var IMimeTypeDetector|\PHPUnit\Framework\MockObject\MockObject */
36-
private $mimeTypeDetector;
37-
38-
/** @var IPreview|\PHPUnit\Framework\MockObject\MockObject */
39-
private $previewManager;
40-
41-
/** @var PreviewController|\PHPUnit\Framework\MockObject\MockObject */
42-
private $controller;
43-
44-
/** @var IUserSession|\PHPUnit\Framework\MockObject\MockObject */
45-
private $userSession;
46-
47-
/** @var IVersionManager|\PHPUnit\Framework\MockObject\MockObject */
48-
private $versionManager;
29+
private IRootFolder&MockObject $rootFolder;
30+
private string $userId;
31+
private IPreview&MockObject $previewManager;
32+
private IUserSession&MockObject $userSession;
33+
private IVersionManager&MockObject $versionManager;
4934

5035
private IMimeIconProvider&MockObject $mimeIconProvider;
36+
private PreviewController $controller;
5137

5238
protected function setUp(): void {
5339
parent::setUp();

apps/files_versions/tests/ExpirationTest.php

Lines changed: 6 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
<?php
2+
3+
declare(strict_types=1);
24
/**
35
* SPDX-FileCopyrightText: 2017 Nextcloud GmbH and Nextcloud contributors
46
* SPDX-FileCopyrightText: 2016 ownCloud, Inc.
@@ -15,7 +17,7 @@
1517
class ExpirationTest extends \Test\TestCase {
1618
public const SECONDS_PER_DAY = 86400; //60*60*24
1719

18-
public function expirationData() {
20+
public static function expirationData(): array {
1921
$today = 100 * self::SECONDS_PER_DAY;
2022
$back10Days = (100 - 10) * self::SECONDS_PER_DAY;
2123
$back20Days = (100 - 20) * self::SECONDS_PER_DAY;
@@ -81,14 +83,8 @@ public function expirationData() {
8183

8284
/**
8385
* @dataProvider expirationData
84-
*
85-
* @param string $retentionObligation
86-
* @param int $timeNow
87-
* @param int $timestamp
88-
* @param bool $quotaExceeded
89-
* @param string $expectedResult
9086
*/
91-
public function testExpiration($retentionObligation, $timeNow, $timestamp, $quotaExceeded, $expectedResult): void {
87+
public function testExpiration(string $retentionObligation, int $timeNow, int $timestamp, bool $quotaExceeded, bool $expectedResult): void {
9288
$mockedConfig = $this->getMockedConfig($retentionObligation);
9389
$mockedTimeFactory = $this->getMockedTimeFactory($timeNow);
9490
$mockedLogger = $this->createMock(LoggerInterface::class);
@@ -100,11 +96,7 @@ public function testExpiration($retentionObligation, $timeNow, $timestamp, $quot
10096
}
10197

10298

103-
/**
104-
* @param int $time
105-
* @return ITimeFactory|MockObject
106-
*/
107-
private function getMockedTimeFactory($time) {
99+
private function getMockedTimeFactory(int $time): ITimeFactory&MockObject {
108100
$mockedTimeFactory = $this->createMock(ITimeFactory::class);
109101
$mockedTimeFactory->expects($this->any())
110102
->method('getTime')
@@ -113,11 +105,7 @@ private function getMockedTimeFactory($time) {
113105
return $mockedTimeFactory;
114106
}
115107

116-
/**
117-
* @param string $returnValue
118-
* @return IConfig|MockObject
119-
*/
120-
private function getMockedConfig($returnValue) {
108+
private function getMockedConfig(string $returnValue): IConfig&MockObject {
121109
$mockedConfig = $this->createMock(IConfig::class);
122110
$mockedConfig->expects($this->any())
123111
->method('getSystemValue')

apps/files_versions/tests/StorageTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ class StorageTest extends TestCase {
2424

2525
private $versionsRoot;
2626
private $userFolder;
27-
private $expireTimestamp = 10;
27+
private int $expireTimestamp = 10;
2828

2929
protected function setUp(): void {
3030
parent::setUp();
@@ -46,7 +46,7 @@ protected function setUp(): void {
4646
}
4747

4848

49-
protected function createPastFile(string $path, int $mtime) {
49+
protected function createPastFile(string $path, int $mtime): void {
5050
try {
5151
$file = $this->userFolder->get($path);
5252
} catch (NotFoundException $e) {

apps/files_versions/tests/VersioningTest.php

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
<?php
2+
3+
declare(strict_types=1);
24
/**
35
* SPDX-FileCopyrightText: 2016 Nextcloud GmbH and Nextcloud contributors
46
* SPDX-FileCopyrightText: 2016 ownCloud, Inc.
@@ -163,7 +165,7 @@ public function testGetExpireList($versions, $sizeOfAllDeletedFiles): void {
163165
}
164166
}
165167

166-
public function versionsProvider() {
168+
public static function versionsProvider(): array {
167169
return [
168170
// first set of versions uniformly distributed versions
169171
[
@@ -683,7 +685,7 @@ public function testRestoreNoPermission(): void {
683685

684686
$firstVersion = current($versions);
685687

686-
$this->assertFalse(Storage::rollback('folder/test.txt', $firstVersion['version'], $this->user2), 'Revert did not happen');
688+
$this->assertFalse(Storage::rollback('folder/test.txt', (int)$firstVersion['version'], $this->user2), 'Revert did not happen');
687689

688690
$this->loginAsUser(self::TEST_VERSIONS_USER);
689691

@@ -743,8 +745,8 @@ private function connectMockHooks($hookName, &$params) {
743745
return;
744746
}
745747

746-
$eventHandler = $this->getMockBuilder(\stdclass::class)
747-
->setMethods(['callback'])
748+
$eventHandler = $this->getMockBuilder(DummyHookListener::class)
749+
->onlyMethods(['callback'])
748750
->getMock();
749751

750752
$eventHandler->expects($this->any())
@@ -763,7 +765,7 @@ function ($p) use (&$params): void {
763765
);
764766
}
765767

766-
private function doTestRestore() {
768+
private function doTestRestore(): void {
767769
$filePath = self::TEST_VERSIONS_USER . '/files/sub/test.txt';
768770
$this->rootView->file_put_contents($filePath, 'test file');
769771

@@ -941,11 +943,7 @@ public function testStoreVersionAsAnonymous(): void {
941943
);
942944
}
943945

944-
/**
945-
* @param View $view
946-
* @param string $path
947-
*/
948-
private function createAndCheckVersions(View $view, $path) {
946+
private function createAndCheckVersions(View $view, string $path): array {
949947
$view->file_put_contents($path, 'test file');
950948
$view->file_put_contents($path, 'version 1');
951949
$view->file_put_contents($path, 'version 2');
@@ -967,11 +965,7 @@ private function createAndCheckVersions(View $view, $path) {
967965
return $versions;
968966
}
969967

970-
/**
971-
* @param string $user
972-
* @param bool $create
973-
*/
974-
public static function loginHelper($user, $create = false) {
968+
public static function loginHelper(string $user, bool $create = false) {
975969
if ($create) {
976970
$backend = new \Test\Util\User\Dummy();
977971
$backend->createUser($user, $user);
@@ -987,6 +981,11 @@ public static function loginHelper($user, $create = false) {
987981
}
988982
}
989983

984+
class DummyHookListener {
985+
public function callback() {
986+
}
987+
}
988+
990989
// extend the original class to make it possible to test protected methods
991990
class VersionStorageToTest extends Storage {
992991

0 commit comments

Comments
 (0)