|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
|
|
4
|
|
|
namespace Yiisoft\Router\Tests; |
|
5
|
|
|
|
|
6
|
|
|
use PHPUnit\Framework\TestCase; |
|
7
|
|
|
use Psr\Container\ContainerInterface; |
|
8
|
|
|
use Yiisoft\Router\Group; |
|
9
|
|
|
use Yiisoft\Router\GroupFactory; |
|
10
|
|
|
use Yiisoft\Router\Route; |
|
11
|
|
|
use Yiisoft\Router\Tests\Support\Container; |
|
12
|
|
|
|
|
13
|
|
|
final class GroupFactoryTest extends TestCase |
|
14
|
|
|
{ |
|
15
|
|
|
public function testContainerInjected(): void |
|
16
|
|
|
{ |
|
17
|
|
|
$container = $this->getContainer(); |
|
18
|
|
|
|
|
19
|
|
|
$apiGroup = Group::create( |
|
20
|
|
|
'/api', |
|
21
|
|
|
[ |
|
22
|
|
|
Route::get('/info')->name('api-info'), |
|
23
|
|
|
Group::create( |
|
24
|
|
|
'/v1', |
|
25
|
|
|
[ |
|
26
|
|
|
Route::get('/user')->name('api-v1-user/index'), |
|
27
|
|
|
Route::get('/user/{id}')->name('api-v1-user/view'), |
|
28
|
|
|
Group::create( |
|
29
|
|
|
'/news', |
|
30
|
|
|
[ |
|
31
|
|
|
Route::get('/post')->name('api-v1-news-post/index'), |
|
32
|
|
|
Route::get('/post/{id}')->name('api-v1-news-post/view'), |
|
33
|
|
|
] |
|
34
|
|
|
), |
|
35
|
|
|
Group::create( |
|
36
|
|
|
'/blog', |
|
37
|
|
|
[ |
|
38
|
|
|
Route::get('/post')->name('api-v1-blog-post/index'), |
|
39
|
|
|
Route::get('/post/{id}')->name('api-v1-blog-post/view'), |
|
40
|
|
|
] |
|
41
|
|
|
), |
|
42
|
|
|
Route::get('/note')->name('api-v1-note/index'), |
|
43
|
|
|
Route::get('/note/{id}')->name('api-v1-note/view'), |
|
44
|
|
|
] |
|
45
|
|
|
), |
|
46
|
|
|
Group::create( |
|
47
|
|
|
'/v2', |
|
48
|
|
|
[ |
|
49
|
|
|
Route::get('/user')->name('api-v2-user/index'), |
|
50
|
|
|
Route::get('/user/{id}')->name('api-v2-user/view'), |
|
51
|
|
|
Group::create( |
|
52
|
|
|
'/news', |
|
53
|
|
|
[ |
|
54
|
|
|
Route::get('/post')->name('api-v2-news-post/index'), |
|
55
|
|
|
Route::get('/post/{id}')->name('api-v2-news-post/view'), |
|
56
|
|
|
] |
|
57
|
|
|
), |
|
58
|
|
|
Group::create( |
|
59
|
|
|
'/blog', |
|
60
|
|
|
[ |
|
61
|
|
|
Route::get('/post')->name('api-v2-blog-post/index'), |
|
62
|
|
|
Route::get('/post/{id}')->name('api-v2-blog-post/view'), |
|
63
|
|
|
] |
|
64
|
|
|
), |
|
65
|
|
|
Route::get('/note')->name('api-v2-note/index'), |
|
66
|
|
|
Route::get('/note/{id}')->name('api-v2-note/view'), |
|
67
|
|
|
] |
|
68
|
|
|
) |
|
69
|
|
|
], $container); |
|
70
|
|
|
|
|
71
|
|
|
$items = $apiGroup->getItems(); |
|
72
|
|
|
|
|
73
|
|
|
$this->assertAllRoutesAndGroupsHaveContainer($items); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
private function getContainer(array $instances = []): ContainerInterface |
|
77
|
|
|
{ |
|
78
|
|
|
return new Container($instances); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
private function assertAllRoutesAndGroupsHaveContainer(array $items): void |
|
82
|
|
|
{ |
|
83
|
|
|
$func = function ($item) use (&$func) { |
|
84
|
|
|
$this->assertTrue($item->hasContainer()); |
|
85
|
|
|
if ($item instanceof Group) { |
|
86
|
|
|
$items = $item->getItems(); |
|
87
|
|
|
array_walk($items, $func); |
|
88
|
|
|
} |
|
89
|
|
|
}; |
|
90
|
|
|
array_walk($items, $func); |
|
91
|
|
|
} |
|
92
|
|
|
} |
|
93
|
|
|
|