public function testUrl() { $params = []; $router = new Router(); // Set up some dummy rules $router->route('authorDetails', '/authors/:author_id?/:details?'); $router->route('blogPost', 'blog/post'); $router->route('userProfile', 'profile/:username'); $router->route('jobRequest', 'job/:type?request/:id'); $router->route('productPage', '/product/:category?/:id'); $router->route('portfolioPage', '/portfolio/:year?noYear/:category?noCategory/:budget?noBudget'); $result = $router->url('blogPost'); $this->assertEquals('/blog/post', $result); $result = $router->url('authorDetails'); $this->assertEquals('/authors', $result); $result = $router->url('authorDetails', ['author_id' => 20]); $this->assertEquals('/authors/20', $result); $result = $router->url('authorDetails', ['details' => 'history']); $this->assertEquals('/authors/default/history', $result); $result = $router->url('authorDetails', ['author_id' => 'default']); $this->assertEquals('/authors/default', $result); $result = $router->url('userProfile', ['username' => 'shaq']); $this->assertEquals('/profile/shaq', $result); $result = $router->url('jobRequest', ['id' => '9']); $this->assertEquals('/job/request/9', $result); $result = $router->url('jobRequest'); $this->assertEquals('/job/request/default', $result); $result = $router->url('productPage', ['id' => '7']); $this->assertEquals('/product/default/7', $result); $result = $router->url('productPage', ['id' => '7', 'category' => 'helmets']); $this->assertEquals('/product/helmets/7', $result); $result = $router->url('portfolioPage'); $this->assertEquals('/portfolio', $result); $result = $router->url('portfolioPage', ['year' => '2020']); $this->assertEquals('/portfolio/2020', $result); $result = $router->url('portfolioPage', ['category' => 'shoes']); $this->assertEquals('/portfolio/noYear/shoes', $result); $result = $router->url('portfolioPage', ['category' => null, 'budget' => '50000-above']); $this->assertEquals('/portfolio/noYear/noCategory/50000-above', $result); $result = $router->url('portfolioPage', ['year' => false, 'category' => null, 'budget' => 0]); $this->assertEquals('/portfolio/noYear/noCategory/0', $result); $result = $router->url('portfolioPage', ['budget' => 0]); $this->assertEquals('/portfolio/noYear/noCategory/0', $result); $result = $router->url('portfolioPage', ['year' => '2020', 'category' => 'noCategory']); $this->assertEquals('/portfolio/2020', $result); $result = $router->url('portfolioPage', ['year' => 'default', 'category' => 'noCategory', 'budget' => '200-500']); $this->assertEquals('/portfolio/default/noCategory/200-500', $result); }
/** * Autoloads the URL map only allowing a single execution. * @return array Returns the URL map. */ protected function getRouterObject() { if (self::$routerObj !== null) { return self::$routerObj; } /* * Load up each route rule */ $router = new RainRouter(); foreach ($this->getUrlMap() as $pageInfo) { $router->route($pageInfo['file'], $pageInfo['pattern']); } /* * Sort all the rules */ $router->sortRules(); return self::$routerObj = $router; }
public function testUrl() { $params = array(); $router = new Router(); // Set up some dummy rules $router->route('authorDetails', '/authors/:author_id?/:details?'); $router->route('blogPost', 'blog/post'); $router->route('userProfile', 'profile/:username'); $router->route('jobRequest', 'job/:type?request/:id'); $router->route('productPage', '/product/:category?/:id'); $result = $router->url('blogPost'); $this->assertEquals('/blog/post', $result); $result = $router->url('authorDetails'); $this->assertEquals('/authors', $result); $result = $router->url('authorDetails', array('author_id' => 20)); $this->assertEquals('/authors/20', $result); $result = $router->url('authorDetails', array('details' => 'history')); $this->assertEquals('/authors/default/history', $result); $result = $router->url('userProfile', array('username' => 'shaq')); $this->assertEquals('/profile/shaq', $result); $result = $router->url('jobRequest', array('id' => '9')); $this->assertEquals('/job/request/9', $result); $result = $router->url('jobRequest'); $this->assertEquals('/job/request/default', $result); $result = $router->url('productPage', array('id' => '7')); $this->assertEquals('/product/default/7', $result); $result = $router->url('productPage', array('id' => '7', 'category' => 'helmets')); $this->assertEquals('/product/helmets/7', $result); }