Permalink
Browse files

Add support of route mocks

  • Loading branch information...
jsifalda committed Sep 8, 2013
1 parent bf5e800 commit 4c66e1b3e2948161bab83421ac54c3842b5c3f9a
@@ -7,6 +7,8 @@
*/
namespace Flame\Modules\Application;
use Flame\Modules\Application\Routers\IRouteMock;
use Flame\Modules\Application\Routers\RouteMock;
use Nette\Application\Routers\RouteList;
use Nette\Application\Routers\Route;
use Nette\InvalidStateException;
@@ -62,12 +64,15 @@ public static function prependTo(Nette\Application\IRouter &$router, array $rout
*/
private static function createRoute($route)
{
if(!is_array($route)) {
throw new InvalidStateException('Route definition must be array, ' . gettype($route) . ' given');
if(is_array($route) && count($route) >= 1) {
$class = key($route);
$route = new RouteMock($class, $route[$class]);
}
$class = (string) key($route);
$instance = new ClassType($class);
return $instance->newInstanceArgs($route[$class]);
if($route instanceof IRouteMock) {
return $route->getInstance();
}
throw new InvalidStateException('Route definition must be array or instance of Flame\Modules\Application\Routers\IRouteMock, ' . gettype($route) . ' given');
}
}
@@ -0,0 +1,19 @@
<?php
/**
* Class IRouteMock
*
* @author: Jiří Šifalda <sifalda.jiri@gmail.com>
* @date: 08.09.13
*/
namespace Flame\Modules\Application\Routers;
interface IRouteMock
{
/**
* @return \Nette\Application\IRouter
*/
public function getInstance();
}
@@ -0,0 +1,35 @@
<?php
/**
* Class RouteMock
*
* @author: Jiří Šifalda <sifalda.jiri@gmail.com>
* @date: 08.09.13
*/
namespace Flame\Modules\Application\Routers;
use Nette\Object;
class NetteRouteMock extends Object implements IRouteMock
{
/** @var \Flame\Modules\Application\Routers\RouteMock */
public $factory;
/**
* @param string URL mask, e.g. '<presenter>/<action>/<id \d{1,3}>'
* @param array|string default values or metadata
* @param int flags
*/
public function __construct($mask, $metadata = array(), $flags = 0)
{
$this->factory = new RouteMock('Nette\Application\Routers\Route', array($mask, $metadata, $flags));
}
/**
* @return \Nette\Application\IRouter
*/
public function getInstance()
{
return $this->factory->getInstance();
}
}
@@ -0,0 +1,41 @@
<?php
/**
* Class Mock
*
* @author: Jiří Šifalda <sifalda.jiri@gmail.com>
* @date: 08.09.13
*/
namespace Flame\Modules\Application\Routers;
use Nette\Object;
use Nette\Reflection\ClassType;
class RouteMock extends Object implements IRouteMock
{
/** @var string */
public $class;
/** @var array */
public $args;
/**
* @param string $class
* @param array $args
*/
function __construct($class, array $args = array())
{
$this->args = $args;
$this->class = (string) $class;
}
/**
* @return object
*/
public function getInstance()
{
$route = new ClassType($this->class);
return $route->newInstanceArgs($this->args);
}
}

0 comments on commit 4c66e1b

Please sign in to comment.