/** * test that routes match their pattern. * * @return void */ public function testMatchBasic() { $route = new DashedRoute('/:controller/:action/:id', ['plugin' => null]); $result = $route->match(['controller' => 'Posts', 'action' => 'myView', 'plugin' => null]); $this->assertFalse($result); $result = $route->match(['plugin' => null, 'controller' => 'Posts', 'action' => 'myView', 0]); $this->assertFalse($result); $result = $route->match(['plugin' => null, 'controller' => 'MyPosts', 'action' => 'myView', 'id' => 1]); $this->assertEquals('/my-posts/my-view/1', $result); $route = new DashedRoute('/', ['controller' => 'Pages', 'action' => 'myDisplay', 'home']); $result = $route->match(['controller' => 'Pages', 'action' => 'myDisplay', 'home']); $this->assertEquals('/', $result); $result = $route->match(['controller' => 'Pages', 'action' => 'display', 'about']); $this->assertFalse($result); $route = new DashedRoute('/blog/:action', ['controller' => 'Posts']); $result = $route->match(['controller' => 'Posts', 'action' => 'myView']); $this->assertEquals('/blog/my-view', $result); $result = $route->match(['controller' => 'Posts', 'action' => 'myView', 'id' => 2]); $this->assertEquals('/blog/my-view?id=2', $result); $result = $route->match(['controller' => 'Posts', 'action' => 'myView', 1]); $this->assertFalse($result); $route = new DashedRoute('/foo/:controller/:action', ['action' => 'index']); $result = $route->match(['controller' => 'Posts', 'action' => 'myView']); $this->assertEquals('/foo/posts/my-view', $result); $route = new DashedRoute('/:plugin/:id/*', ['controller' => 'Posts', 'action' => 'myView']); $result = $route->match(['plugin' => 'TestPlugin', 'controller' => 'Posts', 'action' => 'myView', 'id' => '1']); $this->assertEquals('/test-plugin/1/', $result); $result = $route->match(['plugin' => 'TestPlugin', 'controller' => 'Posts', 'action' => 'myView', 'id' => '1', '0']); $this->assertEquals('/test-plugin/1/0', $result); $result = $route->match(['plugin' => 'TestPlugin', 'controller' => 'Nodes', 'action' => 'myView', 'id' => 1]); $this->assertFalse($result); $result = $route->match(['plugin' => 'TestPlugin', 'controller' => 'Posts', 'action' => 'edit', 'id' => 1]); $this->assertFalse($result); $route = new DashedRoute('/admin/subscriptions/:action/*', ['controller' => 'Subscribe', 'prefix' => 'admin']); $result = $route->match(['controller' => 'Subscribe', 'prefix' => 'admin', 'action' => 'editAdminE', 1]); $expected = '/admin/subscriptions/edit-admin-e/1'; $this->assertEquals($expected, $result); $route = new DashedRoute('/:controller/:action-:id'); $result = $route->match(['controller' => 'MyPosts', 'action' => 'myView', 'id' => 1]); $this->assertEquals('/my-posts/my-view-1', $result); $route = new DashedRoute('/:controller/:action/:slug-:id', [], ['id' => Router::ID]); $result = $route->match(['controller' => 'MyPosts', 'action' => 'myView', 'id' => 1, 'slug' => 'the-slug']); $this->assertEquals('/my-posts/my-view/the-slug-1', $result); }
/** * @return void */ public function testMatchThenParse() { $route = new DashedRoute('/plugin/:controller/:action', ['plugin' => 'Vendor/PluginName']); $url = $route->match(['plugin' => 'Vendor/PluginName', 'controller' => 'ControllerName', 'action' => 'actionName']); $expectedUrl = '/plugin/controller-name/action-name'; $this->assertEquals($expectedUrl, $url); $result = $route->parse($expectedUrl); $this->assertEquals('ControllerName', $result['controller']); $this->assertEquals('actionName', $result['action']); $this->assertEquals('Vendor/PluginName', $result['plugin']); }