|
1
|
|
|
<?php |
|
2
|
|
|
namespace Slince\Routing\Tests; |
|
3
|
|
|
|
|
4
|
|
|
use PHPUnit\Framework\TestCase; |
|
5
|
|
|
use Slince\Routing\Route; |
|
6
|
|
|
use Slince\Routing\RouteCollection; |
|
7
|
|
|
|
|
8
|
|
|
class RouteCollectionTest extends TestCase |
|
9
|
|
|
{ |
|
10
|
|
|
public function testAdd() |
|
11
|
|
|
{ |
|
12
|
|
|
$routes = new RouteCollection(); |
|
13
|
|
|
$this->assertCount(0, $routes); |
|
14
|
|
|
$routes->add(new Route('/foo', 'Pages::foo')); |
|
15
|
|
|
$this->assertCount(1, $routes); |
|
16
|
|
|
return $routes; |
|
17
|
|
|
} |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* @depends testAdd |
|
21
|
|
|
* @param RouteCollection $routes |
|
22
|
|
|
*/ |
|
23
|
|
|
public function testGetRoute(RouteCollection $routes) |
|
24
|
|
|
{ |
|
25
|
|
|
$route = new Route('/bar', 'Pages::bar'); |
|
26
|
|
|
$route->setName('bar'); |
|
27
|
|
|
$routes->add($route); |
|
28
|
|
|
$this->assertTrue($route === $routes->getByName('bar')); |
|
29
|
|
|
$this->assertTrue($route === $routes->getByAction('Pages::bar')); |
|
30
|
|
|
$this->assertEquals(['bar' => $route], $routes->getNamedRoutes()); |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
public function testAll() |
|
34
|
|
|
{ |
|
35
|
|
|
$routes = new RouteCollection(); |
|
36
|
|
|
$route = new Route('/foo', 'Pages::foo'); |
|
37
|
|
|
$routes->add($route); |
|
38
|
|
|
$this->assertEquals([$route], $routes->all()); |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
public function testHttp() |
|
42
|
|
|
{ |
|
43
|
|
|
$routes = new RouteCollection(); |
|
44
|
|
|
$route = $routes->http('/foo', 'action1'); |
|
45
|
|
|
$this->assertCount(1, $routes); |
|
46
|
|
|
$this->assertEquals(['http'], $route->getSchemes()); |
|
47
|
|
|
return $routes; |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
public function testHttps() |
|
51
|
|
|
{ |
|
52
|
|
|
$routes = new RouteCollection(); |
|
53
|
|
|
$route = $routes->https('/bar', 'action'); |
|
54
|
|
|
$this->assertCount(1, $routes); |
|
55
|
|
|
$this->assertEquals(['https'], $route->getSchemes()); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
public function testGet() |
|
59
|
|
|
{ |
|
60
|
|
|
$routes = new RouteCollection(); |
|
61
|
|
|
$route = $routes->get('/bar', 'action'); |
|
62
|
|
|
$this->assertCount(1, $routes); |
|
63
|
|
|
$this->assertEquals(['GET', 'HEAD'], $route->getMethods()); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
public function testPost() |
|
67
|
|
|
{ |
|
68
|
|
|
$routes = new RouteCollection(); |
|
69
|
|
|
$route = $routes->post('/bar', 'action'); |
|
70
|
|
|
$this->assertCount(1, $routes); |
|
71
|
|
|
$this->assertEquals(['POST'], $route->getMethods()); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
public function testPut() |
|
75
|
|
|
{ |
|
76
|
|
|
$routes = new RouteCollection(); |
|
77
|
|
|
$route = $routes->put('/bar', 'action'); |
|
78
|
|
|
$this->assertCount(1, $routes); |
|
79
|
|
|
$this->assertEquals(['PUT'], $route->getMethods()); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
public function testPatch() |
|
83
|
|
|
{ |
|
84
|
|
|
$routes = new RouteCollection(); |
|
85
|
|
|
$route = $routes->patch('/bar', 'action'); |
|
86
|
|
|
$this->assertCount(1, $routes); |
|
87
|
|
|
$this->assertEquals(['PATCH'], $route->getMethods()); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
public function testDelete() |
|
91
|
|
|
{ |
|
92
|
|
|
$routes = new RouteCollection(); |
|
93
|
|
|
$route = $routes->delete('/bar', 'action'); |
|
94
|
|
|
$this->assertCount(1, $routes); |
|
95
|
|
|
$this->assertEquals(['DELETE'], $route->getMethods()); |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
public function testSimpleGroup() |
|
99
|
|
|
{ |
|
100
|
|
|
$routes = new RouteCollection(); |
|
101
|
|
|
$routes->group('admin', function(RouteCollection $routes){ |
|
102
|
|
|
$routes->get('/users', 'action'); |
|
103
|
|
|
}); |
|
104
|
|
|
$route = $routes->getByAction('action'); |
|
105
|
|
|
$this->assertEquals('/admin/users', $route->getPath()); |
|
106
|
|
|
|
|
107
|
|
|
$routes->group('/api', function(RouteCollection $routes){ |
|
108
|
|
|
$routes->get('/users', 'action2'); |
|
109
|
|
|
}); |
|
110
|
|
|
$route = $routes->getByAction('action2'); |
|
111
|
|
|
$this->assertEquals('/api/users', $route->getPath()); |
|
112
|
|
|
|
|
113
|
|
|
$routes->group('/web/', function(RouteCollection $routes){ |
|
114
|
|
|
$routes->get('/users/', 'action3'); |
|
115
|
|
|
}); |
|
116
|
|
|
$route = $routes->getByAction('action3'); |
|
117
|
|
|
$this->assertEquals('/web/users', $route->getPath()); |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
public function testGroup() |
|
121
|
|
|
{ |
|
122
|
|
|
$routes = new RouteCollection(); |
|
123
|
|
|
$routes->get('/users', 'action'); |
|
124
|
|
|
$routes->group([ |
|
125
|
|
|
'host' => 'locale.domain.com:422', |
|
126
|
|
|
'methods' => 'GET', |
|
127
|
|
|
'schemes' => 'https', |
|
128
|
|
|
'requirements' => [ |
|
129
|
|
|
'locale' => 'en', |
|
130
|
|
|
'id' => '\w+' |
|
131
|
|
|
], |
|
132
|
|
|
'foo' => 'bar' |
|
133
|
|
|
], function(RouteCollection $routes){ |
|
134
|
|
|
$routes->http('/products', 'Products::index')->setName('products.index'); |
|
135
|
|
|
$routes->get('/products/{id}', 'Products::view')->setName('products.view'); |
|
136
|
|
|
$routes->delete('/products/{id}', 'Products::delete') |
|
137
|
|
|
->setName('products.delete') |
|
138
|
|
|
->setRequirement('locale', 'en_US'); |
|
139
|
|
|
}); |
|
140
|
|
|
$this->assertCount(4, $routes); |
|
141
|
|
|
|
|
142
|
|
|
$productsIndexRoute = $routes->getByName('products.index'); |
|
143
|
|
|
$productsViewRoute = $routes->getByName('products.view'); |
|
144
|
|
|
$productsDeleteRoute = $routes->getByName('products.delete'); |
|
145
|
|
|
|
|
146
|
|
|
$this->assertEquals('locale.domain.com:422', $productsIndexRoute->getHost()); |
|
147
|
|
|
$this->assertEquals('locale.domain.com:422', $productsViewRoute->getHost()); |
|
148
|
|
|
$this->assertEquals('locale.domain.com:422', $productsDeleteRoute->getHost()); |
|
149
|
|
|
|
|
150
|
|
|
$this->assertEquals(['GET'], $productsIndexRoute->getMethods()); |
|
151
|
|
|
$this->assertEquals(['GET', 'HEAD'], $productsViewRoute->getMethods()); |
|
152
|
|
|
$this->assertEquals(['DELETE'], $productsDeleteRoute->getMethods()); |
|
153
|
|
|
|
|
154
|
|
|
$this->assertEquals(['http'], $productsIndexRoute->getSchemes()); |
|
155
|
|
|
$this->assertEquals(['https'], $productsViewRoute->getSchemes()); |
|
156
|
|
|
$this->assertEquals(['https'], $productsDeleteRoute->getSchemes()); |
|
157
|
|
|
|
|
158
|
|
|
$this->assertEquals([ |
|
159
|
|
|
'locale' => 'en', |
|
160
|
|
|
'id' => '\w+' |
|
161
|
|
|
], $productsIndexRoute->getRequirements()); |
|
162
|
|
|
$this->assertEquals([ |
|
163
|
|
|
'locale' => 'en', |
|
164
|
|
|
'id' => '\w+' |
|
165
|
|
|
], $productsViewRoute->getRequirements()); |
|
166
|
|
|
$this->assertEquals([ |
|
167
|
|
|
'locale' => 'en_US', |
|
168
|
|
|
'id' => '\w+' |
|
169
|
|
|
], $productsDeleteRoute->getRequirements()); |
|
170
|
|
|
|
|
171
|
|
|
$this->assertEquals(['foo' => 'bar'], $productsIndexRoute->getParameters()); |
|
172
|
|
|
$this->assertEquals(['foo' => 'bar'], $productsViewRoute->getParameters()); |
|
173
|
|
|
$this->assertEquals(['foo' => 'bar'], $productsDeleteRoute->getParameters()); |
|
174
|
|
|
} |
|
175
|
|
|
} |