diff options
Diffstat (limited to 'apps/federation/tests/TrustedServersTest.php')
-rw-r--r-- | apps/federation/tests/TrustedServersTest.php | 114 |
1 files changed, 46 insertions, 68 deletions
diff --git a/apps/federation/tests/TrustedServersTest.php b/apps/federation/tests/TrustedServersTest.php index c3f0368858b..4d64df7a5e1 100644 --- a/apps/federation/tests/TrustedServersTest.php +++ b/apps/federation/tests/TrustedServersTest.php @@ -1,5 +1,6 @@ <?php +declare(strict_types=1); /** * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors * SPDX-FileCopyrightText: 2016 ownCloud, Inc. @@ -7,6 +8,7 @@ */ namespace OCA\Federation\Tests; +use OCA\Federation\BackgroundJob\RequestSharedSecret; use OCA\Federation\DbHandler; use OCA\Federation\TrustedServers; use OCP\AppFramework\Utility\ITimeFactory; @@ -19,57 +21,35 @@ use OCP\Http\Client\IClientService; use OCP\Http\Client\IResponse; use OCP\IConfig; use OCP\Security\ISecureRandom; +use PHPUnit\Framework\MockObject\MockObject; use Psr\Log\LoggerInterface; use Test\TestCase; class TrustedServersTest extends TestCase { - /** @var \PHPUnit\Framework\MockObject\MockObject | TrustedServers */ - private $trustedServers; - - /** @var \PHPUnit\Framework\MockObject\MockObject | DbHandler */ - private $dbHandler; - - /** @var \PHPUnit\Framework\MockObject\MockObject | IClientService */ - private $httpClientService; - - /** @var \PHPUnit\Framework\MockObject\MockObject | IClient */ - private $httpClient; - - /** @var \PHPUnit\Framework\MockObject\MockObject | IResponse */ - private $response; - - /** @var \PHPUnit\Framework\MockObject\MockObject | LoggerInterface */ - private $logger; - - /** @var \PHPUnit\Framework\MockObject\MockObject | IJobList */ - private $jobList; - - /** @var \PHPUnit\Framework\MockObject\MockObject | ISecureRandom */ - private $secureRandom; - - /** @var \PHPUnit\Framework\MockObject\MockObject | IConfig */ - private $config; - - /** @var \PHPUnit\Framework\MockObject\MockObject | IEventDispatcher */ - private $dispatcher; - - /** @var \PHPUnit\Framework\MockObject\MockObject|ITimeFactory */ - private $timeFactory; + private TrustedServers $trustedServers; + private DbHandler&MockObject $dbHandler; + private IClientService&MockObject $httpClientService; + private IClient&MockObject $httpClient; + private IResponse&MockObject $response; + private LoggerInterface&MockObject $logger; + private IJobList&MockObject $jobList; + private ISecureRandom&MockObject $secureRandom; + private IConfig&MockObject $config; + private IEventDispatcher&MockObject $dispatcher; + private ITimeFactory&MockObject $timeFactory; protected function setUp(): void { parent::setUp(); - $this->dbHandler = $this->getMockBuilder(DbHandler::class) - ->disableOriginalConstructor()->getMock(); - $this->dispatcher = $this->getMockBuilder(IEventDispatcher::class) - ->disableOriginalConstructor()->getMock(); - $this->httpClientService = $this->getMockBuilder(IClientService::class)->getMock(); - $this->httpClient = $this->getMockBuilder(IClient::class)->getMock(); - $this->response = $this->getMockBuilder(IResponse::class)->getMock(); - $this->logger = $this->getMockBuilder(LoggerInterface::class)->getMock(); - $this->jobList = $this->getMockBuilder(IJobList::class)->getMock(); - $this->secureRandom = $this->getMockBuilder(ISecureRandom::class)->getMock(); - $this->config = $this->getMockBuilder(IConfig::class)->getMock(); + $this->dbHandler = $this->createMock(DbHandler::class); + $this->dispatcher = $this->createMock(IEventDispatcher::class); + $this->httpClientService = $this->createMock(IClientService::class); + $this->httpClient = $this->createMock(IClient::class); + $this->response = $this->createMock(IResponse::class); + $this->logger = $this->createMock(LoggerInterface::class); + $this->jobList = $this->createMock(IJobList::class); + $this->secureRandom = $this->createMock(ISecureRandom::class); + $this->config = $this->createMock(IConfig::class); $this->timeFactory = $this->createMock(ITimeFactory::class); $this->trustedServers = new TrustedServers( @@ -85,8 +65,8 @@ class TrustedServersTest extends TestCase { } public function testAddServer(): void { - /** @var \PHPUnit\Framework\MockObject\MockObject|TrustedServers $trustedServers */ - $trustedServers = $this->getMockBuilder('OCA\Federation\TrustedServers') + /** @var TrustedServers&MockObject $trustedServers */ + $trustedServers = $this->getMockBuilder(TrustedServers::class) ->setConstructorArgs( [ $this->dbHandler, @@ -99,7 +79,7 @@ class TrustedServersTest extends TestCase { $this->timeFactory ] ) - ->setMethods(['normalizeUrl', 'updateProtocol']) + ->onlyMethods(['updateProtocol']) ->getMock(); $trustedServers->expects($this->once())->method('updateProtocol') ->with('url')->willReturn('https://url'); @@ -112,12 +92,12 @@ class TrustedServersTest extends TestCase { ->willReturn('token'); $this->dbHandler->expects($this->once())->method('addToken')->with('https://url', 'token'); $this->jobList->expects($this->once())->method('add') - ->with('OCA\Federation\BackgroundJob\RequestSharedSecret', + ->with(RequestSharedSecret::class, ['url' => 'https://url', 'token' => 'token', 'created' => 1234567]); $this->assertSame( - $trustedServers->addServer('url'), - 1 + 1, + $trustedServers->addServer('url') ); } @@ -196,8 +176,8 @@ class TrustedServersTest extends TestCase { public function testIsNextcloudServer(int $statusCode, bool $isValidNextcloudVersion, bool $expected): void { $server = 'server1'; - /** @var \PHPUnit\Framework\MockObject\MockObject | TrustedServers $trustedServers */ - $trustedServers = $this->getMockBuilder('OCA\Federation\TrustedServers') + /** @var TrustedServers&MockObject $trustedServers */ + $trustedServers = $this->getMockBuilder(TrustedServers::class) ->setConstructorArgs( [ $this->dbHandler, @@ -210,7 +190,7 @@ class TrustedServersTest extends TestCase { $this->timeFactory ] ) - ->setMethods(['checkNextcloudVersion']) + ->onlyMethods(['checkNextcloudVersion']) ->getMock(); $this->httpClientService->expects($this->once())->method('newClient') @@ -236,7 +216,7 @@ class TrustedServersTest extends TestCase { ); } - public function dataTestIsNextcloudServer(): array { + public static function dataTestIsNextcloudServer(): array { return [ [200, true, true], [200, false, false], @@ -244,19 +224,17 @@ class TrustedServersTest extends TestCase { ]; } - /** - * @expectedExceptionMessage simulated exception - */ public function testIsNextcloudServerFail(): void { $server = 'server1'; - $this->httpClientService->expects($this->once())->method('newClient') + $this->httpClientService->expects($this->once()) + ->method('newClient') ->willReturn($this->httpClient); - $this->httpClient->expects($this->once())->method('get')->with($server . '/status.php') - ->willReturnCallback(function (): void { - throw new \Exception('simulated exception'); - }); + $this->httpClient->expects($this->once()) + ->method('get') + ->with($server . '/status.php') + ->willThrowException(new \Exception('simulated exception')); $this->assertFalse($this->trustedServers->isNextcloudServer($server)); } @@ -264,11 +242,11 @@ class TrustedServersTest extends TestCase { /** * @dataProvider dataTestCheckNextcloudVersion */ - public function testCheckNextcloudVersion($status): void { - $this->assertTrue($this->invokePrivate($this->trustedServers, 'checkNextcloudVersion', [$status])); + public function testCheckNextcloudVersion(string $status): void { + $this->assertTrue(self::invokePrivate($this->trustedServers, 'checkNextcloudVersion', [$status])); } - public function dataTestCheckNextcloudVersion(): array { + public static function dataTestCheckNextcloudVersion(): array { return [ ['{"version":"9.0.0"}'], ['{"version":"9.1.0"}'] @@ -282,10 +260,10 @@ class TrustedServersTest extends TestCase { $this->expectException(HintException::class); $this->expectExceptionMessage('Remote server version is too low. 9.0 is required.'); - $this->invokePrivate($this->trustedServers, 'checkNextcloudVersion', [$status]); + self::invokePrivate($this->trustedServers, 'checkNextcloudVersion', [$status]); } - public function dataTestCheckNextcloudVersionTooLow(): array { + public static function dataTestCheckNextcloudVersionTooLow(): array { return [ ['{"version":"8.2.3"}'], ]; @@ -296,11 +274,11 @@ class TrustedServersTest extends TestCase { */ public function testUpdateProtocol(string $url, string $expected): void { $this->assertSame($expected, - $this->invokePrivate($this->trustedServers, 'updateProtocol', [$url]) + self::invokePrivate($this->trustedServers, 'updateProtocol', [$url]) ); } - public function dataTestUpdateProtocol(): array { + public static function dataTestUpdateProtocol(): array { return [ ['http://owncloud.org', 'http://owncloud.org'], ['https://owncloud.org', 'https://owncloud.org'], |