|
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+}#{tag:\w+}')->name('view'), |
|
42
|
|
|
]; |
|
43
|
|
|
$routerCollector = $this->createRouterCollector($routes); |
|
44
|
|
|
|
|
45
|
|
|
$url = $routerCollector->generate('view', ['id' => 100, 'tag' => 'yii']); |
|
46
|
|
|
|
|
47
|
|
|
$this->assertEquals('/view/100#yii', $url); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
public function testRouteWithoutParameters(): void |
|
51
|
|
|
{ |
|
52
|
|
|
$routes = [ |
|
53
|
|
|
Route::get('/view/{id:\d+}#{tag:\w+}')->name('view'), |
|
54
|
|
|
]; |
|
55
|
|
|
$routerCollector = $this->createRouterCollector($routes); |
|
56
|
|
|
|
|
57
|
|
|
$this->expectException(\RuntimeException::class); |
|
58
|
|
|
$routerCollector->generate('view'); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* @param array $routes |
|
63
|
|
|
* @return RouterInterface |
|
64
|
|
|
*/ |
|
65
|
|
|
private function createRouterCollector(array $routes): RouterInterface |
|
66
|
|
|
{ |
|
67
|
|
|
$container = new DummyContainer(); |
|
68
|
|
|
$factory = new RouteFactory(); |
|
69
|
|
|
|
|
70
|
|
|
return $factory($routes, $container); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
public function testGroup(): void |
|
74
|
|
|
{ |
|
75
|
|
|
$routes = [ |
|
76
|
|
|
Route::get('/home/index')->name('index'), |
|
77
|
|
|
['/api', static function (RouteCollectorInterface $r) { |
|
78
|
|
|
$r->addRoute(Route::get('/post')->name('post/index')); |
|
79
|
|
|
$r->addRoute(Route::get('/post/{id}')->name('post/view')); |
|
80
|
|
|
}], |
|
81
|
|
|
]; |
|
82
|
|
|
$routerCollector = $this->createRouterCollector($routes); |
|
83
|
|
|
|
|
84
|
|
|
$url = $routerCollector->generate('index'); |
|
85
|
|
|
$this->assertEquals('/home/index', $url); |
|
86
|
|
|
|
|
87
|
|
|
$url = $routerCollector->generate('post/index'); |
|
88
|
|
|
$this->assertEquals('/api/post', $url); |
|
89
|
|
|
|
|
90
|
|
|
$url = $routerCollector->generate('post/view', ['id' => 42]); |
|
91
|
|
|
$this->assertEquals('/api/post/42', $url); |
|
92
|
|
|
} |
|
93
|
|
|
} |
|
94
|
|
|
|