|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Yiisoft\Router\FastRoute\Tests; |
|
4
|
|
|
|
|
5
|
|
|
use PHPUnit\Framework\TestCase; |
|
6
|
|
|
use Yiisoft\Router\Route; |
|
7
|
|
|
use Yiisoft\Router\RouteCollectorInterface; |
|
8
|
|
|
use Yiisoft\Router\RouteNotFoundException; |
|
9
|
|
|
use Yiisoft\Router\RouterInterface; |
|
10
|
|
|
|
|
11
|
|
|
class FastRouteTest extends TestCase |
|
12
|
|
|
{ |
|
13
|
|
|
public function testSimpleNamedRoute(): void |
|
14
|
|
|
{ |
|
15
|
|
|
$routes = [ |
|
16
|
|
|
Route::get('/home/index')->name('index'), |
|
17
|
|
|
]; |
|
18
|
|
|
$routerCollector = $this->createRouterCollector($routes); |
|
19
|
|
|
|
|
20
|
|
|
$url = $routerCollector->generate('index'); |
|
21
|
|
|
|
|
22
|
|
|
$this->assertEquals('/home/index', $url); |
|
23
|
|
|
} |
|
24
|
|
|
|
|
25
|
|
|
public function testRouteWithoutNameNotFound(): void |
|
26
|
|
|
{ |
|
27
|
|
|
$routes = [ |
|
28
|
|
|
Route::get('/home/index'), |
|
29
|
|
|
Route::get('/index'), |
|
30
|
|
|
Route::get('index'), |
|
31
|
|
|
]; |
|
32
|
|
|
$routerCollector = $this->createRouterCollector($routes); |
|
33
|
|
|
|
|
34
|
|
|
$this->expectException(RouteNotFoundException::class); |
|
35
|
|
|
$routerCollector->generate('index'); |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
public function testRouteWithParameters(): void |
|
39
|
|
|
{ |
|
40
|
|
|
$routes = [ |
|
41
|
|
|
Route::get('/view/{id:\d+}/{text:~[\w]+}#{tag:\w+}')->name('view'), |
|
42
|
|
|
]; |
|
43
|
|
|
$routerCollector = $this->createRouterCollector($routes); |
|
44
|
|
|
|
|
45
|
|
|
$url = $routerCollector->generate('view', ['id' => 100, 'tag' => 'yii', 'text' => '~test']); |
|
46
|
|
|
|
|
47
|
|
|
$this->assertEquals('/view/100/~test#yii', $url); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
public function testParamPattern(): void |
|
51
|
|
|
{ |
|
52
|
|
|
$routes = [ |
|
53
|
|
|
Route::get('/view/{id:\w+}')->name('view'), |
|
54
|
|
|
]; |
|
55
|
|
|
$routerCollector = $this->createRouterCollector($routes); |
|
56
|
|
|
|
|
57
|
|
|
$this->expectExceptionMessage('Parameter value for [id] did not match the regex `\w+`'); |
|
58
|
|
|
$routerCollector->generate('view', ['id' => null]); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
public function testMissedParams(): void |
|
62
|
|
|
{ |
|
63
|
|
|
$routes = [ |
|
64
|
|
|
Route::get('/view/{id:\w+}')->name('view'), |
|
65
|
|
|
]; |
|
66
|
|
|
$routerCollector = $this->createRouterCollector($routes); |
|
67
|
|
|
|
|
68
|
|
|
$this->expectExceptionMessage('Route `view` expects at least parameter values for [id], but received []'); |
|
69
|
|
|
$routerCollector->generate('view'); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
public function testMissedParam(): void |
|
73
|
|
|
{ |
|
74
|
|
|
$routes = [ |
|
75
|
|
|
Route::get('/view/{id:\d+}/{value}')->name('view'), |
|
76
|
|
|
]; |
|
77
|
|
|
$routerCollector = $this->createRouterCollector($routes); |
|
78
|
|
|
|
|
79
|
|
|
$this->expectExceptionMessage('Route `view` expects at least parameter values for [id,value], but received [id]'); |
|
80
|
|
|
$routerCollector->generate('view', ['id' => 123]); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
public function testRouteWithoutParameters(): void |
|
84
|
|
|
{ |
|
85
|
|
|
$routes = [ |
|
86
|
|
|
Route::get('/view/{id:\d+}#{tag:\w+}')->name('view'), |
|
87
|
|
|
]; |
|
88
|
|
|
$routerCollector = $this->createRouterCollector($routes); |
|
89
|
|
|
|
|
90
|
|
|
$this->expectException(\RuntimeException::class); |
|
91
|
|
|
$routerCollector->generate('view'); |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* @param array $routes |
|
96
|
|
|
* @return RouterInterface |
|
97
|
|
|
*/ |
|
98
|
|
|
private function createRouterCollector(array $routes): RouterInterface |
|
99
|
|
|
{ |
|
100
|
|
|
$container = new DummyContainer(); |
|
101
|
|
|
$factory = new RouteFactory(); |
|
102
|
|
|
|
|
103
|
|
|
return $factory($routes, $container); |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
public function testGroup(): void |
|
107
|
|
|
{ |
|
108
|
|
|
$routes = [ |
|
109
|
|
|
Route::get('/home/index')->name('index'), |
|
110
|
|
|
['/api', static function (RouteCollectorInterface $r) { |
|
111
|
|
|
$r->addRoute(Route::get('/post')->name('post/index')); |
|
112
|
|
|
$r->addRoute(Route::get('/post/{id}')->name('post/view')); |
|
113
|
|
|
}], |
|
114
|
|
|
]; |
|
115
|
|
|
$routerCollector = $this->createRouterCollector($routes); |
|
116
|
|
|
|
|
117
|
|
|
$url = $routerCollector->generate('index'); |
|
118
|
|
|
$this->assertEquals('/home/index', $url); |
|
119
|
|
|
|
|
120
|
|
|
$url = $routerCollector->generate('post/index'); |
|
121
|
|
|
$this->assertEquals('/api/post', $url); |
|
122
|
|
|
|
|
123
|
|
|
$url = $routerCollector->generate('post/view', ['id' => 42]); |
|
124
|
|
|
$this->assertEquals('/api/post/42', $url); |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
public function testDefaultIsNotUsedWithOptionalParameter(): void |
|
128
|
|
|
{ |
|
129
|
|
|
$routes = [ |
|
130
|
|
|
Route::get('/[{name}]') |
|
131
|
|
|
->name('defaults') |
|
132
|
|
|
->defaults(['name' => 'default']) |
|
133
|
|
|
]; |
|
134
|
|
|
|
|
135
|
|
|
$url = $this->createRouterCollector($routes)->generate('defaults'); |
|
136
|
|
|
$this->assertEquals('/', $url); |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
|
public function testOptionalParameterIsUsedWhenSpecified(): void |
|
140
|
|
|
{ |
|
141
|
|
|
$routes = [ |
|
142
|
|
|
Route::get('/[{name}]') |
|
143
|
|
|
->name('defaults') |
|
144
|
|
|
->defaults(['name' => 'default']) |
|
145
|
|
|
]; |
|
146
|
|
|
|
|
147
|
|
|
$url = $this->createRouterCollector($routes)->generate('defaults', ['name' => 'test']); |
|
148
|
|
|
$this->assertEquals('/test', $url); |
|
149
|
|
|
} |
|
150
|
|
|
|
|
151
|
|
|
public function testDefaultIsUsedWithRequiredParameter(): void |
|
152
|
|
|
{ |
|
153
|
|
|
$routes = [ |
|
154
|
|
|
Route::get('/{name}') |
|
155
|
|
|
->name('defaults') |
|
156
|
|
|
->defaults(['name' => 'default']) |
|
157
|
|
|
]; |
|
158
|
|
|
|
|
159
|
|
|
$url = $this->createRouterCollector($routes)->generate('defaults', ['name' => 'test']); |
|
160
|
|
|
$this->assertEquals('/test', $url); |
|
161
|
|
|
} |
|
162
|
|
|
} |
|
163
|
|
|
|