diff --git a/AltoRouter.php b/AltoRouter.php index 77bba01..aee4fdd 100644 --- a/AltoRouter.php +++ b/AltoRouter.php @@ -38,7 +38,10 @@ class AltoRouter { * @return void * @author Koen Punt */ - public function addRoutes(array $routes){ + 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); } @@ -68,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) { diff --git a/AltoRouterTest.php b/AltoRouterTest.php index af833b0..1594d98 100644 --- a/AltoRouterTest.php +++ b/AltoRouterTest.php @@ -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. */ @@ -65,6 +92,35 @@ class AltoRouterTest extends PHPUnit_Framework_TestCase $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 */