1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Nip\Router\Tests\Router\Traits; |
4
|
|
|
|
5
|
|
|
use Nip\Request; |
6
|
|
|
use Nip\Router\Route\Route; |
7
|
|
|
use Nip\Router\RouteFactory; |
8
|
|
|
use Nip\Router\Router; |
9
|
|
|
use Nip\Router\Tests\AbstractTest; |
10
|
|
|
use Nip\Router\Tests\Fixtures\Application\Library\Router\Route\StandardRoute; |
11
|
|
|
use Symfony\Component\Routing\Exception\ResourceNotFoundException; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Class HasMatcherTraitTest |
15
|
|
|
* @package Nip\Router\Tests\Router\Traits |
16
|
|
|
*/ |
17
|
|
|
class HasMatcherTraitTest extends AbstractTest |
18
|
|
|
{ |
19
|
|
|
|
20
|
|
|
public function testNotFound() |
21
|
|
|
{ |
22
|
|
|
$router = new Router(); |
23
|
|
|
self::expectException(ResourceNotFoundException::class); |
24
|
|
|
|
25
|
|
|
$request = Request::create('/404'); |
26
|
|
|
$router->matchRequest($request); |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
public function testRouteLiteral() |
30
|
|
|
{ |
31
|
|
|
$router = new Router(); |
32
|
|
|
$collection = $router->getRoutes(); |
33
|
|
|
|
34
|
|
|
RouteFactory::generateLiteralRoute($collection, "admin.index", Route::class, "/admin", "/index"); |
|
|
|
|
35
|
|
|
RouteFactory::generateLiteralRoute($collection, "api.index", Route::class, "/api", "/index"); |
|
|
|
|
36
|
|
|
|
37
|
|
|
$request = Request::create('/api/index'); |
38
|
|
|
$params = $router->matchRequest($request); |
39
|
|
|
self::assertEquals(['_route' => 'api.index'], $params); |
40
|
|
|
|
41
|
|
|
$currentRoute = $router->getCurrent(); |
42
|
|
|
self::assertInstanceOf(Route::class, $currentRoute); |
43
|
|
|
self::assertEquals('api.index', $currentRoute->getName()); |
44
|
|
|
|
45
|
|
|
$request = Request::create('/admin/index'); |
46
|
|
|
$router->matchRequest($request); |
47
|
|
|
self::assertEquals('admin.index', $router->getCurrent()->getName()); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
public function testRouteDynamic() |
51
|
|
|
{ |
52
|
|
|
$router = new Router(); |
53
|
|
|
$collection = $router->getRoutes(); |
54
|
|
|
|
55
|
|
|
RouteFactory::generateStandardRoute( |
56
|
|
|
$collection, |
|
|
|
|
57
|
|
|
"admin.standard", |
58
|
|
|
StandardRoute::class, |
59
|
|
|
"/admin", |
60
|
|
|
'/:controller/:action', ['module' => 'admin']); |
61
|
|
|
RouteFactory::generateStandardRoute( |
62
|
|
|
$collection, "api.standard", |
|
|
|
|
63
|
|
|
StandardRoute::class, |
64
|
|
|
"/api", |
65
|
|
|
'/:controller/:action', ['module' => 'api']); |
66
|
|
|
|
67
|
|
|
RouteFactory::generateStandardRoute( |
68
|
|
|
$collection, |
|
|
|
|
69
|
|
|
"frontend.standard", |
70
|
|
|
StandardRoute::class, |
71
|
|
|
"/"); |
72
|
|
|
|
73
|
|
|
$request = Request::create('/api/pages/delete'); |
74
|
|
|
self::assertEquals( |
75
|
|
|
['module' => 'api', 'controller' => 'pages', 'action' => 'delete', '_route' => 'api.standard'], |
76
|
|
|
$router->matchRequest($request) |
77
|
|
|
); |
78
|
|
|
|
79
|
|
|
$request = Request::create('/api/pages/'); |
80
|
|
|
self::assertEquals( |
81
|
|
|
['module' => 'api', 'controller' => 'pages', 'action' => 'index', '_route' => 'api.standard'], |
82
|
|
|
$router->matchRequest($request) |
83
|
|
|
); |
84
|
|
|
|
85
|
|
|
$request = Request::create('/api/pages'); |
86
|
|
|
self::assertEquals( |
87
|
|
|
['module' => 'api', 'controller' => 'pages', 'action' => 'index', '_route' => 'api.standard'], |
88
|
|
|
$router->matchRequest($request) |
89
|
|
|
); |
90
|
|
|
|
91
|
|
|
$request = Request::create('/admin/pages/delete'); |
92
|
|
|
self::assertEquals( |
93
|
|
|
['module' => 'admin', 'controller' => 'pages', 'action' => 'delete', '_route' => 'admin.standard'], |
94
|
|
|
$router->matchRequest($request) |
95
|
|
|
); |
96
|
|
|
|
97
|
|
|
$request = Request::create('/users/delete'); |
98
|
|
|
self::assertEquals( |
99
|
|
|
['controller' => 'users', 'action' => 'delete', '_route' => 'frontend.standard'], |
100
|
|
|
$router->matchRequest($request) |
101
|
|
|
); |
102
|
|
|
|
103
|
|
|
$request = Request::create('/users/'); |
104
|
|
|
self::assertEquals( |
105
|
|
|
['controller' => 'users', 'action' => 'index', '_route' => 'frontend.standard'], |
106
|
|
|
$router->matchRequest($request) |
107
|
|
|
); |
108
|
|
|
|
109
|
|
|
$request = Request::create('/users'); |
110
|
|
|
self::assertEquals( |
111
|
|
|
['controller' => 'users', 'action' => 'index', '_route' => 'frontend.standard'], |
112
|
|
|
$router->matchRequest($request) |
113
|
|
|
); |
114
|
|
|
} |
115
|
|
|
} |
116
|
|
|
|
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.