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