|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace OCA\Solid\Controller\ServerController; |
| 4 | + |
| 5 | +use OCA\Solid\AppInfo\Application; |
| 6 | +use OCA\Solid\BaseServerConfig; |
| 7 | +use OCA\Solid\Controller\ServerController; |
| 8 | +use OCA\Solid\Service\UserService; |
| 9 | +use OCP\IConfig; |
| 10 | +use OCP\IDBConnection; |
| 11 | +use OCP\IRequest; |
| 12 | +use OCP\ISession; |
| 13 | +use OCP\IURLGenerator; |
| 14 | +use OCP\IUserManager; |
| 15 | +use PHPUnit\Framework\TestCase; |
| 16 | + |
| 17 | +/** |
| 18 | + * @coversDefaultClass \OCA\Solid\Controller\ServerController |
| 19 | + * @covers ::__construct |
| 20 | + * |
| 21 | + * @uses \OCA\Solid\Controller\ServerController |
| 22 | + */ |
| 23 | +class ServerControllerTest extends TestCase |
| 24 | +{ |
| 25 | + ////////////////////////////////// FIXTURES \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\ |
| 26 | + |
| 27 | + private const MOCK_CLIENT_ID = 'mock-client-id'; |
| 28 | + private static string $encryptionKey; |
| 29 | + private static string $privateKey; |
| 30 | + private static string $publicKey; |
| 31 | + |
| 32 | + |
| 33 | + public static function setUpBeforeClass(): void |
| 34 | + { |
| 35 | + $keyPath = __DIR__ . '/../../fixtures/keys'; |
| 36 | + self::$privateKey = file_get_contents($keyPath . '/private.key'); |
| 37 | + } |
| 38 | + /////////////////////////////////// TESTS \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\ |
| 39 | + |
| 40 | + /** |
| 41 | + * @testdox ServerController should complain when constructed without required parameter |
| 42 | + * |
| 43 | + * @covers ::__construct |
| 44 | + * |
| 45 | + * @dataProvider provideConstructorParameterIndex |
| 46 | + */ |
| 47 | + public function testInstatiationWithoutRequiredParameter($index) |
| 48 | + { |
| 49 | + $parameters = [ |
| 50 | + 'mock appname', |
| 51 | + $this->createMock(IRequest::class), |
| 52 | + $this->createMock(ISession::class), |
| 53 | + $this->createMock(IUserManager::class), |
| 54 | + $this->createMock(IURLGenerator::class), |
| 55 | + 'mock user id', |
| 56 | + $this->createMock(IConfig::class), |
| 57 | + $this->createMock(UserService::class), |
| 58 | + $this->createMock(IDBConnection::class), |
| 59 | + ]; |
| 60 | + |
| 61 | + $parameters = array_slice($parameters, 0, $index); |
| 62 | + |
| 63 | + $this->expectException(\ArgumentCountError::class); |
| 64 | + $message = vsprintf( |
| 65 | + 'Too few arguments to function %s::%s, %s passed in %s on line %s and exactly %s expected', |
| 66 | + [ |
| 67 | + 'class' => preg_quote('OCA\Solid\Controller\ServerController', '/'), |
| 68 | + 'method' => preg_quote('__construct()', '/'), |
| 69 | + 'index' => $index, |
| 70 | + 'file' => '.*', |
| 71 | + 'line' => '\d+', |
| 72 | + 'count' => 9, |
| 73 | + |
| 74 | + ] |
| 75 | + ); |
| 76 | + |
| 77 | + $this->expectExceptionMessageMatches('/^' . $message . '$/'); |
| 78 | + |
| 79 | + new ServerController(...$parameters); |
| 80 | + } |
| 81 | + |
| 82 | + /** |
| 83 | + * @testdox ServerController should be instantiable with all required parameters |
| 84 | + * |
| 85 | + * @covers ::__construct |
| 86 | + * |
| 87 | + * @uses \League\OAuth2\Server\AuthorizationServer |
| 88 | + * @uses \OCA\Solid\BaseServerConfig |
| 89 | + * @uses \OCA\Solid\JtiReplayDetector |
| 90 | + * @uses \OCA\Solid\ServerConfig |
| 91 | + * @uses \Pdsinterop\Solid\Auth\Factory\AuthorizationServerFactory |
| 92 | + * @uses \Pdsinterop\Solid\Auth\Factory\GrantTypeFactory |
| 93 | + * @uses \Pdsinterop\Solid\Auth\Factory\RepositoryFactory |
| 94 | + * @uses \Pdsinterop\Solid\Auth\TokenGenerator |
| 95 | + */ |
| 96 | + public function testInstatiation() |
| 97 | + { |
| 98 | + $configMock = $this->createMock(IConfig::class); |
| 99 | + |
| 100 | + $configMock->method('getAppValue')->willReturnMap([ |
| 101 | + [Application::APP_ID, 'client-' . self::MOCK_CLIENT_ID, '{}', 'return' => '{}'], |
| 102 | + [Application::APP_ID, 'client-d6d7896757f61ac4c397d914053180ff', '{}', 'return' => '{}'], |
| 103 | + [Application::APP_ID, 'client-', '{}', 'return' => '{}'], |
| 104 | + [Application::APP_ID, 'profileData', '', 'return' => ''], |
| 105 | + [Application::APP_ID, 'encryptionKey', '', 'return' => 'mock encryption key'], |
| 106 | + [Application::APP_ID, 'privateKey', '', 'return' => self::$privateKey], |
| 107 | + ]); |
| 108 | + |
| 109 | + $parameters = [ |
| 110 | + 'mock appname', |
| 111 | + $this->createMock(IRequest::class), |
| 112 | + $this->createMock(ISession::class), |
| 113 | + $this->createMock(IUserManager::class), |
| 114 | + $this->createMock(IURLGenerator::class), |
| 115 | + 'mock user id', |
| 116 | + $configMock, |
| 117 | + $this->createMock(UserService::class), |
| 118 | + $this->createMock(IDBConnection::class), |
| 119 | + ]; |
| 120 | + |
| 121 | + $controller = new ServerController(...$parameters); |
| 122 | + |
| 123 | + $this->assertInstanceOf(ServerController::class, $controller); |
| 124 | + } |
| 125 | + |
| 126 | + /////////////////////////////// DATAPROVIDERS \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\ |
| 127 | + |
| 128 | + public static function provideConstructorParameterIndex() |
| 129 | + { |
| 130 | + return [ |
| 131 | + 'appName' => [0], |
| 132 | + 'request' => [1], |
| 133 | + 'session' => [2], |
| 134 | + 'userManager' => [3], |
| 135 | + 'urlGenerator' => [4], |
| 136 | + 'userId' => [5], |
| 137 | + 'config' => [6], |
| 138 | + 'userService' => [7], |
| 139 | + 'connection' => [8], |
| 140 | + ]; |
| 141 | + } |
| 142 | +} |
0 commit comments