Skip to content
Browse files

Adding tests for locations

  • Loading branch information...
1 parent f4c2674 commit 420c59a322c38828993fc892c0e1e3ef359b5bb3 @jails jails committed Apr 6, 2012
Showing with 78 additions and 13 deletions.
  1. +2 −2 net/http/Media.php
  2. +8 −4 net/http/Router.php
  3. +61 −7 tests/cases/net/http/RouterTest.php
  4. +7 −0 tests/cases/template/helper/HtmlTest.php
View
4 net/http/Media.php
@@ -33,14 +33,14 @@
class Media extends \lithium\core\StaticObject {
/**
- * Contain the configuration of router locations.
+ * Contain the configuration of locations.
*
* @var array of locations
*/
protected static $_locations = null;
/**
- * Stores the name of the location to use for building assets paths.
+ * Stores the name of the location in use.
* If set to `false`, no location is used.
*
* @see LF\Net\Http\Media::location()
View
12 net/http/Router.php
@@ -46,14 +46,14 @@
class Router extends \lithium\core\StaticObject {
/**
- * Contain the configuration of router locations.
+ * Contain the configuration of locations.
*
* @var array of locations
*/
protected static $_locations = null;
/**
- * Stores the name of the location to use.
+ * Stores the name of the location in use.
* If set to `false`, no location is used.
* saved
* @see lithium\net\http\Router::location()
@@ -272,9 +272,12 @@ public static function match($url = array(), $context = null, array $options = a
}
$options += array('location' => static::getLocation());
+ $vars = array();
$name = $options['location'];
-
- if($name && $config = static::location($name)) {
+ if(is_array($name)){
+ list($name, $vars) = each($name);
+ }
+ if($name && $config = static::location($name, null, $vars)) {
$config['host'] = $config['host'] ?: $defaults['host'];
$config['scheme'] = $config['scheme'] ?: $defaults['scheme'];
$defaults = array_merge($defaults, $config);
@@ -531,6 +534,7 @@ protected static function _parseString($path, $options) {
* ));
* }}}
*
+ * NOTICE : The following shouldn't be used directly
* To get the parsed location with some variables, use it as following :
* {{{
* Router::location('app', null, array(
View
68 tests/cases/net/http/RouterTest.php
@@ -1136,8 +1136,7 @@ public function testLocationWithRelativeBaseAndRequest() {
/**
* When a location is created with empty `'host'`, `'scheme'` or both,
- * the Request `'host'`, `'scheme'` must be used instead (if provided)
- * when a absolute url is asked
+ * the Request's `'host'` and/or `'scheme'` must be used instead (if provided)
*/
public function testOverrideEmptyLocationOptionWithRequest() {
$request = new Request(array(
@@ -1158,7 +1157,10 @@ public function testOverrideEmptyLocationOptionWithRequest() {
$this->assertEqual('/request/base/controller/action/hello', $result);
}
- public function testParseWithUrlRelativeLocation() {
+ /**
+ * Test Router::process with location and absolute = false
+ */
+ public function testProcessWithUrlRelativeLocation() {
Router::connect('/home/welcome', array('Home::default'));
@@ -1266,7 +1268,10 @@ public function testParseWithUrlRelativeLocation() {
$this->assertEqual('app', Router::getLocation());
}
- public function testParseWithUrlAbsoluteLocation() {
+ /**
+ * Test Router::process with location and absolute = true
+ */
+ public function testProcessWithUrlAbsoluteLocation() {
Router::connect('/home/welcome', array('Home::default'));
@@ -1307,7 +1312,10 @@ public function testParseWithUrlAbsoluteLocation() {
$this->assertEqual('app', Router::getLocation());
}
- public function testParseWithHttpsLocation() {
+ /**
+ * Test Router::process with HTTPS scheme
+ */
+ public function testProcessWithHttpsLocation() {
Router::connect('/home/welcome', array('Home::default'));
@@ -1398,6 +1406,9 @@ public function testMatchEnvWithRequest() {
$this->assertTrue($result);
}*/
+ /**
+ * Test environment based location
+ */
public function testMatchEnvWithGlobalRequest() {
$request = new Request(array('env' => array(
'HTTP_HOST' => 'www.amiga.com',
@@ -1451,8 +1462,9 @@ public function testLocationWithSubdomain() {
);
$this->assertEqual($expected, $result);
}
+
/**
- * Test subdomained location and the populated request params
+ * Test Router::process on subdomained location and check the populated request params
*/
public function testLocationPopulatedParamsWithSubdomain() {
$request = new Request(array(
@@ -1473,7 +1485,7 @@ public function testLocationPopulatedParamsWithSubdomain() {
Router::connect('/home/index', array('Home::index'));
Router::endLocation();
- Router::parse($request);
+ Router::process($request);
$expected = array(
'controller' => 'home',
'action' => 'index',
@@ -1485,6 +1497,48 @@ public function testLocationPopulatedParamsWithSubdomain() {
$result = Router::location('app');
$this->assertEqual($expected, $result['values']);
}
+
+ /**
+ * Test Router::match on subdomained location
+ */
+ public function testMatchWithLocationAndSubdomain() {
+ $request = new Request(array(
+ 'url' => '/home/index',
+ 'env' => array(
+ 'HTTP_HOST' => 'bob.amiga.com',
+ 'HTTPS' => false
+ )
+ ));
+ Router::location('app', array(
+ 'absolute' => true,
+ 'host' => '{:subdomain}.amiga.{:tld}',
+ 'scheme' => 'http://',
+ 'base' => '/'
+ ));
+
+ Router::beginLocation('app');
+ Router::connect('/home/index', array('Home::index'));
+ Router::endLocation();
+
+ Router::process($request);
+ $expected = 'http://bob.amiga.com/home/index';
+ $result = Router::match('/home/index', $request);
+ $this->assertEqual($expected, $result);
+
+ $expected = 'http://bob.amiga.com/home/index';
+ $result = Router::match('/home/index', $request, array('location' => 'app'));
+ $this->assertEqual($expected, $result);
+
+ $expected = 'http://max.amiga.com/home/index';
+ $result = Router::match('/home/index', $request, array(
+ 'location' => array(
+ 'app' => array(
+ 'subdomain' => 'max'
+ )
+ )
+ ));
+ $this->assertEqual($expected, $result);
+ }
}
?>
View
7 tests/cases/template/helper/HtmlTest.php
@@ -498,6 +498,13 @@ public function testLocationOption() {
$this->assertFalse(isset($result['options']['location']));
$this->html->style('screen', array('location' => 'app'));
$this->assertEqual('app', $result['options']['location']);
+
+ $this->html->link('home', '/home');
+ $this->assertFalse(isset($result['options']['location']));
+
+ $expected = array('app' => array('domain' => 'bob');
+ $this->html->link('home', '/home', array('location' => $expected ));
+ $this->assertEqual($expected, $result['options']['location']);
}
}

0 comments on commit 420c59a

Please sign in to comment.
Something went wrong with that request. Please try again.