|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
|
|
4
|
|
|
namespace Yiisoft\Router\Tests; |
|
5
|
|
|
|
|
6
|
|
|
use PHPUnit\Framework\TestCase; |
|
7
|
|
|
use Psr\Container\ContainerInterface; |
|
8
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
|
9
|
|
|
use Yiisoft\Router\Group; |
|
10
|
|
|
use Yiisoft\Router\MatchingResult; |
|
11
|
|
|
use Yiisoft\Router\Route; |
|
12
|
|
|
use Yiisoft\Router\RouterFactory; |
|
13
|
|
|
use Yiisoft\Router\RouterInterface; |
|
14
|
|
|
use Yiisoft\Router\Tests\Support\Container; |
|
15
|
|
|
|
|
16
|
|
|
final class RouterFactoryTest extends TestCase |
|
17
|
|
|
{ |
|
18
|
|
|
public function testContainerInjected(): void |
|
19
|
|
|
{ |
|
20
|
|
|
$container = $this->getContainer(); |
|
21
|
|
|
|
|
22
|
|
|
$routes = [ |
|
23
|
|
|
Route::get('/info')->name('api-info'), |
|
24
|
|
|
Group::create( |
|
25
|
|
|
'/v2', |
|
26
|
|
|
[ |
|
27
|
|
|
Route::get('/user')->name('api-v2-user/index'), |
|
28
|
|
|
Route::get('/user/{id}')->name('api-v2-user/view'), |
|
29
|
|
|
Group::create( |
|
30
|
|
|
'/news', |
|
31
|
|
|
[ |
|
32
|
|
|
Route::get('/post')->name('api-v2-news-post/index'), |
|
33
|
|
|
Route::get('/post/{id}')->name('api-v2-news-post/view'), |
|
34
|
|
|
] |
|
35
|
|
|
), |
|
36
|
|
|
Group::create( |
|
37
|
|
|
'/blog', |
|
38
|
|
|
[ |
|
39
|
|
|
Route::get('/post')->name('api-v2-blog-post/index'), |
|
40
|
|
|
Route::get('/post/{id}')->name('api-v2-blog-post/view'), |
|
41
|
|
|
] |
|
42
|
|
|
), |
|
43
|
|
|
Route::get('/note')->name('api-v2-note/index'), |
|
44
|
|
|
Route::get('/note/{id}')->name('api-v2-note/view'), |
|
45
|
|
|
] |
|
46
|
|
|
), |
|
47
|
|
|
Group::create( |
|
48
|
|
|
'/v2', |
|
49
|
|
|
[ |
|
50
|
|
|
Route::get('/user')->name('api-v2-user/index'), |
|
51
|
|
|
Route::get('/user/{id}')->name('api-v2-user/view'), |
|
52
|
|
|
Group::create( |
|
53
|
|
|
'/news', |
|
54
|
|
|
[ |
|
55
|
|
|
Route::get('/post')->name('api-v2-news-post/index'), |
|
56
|
|
|
Route::get('/post/{id}')->name('api-v2-news-post/view'), |
|
57
|
|
|
] |
|
58
|
|
|
), |
|
59
|
|
|
Group::create( |
|
60
|
|
|
'/blog', |
|
61
|
|
|
[ |
|
62
|
|
|
Route::get('/post')->name('api-v2-blog-post/index'), |
|
63
|
|
|
Route::get('/post/{id}')->name('api-v2-blog-post/view'), |
|
64
|
|
|
] |
|
65
|
|
|
), |
|
66
|
|
|
Route::get('/note')->name('api-v2-note/index'), |
|
67
|
|
|
Route::get('/note/{id}')->name('api-v2-note/view'), |
|
68
|
|
|
] |
|
69
|
|
|
) |
|
70
|
|
|
]; |
|
71
|
|
|
|
|
72
|
|
|
$factory = new RouterFactory($this->getEngineFactory(), $routes); |
|
73
|
|
|
$router = $factory($container); |
|
74
|
|
|
$items = $router->getItems(); |
|
75
|
|
|
|
|
76
|
|
|
$this->assertAllRoutesAndGroupsHaveContainer($items); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
private function assertAllRoutesAndGroupsHaveContainer(array $items): void |
|
80
|
|
|
{ |
|
81
|
|
|
$func = function ($item) use (&$func) { |
|
82
|
|
|
$this->assertTrue($item->hasContainer()); |
|
83
|
|
|
if ($item instanceof Group) { |
|
84
|
|
|
$items = $item->getItems(); |
|
85
|
|
|
array_walk($items, $func); |
|
86
|
|
|
} |
|
87
|
|
|
}; |
|
88
|
|
|
array_walk($items, $func); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
private function getContainer(array $instances = []): ContainerInterface |
|
92
|
|
|
{ |
|
93
|
|
|
return new Container($instances); |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
private function getEngineFactory(): callable |
|
97
|
|
|
{ |
|
98
|
|
|
return new class() { |
|
99
|
|
|
public function __invoke(): RouterInterface |
|
100
|
|
|
{ |
|
101
|
|
|
return new class() extends Group implements RouterInterface { |
|
102
|
|
|
public function match(ServerRequestInterface $request): MatchingResult |
|
103
|
|
|
{ |
|
104
|
|
|
} |
|
|
|
|
|
|
105
|
|
|
|
|
106
|
|
|
public function generateAbsolute(string $name, array $parameters = [], string $scheme = null, string $host = null): string |
|
107
|
|
|
{ |
|
108
|
|
|
} |
|
|
|
|
|
|
109
|
|
|
|
|
110
|
|
|
public function generate(string $name, array $parameters = []): string |
|
111
|
|
|
{ |
|
112
|
|
|
} |
|
|
|
|
|
|
113
|
|
|
|
|
114
|
|
|
public function getUriPrefix(): string |
|
115
|
|
|
{ |
|
116
|
|
|
} |
|
|
|
|
|
|
117
|
|
|
|
|
118
|
|
|
public function setUriPrefix(string $name): void |
|
119
|
|
|
{ |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
public function getCurrentRoute(): ?Route |
|
123
|
|
|
{ |
|
124
|
|
|
} |
|
125
|
|
|
}; |
|
126
|
|
|
} |
|
127
|
|
|
}; |
|
128
|
|
|
} |
|
129
|
|
|
} |
|
130
|
|
|
|
For hinted functions/methods where all return statements with the correct type are only reachable via conditions, ?null? gets implicitly returned which may be incompatible with the hinted type. Let?s take a look at an example: