/** * @expectedException PHPUnit_Framework_Error_Warning */ public function testRepeatingTokens() { $router = new Router(); $router->addToken('r', '[a-z]+'); $router->add('repeating-tokens', array('method' => 'GET, POST', 'route' => '/r/r/$subreddit/comments/#thread_id/$thread_slug/')); $router->match('GET', '/r/r/php/comments/12/router/'); }
public function testStaticAndDynamic() { $router = new Router(); $router->add('static-and-dynamic', array('method' => 'GET, POST', 'route' => '/r/$subreddit/comments/#thread_id/$thread_slug/')); $result = $router->match('GET', '/r/php/comments/12/router/'); $expected = array('url' => array('static0' => 'r', 'subreddit' => 'php', 'static1' => 'comments', 'thread_id' => '12', 'thread_slug' => 'router'), 'id' => 'static-and-dynamic', 'method' => array(0 => 'GET', 1 => 'POST'), 'data' => array('static0' => 'r', 'subreddit' => 'php', 'static1' => 'comments', 'thread_id' => '12', 'thread_slug' => 'router', 'action' => 'view')); $this->assertSame($expected, $result); }
public function testCompiledRegex() { $router = new Router($this->router->compile()); $result = $router->match('GET', '/news/add.xml/12/some-slug'); $expected = array('url' => array('controller' => 'news', 'action' => 'add', 'format' => 'xml', 'id' => '12', 'slug' => 'some-slug'), 'id' => 'general', 'method' => 'GET', 'data' => array('controller' => 'news', 'action' => 'add', 'format' => 'xml', 'id' => '12', 'slug' => 'some-slug')); // should use regex instead of compiling $router->compile(); $this->assertSame($expected, $result); }
public function testAdvanced() { $router = new Router(); $router->addToken('y', '[0-9]{4}'); $router->addToken('m', '[0-9]{2}'); $router->addToken('d', '[0-9]{2}'); $router->add('article-with-date-and-slug', array('method' => 'GET, POST', 'route' => '(/controller)(/action(.~format))(/y-m-d(/^slug))', 'defaults' => array('controller' => 'index', 'action' => 'index', 'format' => 'html'))); $result = $router->match('GET', '/articles/2009-01-01/some-slug-for-article'); $expected = array('url' => array('controller' => 'articles', 'y' => '2009', 'm' => '01', 'd' => '01', 'slug' => 'some-slug-for-article'), 'id' => 'article-with-date-and-slug', 'method' => array(0 => 'GET', 1 => 'POST'), 'data' => array('controller' => 'articles', 'action' => 'index', 'format' => 'html', 'y' => '2009', 'm' => '01', 'd' => '01', 'slug' => 'some-slug-for-article')); $this->assertSame($expected, $result); }
<?php use Bike\Router; use Bike\RouterException; try { $router = new Router(); $router->add('controller-only', array('method' => 'GET, TEST', 'route' => '/controller')); $result = $router->match('GET', '/news'); } catch (RouterException $e) { if ($e->getCode() === 1) { $result = $e->getMessage(); } } // output: 'Method TEST is not supported'
<?php use Bike\Router; use Bike\RouterException; try { $router = new Router(); $router->add('all-methods', array('method' => '*', 'route' => '/controller')); $result = $router->match('PUT', '/news'); } catch (RouterException $e) { $result = $e->getMessage(); }