1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
|
4
|
|
|
namespace Northwoods\Router; |
5
|
|
|
|
6
|
|
|
use Nyholm\Psr7\ServerRequest; |
7
|
|
|
use PHPUnit\Framework\TestCase; |
8
|
|
|
|
9
|
|
|
class RouterTest extends TestCase |
10
|
|
|
{ |
11
|
|
|
use CanMockHandler; |
12
|
|
|
|
13
|
|
|
/** @var Router */ |
14
|
|
|
private $router; |
15
|
|
|
|
16
|
|
|
public function setUp() |
17
|
|
|
{ |
18
|
|
|
$this->router = new Router(); |
19
|
|
|
} |
20
|
|
|
|
21
|
|
|
public function testExposesCollection(): void |
22
|
|
|
{ |
23
|
|
|
$this->assertCount(0, $this->router->routes()); |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
public function testCanHandleGetRequest(): void |
27
|
|
|
{ |
28
|
|
|
$this->router->get('users.list', '/users', $this->mockHandler(function ($request) { |
29
|
|
|
return $request->getAttributes() === []; |
30
|
|
|
})); |
31
|
|
|
|
32
|
|
|
$this->router->get('users.detail', '/users/{id}', $this->mockHandler(function ($request) { |
33
|
|
|
return $request->getAttribute('id') === '22'; |
34
|
|
|
})); |
35
|
|
|
|
36
|
|
|
$response = $this->router->handle(new ServerRequest('GET', '/users/22')); |
37
|
|
|
|
38
|
|
|
$this->assertEquals(200, $response->getStatusCode()); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
public function testCanHandlePostRequests(): void |
42
|
|
|
{ |
43
|
|
|
$this->router->post('users.create', '/users', $this->mockHandler()); |
44
|
|
|
|
45
|
|
|
$response = $this->router->handle(new ServerRequest('POST', '/users')); |
46
|
|
|
|
47
|
|
|
$this->assertEquals(200, $response->getStatusCode()); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
public function testCanHandlePutRequests(): void |
51
|
|
|
{ |
52
|
|
|
$this->router->put('users.update', '/users', $this->mockHandler()); |
53
|
|
|
|
54
|
|
|
$response = $this->router->handle(new ServerRequest('PUT', '/users')); |
55
|
|
|
|
56
|
|
|
$this->assertEquals(200, $response->getStatusCode()); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
public function testCanHandlePatchRequests(): void |
60
|
|
|
{ |
61
|
|
|
$this->router->patch('users.update', '/users', $this->mockHandler()); |
62
|
|
|
|
63
|
|
|
$response = $this->router->handle(new ServerRequest('PATCH', '/users')); |
64
|
|
|
|
65
|
|
|
$this->assertEquals(200, $response->getStatusCode()); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
public function testCanHandleDeleteRequests(): void |
69
|
|
|
{ |
70
|
|
|
$this->router->delete('users.delete', '/users/{id}', $this->mockHandler()); |
71
|
|
|
|
72
|
|
|
$response = $this->router->handle(new ServerRequest('DELETE', '/users/5')); |
73
|
|
|
|
74
|
|
|
$this->assertEquals(200, $response->getStatusCode()); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
public function testCanHandleHeadRequests(): void |
78
|
|
|
{ |
79
|
|
|
$this->router->head('users.check', '/users', $this->mockHandler()); |
80
|
|
|
|
81
|
|
|
$response = $this->router->handle(new ServerRequest('HEAD', '/users')); |
82
|
|
|
|
83
|
|
|
$this->assertEquals(200, $response->getStatusCode()); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
public function testCanHandleOptionsRequests(): void |
87
|
|
|
{ |
88
|
|
|
$this->router->options('users.links', '/users', $this->mockHandler()); |
89
|
|
|
|
90
|
|
|
$response = $this->router->handle(new ServerRequest('OPTIONS', '/users')); |
91
|
|
|
|
92
|
|
|
$this->assertEquals(200, $response->getStatusCode()); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
public function testCanDetectNotFoundRoutes(): void |
96
|
|
|
{ |
97
|
|
|
$response = $this->router->handle(new ServerRequest('POST', '/')); |
98
|
|
|
|
99
|
|
|
$this->assertEquals(404, $response->getStatusCode()); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
public function testCanDetectInvalidMethods(): void |
103
|
|
|
{ |
104
|
|
|
$this->router->get('users.list', '/users', $this->mockHandler()); |
105
|
|
|
$this->router->post('users.create', '/users', $this->mockHandler()); |
106
|
|
|
|
107
|
|
|
$response = $this->router->handle(new ServerRequest('DELETE', '/users')); |
108
|
|
|
|
109
|
|
|
$this->assertEquals(405, $response->getStatusCode()); |
110
|
|
|
|
111
|
|
|
$allow = preg_split('/,\s*/', $response->getHeaderLine('allow')); |
112
|
|
|
|
113
|
|
|
$this->assertContains('GET', $allow); |
114
|
|
|
$this->assertContains('POST', $allow); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
public function testCanGenerateUri(): void |
118
|
|
|
{ |
119
|
|
|
$this->router->patch('users.update', '/users/{id}', $this->mockHandler()); |
120
|
|
|
|
121
|
|
|
$uri = $this->router->uri('users.update', ['id' => 1]); |
122
|
|
|
|
123
|
|
|
$this->assertEquals('/users/1', $uri); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
public function testCanDetectMissingParametersWhenGeneratingUri(): void |
127
|
|
|
{ |
128
|
|
|
$this->router->patch('users.update', '/users/{id}', $this->mockHandler()); |
129
|
|
|
|
130
|
|
|
$this->expectException(Error\UriParametersMissingException::class); |
131
|
|
|
$this->expectExceptionMessage('[id]'); |
132
|
|
|
|
133
|
|
|
$this->router->uri('users.update'); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
public function testCanDetectInvalidParameterWhenGeneratingUri(): void |
137
|
|
|
{ |
138
|
|
|
$this->router->post('hello', '/hello/{name:a-z+}', $this->mockHandler()); |
139
|
|
|
|
140
|
|
|
$this->expectException(Error\UriParameterInvalidException::class); |
141
|
|
|
$this->expectExceptionMessage('[name]'); |
142
|
|
|
|
143
|
|
|
$this->router->uri('hello', ['name' => 1]); |
144
|
|
|
} |
145
|
|
|
} |
146
|
|
|
|