Skip to content

Commit 4c66e1b

Browse files
committed
Add support of route mocks
1 parent bf5e800 commit 4c66e1b

File tree

4 files changed

+105
-5
lines changed

4 files changed

+105
-5
lines changed

Flame/Modules/Application/RouterFactory.php

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
*/
88
namespace Flame\Modules\Application;
99

10+
use Flame\Modules\Application\Routers\IRouteMock;
11+
use Flame\Modules\Application\Routers\RouteMock;
1012
use Nette\Application\Routers\RouteList;
1113
use Nette\Application\Routers\Route;
1214
use Nette\InvalidStateException;
@@ -62,12 +64,15 @@ public static function prependTo(Nette\Application\IRouter &$router, array $rout
6264
*/
6365
private static function createRoute($route)
6466
{
65-
if(!is_array($route)) {
66-
throw new InvalidStateException('Route definition must be array, ' . gettype($route) . ' given');
67+
if(is_array($route) && count($route) >= 1) {
68+
$class = key($route);
69+
$route = new RouteMock($class, $route[$class]);
6770
}
6871

69-
$class = (string) key($route);
70-
$instance = new ClassType($class);
71-
return $instance->newInstanceArgs($route[$class]);
72+
if($route instanceof IRouteMock) {
73+
return $route->getInstance();
74+
}
75+
76+
throw new InvalidStateException('Route definition must be array or instance of Flame\Modules\Application\Routers\IRouteMock, ' . gettype($route) . ' given');
7277
}
7378
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
/**
3+
* Class IRouteMock
4+
*
5+
* @author: Jiří Šifalda <sifalda.jiri@gmail.com>
6+
* @date: 08.09.13
7+
*/
8+
9+
namespace Flame\Modules\Application\Routers;
10+
11+
12+
interface IRouteMock
13+
{
14+
15+
/**
16+
* @return \Nette\Application\IRouter
17+
*/
18+
public function getInstance();
19+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
/**
3+
* Class RouteMock
4+
*
5+
* @author: Jiří Šifalda <sifalda.jiri@gmail.com>
6+
* @date: 08.09.13
7+
*/
8+
namespace Flame\Modules\Application\Routers;
9+
10+
use Nette\Object;
11+
12+
class NetteRouteMock extends Object implements IRouteMock
13+
{
14+
15+
/** @var \Flame\Modules\Application\Routers\RouteMock */
16+
public $factory;
17+
18+
/**
19+
* @param string URL mask, e.g. '<presenter>/<action>/<id \d{1,3}>'
20+
* @param array|string default values or metadata
21+
* @param int flags
22+
*/
23+
public function __construct($mask, $metadata = array(), $flags = 0)
24+
{
25+
$this->factory = new RouteMock('Nette\Application\Routers\Route', array($mask, $metadata, $flags));
26+
}
27+
28+
/**
29+
* @return \Nette\Application\IRouter
30+
*/
31+
public function getInstance()
32+
{
33+
return $this->factory->getInstance();
34+
}
35+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
/**
3+
* Class Mock
4+
*
5+
* @author: Jiří Šifalda <sifalda.jiri@gmail.com>
6+
* @date: 08.09.13
7+
*/
8+
namespace Flame\Modules\Application\Routers;
9+
10+
use Nette\Object;
11+
use Nette\Reflection\ClassType;
12+
13+
class RouteMock extends Object implements IRouteMock
14+
{
15+
16+
/** @var string */
17+
public $class;
18+
19+
/** @var array */
20+
public $args;
21+
22+
/**
23+
* @param string $class
24+
* @param array $args
25+
*/
26+
function __construct($class, array $args = array())
27+
{
28+
$this->args = $args;
29+
$this->class = (string) $class;
30+
}
31+
32+
/**
33+
* @return object
34+
*/
35+
public function getInstance()
36+
{
37+
$route = new ClassType($this->class);
38+
return $route->newInstanceArgs($this->args);
39+
}
40+
41+
}

0 commit comments

Comments
 (0)