public function testGenerateShortRoute() { $router = new Router(); $router->get('something/{a}/{b}/{c}')->name('namedRoute'); $generated = $router->to('namedRoute', ['a' => 'paramA', 'c' => 'paramC', 'b' => 'paramB', 'extra' => 'foobar', 'other' => 'baz']); $this->assertEquals('something/paramA/paramB/paramC?extra=foobar&other=baz', $generated); }
public function testCallbacksAreCalled() { $called = false; $this->router->resource('singular')->onMatch(function ($action) use(&$called) { $this->assertEquals('edit', $action); $called = true; }); $this->router->match(new Request('GET', 'singular/edit')); $this->assertTrue($called); }
private function addRoutes($actions, $namedActionName, $unnamedActionName, $actionPathBase) { $unnamedAdded = false; foreach ($actions as $action => $method) { if (in_array($action, $this->unnamedActions)) { $initializer = $this->owner->add($method, $actionPathBase); if (!$unnamedAdded) { $unnamedAdded = true; $initializer->name($unnamedActionName); } } else { $initializer = $this->owner->add($method, $actionPathBase . '/' . $action)->name($action . '_' . $namedActionName); } $initializer->extras(['controller' => $this->controllerName, 'action' => $action]); if ($this->onMatch !== null) { $initializer->onMatch($this->onMatch); } } }
public function testBasePath() { $called = false; $config = new Configuration(); $config->basePath = 'base/'; $router = new Router($config); $router->get('hello')->name('hello')->onMatch(function () use(&$called) { $called = true; }); $_SERVER['REQUEST_METHOD'] = 'GET'; $_SERVER['REQUEST_URI'] = 'base/hello'; $router->matchCurrentRequest(); $this->assertTrue($called); $this->assertEquals('base/hello', $router->to('hello')); }