@@ -22,6 +22,21 @@ public function test($input, $expected)
2222 }
2323}
2424
25+ class ReplaceTester extends UrlHelper
26+ {
27+ /**
28+ * @param string $template
29+ * @param array $tokens
30+ * @param string $expected
31+ *
32+ * @return string
33+ */
34+ public function test ($ template , $ tokens , $ expected )
35+ {
36+ eq ($ this ->replace ($ template , $ tokens ), $ expected );
37+ }
38+ }
39+
2540class SampleUrlHelper extends UrlHelper
2641{
2742 /**
@@ -40,7 +55,7 @@ class SampleController implements Controller
4055{
4156 public function run ($ id , $ title )
4257 {
43- return array ( $ id , $ title) ;
58+ return [ $ id , $ title] ;
4459 }
4560}
4661
@@ -168,7 +183,7 @@ function () {
168183 function () use ($ router ) {
169184 $ router ->resolve ('GET ' , '/users/bob?crazy-yo ' );
170185 },
171- '#^ ' . preg_quote ('unexpected query string in $url: /users/bob?crazy-yo ' ). '$# '
186+ '#^ ' . preg_quote ('unexpected query string in $url: /users/bob?crazy-yo ' ) . '$# '
172187 );
173188 }
174189);
@@ -331,6 +346,28 @@ function () use ($url) {
331346 }
332347);
333348
349+ test (
350+ 'can replace tokens in templates ' ,
351+ function () {
352+ $ replace = new ReplaceTester ();
353+
354+ $ replace ->test ('foo/<bar> ' , ['bar ' => 'hello ' ], 'foo/hello ' );
355+
356+ $ replace ->test ('foo/<bar:slug> ' , ['bar ' => 'hello ' ], 'foo/hello ' );
357+
358+ $ replace ->test ('foo/<bar:slug>/<baz:\w+> ' , ['bar ' => 'hello ' , 'baz ' => 'world ' ], 'foo/hello/world ' );
359+
360+ expect (
361+ 'InvalidArgumentException ' ,
362+ 'should throw for missing replacements token ' ,
363+ function () use ($ replace ) {
364+ $ replace ->test ('foo/<bar>/<baz> ' , ['bar ' => 'hello ' ], 'foo/hello/ ' );
365+ },
366+ '/for token: baz/ '
367+ );
368+ }
369+ );
370+
334371test (
335372 'can use wildcard in patterns ' ,
336373 function () {
0 commit comments