@@ -36,14 +36,38 @@ public function setUp()
3636 return 'well done ' ;
3737 });
3838
39- $ router ->add ('/hello /{person} ' , 'POST ' , function ($ person ){
39+ $ router ->add ('/normal /{person} ' , 'GET ' , function ($ person ) {
4040 return $ person ;
4141 });
4242
43- $ router ->add ('/bye /{n:number} ' , 'GET ' , function ($ number ){
43+ $ router ->add ('/nummeric /{n:number} ' , 'GET ' , function ($ number ) {
4444 return $ number ;
4545 });
4646
47+ $ router ->add ('/alpha-numeric/{an:alphanumeric} ' , 'GET ' , function ($ alphaNumeric ) {
48+ return $ alphaNumeric ;
49+ });
50+
51+ $ router ->add ('/alpha/{a:alpha} ' , 'GET ' , function ($ alpha ) {
52+ return $ alpha ;
53+ });
54+
55+ $ router ->add ('/word/{w:word} ' , 'GET ' , function ($ word ) {
56+ return $ word ;
57+ });
58+
59+ $ router ->add ('/all/{*:all} ' , 'GET ' , function ($ all ) {
60+ return $ all ;
61+ });
62+
63+ $ router ->add ('/optional/{a:alfa}{?:optional} ' , 'GET ' , function ($ alfa , $ optional = null ) {
64+ return $ alfa ;
65+ });
66+
67+ $ router ->add ('/optional-slug/{?:optional} ' , 'GET ' , function ($ optional = null ) {
68+ return $ optional ;
69+ });
70+
4771 $ router ->add ('/call/controller ' , 'GET ' , 'TestController::index ' );
4872
4973 $ this ->router = $ router ;
@@ -60,38 +84,108 @@ public function testBasicResolve()
6084 $ this ->assertInstanceOf ('Closure ' , $ response ['handler ' ]);
6185 }
6286
87+ /**
88+ * Test simple route with wrong method
89+ */
90+ public function testBasicResolveWithWrongMethodType ()
91+ {
92+ $ response = $ this ->router ->resolve ('/test ' , 'POST ' );
93+
94+ $ this ->assertEquals (\Szenis \Routing \Route::STATUS_NOT_FOUND , $ response ['code ' ]);
95+ }
96+
6397 /**
6498 * Test route with a argument
6599 */
66100 public function testResolveWithNormalArgument ()
67101 {
68- $ response = $ this ->router ->resolve ('/hello/mike ' , 'POST ' );
102+ $ response = $ this ->router ->resolve ('/normal/test-123 ' , 'GET ' );
69103
70104 $ this ->assertEquals (\Szenis \Routing \Route::STATUS_FOUND , $ response ['code ' ]);
71- $ this ->assertEquals ('mike ' , $ response ['arguments ' ]['person ' ]);
105+ $ this ->assertEquals ('test-123 ' , $ response ['arguments ' ]['person ' ]);
72106 }
73107
74108 /**
75109 * Test route with special argument (numeric only)
110+ *
111+ * /nummeric/{n:number} should match /nummeric/12345 but not /nummeric/test
76112 */
77- public function testResolveWithSpecialArgument ()
113+ public function testResolveWithNumericArgument ()
78114 {
79- $ response = $ this ->router ->resolve ('/bye/97722 ' , 'GET ' );
115+ $ response = $ this ->router ->resolve ('/nummeric/12345 ' , 'GET ' );
80116
81117 $ this ->assertEquals (\Szenis \Routing \Route::STATUS_FOUND , $ response ['code ' ]);
82- $ this ->assertEquals ('97722 ' , $ response ['arguments ' ]['number ' ]);
118+ $ this ->assertEquals ('12345 ' , $ response ['arguments ' ]['number ' ]);
119+
120+ $ response = $ this ->router ->resolve ('/nummeric/test ' , 'GET ' );
121+
122+ $ this ->assertEquals (\Szenis \Routing \Route::STATUS_NOT_FOUND , $ response ['code ' ]);
83123 }
84124
85125 /**
86- * Test route not found exception
126+ * Test route with alpha numeric parameter
127+ *
128+ * /alpha-numeric/{an:alphanumeric} should match /alpha-numeric/test12345 but not /alpha-numeric/test-12345
87129 */
88- public function testRouteNotFoundException ()
130+ public function testResolveWithAlphaNumericArgument ()
89131 {
90- $ response = $ this ->router ->resolve ('/bye/mike ' , 'GET ' );
132+ $ response = $ this ->router ->resolve ('/alpha-numeric/test12345 ' , 'GET ' );
133+
134+ $ this ->assertEquals (\Szenis \Routing \Route::STATUS_FOUND , $ response ['code ' ]);
135+ $ this ->assertEquals ('test12345 ' , $ response ['arguments ' ]['alphanumeric ' ]);
136+
137+ $ response = $ this ->router ->resolve ('/alpha-numeric/test-12345 ' , 'GET ' );
91138
92139 $ this ->assertEquals (\Szenis \Routing \Route::STATUS_NOT_FOUND , $ response ['code ' ]);
93140 }
94141
142+ /**
143+ * Test route with alpha parameter
144+ *
145+ * /alpha/{a:alpha} should match /alpha/test but not /alpha/12345
146+ */
147+ public function testResolveWithAlphaArgument ()
148+ {
149+ $ response = $ this ->router ->resolve ('/alpha/test ' , 'GET ' );
150+
151+ $ this ->assertEquals (\Szenis \Routing \Route::STATUS_FOUND , $ response ['code ' ]);
152+ $ this ->assertEquals ('test ' , $ response ['arguments ' ]['alpha ' ]);
153+
154+ $ response = $ this ->router ->resolve ('/alpha/12345 ' , 'GET ' );
155+
156+ $ this ->assertEquals (\Szenis \Routing \Route::STATUS_NOT_FOUND , $ response ['code ' ]);
157+ }
158+
159+ /**
160+ * Test route with word parameter
161+ *
162+ * /word/{w:word} should match /word/this-is-a-word-12345 but not /word/special:char
163+ */
164+ public function testResolveWithWord ()
165+ {
166+ $ response = $ this ->router ->resolve ('/word/this-is-a-word-12345 ' , 'GET ' );
167+
168+ $ this ->assertEquals (\Szenis \Routing \Route::STATUS_FOUND , $ response ['code ' ]);
169+ $ this ->assertEquals ('this-is-a-word-12345 ' , $ response ['arguments ' ]['word ' ]);
170+
171+ $ response = $ this ->router ->resolve ('/word/special:char ' , 'GET ' );
172+
173+ $ this ->assertEquals (\Szenis \Routing \Route::STATUS_NOT_FOUND , $ response ['code ' ]);
174+ }
175+
176+ /**
177+ * Test route with 'all' parameter
178+ *
179+ * /all/{*:all} should match anything after the /all so /all/test/123/slug-123/special:char should work
180+ */
181+ public function testResolveWithAll ()
182+ {
183+ $ response = $ this ->router ->resolve ('/all/test/123/slug-123/special:char ' , 'GET ' );
184+
185+ $ this ->assertEquals (\Szenis \Routing \Route::STATUS_FOUND , $ response ['code ' ]);
186+ $ this ->assertEquals ('test/123/slug-123/special:char ' , $ response ['arguments ' ]['all ' ]);
187+ }
188+
95189 /**
96190 * Test route with callable path
97191 */
@@ -102,4 +196,42 @@ public function testResolveWithClassPath()
102196 $ this ->assertEquals (\Szenis \Routing \Route::STATUS_FOUND , $ response ['code ' ]);
103197 $ this ->assertEquals ('TestController::index ' , $ response ['handler ' ]);
104198 }
199+
200+ /**
201+ * Test if a route can have a normal and optional argument in the same part of the slug
202+ *
203+ * /{a:alfa}{?:optional} should match things like /hello123 where 123 would be the optional part.
204+ */
205+ public function testResolveWithAlfaAndOptional ()
206+ {
207+ $ response = $ this ->router ->resolve ('/optional/hello123 ' , 'GET ' );
208+
209+ $ this ->assertEquals (\Szenis \Routing \Route::STATUS_FOUND , $ response ['code ' ]);
210+ $ this ->assertEquals ('hello ' , $ response ['arguments ' ]['alfa ' ]);
211+ $ this ->assertEquals (123 , $ response ['arguments ' ]['optional ' ]);
212+
213+ $ response = $ this ->router ->resolve ('/optional/hello ' , 'GET ' );
214+
215+ $ this ->assertEquals (\Szenis \Routing \Route::STATUS_FOUND , $ response ['code ' ]);
216+ $ this ->assertEquals ('hello ' , $ response ['arguments ' ]['alfa ' ]);
217+ $ this ->assertEquals (null , $ response ['arguments ' ]['optional ' ]);
218+ }
219+
220+ /**
221+ * Test if an optional parameter also works when divided by a slash.
222+ *
223+ * /optional-slug/{?:optional} should match both /optional-slug and /optional-slug/test
224+ */
225+ public function testResolveWithOptional ()
226+ {
227+ $ response = $ this ->router ->resolve ('/optional-slug ' , 'GET ' );
228+
229+ $ this ->assertEquals (\Szenis \Routing \Route::STATUS_FOUND , $ response ['code ' ]);
230+ $ this ->assertEquals (null , $ response ['arguments ' ]['optional ' ]);
231+
232+ $ response = $ this ->router ->resolve ('/optional-slug/test ' , 'GET ' );
233+
234+ $ this ->assertEquals (\Szenis \Routing \Route::STATUS_FOUND , $ response ['code ' ]);
235+ $ this ->assertEquals ('test ' , $ response ['arguments ' ]['optional ' ]);
236+ }
105237}
0 commit comments