1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Nip\Router\Tests; |
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\Fixtures\Application\Library\Router\Route\StandardRoute; |
10
|
|
|
use Symfony\Component\Routing\Exception\ResourceNotFoundException; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Class RouterTest |
14
|
|
|
* @package Nip\Router\Tests |
15
|
|
|
*/ |
16
|
|
|
class RouterTest extends AbstractTest |
17
|
|
|
{ |
18
|
|
|
public function testMatchRequest404() |
19
|
|
|
{ |
20
|
|
|
$router = new Router(); |
21
|
|
|
$router->initRouteCollection(); |
22
|
|
|
$collection = $router->getRoutes(); |
23
|
|
|
|
24
|
|
|
RouteFactory::generateLiteralRoute( |
25
|
|
|
$collection, "admin.index", Route::class, "/admin", "/index", |
26
|
|
|
['module' => 'admin'] |
27
|
|
|
); |
28
|
|
|
RouteFactory::generateLiteralRoute( |
29
|
|
|
$collection, "api.index", Route::class, "/api", "/index", |
30
|
|
|
['module' => 'api'] |
31
|
|
|
); |
32
|
|
|
self::assertCount(2, $router->getRoutes()); |
33
|
|
|
|
34
|
|
|
$request = Request::create('/api/404'); |
35
|
|
|
$this->expectException(ResourceNotFoundException::class); |
36
|
|
|
$router->matchRequest($request); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
public function testMatchRequest() |
40
|
|
|
{ |
41
|
|
|
$router = new Router(); |
42
|
|
|
$router->initRouteCollection(); |
43
|
|
|
$collection = $router->getRoutes(); |
44
|
|
|
|
45
|
|
|
RouteFactory::generateLiteralRoute($collection, "admin.index", Route::class, "/admin", "/index", |
46
|
|
|
['module' => 'admin']); |
47
|
|
|
RouteFactory::generateLiteralRoute($collection, "api.index", Route::class, "/api", "/index", |
48
|
|
|
['module' => 'api']); |
49
|
|
|
self::assertCount(2, $router->getRoutes()); |
50
|
|
|
|
51
|
|
|
$request = Request::create('/api/index'); |
52
|
|
|
$params = $router->matchRequest($request); |
53
|
|
|
self::assertEquals( |
54
|
|
|
['module' => 'api', '_route' => 'api.index'], |
55
|
|
|
$params |
56
|
|
|
); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
public function testMatchRequestWithStandardRoute() |
60
|
|
|
{ |
61
|
|
|
$router = new Router(); |
62
|
|
|
$router->initRouteCollection(); |
63
|
|
|
$collection = $router->getRoutes(); |
64
|
|
|
|
65
|
|
|
RouteFactory::generateStandardRoute($collection, "admin.standard", StandardRoute::class, "/admin", |
66
|
|
|
"/:controller/:action", |
67
|
|
|
['module' => 'admin']); |
68
|
|
|
|
69
|
|
|
$request = Request::create('/admin/post/create'); |
70
|
|
|
$params = $router->matchRequest($request); |
71
|
|
|
self::assertEquals( |
72
|
|
|
['module' => 'admin', 'controller' => 'post', 'action' => 'create', '_route' => 'admin.standard'], |
73
|
|
|
$params |
74
|
|
|
); |
75
|
|
|
} |
76
|
|
|
} |
77
|
|
|
|