Skip to content

Commit 3bdb89c

Browse files
Merge pull request #53175 from nextcloud/tests/noid/migrate-federation-and-files_trashbin
test: Migrate Federation and Files_Trashbin to PHPUnit 10
2 parents 42bfbc6 + 8b70f59 commit 3bdb89c

19 files changed

+344
-491
lines changed

apps/federation/tests/BackgroundJob/GetSharedSecretTest.php

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
<?php
22

3+
declare(strict_types=1);
34
/**
45
* SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
56
* SPDX-FileCopyrightText: 2016 ownCloud, Inc.
@@ -77,12 +78,9 @@ protected function setUp(): void {
7778

7879
/**
7980
* @dataProvider dataTestExecute
80-
*
81-
* @param bool $isTrustedServer
82-
* @param bool $retainBackgroundJob
8381
*/
8482
public function testExecute(bool $isTrustedServer, bool $retainBackgroundJob): void {
85-
/** @var GetSharedSecret |\PHPUnit\Framework\MockObject\MockObject $getSharedSecret */
83+
/** @var GetSharedSecret&MockObject $getSharedSecret */
8684
$getSharedSecret = $this->getMockBuilder(GetSharedSecret::class)
8785
->setConstructorArgs(
8886
[
@@ -95,8 +93,10 @@ public function testExecute(bool $isTrustedServer, bool $retainBackgroundJob): v
9593
$this->timeFactory,
9694
$this->config,
9795
]
98-
)->setMethods(['parentStart'])->getMock();
99-
$this->invokePrivate($getSharedSecret, 'argument', [['url' => 'url', 'token' => 'token']]);
96+
)
97+
->onlyMethods(['parentStart'])
98+
->getMock();
99+
self::invokePrivate($getSharedSecret, 'argument', [['url' => 'url', 'token' => 'token']]);
100100

101101
$this->trustedServers->expects($this->once())->method('isTrustedServer')
102102
->with('url')->willReturn($isTrustedServer);
@@ -105,7 +105,7 @@ public function testExecute(bool $isTrustedServer, bool $retainBackgroundJob): v
105105
} else {
106106
$getSharedSecret->expects($this->never())->method('parentStart');
107107
}
108-
$this->invokePrivate($getSharedSecret, 'retainJob', [$retainBackgroundJob]);
108+
self::invokePrivate($getSharedSecret, 'retainJob', [$retainBackgroundJob]);
109109
$this->jobList->expects($this->once())->method('remove');
110110

111111
$this->timeFactory->method('getTime')->willReturn(42);
@@ -128,7 +128,7 @@ public function testExecute(bool $isTrustedServer, bool $retainBackgroundJob): v
128128
$getSharedSecret->start($this->jobList);
129129
}
130130

