/** * Tests that the client attempts to load a cached response from the * cache library, but fails. * * @return void */ public function test_cache_miss() { $route = new Route('welcome/index'); $route->defaults(array('controller' => 'Kohana_Request_CacheTest_Dummy', 'action' => 'index')); $request = new Request('welcome/index', NULL, array($route)); $cache_mock = $this->_get_cache_mock(); $request->client()->cache(HTTP_Cache::factory($cache_mock)); $cache_mock->expects($this->once())->method('get')->with($request->client()->cache()->create_cache_key($request))->will($this->returnValue(FALSE)); $response = $request->client()->execute($request); $this->assertSame(HTTP_Cache::CACHE_STATUS_MISS, $response->headers(HTTP_Cache::CACHE_STATUS_KEY)); }
/** * Defaults specified with defaults() should be used if their values aren't * present in the uri * * @test * @covers Route */ function testDefaultsAreUsedIfParamsArentSpecified() { $route = new Route('(<controller>(/<action>(/<id>)))'); $route->defaults(array('controller' => 'welcome', 'action' => 'index')); $matches = $route->matches(''); $this->assertType('array', $matches); $this->assertArrayHasKey('controller', $matches); $this->assertArrayHasKey('action', $matches); $this->assertArrayNotHasKey('id', $matches); $this->assertSame(2, count($matches)); $this->assertSame('welcome', $matches['controller']); $this->assertSame('index', $matches['action']); }
/** * Create a Route from an array. * * @param array $data * @return Route */ public static function fromArray(array $data) : self { $methods = ['*']; if (isset($data['methods'])) { $methods = preg_split('/\\W+/', strtoupper($data['methods'])); } $path = '/' . ltrim($data['path'], '/'); $route = new Route(); $route->path($path)->methods(...$methods)->handler($data['handler']); if (isset($data['defaults'])) { $route->defaults($data['defaults']); } if (isset($dat['filters'])) { $route->filters(...$data['filters']); } return $route; }
/** * Ensure that parameters can be read * * @test */ public function test_param() { $uri = 'foo/bar/id'; $request = Request::factory($uri); $this->assertArrayHasKey('id', $request->param()); $this->assertArrayNotHasKey('foo', $request->param()); $this->assertEquals($request->uri(), $uri); // Ensure the params do not contain contamination from controller, action, route, uri etc etc $params = $request->param(); // Test for illegal components $this->assertArrayNotHasKey('controller', $params); $this->assertArrayNotHasKey('action', $params); $this->assertArrayNotHasKey('directory', $params); $this->assertArrayNotHasKey('uri', $params); $this->assertArrayNotHasKey('route', $params); $route = new Route('(<uri>)', array('uri' => '.+')); $route->defaults(array('controller' => 'foobar', 'action' => 'index')); $request = Request::factory('foobar', NULL, array($route)); $this->assertSame('foobar', $request->param('uri')); }
public static function uri($page_id, $name, array $params = NULL, $region = TRUE) { if (empty(self::$dynamic_routes)) { foreach (Kohana::list_files('config' . DIRECTORY_SEPARATOR . 'routes') as $file_key => $file) { $_tmp = explode(DIRECTORY_SEPARATOR, $file_key); $config_file_name = array_pop($_tmp); $routes_config = Kohana::$config->load('routes/' . str_replace('.php', '', $config_file_name))->as_array(); foreach ($routes_config as $key => $value) { $route = new Route(Arr::get($value, 'uri_callback'), Arr::get($value, 'regex')); $route->defaults(Arr::get($value, 'defaults')); self::$dynamic_routes[$key] = $route; } } } if (isset(self::$dynamic_routes[$name])) { $base_uri = $page_id !== FALSE ? Page_Route::dynamic_base_uri($page_id) : '_module/' . $name; $route_uri = self::$dynamic_routes[$name]->uri($params, $region); return $base_uri . $route_uri; } return NULL; }
/** * Tests that route filters can modify parameters * * @covers Route::filter * @dataProvider provider_route_filter_modify_params */ public function test_route_filter_modify_params($route, $defaults, $filter, $uri, $expected_params) { $route = new Route($route); // Mock a request class $request = $this->get_request_mock($uri); $params = $route->defaults($defaults)->filter($filter)->matches($request); $this->assertSame($expected_params, $params); }
/** * Defaults specified with defaults() should be used if their values aren't * present in the uri * * @dataProvider provider_defaults_are_used_if_params_arent_specified * * @test * @covers Route::matches */ public function test_defaults_are_used_if_params_arent_specified($uri, $regex, $defaults, $c, $a, $test_uri, $test_uri_array, $default_uri) { $route = new Route($uri, $regex); $route->defaults($defaults); $matches = $route->matches($default_uri); $this->assertInternalType('array', $matches); $this->assertArrayHasKey('controller', $matches); $this->assertArrayHasKey('action', $matches); $this->assertArrayNotHasKey('id', $matches); // $this->assertSame(4, count($matches)); $this->assertSame($c, $matches['controller']); $this->assertSame($a, $matches['action']); $this->assertSame($test_uri, $route->uri($test_uri_array)); $this->assertSame($default_uri, $route->uri()); }
/** * @covers ::set_routes */ public function test_set_routes() { $this->env->backup_and_set(array('site-versions.versions' => array('test' => array()))); $this->assertCount(1, Route::all()); $version = new Site_Version('test'); $version->set_routes(array('homepage' => array('home(/<id>)', array('action' => '\\d+'), array('controller' => 'test', 'action' => 'index')))); $this->assertCount(2, Route::all()); $expected = new Route('home(/<id>)', array('action' => '\\d+')); $expected->defaults(array('controller' => 'test', 'action' => 'index')); $this->assertEquals($expected, Route::get('homepage')); }
/** * Defaults specified with defaults() should be used if their values aren't * present in the uri * * @test * @covers Route::matches */ public function test_defaults_are_used_if_params_arent_specified() { $route = new Route('(<controller>(/<action>(/<id>)))'); $route->defaults(array('controller' => 'welcome', 'action' => 'index')); $matches = $route->matches(''); $this->assertType('array', $matches); $this->assertArrayHasKey('controller', $matches); $this->assertArrayHasKey('action', $matches); $this->assertArrayNotHasKey('id', $matches); $this->assertSame(2, count($matches)); $this->assertSame('welcome', $matches['controller']); $this->assertSame('index', $matches['action']); $this->assertSame('unit/test/1', $route->uri(array('controller' => 'unit', 'action' => 'test', 'id' => '1'))); $this->assertSame('welcome/index', $route->uri()); }
/** * Provides data for test_uri_only_trimed_on_internal() * * @return array */ public function provider_uri_only_trimed_on_internal() { // issue #3967: inject the route so that we don't conflict with the application's default route $route = new Route('(<controller>(/<action>))'); $route->defaults(array('controller' => 'welcome', 'action' => 'index')); $old_request = Request::$initial; Request::$initial = new Request(TRUE, array(), TRUE, array($route)); $result = array(array(new Request('http://www.google.com'), 'http://www.google.com'), array(new Request('http://www.google.com/'), 'http://www.google.com/'), array(new Request('foo/bar/'), 'foo/bar'), array(new Request('foo/bar'), 'foo/bar'), array(new Request('/'), '/'), array(new Request(''), '/')); Request::$initial = $old_request; return $result; }
/** * Tests that route filters can modify parameters * * @covers Route::filter * @dataProvider provider_route_filter_modify_params */ public function test_route_filter_modify_params($route, $defaults, $filter, $uri, $expected_params) { $route = new Route($route); // Mock a request class $request = $this->getMock('Request', array('uri'), array($uri)); $request->expects($this->any())->method('uri')->will($this->returnValue($uri)); $params = $route->defaults($defaults)->filter($filter)->matches($request); $this->assertSame($expected_params, $params); }
/** * Ensure that parameters can be read * * @test */ public function test_param() { $route = new Route('(<controller>(/<action>(/<id>)))'); $uri = 'foo/bar/id'; $request = Request::factory($uri, NULL, TRUE, array($route)); // We need to execute the request before it has matched a route try { $request->execute(); } catch (Exception $e) { } $this->assertArrayHasKey('id', $request->param()); $this->assertArrayNotHasKey('foo', $request->param()); $this->assertEquals($request->uri(), $uri); // Ensure the params do not contain contamination from controller, action, route, uri etc etc $params = $request->param(); // Test for illegal components $this->assertArrayNotHasKey('controller', $params); $this->assertArrayNotHasKey('action', $params); $this->assertArrayNotHasKey('directory', $params); $this->assertArrayNotHasKey('uri', $params); $this->assertArrayNotHasKey('route', $params); $route = new Route('(<uri>)', array('uri' => '.+')); $route->defaults(array('controller' => 'foobar', 'action' => 'index')); $request = Request::factory('foobar', NULL, TRUE, array($route)); // We need to execute the request before it has matched a route try { $request->execute(); } catch (Exception $e) { } $this->assertSame('foobar', $request->param('uri')); }
public function set_module_routes($routes_config, $uri_base, $prefix, $cache) { $routes = array(); foreach ($routes_config as $name => $route) { $name = $prefix . '<->' . $name; $uri_callback = Arr::get($route, 'uri_callback'); if (is_string($uri_callback)) { $uri_callback = $uri_base . $uri_callback; } $regex = Arr::get($route, 'regex'); $defaults = Arr::get($route, 'defaults'); $route = new Route($uri_callback, $regex, $name); $route->defaults($defaults); $routes[$name] = $route; Route::set($name, $uri_callback, $regex)->defaults($defaults); } $processed_uri = Request::process_uri($this->uri(), $routes); if ($processed_uri !== NULL) { $this->set_dinamic_route($processed_uri, $cache); } }