1+ <?php
2+
3+ use Rakit \Framework \App ;
4+ use Rakit \Framework \Router \Route ;
5+
6+ class RouterTests extends PHPUnit_Framework_TestCase {
7+
8+ protected $ app ;
9+
10+ public function setUp () {
11+ $ this ->app = new App ('router-test ' , ['app ' => ['debug ' => true ]]);
12+ $ this ->router = $ this ->app ->router ;
13+ }
14+
15+ public function tearDown () {
16+ $ this ->app = null ;
17+ }
18+
19+ public function testDispatchRouteGet ()
20+ {
21+ $ this ->router ->post ('/route/:param ' , 'handler ' );
22+ $ this ->router ->get ('/route/:param ' , 'handler ' );
23+ $ this ->router ->put ('/route/:param ' , 'handler ' );
24+
25+ $ route = $ this ->router ->dispatch ('GET ' , '/route/value ' );
26+
27+ $ this ->assertInstanceOf ('Rakit\Framework\Router\Route ' , $ route );
28+ $ this ->assertEquals ('GET ' , $ route ->getMethod ());
29+ }
30+
31+ public function testDispatchRoutePost ()
32+ {
33+ $ this ->router ->get ('/route/:param ' , 'handler ' );
34+ $ this ->router ->post ('/route/:param ' , 'handler ' );
35+ $ this ->router ->put ('/route/:param ' , 'handler ' );
36+
37+ $ route = $ this ->router ->dispatch ('POST ' , '/route/value ' );
38+
39+ $ this ->assertInstanceOf ('Rakit\Framework\Router\Route ' , $ route );
40+ $ this ->assertEquals ('POST ' , $ route ->getMethod ());
41+ }
42+
43+ public function testDispatchRoutePut ()
44+ {
45+ $ this ->router ->get ('/route/:param ' , 'handler ' );
46+ $ this ->router ->put ('/route/:param ' , 'handler ' );
47+ $ this ->router ->post ('/route/:param ' , 'handler ' );
48+
49+ $ route = $ this ->router ->dispatch ('PUT ' , '/route/value ' );
50+
51+ $ this ->assertInstanceOf ('Rakit\Framework\Router\Route ' , $ route );
52+ $ this ->assertEquals ('PUT ' , $ route ->getMethod ());
53+ }
54+
55+ public function testDispatchRoutePatch ()
56+ {
57+ $ this ->router ->get ('/route/:param ' , 'handler ' );
58+ $ this ->router ->patch ('/route/:param ' , 'handler ' );
59+ $ this ->router ->post ('/route/:param ' , 'handler ' );
60+
61+ $ route = $ this ->router ->dispatch ('PATCH ' , '/route/value ' );
62+
63+ $ this ->assertInstanceOf ('Rakit\Framework\Router\Route ' , $ route );
64+ $ this ->assertEquals ('PATCH ' , $ route ->getMethod ());
65+ }
66+
67+ public function testDispatchRouteDelete ()
68+ {
69+ $ this ->router ->get ('/route/:param ' , 'handler ' );
70+ $ this ->router ->delete ('/route/:param ' , 'handler ' );
71+ $ this ->router ->post ('/route/:param ' , 'handler ' );
72+
73+ $ route = $ this ->router ->dispatch ('DELETE ' , '/route/value ' );
74+
75+ $ this ->assertInstanceOf ('Rakit\Framework\Router\Route ' , $ route );
76+ $ this ->assertEquals ('DELETE ' , $ route ->getMethod ());
77+ }
78+
79+ public function testDispatchParameters ()
80+ {
81+ $ this ->router ->get ('/route/:foo/:bar ' , 'handler ' );
82+
83+ $ route = $ this ->router ->dispatch ('GET ' , '/route/param1/param2 ' );
84+
85+ $ this ->assertInstanceOf ('Rakit\Framework\Router\Route ' , $ route );
86+
87+ $ params = $ route ->params ;
88+ $ this ->assertEquals (2 , count ($ params ));
89+ $ this ->assertEquals ($ params ['foo ' ], 'param1 ' );
90+ $ this ->assertEquals ($ params ['bar ' ], 'param2 ' );
91+ }
92+
93+ public function testDispatchOptionalParameter ()
94+ {
95+ $ this ->router ->get ('/route/:foo(/:bar(/:baz)) ' , 'handler ' );
96+
97+ foreach (['/route/one ' => 1 , '/route/one/two ' => 2 , '/route/one/two/three ' => 3 ] as $ path => $ expected_count ) {
98+ $ route = $ this ->router ->dispatch ('GET ' , $ path );
99+ $ this ->assertInstanceOf ('Rakit\Framework\Router\Route ' , $ route );
100+ $ this ->assertEquals ($ expected_count , count ($ route ->params ));
101+ }
102+ }
103+
104+ public function testDispatchCustomRegexParameter ()
105+ {
106+ $ this ->router ->get ('/edit/:id ' , 'handler ' )->where ('id ' , '\d{2} ' );
107+
108+ $ tests = [
109+ '/edit/foo ' => false ,
110+ '/edit/12foo ' => false ,
111+ '/edit/foo99 ' => false ,
112+ '/edit/1 ' => false ,
113+ '/edit/123 ' => false ,
114+ '/edit/11 ' => true ,
115+ ];
116+
117+ foreach ($ tests as $ path => $ expected ) {
118+ $ route = $ this ->router ->dispatch ('GET ' , $ path );
119+ $ this ->assertEquals ($ expected , !is_null ($ route ));
120+ }
121+ }
122+
123+ public function testRouteNaming ()
124+ {
125+ $ this ->router ->get ('/login ' , 'handler ' )->name ('form-login ' );
126+ $ this ->router ->post ('/login ' , 'handler ' )->name ('post-login ' );
127+
128+ $ route = $ this ->router ->findRouteByName ('post-login ' );
129+
130+ $ this ->assertInstanceOf ('Rakit\Framework\Router\Route ' , $ route );
131+ $ this ->assertEquals ('POST ' , $ route ->getMethod ());
132+ }
133+
134+ }
0 commit comments