Example #1
0
 public function test_add_pattern()
 {
     $router = new Router();
     $router->addPattern('id', '[a-zA-Z]+');
     $router->get('items/{id}', 'id');
     $router->group(function (IRouter $router) {
         $router->get('items/{id}/slug', 'slug');
     });
     $this->assertNotNull($router->match(HttpRequestMethod::GET, new Url('items/foo')));
     $this->assertNotNull($router->match(HttpRequestMethod::GET, new Url('items/foo/slug')));
     $this->assertNull($router->match(HttpRequestMethod::GET, new Url('items/1')));
     $this->assertNull($router->match(HttpRequestMethod::GET, new Url('items/1/slug')));
     $router->addPattern('id', '[0-9]+');
     $this->assertNotNull($router->match(HttpRequestMethod::GET, new Url('items/1')));
     $this->assertNull($router->match(HttpRequestMethod::GET, new Url('items/1/slug')));
     $this->assertNull($router->match(HttpRequestMethod::GET, new Url('items/foo')));
     $this->assertNotNull($router->match(HttpRequestMethod::GET, new Url('items/foo/slug')));
     $router->group(function (IRouter $router) {
         $router->addPattern('id', '[0-9]+');
         $router->get('items/{id}/slug', 'slug');
     });
     $this->assertNotNull($router->match(HttpRequestMethod::GET, new Url('items/1')));
     $this->assertNotNull($router->match(HttpRequestMethod::GET, new Url('items/1/slug')));
     $this->assertNull($router->match(HttpRequestMethod::GET, new Url('items/foo')));
     $this->assertNotNull($router->match(HttpRequestMethod::GET, new Url('items/foo/slug')));
 }