131-
public function dataTestExecute() {
131+
public static function dataTestExecute(): array {
132132
return [
133133
[true, true],
134134
[true, false],
@@ -138,10 +138,8 @@ public function dataTestExecute() {
138138

139139
/**
140140
* @dataProvider dataTestRun
141-
*
142-
* @param int $statusCode
143141
*/
144-
public function testRun($statusCode): void {
142+
public function testRun(int $statusCode): void {
145143
$target = 'targetURL';
146144
$source = 'sourceURL';
147145
$token = 'token';
@@ -181,18 +179,18 @@ public function testRun($statusCode): void {
181179
$this->trustedServers->expects($this->never())->method('addSharedSecret');
182180
}
183181

184-
$this->invokePrivate($this->getSharedSecret, 'run', [$argument]);
182+
self::invokePrivate($this->getSharedSecret, 'run', [$argument]);
185183
if (
186184
$statusCode !== Http::STATUS_OK
187185
&& $statusCode !== Http::STATUS_FORBIDDEN
188186
) {
189-
$this->assertTrue($this->invokePrivate($this->getSharedSecret, 'retainJob'));
187+
$this->assertTrue(self::invokePrivate($this->getSharedSecret, 'retainJob'));
190188
} else {
191-
$this->assertFalse($this->invokePrivate($this->getSharedSecret, 'retainJob'));
189+
$this->assertFalse(self::invokePrivate($this->getSharedSecret, 'retainJob'));
192190
}
193191
}
194192

195-
public function dataTestRun() {
193+
public static function dataTestRun(): array {
196194
return [
197195
[Http::STATUS_OK],
198196
[Http::STATUS_FORBIDDEN],
@@ -227,7 +225,7 @@ public function testRunExpired(): void {
227225
TrustedServers::STATUS_FAILURE
228226
);
229227

230-
$this->invokePrivate($this->getSharedSecret, 'run', [$argument]);
228+
self::invokePrivate($this->getSharedSecret, 'run', [$argument]);
231229
}
232230

233231
public function testRunConnectionError(): void {
@@ -263,8 +261,8 @@ public function testRunConnectionError(): void {
263261

264262
$this->trustedServers->expects($this->never())->method('addSharedSecret');
265263

266-
$this->invokePrivate($this->getSharedSecret, 'run', [$argument]);
264+
self::invokePrivate($this->getSharedSecret, 'run', [$argument]);
267265

268-
$this->assertTrue($this->invokePrivate($this->getSharedSecret, 'retainJob'));
266+
$this->assertTrue(self::invokePrivate($this->getSharedSecret, 'retainJob'));
269267
}
270268
}

apps/federation/tests/BackgroundJob/RequestSharedSecretTest.php

Lines changed: 34 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
<?php
22

3+
declare(strict_types=1);
34
/**
45
* SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
56
* SPDX-FileCopyrightText: 2016 ownCloud, Inc.
@@ -24,50 +25,28 @@
2425
use Test\TestCase;
2526

2627
class RequestSharedSecretTest extends TestCase {
27-
/** @var MockObject|IClientService */
28-
private $httpClientService;
29-
30-
/** @var MockObject|IClient */
31-
private $httpClient;
32-
33-
/** @var MockObject|IJobList */
34-
private $jobList;
35-
36-
/** @var MockObject|IURLGenerator */
37-
private $urlGenerator;
38-
39-
/** @var MockObject|TrustedServers */
40-
private $trustedServers;
41-
42-
/** @var MockObject|IResponse */
43-
private $response;
44-
45-
/** @var MockObject|IDiscoveryService */
46-
private $discoveryService;
47-
48-
/** @var MockObject|LoggerInterface */
49-
private $logger;
50-
51-
/** @var MockObject|ITimeFactory */
52-
private $timeFactory;
53-
54-
/** @var MockObject|IConfig */
55-
private $config;
56-
57-
/** @var RequestSharedSecret */
58-
private $requestSharedSecret;
28+
private IClientService&MockObject $httpClientService;
29+
private IClient&MockObject $httpClient;
30+
private IJobList&MockObject $jobList;
31+
private IURLGenerator&MockObject $urlGenerator;
32+
private TrustedServers&MockObject $trustedServers;
33+
private IResponse&MockObject $response;
34+
private IDiscoveryService&MockObject $discoveryService;
35+
private LoggerInterface&MockObject $logger;
36+
private ITimeFactory&MockObject $timeFactory;
37+
private IConfig&MockObject $config;
38+
private RequestSharedSecret $requestSharedSecret;
5939

6040
protected function setUp(): void {
6141
parent::setUp();
6242

6343
$this->httpClientService = $this->createMock(IClientService::class);
64-
$this->httpClient = $this->getMockBuilder(IClient::class)->getMock();
65-
$this->jobList = $this->getMockBuilder(IJobList::class)->getMock();
66-
$this->urlGenerator = $this->getMockBuilder(IURLGenerator::class)->getMock();
67-
$this->trustedServers = $this->getMockBuilder(TrustedServers::class)
68-
->disableOriginalConstructor()->getMock();
69-
$this->response = $this->getMockBuilder(IResponse::class)->getMock();
70-
$this->discoveryService = $this->getMockBuilder(IDiscoveryService::class)->getMock();
44+
$this->httpClient = $this->createMock(IClient::class);
45+
$this->jobList = $this->createMock(IJobList::class);
46+
$this->urlGenerator = $this->createMock(IURLGenerator::class);
47+
$this->trustedServers = $this->createMock(TrustedServers::class);
48+
$this->response = $this->createMock(IResponse::class);
49+
$this->discoveryService = $this->createMock(IDiscoveryService::class);
7150
$this->logger = $this->createMock(LoggerInterface::class);
7251
$this->timeFactory = $this->createMock(ITimeFactory::class);
7352
$this->config = $this->createMock(IConfig::class);
@@ -89,13 +68,10 @@ protected function setUp(): void {
8968

9069
/**
9170
* @dataProvider dataTestStart
92-
*
93-
* @param bool $isTrustedServer
94-
* @param bool $retainBackgroundJob
9571
*/
96-
public function testStart($isTrustedServer, $retainBackgroundJob): void {
97-
/** @var RequestSharedSecret |MockObject $requestSharedSecret */
98-
$requestSharedSecret = $this->getMockBuilder('OCA\Federation\BackgroundJob\RequestSharedSecret')
72+
public function testStart(bool $isTrustedServer, bool $retainBackgroundJob): void {
73+
/** @var RequestSharedSecret&MockObject $requestSharedSecret */
74+
$requestSharedSecret = $this->getMockBuilder(RequestSharedSecret::class)
9975
->setConstructorArgs(
10076
[
10177
$this->httpClientService,
@@ -107,8 +83,10 @@ public function testStart($isTrustedServer, $retainBackgroundJob): void {
10783
$this->timeFactory,
10884
$this->config,
10985
]
110-
)->setMethods(['parentStart'])->getMock();
111-
$this->invokePrivate($requestSharedSecret, 'argument', [['url' => 'url', 'token' => 'token']]);
86+
)
87+
->onlyMethods(['parentStart'])
88+
->getMock();
89+
self::invokePrivate($requestSharedSecret, 'argument', [['url' => 'url', 'token' => 'token']]);
11290

11391
$this->trustedServers->expects($this->once())->method('isTrustedServer')
11492
->with('url')->willReturn($isTrustedServer);
@@ -117,7 +95,7 @@ public function testStart($isTrustedServer, $retainBackgroundJob): void {
11795
} else {
11896
$requestSharedSecret->expects($this->never())->method('parentStart');
11997
}
120-
$this->invokePrivate($requestSharedSecret, 'retainJob', [$retainBackgroundJob]);
98+
self::invokePrivate($requestSharedSecret, 'retainJob', [$retainBackgroundJob]);
12199
$this->jobList->expects($this->once())->method('remove');
122100

123101
$this->timeFactory->method('getTime')->willReturn(42);
@@ -141,7 +119,7 @@ public function testStart($isTrustedServer, $retainBackgroundJob): void {
141119
$requestSharedSecret->start($this->jobList);
142120
}
143121

144-
public function dataTestStart() {
122+
public static function dataTestStart(): array {
145123
return [
146124
[true, true],
147125
[true, false],
@@ -151,8 +129,6 @@ public function dataTestStart() {
151129

152130
/**
153131
* @dataProvider dataTestRun
154-
*
155-
* @param int $statusCode
156132
*/
157133
public function testRun(int $statusCode, int $attempt = 0): void {
158134
$target = 'targetURL';
@@ -184,18 +160,18 @@ public function testRun(int $statusCode, int $attempt = 0): void {
184160
$this->response->expects($this->once())->method('getStatusCode')
185161
->willReturn($statusCode);
186162

187-
$this->invokePrivate($this->requestSharedSecret, 'run', [$argument]);
163+
self::invokePrivate($this->requestSharedSecret, 'run', [$argument]);
188164
if (
189165
$statusCode !== Http::STATUS_OK
190166
&& ($statusCode !== Http::STATUS_FORBIDDEN || $attempt < 5)
191167
) {
192-
$this->assertTrue($this->invokePrivate($this->requestSharedSecret, 'retainJob'));
168+
$this->assertTrue(self::invokePrivate($this->requestSharedSecret, 'retainJob'));
193169
} else {
194-
$this->assertFalse($this->invokePrivate($this->requestSharedSecret, 'retainJob'));
170+
$this->assertFalse(self::invokePrivate($this->requestSharedSecret, 'retainJob'));
195171
}
196172
}
197173

198-
public function dataTestRun() {
174+
public static function dataTestRun(): array {
199175
return [
200176
[Http::STATUS_OK],
201177
[Http::STATUS_FORBIDDEN, 5],
@@ -231,7 +207,7 @@ public function testRunExpired(): void {
231207
TrustedServers::STATUS_FAILURE
232208
);
233209

234-
$this->invokePrivate($this->requestSharedSecret, 'run', [$argument]);
210+
self::invokePrivate($this->requestSharedSecret, 'run', [$argument]);
235211
}
236212

237213
public function testRunConnectionError(): void {
@@ -267,7 +243,7 @@ public function testRunConnectionError(): void {
267243
]
268244
)->willThrowException($this->createMock(ConnectException::class));
269245

270-
$this->invokePrivate($this->requestSharedSecret, 'run', [$argument]);
271-
$this->assertTrue($this->invokePrivate($this->requestSharedSecret, 'retainJob'));
246+
self::invokePrivate($this->requestSharedSecret, 'run', [$argument]);
247+
$this->assertTrue(self::invokePrivate($this->requestSharedSecret, 'retainJob'));
272248
}
273249
}

apps/federation/tests/Controller/OCSAuthAPIControllerTest.php

Lines changed: 19 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
<?php
22

3+
declare(strict_types=1);
34
/**
45
* SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
56
* SPDX-FileCopyrightText: 2016 ownCloud, Inc.
@@ -8,6 +9,7 @@
89
namespace OCA\Federation\Tests\Controller;
910

1011
use OC\BackgroundJob\JobList;
12+
use OCA\Federation\BackgroundJob\GetSharedSecret;
1113
use OCA\Federation\Controller\OCSAuthAPIController;
1214
use OCA\Federation\DbHandler;
1315
use OCA\Federation\TrustedServers;
@@ -16,34 +18,19 @@
1618
use OCP\IRequest;
1719
use OCP\Security\Bruteforce\IThrottler;
1820
use OCP\Security\ISecureRandom;
21+
use PHPUnit\Framework\MockObject\MockObject;
1922
use Psr\Log\LoggerInterface;
2023
use Test\TestCase;
2124

2225
class OCSAuthAPIControllerTest extends TestCase {
23-
/** @var \PHPUnit\Framework\MockObject\MockObject|IRequest */
24-
private $request;
25-
26-
/** @var \PHPUnit\Framework\MockObject\MockObject|ISecureRandom */
27-
private $secureRandom;
28-
29-
/** @var \PHPUnit\Framework\MockObject\MockObject|JobList */
30-
private $jobList;
31-
32-
/** @var \PHPUnit\Framework\MockObject\MockObject|TrustedServers */
33-
private $trustedServers;
34-
35-
/** @var \PHPUnit\Framework\MockObject\MockObject|DbHandler */
36-
private $dbHandler;
37-
38-
/** @var \PHPUnit\Framework\MockObject\MockObject|LoggerInterface */
39-
private $logger;
40-
41-
/** @var \PHPUnit\Framework\MockObject\MockObject|ITimeFactory */
42-
private $timeFactory;
43-
44-
/** @var \PHPUnit\Framework\MockObject\MockObject|IThrottler */
45-
private $throttler;
46-
26+
private IRequest&MockObject $request;
27+
private ISecureRandom&MockObject $secureRandom;
28+
private JobList&MockObject $jobList;
29+
private TrustedServers&MockObject $trustedServers;
30+
private DbHandler&MockObject $dbHandler;
31+
private LoggerInterface&MockObject $logger;
32+
private ITimeFactory&MockObject $timeFactory;
33+
private IThrottler&MockObject $throttler;
4734
private OCSAuthAPIController $ocsAuthApi;
4835

4936
/** @var int simulated timestamp */
@@ -91,7 +78,7 @@ public function testRequestSharedSecret(string $token, string $localToken, bool
9178

9279
if ($ok) {
9380
$this->jobList->expects($this->once())->method('add')
94-
->with('OCA\Federation\BackgroundJob\GetSharedSecret', ['url' => $url, 'token' => $token, 'created' => $this->currentTime]);
81+
->with(GetSharedSecret::class, ['url' => $url, 'token' => $token, 'created' => $this->currentTime]);
9582
} else {
9683
$this->jobList->expects($this->never())->method('add');
9784
$this->jobList->expects($this->never())->method('remove');
@@ -111,7 +98,7 @@ public function testRequestSharedSecret(string $token, string $localToken, bool
11198
}
11299
}
113100

114-
public function dataTestRequestSharedSecret() {
101+
public static function dataTestRequestSharedSecret(): array {
115102
return [
116103
['token2', 'token1', true, true],
117104
['token1', 'token2', false, false],
@@ -126,8 +113,8 @@ public function testGetSharedSecret(bool $isTrustedServer, bool $isValidToken, b
126113
$url = 'url';
127114
$token = 'token';
128115

129-
/** @var OCSAuthAPIController | \PHPUnit\Framework\MockObject\MockObject $ocsAuthApi */
130-
$ocsAuthApi = $this->getMockBuilder('OCA\Federation\Controller\OCSAuthAPIController')
116+
/** @var OCSAuthAPIController&MockObject $ocsAuthApi */
117+
$ocsAuthApi = $this->getMockBuilder(OCSAuthAPIController::class)
131118
->setConstructorArgs(
132119
[
133120
'federation',
@@ -140,7 +127,9 @@ public function testGetSharedSecret(bool $isTrustedServer, bool $isValidToken, b
140127
$this->timeFactory,
141128
$this->throttler
142129
]
143-
)->setMethods(['isValidToken'])->getMock();
130+
)
131+
->onlyMethods(['isValidToken'])
132+
->getMock();
144133

145134
$this->trustedServers
146135
->expects($this->any())
@@ -171,7 +160,7 @@ public function testGetSharedSecret(bool $isTrustedServer, bool $isValidToken, b
171160
}
172161
}
173162

174-
public function dataTestGetSharedSecret() {
163+
public static function dataTestGetSharedSecret(): array {
175164
return [
176165
[true, true, true],
177166
[false, true, false],

0 commit comments

Comments
 (0)