Skip to content
This repository was archived by the owner on May 8, 2024. It is now read-only.

Commit

Permalink
Add unit tests for chain route
Browse files Browse the repository at this point in the history
  • Loading branch information
DASPRiD committed Apr 28, 2013
1 parent 87f1d36 commit 638ec68
Show file tree
Hide file tree
Showing 3 changed files with 153 additions and 8 deletions.
15 changes: 8 additions & 7 deletions library/Zend/Mvc/Router/Http/Chain.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,9 @@ class Chain extends TreeRouteStack implements RouteInterface
*
* @param array $routes
* @param RoutePluginManager $routePlugins
* @param ArrayObject $prototypes
* @param ArrayObject|null $prototypes
*/
public function __construct(array $routes, RoutePluginManager $routePlugins, ArrayObject $prototypes)
public function __construct(array $routes, RoutePluginManager $routePlugins, ArrayObject $prototypes = null)
{
$this->chainRoutes = array_reverse($routes);
$this->routePluginManager = $routePlugins;
Expand All @@ -68,11 +68,11 @@ public static function factory($options = array())
}

if (!isset($options['routes'])) {
throw new Exception\InvalidArgumentException('Missing "route" in options array');
throw new Exception\InvalidArgumentException('Missing "routes" in options array');
}

if (!isset($options['prototypes'])) {
throw new Exception\InvalidArgumentException('Missing "prototypes" in options array');
$options['prototypes'] = null;
}

if ($options['routes'] instanceof Traversable) {
Expand All @@ -96,9 +96,10 @@ public static function factory($options = array())
* @see \Zend\Mvc\Router\RouteInterface::match()
* @param Request $request
* @param int|null $pathOffset
* @param array $options
* @return RouteMatch|null
*/
public function match(Request $request, $pathOffset = null)
public function match(Request $request, $pathOffset = null, array $options = array())
{
if (!method_exists($request, 'getUri')) {
return null;
Expand All @@ -121,7 +122,7 @@ public function match(Request $request, $pathOffset = null)
$pathLength = strlen($uri->getPath());

foreach ($this->routes as $route) {
$subMatch = $route->match($request, $pathOffset);
$subMatch = $route->match($request, $pathOffset, $options);

if ($subMatch === null) {
return null;
Expand Down Expand Up @@ -161,7 +162,7 @@ public function assemble(array $params = array(), array $options = array())

foreach ($this->routes as $key => $route) {
$chainOptions = $options;
$chainOptions['has_child'] = ($options['has_child'] || $key !== $lastRouteKey);
$chainOptions['has_child'] = ((isset($options['has_child']) ? $options['has_child'] : false) || $key !== $lastRouteKey);

$path .= $route->assemble($params, $chainOptions);
$params = array_diff_key($params, array_flip($route->getAssembledParams()));
Expand Down
2 changes: 1 addition & 1 deletion library/Zend/Mvc/Router/Http/Part.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ class Part extends TreeRouteStack implements RouteInterface
* @param bool $mayTerminate
* @param RoutePluginManager $routePlugins
* @param array|null $childRoutes
* @param ArrayObject $prototypes
* @param ArrayObject|null $prototypes
* @throws Exception\InvalidArgumentException
*/
public function __construct($route, $mayTerminate, RoutePluginManager $routePlugins, array $childRoutes = null, ArrayObject $prototypes = null)
Expand Down
144 changes: 144 additions & 0 deletions tests/ZendTest/Mvc/Router/Http/ChainTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
<?php
/**
* Zend Framework (http://framework.zend.com/)
*
* @link http://github.com/zendframework/zf2 for the canonical source repository
* @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
* @package Zend_Mvc
*/

namespace ZendTest\Mvc\Router\Http;

use PHPUnit_Framework_TestCase as TestCase;
use Zend\Http\Request as Request;
use Zend\Mvc\Router\RoutePluginManager;
use Zend\Mvc\Router\Http\Chain;
use ZendTest\Mvc\Router\FactoryTester;

class ChainTest extends TestCase
{
public static function getRoute()
{
$routePlugins = new RoutePluginManager();

return new Chain(
array(
array(
'type' => 'Zend\Mvc\Router\Http\Segment',
'options' => array(
'route' => '/:controller',
'defaults' => array(
'controller' => 'foo'
)
),
),
array(
'type' => 'Zend\Mvc\Router\Http\Segment',
'options' => array(
'route' => '/:bar',
'defaults' => array(
'bar' => 'bar'
)
),
),
array(
'type' => 'Zend\Mvc\Router\Http\Wildcard',
),
),
$routePlugins
);
}

public static function routeProvider()
{
return array(
'simple-match' => array(
self::getRoute(),
'/foo/bar',
null,
array('controller' => 'foo', 'bar' => 'bar')
),
'offset-skips-beginning' => array(
self::getRoute(),
'/baz/foo/bar',
4,
array('controller' => 'foo', 'bar' => 'bar')
),
'parameters-are-used-only-once' => array(
self::getRoute(),
'/foo/baz',
null,
array('controller' => 'foo', 'bar' => 'baz')
),
);
}

/**
* @dataProvider routeProvider
* @param Chain $route
* @param string $path
* @param integer $offset
* @param array $params
*/
public function testMatching(Chain $route, $path, $offset, array $params = null)
{
$request = new Request();
$request->setUri('http://example.com' . $path);
$match = $route->match($request, $offset);

if ($params === null) {
$this->assertNull($match);
} else {
$this->assertInstanceOf('Zend\Mvc\Router\Http\RouteMatch', $match);

if ($offset === null) {
$this->assertEquals(strlen($path), $match->getLength());
}

foreach ($params as $key => $value) {
$this->assertEquals($value, $match->getParam($key));
}
}
}

/**
* @dataProvider routeProvider
* @param Chain $route
* @param string $path
* @param integer $offset
* @param string $routeName
* @param array $params
*/
public function testAssembling(Chain $route, $path, $offset, array $params = null)
{
if ($params === null) {
// Data which will not match are not tested for assembling.
return;
}

$result = $route->assemble($params);

if ($offset !== null) {
$this->assertEquals($offset, strpos($path, $result, $offset));
} else {
$this->assertEquals($path, $result);
}
}

public function testFactory()
{
$tester = new FactoryTester($this);
$tester->testFactory(
'Zend\Mvc\Router\Http\Chain',
array(
'routes' => 'Missing "routes" in options array',
'route_plugins' => 'Missing "route_plugins" in options array'
),
array(
'routes' => array(),
'route_plugins' => new RoutePluginManager()
)
);
}
}

0 comments on commit 638ec68

Please sign in to comment.