Skip to content

Commit 0f69345

Browse files
committed
Code cleanup for unit-test.
1 parent ffe2955 commit 0f69345

1 file changed

Lines changed: 50 additions & 45 deletions

File tree

tests/unit/ServerTest.php

Lines changed: 50 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,13 @@
22
/**
33
* Unit Test for the Server class
44
*/
5+
56
namespace Pdsinterop\Solid\Resources;
67

78
use ArgumentCountError;
89
use EasyRdf\Graph;
910
use Laminas\Diactoros\Response;
11+
use Laminas\Diactoros\ServerRequest;
1012
use League\Flysystem\FilesystemInterface;
1113
use PHPUnit\Framework\TestCase;
1214
use Psr\Http\Message\ResponseInterface;
@@ -15,12 +17,24 @@
1517
/**
1618
* @covers \Pdsinterop\Solid\Resources\Server
1719
* @coversDefaultClass \Pdsinterop\Solid\Resources\Server
20+
*
21+
* @uses \Laminas\Diactoros\Response
22+
* @uses \Laminas\Diactoros\ServerRequest
23+
* @uses \Pdsinterop\Solid\Resources\Exception
1824
* @uses \Pdsinterop\Solid\Resources\Server
1925
*/
2026
class ServerTest extends TestCase
2127
{
28+
////////////////////////////////// FIXTURES \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
29+
2230
const MOCK_SLUG = 'Mock Slug';
2331

32+
/////////////////////////////////// TESTS \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
33+
const MOCK_BODY = 'php://temp';
34+
const MOCK_URL = '';
35+
const MOCK_SERVER_PARAMS = [];
36+
const MOCK_UPLOADED_FILES = [];
37+
2438
/** @testdox Server should complain when instantiated without File System */
2539
public function testInstatiationWithoutFileSystem()
2640
{
@@ -93,19 +107,16 @@ public function testRespondToRequestWithoutRequest()
93107
*
94108
* @covers ::respondToRequest
95109
*
96-
* @uses \Pdsinterop\Solid\Resources\Exception
97-
*
98110
* @dataProvider provideUnsupportedHttpMethods
99111
*/
100112
public function testRespondToRequestWithUnsupportedHttpMethod($httpMethod)
101113
{
102114
// Arrange
103115
$mockFileSystem = $this->getMockBuilder(FilesystemInterface::class)->getMock();
104116
$mockGraph = $this->getMockBuilder(Graph::class)->getMock();
105-
$mockRequest = $this->getMockBuilder(ServerRequestInterface::class)->getMock();
106-
$mockResponse = $this->getMockBuilder(ResponseInterface::class)->getMock();
117+
$request = $this->createRequest($httpMethod);
107118

108-
$mockRequest->method('getMethod')->willReturn($httpMethod);
119+
$mockResponse = new Response();
109120

110121
$server = new Server($mockFileSystem, $mockResponse, $mockGraph);
111122

@@ -114,22 +125,19 @@ public function testRespondToRequestWithUnsupportedHttpMethod($httpMethod)
114125
$this->expectExceptionMessage('Unknown or unsupported HTTP METHOD');
115126

116127
// Act
117-
$server->respondToRequest($mockRequest);
128+
$server->respondToRequest($request);
118129
}
119130

120131
/**
121132
* @testdox Server should create a resource when asked to create a resource with Slug header present
122133
*
123134
* @covers ::respondToRequest
124135
*
125-
* @uses \Pdsinterop\Solid\Resources\Exception
126-
*
127136
* @dataProvider provideMimeTypes
128137
*/
129138
public function testRespondToPOSTCreateRequest($mimetype)
130139
{
131-
$expectedName = self::MOCK_SLUG . self::MOCK_SLUG;
132-
140+
$expected = self::MOCK_SLUG . self::MOCK_SLUG;
133141
$extensions = [
134142
'application/json' => '.json',
135143
'application/ld+json' => '.json',
@@ -151,63 +159,48 @@ public function testRespondToPOSTCreateRequest($mimetype)
151159
152160
For instance 'example.json.ttl' or 'example.ttl.json'.
153161
/*/
154-
$expectedName .= $extensions[$mimetype];
162+
$expected .= $extensions[$mimetype];
155163
}
156164

157165
// Arrange
158-
$expected = new Response(); //@CHECKME: Use mock? $this->getMockBuilder(ResponseInterface::class)->getMock();
159166
$mockFileSystem = $this->getMockBuilder(FilesystemInterface::class)->getMock();
160167
$mockGraph = $this->getMockBuilder(Graph::class)->getMock();
161-
$mockRequest = $this->getMockBuilder(ServerRequestInterface::class)->getMock();
162-
163-
$mockRequest->method('getMethod')->willReturn('POST');
164-
165-
$mockRequest->expects($this->exactly(3))
166-
->method('getHeaderLine')
167-
->willReturnMap([
168-
['Content-Type', $mimetype],
169-
['Link', ''],
170-
['Slug', self::MOCK_SLUG],
171-
]);
172-
173-
$mockRequest->expects($this->exactly(1))
174-
->method('hasHeader')
175-
->with('Slug')
176-
->willReturn(true);
177-
178-
$mockRequest->expects($this->exactly(1))
179-
->method('getHeader')
180-
->with('Slug')
181-
->willReturn([self::MOCK_SLUG, 'Second Mock Slug']);
168+
$request = $this->createRequest('POST', [
169+
'Content-Type' => $mimetype,
170+
'Link' => '',
171+
'Slug' => self::MOCK_SLUG,
172+
]);
182173

183-
$mockFileSystem->expects($this->exactly(2))
174+
$mockFileSystem
184175
->method('has')
185176
->withAnyParameters()
186177
->willReturnMap([
187178
[self::MOCK_SLUG, true],
188-
[$expectedName, false],
179+
[$expected, false],
189180
]);
190181

191-
$mockFileSystem->expects($this->exactly(1))
192-
->method('write')
193-
->with($expectedName, '', [])
194-
->willReturn(true);
195-
196-
$mockFileSystem->expects($this->exactly(1))
182+
$mockFileSystem
197183
->method('getMimetype')
198184
->with(self::MOCK_SLUG)
199185
->willReturn(Server::MIME_TYPE_DIRECTORY);
200186

201-
$server = new Server($mockFileSystem, $expected, $mockGraph);
187+
$mockFileSystem
188+
->method('write')
189+
->with($expected, '', [])
190+
->willReturn(true);
202191

203192
// Act
204-
$response = $server->respondToRequest($mockRequest);
193+
$server = new Server($mockFileSystem, new Response(), $mockGraph);
194+
$response = $server->respondToRequest($request);
205195

196+
// Assert
206197
$actual = $response->getHeaderLine('Location');
207198

208-
$this->assertEquals($expectedName, $actual);
199+
$this->assertEquals($expected, $actual);
209200
}
210201

202+
/////////////////////////////// DATAPROVIDERS \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
203+
211204
public static function provideMimeTypes()
212205
{
213206
return [
@@ -224,10 +217,22 @@ public static function provideMimeTypes()
224217
public static function provideUnsupportedHttpMethods()
225218
{
226219
return [
227-
'string:(empty)' => [''],
228220
'string:CONNECT' => ['CONNECT'],
229221
'string:TRACE' => ['TRACE'],
230222
'string:UNKNOWN' => ['UNKNOWN'],
231223
];
232224
}
225+
226+
////////////////////////////// MOCKS AND STUBS \\\\\\\\\\\\\\\\\\\\\\\\\\\\\
227+
private function createRequest(string $httpMethod, array $headers = []): ServerRequestInterface
228+
{
229+
return new ServerRequest(
230+
self::MOCK_SERVER_PARAMS,
231+
self::MOCK_UPLOADED_FILES,
232+
self::MOCK_URL,
233+
$httpMethod,
234+
self::MOCK_BODY,
235+
$headers
236+
);
237+
}
233238
}

0 commit comments

Comments
 (0)