|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Unit Test for the Server class |
| 4 | + */ |
| 5 | + |
| 6 | +namespace Pdsinterop\Solid\Resources; |
| 7 | + |
| 8 | +use ArgumentCountError; |
| 9 | +use EasyRdf\Graph; |
| 10 | +use Laminas\Diactoros\Response; |
| 11 | +use Laminas\Diactoros\ServerRequest; |
| 12 | +use League\Flysystem\FilesystemInterface; |
| 13 | +use PHPUnit\Framework\TestCase; |
| 14 | +use Psr\Http\Message\ResponseInterface; |
| 15 | +use Psr\Http\Message\ServerRequestInterface; |
| 16 | + |
| 17 | +/** |
| 18 | + * @covers \Pdsinterop\Solid\Resources\Server |
| 19 | + * @coversDefaultClass \Pdsinterop\Solid\Resources\Server |
| 20 | + * |
| 21 | + * @uses \Laminas\Diactoros\Response |
| 22 | + * @uses \Laminas\Diactoros\ServerRequest |
| 23 | + * @uses \Pdsinterop\Solid\Resources\Exception |
| 24 | + * @uses \Pdsinterop\Solid\Resources\Server |
| 25 | + */ |
| 26 | +class ServerTest extends TestCase |
| 27 | +{ |
| 28 | + ////////////////////////////////// FIXTURES \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\ |
| 29 | + |
| 30 | + const MOCK_BODY = 'php://temp'; |
| 31 | + const MOCK_PATH = '/path/to/resource/'; |
| 32 | + const MOCK_SERVER_PARAMS = []; |
| 33 | + const MOCK_UPLOADED_FILES = []; |
| 34 | + const MOCK_URL = 'https://example.com' . self::MOCK_PATH; |
| 35 | + |
| 36 | + /////////////////////////////////// TESTS \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\ |
| 37 | + |
| 38 | + /** @testdox Server should complain when instantiated without File System */ |
| 39 | + public function testInstatiationWithoutFileSystem() |
| 40 | + { |
| 41 | + $this->expectException(ArgumentCountError::class); |
| 42 | + $this->expectExceptionMessageMatches('/Too few arguments .+ 0 passed/'); |
| 43 | + |
| 44 | + new Server(); |
| 45 | + } |
| 46 | + |
| 47 | + /** @testdox Server should complain when instantiated without Response */ |
| 48 | + public function testInstatiationWithoutResponse() |
| 49 | + { |
| 50 | + $this->expectException(ArgumentCountError::class); |
| 51 | + $this->expectExceptionMessageMatches('/Too few arguments .+ 1 passed/'); |
| 52 | + |
| 53 | + $mockFileSystem = $this->getMockBuilder(FilesystemInterface::class)->getMock(); |
| 54 | + |
| 55 | + new Server($mockFileSystem); |
| 56 | + } |
| 57 | + |
| 58 | + /** @testdox Server should be instantiated when constructed without Graph */ |
| 59 | + public function testInstatiationWithoutGraph() |
| 60 | + { |
| 61 | + $mockFileSystem = $this->getMockBuilder(FilesystemInterface::class)->getMock(); |
| 62 | + $mockResponse = $this->getMockBuilder(ResponseInterface::class)->getMock(); |
| 63 | + |
| 64 | + $actual = new Server($mockFileSystem, $mockResponse); |
| 65 | + $expected = Server::class; |
| 66 | + |
| 67 | + $this->assertInstanceOf($expected, $actual); |
| 68 | + } |
| 69 | + |
| 70 | + /** @testdox Server should be instantiated when constructed with Graph */ |
| 71 | + public function testInstatiationWithGraph() |
| 72 | + { |
| 73 | + $mockFileSystem = $this->getMockBuilder(FilesystemInterface::class)->getMock(); |
| 74 | + $mockResponse = $this->getMockBuilder(ResponseInterface::class)->getMock(); |
| 75 | + $mockGraph = $this->getMockBuilder(Graph::class)->getMock(); |
| 76 | + |
| 77 | + $actual = new Server($mockFileSystem, $mockResponse, $mockGraph); |
| 78 | + $expected = Server::class; |
| 79 | + |
| 80 | + $this->assertInstanceOf($expected, $actual); |
| 81 | + } |
| 82 | + |
| 83 | + /** |
| 84 | + * @testdox Server should complain when asked to respond to a request without a Request |
| 85 | + * |
| 86 | + * @covers ::respondToRequest |
| 87 | + */ |
| 88 | + public function testRespondToRequestWithoutRequest() |
| 89 | + { |
| 90 | + // Arrange |
| 91 | + $mockFileSystem = $this->getMockBuilder(FilesystemInterface::class)->getMock(); |
| 92 | + $mockResponse = $this->getMockBuilder(ResponseInterface::class)->getMock(); |
| 93 | + $mockGraph = $this->getMockBuilder(Graph::class)->getMock(); |
| 94 | + |
| 95 | + $server = new Server($mockFileSystem, $mockResponse, $mockGraph); |
| 96 | + |
| 97 | + // Assert |
| 98 | + $this->expectException(ArgumentCountError::class); |
| 99 | + $this->expectExceptionMessageMatches('/Too few arguments .+ 0 passed/'); |
| 100 | + |
| 101 | + // Act |
| 102 | + $server->respondToRequest(); |
| 103 | + } |
| 104 | + |
| 105 | + /** |
| 106 | + * @testdox Server should complain when asked to respond to a Request with an unsupported HTTP METHOD |
| 107 | + * |
| 108 | + * @covers ::respondToRequest |
| 109 | + * |
| 110 | + * @dataProvider provideUnsupportedHttpMethods |
| 111 | + */ |
| 112 | + public function testRespondToRequestWithUnsupportedHttpMethod($httpMethod) |
| 113 | + { |
| 114 | + // Arrange |
| 115 | + $mockFileSystem = $this->getMockBuilder(FilesystemInterface::class)->getMock(); |
| 116 | + $mockGraph = $this->getMockBuilder(Graph::class)->getMock(); |
| 117 | + $request = $this->createRequest($httpMethod); |
| 118 | + |
| 119 | + $mockResponse = new Response(); |
| 120 | + |
| 121 | + $server = new Server($mockFileSystem, $mockResponse, $mockGraph); |
| 122 | + |
| 123 | + // Assert |
| 124 | + $this->expectException(Exception::class); |
| 125 | + $this->expectExceptionMessage('Unknown or unsupported HTTP METHOD'); |
| 126 | + |
| 127 | + // Act |
| 128 | + $server->respondToRequest($request); |
| 129 | + } |
| 130 | + |
| 131 | + /** |
| 132 | + * @testdox Server should create a resource when asked to create a resource with Slug header present |
| 133 | + * |
| 134 | + * @covers ::respondToRequest |
| 135 | + * |
| 136 | + * @dataProvider provideSlugs |
| 137 | + */ |
| 138 | + public function testRespondToPOSTCreateRequest($slug, $mimetype, $expected) |
| 139 | + { |
| 140 | + // Arrange |
| 141 | + $mockFileSystem = $this->getMockBuilder(FilesystemInterface::class)->getMock(); |
| 142 | + $mockGraph = $this->getMockBuilder(Graph::class)->getMock(); |
| 143 | + $request = $this->createRequest('POST', [ |
| 144 | + 'Content-Type' => $mimetype, |
| 145 | + 'Link' => '', |
| 146 | + 'Slug' => $slug, |
| 147 | + ]); |
| 148 | + |
| 149 | + $mockFileSystem |
| 150 | + ->method('has') |
| 151 | + ->withAnyParameters() |
| 152 | + ->willReturnMap([ |
| 153 | + [self::MOCK_PATH, true], |
| 154 | + ]); |
| 155 | + |
| 156 | + $mockFileSystem |
| 157 | + ->method('getMimetype') |
| 158 | + ->with(self::MOCK_PATH) |
| 159 | + ->willReturn(Server::MIME_TYPE_DIRECTORY); |
| 160 | + |
| 161 | + $mockFileSystem |
| 162 | + ->method('write') |
| 163 | + ->withAnyParameters() |
| 164 | + ->willReturn(true); |
| 165 | + |
| 166 | + // Act |
| 167 | + $server = new Server($mockFileSystem, new Response(), $mockGraph); |
| 168 | + $response = $server->respondToRequest($request); |
| 169 | + |
| 170 | + // Assert |
| 171 | + $actual = $response->getHeaderLine('Location'); |
| 172 | + |
| 173 | + $this->assertEquals(self::MOCK_PATH . $expected, $actual); |
| 174 | + } |
| 175 | + |
| 176 | + /////////////////////////////// DATAPROVIDERS \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\ |
| 177 | + |
| 178 | + public static function provideSlugs() |
| 179 | + { |
| 180 | + return [ |
| 181 | + // '' => [$slug, $mimetype, $expectedFilename], |
| 182 | + 'Slug with json extension, with ld+json MIME' => ['Mock Slug.json', 'application/ld+json', 'Mock Slug.json'], |
| 183 | + 'Slug with jsonld extension, with ld+json MIME' => ['Mock Slug.jsonld', 'application/ld+json', 'Mock Slug.jsonld'], |
| 184 | + 'Slug with PNG extension, with PNG MIME' => ['Mock Slug.png', 'image/png', 'Mock Slug.png'], |
| 185 | + 'Slug with some other, extension) with Turtle MIME' => ['Mock Slug.other', 'text/turtle', 'Mock Slug.other'], |
| 186 | + 'Slug with Turtle extension, with other MIME' => ['Mock Slug.ttl', 'some/other', 'Mock Slug.ttl'], |
| 187 | + 'Slug with Turtle extension, with Turtle MIME' => ['Mock Slug.ttl', 'text/turtle', 'Mock Slug.ttl'], |
| 188 | + 'Slug without extension, with some other MIME' => ['Mock Slug', 'some/other', 'Mock Slug'], |
| 189 | + 'Slug without extension, with turtle MIME' => ['Mock Slug', 'text/turtle', 'Mock Slug'], |
| 190 | + ]; |
| 191 | + } |
| 192 | + |
| 193 | + public static function provideUnsupportedHttpMethods() |
| 194 | + { |
| 195 | + return [ |
| 196 | + 'string:CONNECT' => ['CONNECT'], |
| 197 | + 'string:TRACE' => ['TRACE'], |
| 198 | + 'string:UNKNOWN' => ['UNKNOWN'], |
| 199 | + ]; |
| 200 | + } |
| 201 | + |
| 202 | + ////////////////////////////// MOCKS AND STUBS \\\\\\\\\\\\\\\\\\\\\\\\\\\\\ |
| 203 | + private function createRequest(string $httpMethod, array $headers = []): ServerRequestInterface |
| 204 | + { |
| 205 | + return new ServerRequest( |
| 206 | + self::MOCK_SERVER_PARAMS, |
| 207 | + self::MOCK_UPLOADED_FILES, |
| 208 | + self::MOCK_URL, |
| 209 | + $httpMethod, |
| 210 | + self::MOCK_BODY, |
| 211 | + $headers |
| 212 | + ); |
| 213 | + } |
| 214 | +} |
0 commit comments