|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Pdsinterop\Solid\Resources; |
| 4 | + |
| 5 | +use League\Flysystem\FilesystemInterface; |
| 6 | +use Pdsinterop\Rdf\Flysystem\Plugin\AsMime; |
| 7 | +use PHPUnit\Framework\MockObject\MockObject; |
| 8 | +use PHPUnit\Framework\TestCase; |
| 9 | +use Psr\Http\Message\ResponseInterface; |
| 10 | +use Psr\Http\Message\ServerRequestInterface; |
| 11 | +use Psr\Http\Message\StreamInterface; |
| 12 | +use Psr\Http\Message\UriInterface; |
| 13 | +use TypeError; |
| 14 | + |
| 15 | +/** |
| 16 | + * @coversDefaultClass \Pdsinterop\Solid\Resources\Server |
| 17 | + * @covers \Pdsinterop\Solid\Resources\Server |
| 18 | + */ |
| 19 | +class ServerTest extends TestCase |
| 20 | +{ |
| 21 | + ////////////////////////////////// FIXTURES \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\ |
| 22 | + |
| 23 | + const MOCK_HTTP_METHOD = 'MOCK'; |
| 24 | + const MOCK_PATH = '/mock/path'; |
| 25 | + |
| 26 | + /////////////////////////////////// TESTS \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\ |
| 27 | + |
| 28 | + /** |
| 29 | + * @testdox Server should complain when instantiated without a filesystem |
| 30 | + * @covers ::__construct |
| 31 | + */ |
| 32 | + public function testServerConstructWithoutFilesystem() |
| 33 | + { |
| 34 | + $this->expectException(TypeError::class); |
| 35 | + $this->expectExceptionMessageMatches('/Too few arguments .+ 0 passed/'); |
| 36 | + |
| 37 | + new Server(); |
| 38 | + } |
| 39 | + |
| 40 | + /** |
| 41 | + * @testdox Server should complain when instantiated without a response |
| 42 | + * @covers ::__construct |
| 43 | + */ |
| 44 | + public function testServerConstructWithoutResponse() |
| 45 | + { |
| 46 | + $this->expectException(TypeError::class); |
| 47 | + $this->expectExceptionMessageMatches('/Too few arguments .+ 1 passed/'); |
| 48 | + |
| 49 | + $mockFilesystem = $this->createMock(FilesystemInterface::class); |
| 50 | + |
| 51 | + new Server($mockFilesystem); |
| 52 | + } |
| 53 | + |
| 54 | + /** |
| 55 | + * @testdox Server should be instantiated when given a filesystem and a response |
| 56 | + * @covers ::__construct |
| 57 | + */ |
| 58 | + public function testServerConstructWithFilesystemAndResponse() |
| 59 | + { |
| 60 | + $mockFilesystem = $this->createMock(FilesystemInterface::class); |
| 61 | + $mockResponse = $this->createMock(ResponseInterface::class); |
| 62 | + |
| 63 | + $server = new Server($mockFilesystem, $mockResponse); |
| 64 | + |
| 65 | + $this->assertInstanceOf(Server::class, $server); |
| 66 | + } |
| 67 | + |
| 68 | + /** |
| 69 | + * @testdox Server should complain when asked to RespondToRequest without a request |
| 70 | + * @covers ::respondToRequest |
| 71 | + */ |
| 72 | + public function testServerRespondToRequestWithoutRequest() |
| 73 | + { |
| 74 | + $this->expectException(TypeError::class); |
| 75 | + $this->expectExceptionMessageMatches('/Too few arguments .+ 0 passed/'); |
| 76 | + |
| 77 | + $mockFilesystem = $this->createMock(FilesystemInterface::class); |
| 78 | + $mockResponse = $this->createMock(ResponseInterface::class); |
| 79 | + |
| 80 | + $server = new Server($mockFilesystem, $mockResponse); |
| 81 | + |
| 82 | + $server->respondToRequest(); |
| 83 | + } |
| 84 | + |
| 85 | + /** |
| 86 | + * @testdox Server should complain when asked to RespondToRequest with a request with an unknown HTTP method |
| 87 | + * |
| 88 | + * @covers ::respondToRequest |
| 89 | + * |
| 90 | + * @uses \Pdsinterop\Solid\Resources\Exception |
| 91 | + */ |
| 92 | + public function testServerRespondToRequestWithUnknownHttpMethod() |
| 93 | + { |
| 94 | + // Assert |
| 95 | + $this->expectException(Exception::class); |
| 96 | + $this->expectExceptionMessage(vsprintf(Server::ERROR_UNKNOWN_HTTP_METHOD, [self::MOCK_HTTP_METHOD])); |
| 97 | + |
| 98 | + // Arrange |
| 99 | + $mockRequest = $this->createMockRequest(); |
| 100 | + $mockResponse = $this->createMock(ResponseInterface::class); |
| 101 | + $mockFilesystem = $this->createMockFilesystem(); |
| 102 | + |
| 103 | + //Act |
| 104 | + $server = new Server($mockFilesystem, $mockResponse); |
| 105 | + $server->respondToRequest($mockRequest); |
| 106 | + } |
| 107 | + |
| 108 | + /** |
| 109 | + * @testdox Server should return provided response when asked to RespondToRequest with va lid request |
| 110 | + * |
| 111 | + * @covers ::respondToRequest |
| 112 | + */ |
| 113 | + public function testServerRespondToRequestWithRequest() |
| 114 | + { |
| 115 | + // Arrange |
| 116 | + $mockFilesystem = $this->createMockFilesystem(); |
| 117 | + $mockRequest = $this->createMockRequest('GET'); |
| 118 | + $expected = $this->createMockResponse(); |
| 119 | + |
| 120 | + // Act |
| 121 | + $server = new Server($mockFilesystem, $expected); |
| 122 | + $actual = $server->respondToRequest($mockRequest); |
| 123 | + |
| 124 | + // Assert |
| 125 | + $this->assertSame($expected, $actual); |
| 126 | + } |
| 127 | + |
| 128 | + ////////////////////////////// MOCKS AND STUBS \\\\\\\\\\\\\\\\\\\\\\\\\\\\\ |
| 129 | + |
| 130 | + public function createMockFilesystem(): FilesystemInterface|MockObject |
| 131 | + { |
| 132 | + $mockFilesystem = $this->getMockBuilder(FilesystemInterface::class) |
| 133 | + ->onlyMethods([ |
| 134 | + 'addPlugin', 'copy', 'createDir', 'delete', 'deleteDir', 'get', 'getMetadata', 'getMimetype', 'getSize', 'getTimestamp', 'getVisibility', 'has', 'listContents', 'put', 'putStream', 'read', 'readAndDelete', 'readStream', 'rename', 'setVisibility', 'update', 'updateStream', 'write', 'writeStream' |
| 135 | + ]) |
| 136 | + ->addMethods(['asMime']) |
| 137 | + ->getMock(); |
| 138 | + |
| 139 | + $mockAsMime = $this->getMockBuilder(AsMime::class) |
| 140 | + // ->onlyMethods(['getMimetype', 'getSize', 'getTimestamp']) |
| 141 | + ->addMethods(['has']) |
| 142 | + ->disableOriginalConstructor() |
| 143 | + ->getMock(); |
| 144 | + |
| 145 | + $mockFilesystem->method('asMime')->willReturn($mockAsMime); |
| 146 | + |
| 147 | + return $mockFilesystem; |
| 148 | + } |
| 149 | + |
| 150 | + public function createMockRequest($httpMethod = self::MOCK_HTTP_METHOD): ServerRequestInterface|MockObject |
| 151 | + { |
| 152 | + $mockRequest = $this->createMock(ServerRequestInterface::class); |
| 153 | + |
| 154 | + $mockUri = $this->createMock(UriInterface::class); |
| 155 | + $mockUri->method('getPath')->willReturn(self::MOCK_PATH); |
| 156 | + |
| 157 | + $mockBody = $this->createMock(StreamInterface::class); |
| 158 | + |
| 159 | + $mockRequest->method('getUri')->willReturn($mockUri); |
| 160 | + $mockRequest->method('getQueryParams')->willReturn([]); |
| 161 | + $mockRequest->method('getMethod')->willReturn($httpMethod); |
| 162 | + $mockRequest->method('getBody')->willReturn($mockBody); |
| 163 | + // $mockRequest->method('getMethod')->willReturn('GET'); |
| 164 | + $mockRequest->method('getHeaderLine')->willReturn(''); |
| 165 | + |
| 166 | + return $mockRequest; |
| 167 | + } |
| 168 | + |
| 169 | + public function createMockResponse(): ResponseInterface|MockObject |
| 170 | + { |
| 171 | + $mockResponse = $this->createMock(ResponseInterface::class); |
| 172 | + $mockBody = $this->createMock(StreamInterface::class); |
| 173 | + |
| 174 | + $mockResponse->method('getBody')->willReturn($mockBody); |
| 175 | + $mockResponse->method('withStatus')->willReturnSelf(); |
| 176 | + |
| 177 | + return $mockResponse; |
| 178 | + } |
| 179 | +} |
0 commit comments