mirror of
https://github.com/dannyvankooten/AltoRouter.git
synced 2025-08-04 23:47:30 +02:00
Merge pull request #68 from koenpunt/fix-coverage
Add addRoutes method for bulk adding routes, props @koenpunt
This commit is contained in:
@@ -4,4 +4,4 @@ php:
|
||||
- 5.4
|
||||
- 5.5
|
||||
|
||||
script: phpunit ./
|
||||
script: phpunit --coverage-text ./
|
||||
|
@@ -22,11 +22,28 @@ class AltoRouter {
|
||||
* @param array $matchTypes
|
||||
*/
|
||||
public function __construct( $routes = array(), $basePath = '', $matchTypes = array() ) {
|
||||
$this->addRoutes($routes);
|
||||
$this->setBasePath($basePath);
|
||||
$this->addMatchTypes($matchTypes);
|
||||
}
|
||||
|
||||
foreach( $routes as $route ) {
|
||||
call_user_func_array(array($this,'map'),$route);
|
||||
/**
|
||||
* Add multiple routes at once from array in the following format:
|
||||
*
|
||||
* $routes = array(
|
||||
* array($method, $route, $target, $name)
|
||||
* );
|
||||
*
|
||||
* @param array $routes
|
||||
* @return void
|
||||
* @author Koen Punt
|
||||
*/
|
||||
public function addRoutes($routes){
|
||||
if(!is_array($routes) && !$routes instanceof Traversable) {
|
||||
throw new \Exception('Routes should be an array or an instance of Traversable');
|
||||
}
|
||||
foreach($routes as $route) {
|
||||
call_user_func_array(array($this, 'map'), $route);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -54,7 +71,6 @@ class AltoRouter {
|
||||
* @param string $route The route regex, custom regex must start with an @. You can use multiple pre-set regex filters, like [i:id]
|
||||
* @param mixed $target The target where this route should point to. Can be anything.
|
||||
* @param string $name Optional name of this route. Supply if you want to reverse route this url in your application.
|
||||
*
|
||||
*/
|
||||
public function map($method, $route, $target, $name = null) {
|
||||
|
||||
@@ -226,7 +242,7 @@ class AltoRouter {
|
||||
if (preg_match_all('`(/|\.|)\[([^:\]]*+)(?::([^:\]]*+))?\](\?|)`', $route, $matches, PREG_SET_ORDER)) {
|
||||
|
||||
$matchTypes = $this->matchTypes;
|
||||
foreach ($matches as $match) {
|
||||
foreach($matches as $match) {
|
||||
list($block, $pre, $type, $param, $optional) = $match;
|
||||
|
||||
if (isset($matchTypes[$type])) {
|
||||
|
@@ -18,6 +18,33 @@ class AltoRouterDebug extends AltoRouter{
|
||||
|
||||
}
|
||||
|
||||
class SimpleTraversable implements Iterator{
|
||||
|
||||
protected $_position = 0;
|
||||
|
||||
protected $_data = array(
|
||||
array('GET', '/foo', 'foo_action', null),
|
||||
array('POST', '/bar', 'bar_action', 'second_route')
|
||||
);
|
||||
|
||||
public function current(){
|
||||
return $this->_data[$this->_position];
|
||||
}
|
||||
public function key(){
|
||||
return $this->_position;
|
||||
}
|
||||
public function next(){
|
||||
++$this->_position;
|
||||
}
|
||||
public function rewind(){
|
||||
$this->_position = 0;
|
||||
}
|
||||
public function valid(){
|
||||
return isset($this->_data[$this->_position]);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Generated by PHPUnit_SkeletonGenerator 1.2.1 on 2013-07-14 at 17:47:46.
|
||||
*/
|
||||
@@ -45,6 +72,55 @@ class AltoRouterTest extends PHPUnit_Framework_TestCase
|
||||
{
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers AltoRouter::addRoutes
|
||||
*/
|
||||
public function testAddRoutes()
|
||||
{
|
||||
$method = 'POST';
|
||||
$route = '/[:controller]/[:action]';
|
||||
$target = function(){};
|
||||
|
||||
$this->router->addRoutes(array(
|
||||
array($method, $route, $target),
|
||||
array($method, $route, $target, 'second_route')
|
||||
));
|
||||
|
||||
$routes = $this->router->getRoutes();
|
||||
|
||||
$this->assertEquals(array($method, $route, $target, null), $routes[0]);
|
||||
$this->assertEquals(array($method, $route, $target, 'second_route'), $routes[1]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers AltoRouter::addRoutes
|
||||
*/
|
||||
public function testAddRoutesAcceptsTraverable()
|
||||
{
|
||||
$traversable = new SimpleTraversable();
|
||||
$this->router->addRoutes($traversable);
|
||||
|
||||
$traversable->rewind();
|
||||
|
||||
$first = $traversable->current();
|
||||
$traversable->next();
|
||||
$second = $traversable->current();
|
||||
|
||||
$routes = $this->router->getRoutes();
|
||||
|
||||
$this->assertEquals($first, $routes[0]);
|
||||
$this->assertEquals($second, $routes[1]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers AltoRouter::addRoutes
|
||||
* @expectedException Exception
|
||||
*/
|
||||
public function testAddRoutesThrowsExceptionOnInvalidArgument()
|
||||
{
|
||||
$this->router->addRoutes(new stdClass);
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers AltoRouter::setBasePath
|
||||
*/
|
||||
|
Reference in New Issue
Block a user