diff options
Diffstat (limited to 'apps/federation/tests/BackgroundJob/GetSharedSecretTest.php')
-rw-r--r-- | apps/federation/tests/BackgroundJob/GetSharedSecretTest.php | 159 |
1 files changed, 61 insertions, 98 deletions
diff --git a/apps/federation/tests/BackgroundJob/GetSharedSecretTest.php b/apps/federation/tests/BackgroundJob/GetSharedSecretTest.php index baefa86aeda..943bdf352de 100644 --- a/apps/federation/tests/BackgroundJob/GetSharedSecretTest.php +++ b/apps/federation/tests/BackgroundJob/GetSharedSecretTest.php @@ -1,28 +1,10 @@ <?php + +declare(strict_types=1); /** - * @copyright Copyright (c) 2016, ownCloud, Inc. - * - * @author Bjoern Schiessle <bjoern@schiessle.org> - * @author Björn Schießle <bjoern@schiessle.org> - * @author Joas Schilling <coding@schilljs.com> - * @author Morris Jobke <hey@morrisjobke.de> - * @author Roeland Jago Douma <roeland@famdouma.nl> - * @author Thomas Müller <thomas.mueller@tmit.eu> - * - * @license AGPL-3.0 - * - * This code is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License, version 3, - * as published by the Free Software Foundation. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License, version 3, - * along with this program. If not, see <http://www.gnu.org/licenses/> - * + * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors + * SPDX-FileCopyrightText: 2016 ownCloud, Inc. + * SPDX-License-Identifier: AGPL-3.0-only */ namespace OCA\Federation\Tests\BackgroundJob; @@ -36,9 +18,11 @@ use OCP\BackgroundJob\IJobList; use OCP\Http\Client\IClient; use OCP\Http\Client\IClientService; use OCP\Http\Client\IResponse; -use OCP\ILogger; +use OCP\IConfig; use OCP\IURLGenerator; use OCP\OCS\IDiscoveryService; +use PHPUnit\Framework\MockObject\MockObject; +use Psr\Log\LoggerInterface; /** * Class GetSharedSecretTest @@ -49,35 +33,18 @@ use OCP\OCS\IDiscoveryService; */ class GetSharedSecretTest extends TestCase { - /** @var \PHPUnit\Framework\MockObject\MockObject|IClient */ - private $httpClient; - - /** @var \PHPUnit\Framework\MockObject\MockObject|IClientService */ - private $httpClientService; - - /** @var \PHPUnit\Framework\MockObject\MockObject|IJobList */ - private $jobList; - - /** @var \PHPUnit\Framework\MockObject\MockObject|IURLGenerator */ - private $urlGenerator; - - /** @var \PHPUnit\Framework\MockObject\MockObject|TrustedServers */ - private $trustedServers; - - /** @var \PHPUnit\Framework\MockObject\MockObject|ILogger */ - private $logger; - - /** @var \PHPUnit\Framework\MockObject\MockObject|IResponse */ - private $response; - - /** @var \PHPUnit\Framework\MockObject\MockObject|IDiscoveryService */ - private $discoverService; - - /** @var \PHPUnit\Framework\MockObject\MockObject|ITimeFactory */ - private $timeFactory; + private MockObject&IClient $httpClient; + private MockObject&IClientService $httpClientService; + private MockObject&IJobList $jobList; + private MockObject&IURLGenerator $urlGenerator; + private MockObject&TrustedServers $trustedServers; + private MockObject&LoggerInterface $logger; + private MockObject&IResponse $response; + private MockObject&IDiscoveryService $discoverService; + private MockObject&ITimeFactory $timeFactory; + private MockObject&IConfig $config; - /** @var GetSharedSecret */ - private $getSharedSecret; + private GetSharedSecret $getSharedSecret; protected function setUp(): void { parent::setUp(); @@ -88,10 +55,11 @@ class GetSharedSecretTest extends TestCase { $this->urlGenerator = $this->getMockBuilder(IURLGenerator::class)->getMock(); $this->trustedServers = $this->getMockBuilder(TrustedServers::class) ->disableOriginalConstructor()->getMock(); - $this->logger = $this->getMockBuilder(ILogger::class)->getMock(); + $this->logger = $this->getMockBuilder(LoggerInterface::class)->getMock(); $this->response = $this->getMockBuilder(IResponse::class)->getMock(); $this->discoverService = $this->getMockBuilder(IDiscoveryService::class)->getMock(); $this->timeFactory = $this->createMock(ITimeFactory::class); + $this->config = $this->createMock(IConfig::class); $this->discoverService->expects($this->any())->method('discover')->willReturn([]); $this->httpClientService->expects($this->any())->method('newClient')->willReturn($this->httpClient); @@ -103,19 +71,15 @@ class GetSharedSecretTest extends TestCase { $this->trustedServers, $this->logger, $this->discoverService, - $this->timeFactory + $this->timeFactory, + $this->config, ); } - /** - * @dataProvider dataTestExecute - * - * @param bool $isTrustedServer - * @param bool $retainBackgroundJob - */ - public function testExecute($isTrustedServer, $retainBackgroundJob) { - /** @var GetSharedSecret |\PHPUnit\Framework\MockObject\MockObject $getSharedSecret */ - $getSharedSecret = $this->getMockBuilder('OCA\Federation\BackgroundJob\GetSharedSecret') + #[\PHPUnit\Framework\Attributes\DataProvider('dataTestExecute')] + public function testExecute(bool $isTrustedServer, bool $retainBackgroundJob): void { + /** @var GetSharedSecret&MockObject $getSharedSecret */ + $getSharedSecret = $this->getMockBuilder(GetSharedSecret::class) ->setConstructorArgs( [ $this->httpClientService, @@ -124,19 +88,22 @@ class GetSharedSecretTest extends TestCase { $this->trustedServers, $this->logger, $this->discoverService, - $this->timeFactory + $this->timeFactory, + $this->config, ] - )->setMethods(['parentExecute'])->getMock(); - $this->invokePrivate($getSharedSecret, 'argument', [['url' => 'url', 'token' => 'token']]); + ) + ->onlyMethods(['parentStart']) + ->getMock(); + self::invokePrivate($getSharedSecret, 'argument', [['url' => 'url', 'token' => 'token']]); $this->trustedServers->expects($this->once())->method('isTrustedServer') ->with('url')->willReturn($isTrustedServer); if ($isTrustedServer) { - $getSharedSecret->expects($this->once())->method('parentExecute'); + $getSharedSecret->expects($this->once())->method('parentStart'); } else { - $getSharedSecret->expects($this->never())->method('parentExecute'); + $getSharedSecret->expects($this->never())->method('parentStart'); } - $this->invokePrivate($getSharedSecret, 'retainJob', [$retainBackgroundJob]); + self::invokePrivate($getSharedSecret, 'retainJob', [$retainBackgroundJob]); $this->jobList->expects($this->once())->method('remove'); $this->timeFactory->method('getTime')->willReturn(42); @@ -156,10 +123,10 @@ class GetSharedSecretTest extends TestCase { $this->jobList->expects($this->never())->method('add'); } - $getSharedSecret->execute($this->jobList); + $getSharedSecret->start($this->jobList); } - public function dataTestExecute() { + public static function dataTestExecute(): array { return [ [true, true], [true, false], @@ -167,12 +134,8 @@ class GetSharedSecretTest extends TestCase { ]; } - /** - * @dataProvider dataTestRun - * - * @param int $statusCode - */ - public function testRun($statusCode) { + #[\PHPUnit\Framework\Attributes\DataProvider('dataTestRun')] + public function testRun(int $statusCode): void { $target = 'targetURL'; $source = 'sourceURL'; $token = 'token'; @@ -188,14 +151,14 @@ class GetSharedSecretTest extends TestCase { ->with( $target . '/ocs/v2.php/apps/federation/api/v1/shared-secret', [ - 'query' => - [ - 'url' => $source, - 'token' => $token, - 'format' => 'json', - ], + 'query' => [ + 'url' => $source, + 'token' => $token, + 'format' => 'json', + ], 'timeout' => 3, 'connect_timeout' => 3, + 'verify' => true, ] )->willReturn($this->response); @@ -211,18 +174,18 @@ class GetSharedSecretTest extends TestCase { $this->trustedServers->expects($this->never())->method('addSharedSecret'); } - $this->invokePrivate($this->getSharedSecret, 'run', [$argument]); + self::invokePrivate($this->getSharedSecret, 'run', [$argument]); if ( $statusCode !== Http::STATUS_OK && $statusCode !== Http::STATUS_FORBIDDEN ) { - $this->assertTrue($this->invokePrivate($this->getSharedSecret, 'retainJob')); + $this->assertTrue(self::invokePrivate($this->getSharedSecret, 'retainJob')); } else { - $this->assertFalse($this->invokePrivate($this->getSharedSecret, 'retainJob')); + $this->assertFalse(self::invokePrivate($this->getSharedSecret, 'retainJob')); } } - public function dataTestRun() { + public static function dataTestRun(): array { return [ [Http::STATUS_OK], [Http::STATUS_FORBIDDEN], @@ -230,7 +193,7 @@ class GetSharedSecretTest extends TestCase { ]; } - public function testRunExpired() { + public function testRunExpired(): void { $target = 'targetURL'; $source = 'sourceURL'; $token = 'token'; @@ -257,10 +220,10 @@ class GetSharedSecretTest extends TestCase { TrustedServers::STATUS_FAILURE ); - $this->invokePrivate($this->getSharedSecret, 'run', [$argument]); + self::invokePrivate($this->getSharedSecret, 'run', [$argument]); } - public function testRunConnectionError() { + public function testRunConnectionError(): void { $target = 'targetURL'; $source = 'sourceURL'; $token = 'token'; @@ -279,21 +242,21 @@ class GetSharedSecretTest extends TestCase { ->with( $target . '/ocs/v2.php/apps/federation/api/v1/shared-secret', [ - 'query' => - [ - 'url' => $source, - 'token' => $token, - 'format' => 'json', - ], + 'query' => [ + 'url' => $source, + 'token' => $token, + 'format' => 'json', + ], 'timeout' => 3, 'connect_timeout' => 3, + 'verify' => true, ] )->willThrowException($this->createMock(ConnectException::class)); $this->trustedServers->expects($this->never())->method('addSharedSecret'); - $this->invokePrivate($this->getSharedSecret, 'run', [$argument]); + self::invokePrivate($this->getSharedSecret, 'run', [$argument]); - $this->assertTrue($this->invokePrivate($this->getSharedSecret, 'retainJob')); + $this->assertTrue(self::invokePrivate($this->getSharedSecret, 'retainJob')); } } |