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
|
|
View Code Duplication |
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
|
|
View Code Duplication |
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
|
|
|
/** |
51
|
|
|
* @dataProvider dataRouteDynamic |
52
|
|
|
*/ |
53
|
|
|
public function testRouteDynamic($uri, $routeName, $params) |
|
|
|
|
54
|
|
|
{ |
55
|
|
|
$router = new Router(); |
56
|
|
|
$collection = $router->getRoutes(); |
57
|
|
|
|
58
|
|
|
RouteFactory::generateStandardRoute( |
59
|
|
|
$collection, |
|
|
|
|
60
|
|
|
"admin.standard", |
61
|
|
|
StandardRoute::class, |
62
|
|
|
"/admin", |
63
|
|
|
'/:controller/:action', ['module' => 'admin']); |
64
|
|
|
RouteFactory::generateStandardRoute( |
65
|
|
|
$collection, "api.standard", |
|
|
|
|
66
|
|
|
StandardRoute::class, |
67
|
|
|
"/api", |
68
|
|
|
'/:controller/:action', ['module' => 'api']); |
69
|
|
|
|
70
|
|
|
RouteFactory::generateStandardRoute( |
71
|
|
|
$collection, |
|
|
|
|
72
|
|
|
"frontend.standard", |
73
|
|
|
StandardRoute::class, |
74
|
|
|
"/"); |
75
|
|
|
|
76
|
|
|
$request = Request::create($uri); |
77
|
|
|
$matches = $router->matchRequest($request); |
78
|
|
|
|
79
|
|
|
self::assertArrayHasKey('_route', $matches); |
80
|
|
|
|
81
|
|
|
foreach ($params as $param=>$value) { |
82
|
|
|
self::assertArrayHasKey($param, $matches); |
83
|
|
|
self::assertSame($matches[$param], $value); |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* @return array |
89
|
|
|
*/ |
90
|
|
|
public function dataRouteDynamic() |
91
|
|
|
{ |
92
|
|
|
return [ |
93
|
|
|
['/api/pages', 'api.standard', ['module' => 'api', 'controller' => 'pages', 'action' => 'index']], |
94
|
|
|
['/api/pages/', 'api.standard', ['module' => 'api', 'controller' => 'pages', 'action' => 'index']], |
95
|
|
|
['/api/pages/delete', 'api.standard', ['module' => 'api', 'controller' => 'pages', 'action' => 'delete']], |
96
|
|
|
['/admin/pages/delete', 'admin.standard', ['module' => 'admin', 'controller' => 'pages', 'action' => 'delete']], |
97
|
|
|
['/users/delete', 'frontend.standard', ['controller' => 'users', 'action' => 'delete']], |
98
|
|
|
['/users/', 'frontend.standard', ['controller' => 'users', 'action' => 'index']], |
99
|
|
|
['/users', 'frontend.standard', ['controller' => 'users', 'action' => 'index']], |
100
|
|
|
]; |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.