1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Sunrise\Http\ServerRequest\Tests; |
4
|
|
|
|
5
|
|
|
use PHPUnit\Framework\TestCase; |
6
|
|
|
use Psr\Http\Message\ResponseInterface; |
7
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
8
|
|
|
use Psr\Http\Server\MiddlewareInterface; |
9
|
|
|
use Psr\Http\Server\RequestHandlerInterface; |
10
|
|
|
use Sunrise\Http\Router\RequestHandler; |
11
|
|
|
use Sunrise\Http\Router\Route; |
12
|
|
|
use Sunrise\Http\Router\RouteInterface; |
13
|
|
|
use Sunrise\Http\ServerRequest\ServerRequestFactory; |
14
|
|
|
|
15
|
|
|
class RouteTest extends TestCase |
16
|
|
|
{ |
17
|
|
|
public function testConstructor() |
18
|
|
|
{ |
19
|
|
|
$route = new Route('home', '/', $this->getRouteAction()); |
20
|
|
|
|
21
|
|
|
$this->assertInstanceOf(RouteInterface::class, $route); |
22
|
|
|
$this->assertInstanceOf(MiddlewareInterface::class, $route); |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
public function testGetId() |
26
|
|
|
{ |
27
|
|
|
$route = new Route('home', '/', $this->getRouteAction()); |
28
|
|
|
|
29
|
|
|
$this->assertEquals('home', $route->getId()); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
public function testGetPath() |
33
|
|
|
{ |
34
|
|
|
$route = new Route('home', '/', $this->getRouteAction()); |
35
|
|
|
|
36
|
|
|
$this->assertEquals('/', $route->getPath()); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
public function testGetAction() |
40
|
|
|
{ |
41
|
|
|
$action = $this->getRouteAction(); |
42
|
|
|
|
43
|
|
|
$route = new Route('home', '/', $action); |
44
|
|
|
|
45
|
|
|
$this->assertEquals($action, $route->getAction()); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
public function testGetMethods() |
49
|
|
|
{ |
50
|
|
|
$route = new Route('home', '/', $this->getRouteAction()); |
51
|
|
|
|
52
|
|
|
$this->assertEquals([], $route->getMethods()); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
public function testGetPatterns() |
56
|
|
|
{ |
57
|
|
|
$route = new Route('home', '/', $this->getRouteAction()); |
58
|
|
|
|
59
|
|
|
$this->assertEquals([], $route->getPatterns()); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
public function testGetMiddlewareStack() |
63
|
|
|
{ |
64
|
|
|
$route = new Route('home', '/', $this->getRouteAction()); |
65
|
|
|
|
66
|
|
|
$this->assertEquals([], $route->getMiddlewareStack()); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
public function testGetAttributes() |
70
|
|
|
{ |
71
|
|
|
$route = new Route('home', '/', $this->getRouteAction()); |
72
|
|
|
|
73
|
|
|
$this->assertEquals([], $route->getAttributes()); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
public function testAddMethod() |
77
|
|
|
{ |
78
|
|
|
$route = new Route('home', '/', $this->getRouteAction()); |
79
|
|
|
|
80
|
|
|
$this->assertInstanceOf(RouteInterface::class, $route->method('GET')); |
81
|
|
|
$this->assertEquals(['GET'], $route->getMethods()); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
public function testAddSeveralMethods() |
85
|
|
|
{ |
86
|
|
|
$route = new Route('home', '/', $this->getRouteAction()); |
87
|
|
|
|
88
|
|
|
$route->method('GET'); |
89
|
|
|
$route->method('POST'); |
90
|
|
|
|
91
|
|
|
$this->assertEquals(['GET', 'POST'], $route->getMethods()); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
public function testAddLowercasedMethod() |
95
|
|
|
{ |
96
|
|
|
$route = new Route('home', '/', $this->getRouteAction()); |
97
|
|
|
|
98
|
|
|
$route->method('get'); |
99
|
|
|
|
100
|
|
|
$this->assertEquals(['GET'], $route->getMethods()); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
public function testAddPattern() |
104
|
|
|
{ |
105
|
|
|
$route = new Route('home', '/', $this->getRouteAction()); |
106
|
|
|
|
107
|
|
|
$this->assertInstanceOf(RouteInterface::class, $route->pattern('id', '\d+')); |
108
|
|
|
$this->assertEquals(['id' => '\d+'], $route->getPatterns()); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
public function testAddSeveralPatterns() |
112
|
|
|
{ |
113
|
|
|
$route = new Route('home', '/', $this->getRouteAction()); |
114
|
|
|
|
115
|
|
|
$route->pattern('id', '\d+'); |
116
|
|
|
$route->pattern('word', '\w+'); |
117
|
|
|
|
118
|
|
|
$this->assertEquals([ |
119
|
|
|
'id' => '\d+', |
120
|
|
|
'word' => '\w+', |
121
|
|
|
], $route->getPatterns()); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
public function testAddMiddleware() |
125
|
|
|
{ |
126
|
|
|
$foo = $this->getRouteMiddlewareFoo(); |
127
|
|
|
|
128
|
|
|
$route = new Route('home', '/', $this->getRouteAction()); |
129
|
|
|
|
130
|
|
|
$this->assertInstanceOf(RouteInterface::class, $route->middleware($foo)); |
131
|
|
|
$this->assertEquals([$foo], $route->getMiddlewareStack()); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
public function testAddSeveralMiddleware() |
135
|
|
|
{ |
136
|
|
|
$foo = $this->getRouteMiddlewareFoo(); |
137
|
|
|
$bar = $this->getRouteMiddlewareBar(); |
138
|
|
|
|
139
|
|
|
$route = new Route('home', '/', $this->getRouteAction()); |
140
|
|
|
|
141
|
|
|
$route->middleware($foo); |
142
|
|
|
$route->middleware($bar); |
143
|
|
|
|
144
|
|
|
$this->assertEquals([$foo, $bar], $route->getMiddlewareStack()); |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
public function testSetAttributes() |
148
|
|
|
{ |
149
|
|
|
$route = new Route('home', '/', $this->getRouteAction()); |
150
|
|
|
|
151
|
|
|
$clone = $route->withAttributes(['id' => '1']); |
152
|
|
|
$this->assertInstanceOf(RouteInterface::class, $clone); |
153
|
|
|
$this->assertEquals(['id' => '1'], $clone->getAttributes()); |
154
|
|
|
|
155
|
|
|
$this->assertEquals([], $route->getAttributes()); |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
public function testProcess() |
159
|
|
|
{ |
160
|
|
|
$route = new Route('home', '/', $this->getRouteAction()); |
161
|
|
|
|
162
|
|
|
$handler = new RequestHandler(); |
163
|
|
|
$handler->add($route); |
164
|
|
|
|
165
|
|
|
$request = (new ServerRequestFactory) |
166
|
|
|
->createServerRequest('GET', '/'); |
167
|
|
|
|
168
|
|
|
$response = $handler->handle($request); |
169
|
|
|
|
170
|
|
|
$this->assertEquals(['true'], $response->getHeader('x-test')); |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
private function getRouteAction() : callable |
174
|
|
|
{ |
175
|
|
|
return function(ServerRequestInterface $request, ResponseInterface $response) : ResponseInterface |
176
|
|
|
{ |
177
|
|
|
return $response->withHeader('x-test', 'true'); |
178
|
|
|
}; |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
private function getRouteMiddlewareFoo() : MiddlewareInterface |
182
|
|
|
{ |
183
|
|
|
return new class implements MiddlewareInterface |
184
|
|
|
{ |
185
|
|
|
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler) : ResponseInterface |
186
|
|
|
{ |
187
|
|
|
return $handler->handle($request); |
188
|
|
|
} |
189
|
|
|
}; |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
private function getRouteMiddlewareBar() : MiddlewareInterface |
193
|
|
|
{ |
194
|
|
|
return new class implements MiddlewareInterface |
195
|
|
|
{ |
196
|
|
|
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler) : ResponseInterface |
197
|
|
|
{ |
198
|
|
|
return $handler->handle($request); |
199
|
|
|
} |
200
|
|
|
}; |
201
|
|
|
} |
202
|
|
|
} |
203
|
|
|
|