|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Sunrise\Http\Router\Tests; |
|
4
|
|
|
|
|
5
|
|
|
use PHPUnit\Framework\TestCase; |
|
6
|
|
|
use Sunrise\Http\Router\Route; |
|
7
|
|
|
use Sunrise\Http\Router\RouteInterface; |
|
8
|
|
|
|
|
9
|
|
|
// fake middlewares |
|
10
|
|
|
use Sunrise\Http\Router\Tests\Middleware\FooMiddlewareTest; |
|
11
|
|
|
use Sunrise\Http\Router\Tests\Middleware\BarMiddlewareTest; |
|
12
|
|
|
|
|
13
|
|
|
class RouteTest extends TestCase |
|
14
|
|
|
{ |
|
15
|
|
|
public function testConstructor() |
|
16
|
|
|
{ |
|
17
|
|
|
$route = new Route('home', '/', []); |
|
18
|
|
|
|
|
19
|
|
|
$this->assertInstanceOf(RouteInterface::class, $route); |
|
20
|
|
|
} |
|
21
|
|
|
|
|
22
|
|
|
public function testGetId() |
|
23
|
|
|
{ |
|
24
|
|
|
$foo = 'home'; |
|
25
|
|
|
|
|
26
|
|
|
$route = new Route($foo, '/', []); |
|
27
|
|
|
|
|
28
|
|
|
$this->assertEquals($foo, $route->getId()); |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
public function testGetPath() |
|
32
|
|
|
{ |
|
33
|
|
|
$foo = '/'; |
|
34
|
|
|
|
|
35
|
|
|
$route = new Route('home', $foo, []); |
|
36
|
|
|
|
|
37
|
|
|
$this->assertEquals($foo, $route->getPath()); |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
public function testGetMethods() |
|
41
|
|
|
{ |
|
42
|
|
|
$foo = ['HEAD', 'GET']; |
|
43
|
|
|
|
|
44
|
|
|
$route = new Route('home', '/', $foo); |
|
45
|
|
|
|
|
46
|
|
|
$this->assertEquals($foo, $route->getMethods()); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
public function testGetPatterns() |
|
50
|
|
|
{ |
|
51
|
|
|
$foo = []; |
|
52
|
|
|
|
|
53
|
|
|
$route = new Route('home', '/', []); |
|
54
|
|
|
|
|
55
|
|
|
$this->assertEquals($foo, $route->getPatterns()); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
public function testGetMiddlewareStack() |
|
59
|
|
|
{ |
|
60
|
|
|
$foo = []; |
|
61
|
|
|
|
|
62
|
|
|
$route = new Route('home', '/', []); |
|
63
|
|
|
|
|
64
|
|
|
$this->assertEquals($foo, $route->getMiddlewareStack()); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
public function testGetAttributes() |
|
68
|
|
|
{ |
|
69
|
|
|
$foo = []; |
|
70
|
|
|
|
|
71
|
|
|
$route = new Route('home', '/', []); |
|
72
|
|
|
|
|
73
|
|
|
$this->assertEquals($foo, $route->getAttributes()); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
public function testAddPrefix() |
|
77
|
|
|
{ |
|
78
|
|
|
$foo = '/foo'; |
|
79
|
|
|
$bar = '/bar'; |
|
80
|
|
|
|
|
81
|
|
|
$route = new Route('home', $foo, []); |
|
82
|
|
|
|
|
83
|
|
|
$this->assertInstanceOf(RouteInterface::class, $route->addPrefix($bar)); |
|
84
|
|
|
|
|
85
|
|
|
$this->assertEquals($bar.$foo, $route->getPath()); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
public function testAddSeveralPrefixex() |
|
89
|
|
|
{ |
|
90
|
|
|
$foo = '/foo'; |
|
91
|
|
|
$bar = '/bar'; |
|
92
|
|
|
$baz = '/baz'; |
|
93
|
|
|
|
|
94
|
|
|
$route = new Route('home', $foo, []); |
|
95
|
|
|
|
|
96
|
|
|
$route->addPrefix($bar); |
|
97
|
|
|
$route->addPrefix($baz); |
|
98
|
|
|
|
|
99
|
|
|
$this->assertEquals($baz.$bar.$foo, $route->getPath()); |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
public function testAddPattern() |
|
103
|
|
|
{ |
|
104
|
|
|
$foo = ['id', '\d+']; |
|
105
|
|
|
|
|
106
|
|
|
$route = new Route('home', '/', []); |
|
107
|
|
|
|
|
108
|
|
|
$this->assertInstanceOf(RouteInterface::class, $route->addPattern($foo[0], $foo[1])); |
|
109
|
|
|
|
|
110
|
|
|
$this->assertEquals([$foo[0] => $foo[1]], $route->getPatterns()); |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
public function testAddSeveralPatterns() |
|
114
|
|
|
{ |
|
115
|
|
|
$foo = ['id', '\d+']; |
|
116
|
|
|
$bar = ['word', '\w+']; |
|
117
|
|
|
|
|
118
|
|
|
$route = new Route('home', '/', []); |
|
119
|
|
|
|
|
120
|
|
|
$route->addPattern($foo[0], $foo[1]); |
|
121
|
|
|
$route->addPattern($bar[0], $bar[1]); |
|
122
|
|
|
|
|
123
|
|
|
$this->assertEquals([ |
|
124
|
|
|
$foo[0] => $foo[1], |
|
125
|
|
|
$bar[0] => $bar[1], |
|
126
|
|
|
], $route->getPatterns()); |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
public function testAddMiddleware() |
|
130
|
|
|
{ |
|
131
|
|
|
$foo = new FooMiddlewareTest(); |
|
132
|
|
|
|
|
133
|
|
|
$route = new Route('home', '/', []); |
|
134
|
|
|
|
|
135
|
|
|
$this->assertInstanceOf(RouteInterface::class, $route->addMiddleware($foo)); |
|
136
|
|
|
|
|
137
|
|
|
$this->assertEquals([$foo], $route->getMiddlewareStack()); |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
|
|
public function testAddSeveralMiddlewares() |
|
141
|
|
|
{ |
|
142
|
|
|
$foo = new FooMiddlewareTest(); |
|
143
|
|
|
$bar = new BarMiddlewareTest(); |
|
144
|
|
|
|
|
145
|
|
|
$route = new Route('home', '/', []); |
|
146
|
|
|
|
|
147
|
|
|
$route->addMiddleware($foo); |
|
148
|
|
|
$route->addMiddleware($bar); |
|
149
|
|
|
|
|
150
|
|
|
$this->assertEquals([ |
|
151
|
|
|
$foo, |
|
152
|
|
|
$bar, |
|
153
|
|
|
], $route->getMiddlewareStack()); |
|
154
|
|
|
} |
|
155
|
|
|
|
|
156
|
|
|
public function testSetAttributes() |
|
157
|
|
|
{ |
|
158
|
|
|
$foo = ['id' => '1']; |
|
159
|
|
|
|
|
160
|
|
|
$route = new Route('home', '/', []); |
|
161
|
|
|
$clone = $route->withAttributes($foo); |
|
162
|
|
|
|
|
163
|
|
|
$this->assertInstanceOf(RouteInterface::class, $clone); |
|
164
|
|
|
$this->assertNotEquals($clone, $route); |
|
165
|
|
|
|
|
166
|
|
|
$this->assertEquals($foo, $clone->getAttributes()); |
|
167
|
|
|
$this->assertEquals([], $route->getAttributes()); |
|
168
|
|
|
} |
|
169
|
|
|
|
|
170
|
|
|
public function testLowercasedMethod() |
|
171
|
|
|
{ |
|
172
|
|
|
$route = new Route('home', '/', ['foo', 'bar']); |
|
173
|
|
|
|
|
174
|
|
|
$this->assertEquals(['FOO', 'BAR'], $route->getMethods()); |
|
175
|
|
|
} |
|
176
|
|
|
} |
|
177
|
|
|
|