diff options
Diffstat (limited to 'apps/federation/tests/BackgroundJob/RequestSharedSecretTest.php')
-rw-r--r-- | apps/federation/tests/BackgroundJob/RequestSharedSecretTest.php | 122 |
1 files changed, 46 insertions, 76 deletions
diff --git a/apps/federation/tests/BackgroundJob/RequestSharedSecretTest.php b/apps/federation/tests/BackgroundJob/RequestSharedSecretTest.php index 68f8cc070c8..6ef579c7483 100644 --- a/apps/federation/tests/BackgroundJob/RequestSharedSecretTest.php +++ b/apps/federation/tests/BackgroundJob/RequestSharedSecretTest.php @@ -1,5 +1,6 @@ <?php +declare(strict_types=1); /** * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors * SPDX-FileCopyrightText: 2016 ownCloud, Inc. @@ -24,50 +25,28 @@ use Psr\Log\LoggerInterface; use Test\TestCase; class RequestSharedSecretTest extends TestCase { - /** @var MockObject|IClientService */ - private $httpClientService; - - /** @var MockObject|IClient */ - private $httpClient; - - /** @var MockObject|IJobList */ - private $jobList; - - /** @var MockObject|IURLGenerator */ - private $urlGenerator; - - /** @var MockObject|TrustedServers */ - private $trustedServers; - - /** @var MockObject|IResponse */ - private $response; - - /** @var MockObject|IDiscoveryService */ - private $discoveryService; - - /** @var MockObject|LoggerInterface */ - private $logger; - - /** @var MockObject|ITimeFactory */ - private $timeFactory; - - /** @var MockObject|IConfig */ - private $config; - - /** @var RequestSharedSecret */ - private $requestSharedSecret; + private IClientService&MockObject $httpClientService; + private IClient&MockObject $httpClient; + private IJobList&MockObject $jobList; + private IURLGenerator&MockObject $urlGenerator; + private TrustedServers&MockObject $trustedServers; + private IResponse&MockObject $response; + private IDiscoveryService&MockObject $discoveryService; + private LoggerInterface&MockObject $logger; + private ITimeFactory&MockObject $timeFactory; + private IConfig&MockObject $config; + private RequestSharedSecret $requestSharedSecret; protected function setUp(): void { parent::setUp(); $this->httpClientService = $this->createMock(IClientService::class); - $this->httpClient = $this->getMockBuilder(IClient::class)->getMock(); - $this->jobList = $this->getMockBuilder(IJobList::class)->getMock(); - $this->urlGenerator = $this->getMockBuilder(IURLGenerator::class)->getMock(); - $this->trustedServers = $this->getMockBuilder(TrustedServers::class) - ->disableOriginalConstructor()->getMock(); - $this->response = $this->getMockBuilder(IResponse::class)->getMock(); - $this->discoveryService = $this->getMockBuilder(IDiscoveryService::class)->getMock(); + $this->httpClient = $this->createMock(IClient::class); + $this->jobList = $this->createMock(IJobList::class); + $this->urlGenerator = $this->createMock(IURLGenerator::class); + $this->trustedServers = $this->createMock(TrustedServers::class); + $this->response = $this->createMock(IResponse::class); + $this->discoveryService = $this->createMock(IDiscoveryService::class); $this->logger = $this->createMock(LoggerInterface::class); $this->timeFactory = $this->createMock(ITimeFactory::class); $this->config = $this->createMock(IConfig::class); @@ -87,15 +66,10 @@ class RequestSharedSecretTest extends TestCase { ); } - /** - * @dataProvider dataTestStart - * - * @param bool $isTrustedServer - * @param bool $retainBackgroundJob - */ - public function testStart($isTrustedServer, $retainBackgroundJob): void { - /** @var RequestSharedSecret |MockObject $requestSharedSecret */ - $requestSharedSecret = $this->getMockBuilder('OCA\Federation\BackgroundJob\RequestSharedSecret') + #[\PHPUnit\Framework\Attributes\DataProvider('dataTestStart')] + public function testStart(bool $isTrustedServer, bool $retainBackgroundJob): void { + /** @var RequestSharedSecret&MockObject $requestSharedSecret */ + $requestSharedSecret = $this->getMockBuilder(RequestSharedSecret::class) ->setConstructorArgs( [ $this->httpClientService, @@ -107,8 +81,10 @@ class RequestSharedSecretTest extends TestCase { $this->timeFactory, $this->config, ] - )->setMethods(['parentStart'])->getMock(); - $this->invokePrivate($requestSharedSecret, 'argument', [['url' => 'url', 'token' => 'token']]); + ) + ->onlyMethods(['parentStart']) + ->getMock(); + self::invokePrivate($requestSharedSecret, 'argument', [['url' => 'url', 'token' => 'token']]); $this->trustedServers->expects($this->once())->method('isTrustedServer') ->with('url')->willReturn($isTrustedServer); @@ -117,7 +93,7 @@ class RequestSharedSecretTest extends TestCase { } else { $requestSharedSecret->expects($this->never())->method('parentStart'); } - $this->invokePrivate($requestSharedSecret, 'retainJob', [$retainBackgroundJob]); + self::invokePrivate($requestSharedSecret, 'retainJob', [$retainBackgroundJob]); $this->jobList->expects($this->once())->method('remove'); $this->timeFactory->method('getTime')->willReturn(42); @@ -141,7 +117,7 @@ class RequestSharedSecretTest extends TestCase { $requestSharedSecret->start($this->jobList); } - public function dataTestStart() { + public static function dataTestStart(): array { return [ [true, true], [true, false], @@ -149,11 +125,7 @@ class RequestSharedSecretTest extends TestCase { ]; } - /** - * @dataProvider dataTestRun - * - * @param int $statusCode - */ + #[\PHPUnit\Framework\Attributes\DataProvider('dataTestRun')] public function testRun(int $statusCode, int $attempt = 0): void { $target = 'targetURL'; $source = 'sourceURL'; @@ -169,12 +141,11 @@ class RequestSharedSecretTest extends TestCase { ->with( $target . '/ocs/v2.php/apps/federation/api/v1/request-shared-secret', [ - 'body' => - [ - 'url' => $source, - 'token' => $token, - 'format' => 'json', - ], + 'body' => [ + 'url' => $source, + 'token' => $token, + 'format' => 'json', + ], 'timeout' => 3, 'connect_timeout' => 3, 'verify' => true, @@ -184,18 +155,18 @@ class RequestSharedSecretTest extends TestCase { $this->response->expects($this->once())->method('getStatusCode') ->willReturn($statusCode); - $this->invokePrivate($this->requestSharedSecret, 'run', [$argument]); + self::invokePrivate($this->requestSharedSecret, 'run', [$argument]); if ( $statusCode !== Http::STATUS_OK && ($statusCode !== Http::STATUS_FORBIDDEN || $attempt < 5) ) { - $this->assertTrue($this->invokePrivate($this->requestSharedSecret, 'retainJob')); + $this->assertTrue(self::invokePrivate($this->requestSharedSecret, 'retainJob')); } else { - $this->assertFalse($this->invokePrivate($this->requestSharedSecret, 'retainJob')); + $this->assertFalse(self::invokePrivate($this->requestSharedSecret, 'retainJob')); } } - public function dataTestRun() { + public static function dataTestRun(): array { return [ [Http::STATUS_OK], [Http::STATUS_FORBIDDEN, 5], @@ -231,7 +202,7 @@ class RequestSharedSecretTest extends TestCase { TrustedServers::STATUS_FAILURE ); - $this->invokePrivate($this->requestSharedSecret, 'run', [$argument]); + self::invokePrivate($this->requestSharedSecret, 'run', [$argument]); } public function testRunConnectionError(): void { @@ -255,19 +226,18 @@ class RequestSharedSecretTest extends TestCase { ->with( $target . '/ocs/v2.php/apps/federation/api/v1/request-shared-secret', [ - 'body' => - [ - 'url' => $source, - 'token' => $token, - 'format' => 'json', - ], + 'body' => [ + 'url' => $source, + 'token' => $token, + 'format' => 'json', + ], 'timeout' => 3, 'connect_timeout' => 3, 'verify' => true, ] )->willThrowException($this->createMock(ConnectException::class)); - $this->invokePrivate($this->requestSharedSecret, 'run', [$argument]); - $this->assertTrue($this->invokePrivate($this->requestSharedSecret, 'retainJob')); + self::invokePrivate($this->requestSharedSecret, 'run', [$argument]); + $this->assertTrue(self::invokePrivate($this->requestSharedSecret, 'retainJob')); } } |