|
@@ -12,16 +12,29 @@
|
|
|
namespace Sonata\AdminBundle\Tests\Route;
|
|
|
|
|
|
use Sonata\AdminBundle\Route\DefaultRouteGenerator;
|
|
|
+use Sonata\AdminBundle\Route\RouteCollection;
|
|
|
+use Sonata\AdminBundle\Route\RoutesCache;
|
|
|
use Symfony\Component\Routing\Route;
|
|
|
|
|
|
class DefaultRouteGeneratorTest extends \PHPUnit_Framework_TestCase
|
|
|
{
|
|
|
+ protected $cacheTempFolder;
|
|
|
+
|
|
|
+ public function setUp()
|
|
|
+ {
|
|
|
+ $this->cacheTempFolder = sys_get_temp_dir().'/sonata_test_route';
|
|
|
+
|
|
|
+ exec('rm -rf '.$this->cacheTempFolder);
|
|
|
+ }
|
|
|
+
|
|
|
public function testGenerate()
|
|
|
{
|
|
|
$router = $this->getMock('\Symfony\Component\Routing\RouterInterface');
|
|
|
$router->expects($this->once())->method('generate')->will($this->returnValue('/foo/bar'));
|
|
|
|
|
|
- $generator = new DefaultRouteGenerator($router);
|
|
|
+ $cache = new RoutesCache($this->cacheTempFolder);
|
|
|
+
|
|
|
+ $generator = new DefaultRouteGenerator($router, $cache);
|
|
|
|
|
|
$this->assertEquals('/foo/bar', $generator->generate('foo_bar'));
|
|
|
}
|
|
@@ -31,35 +44,24 @@ class DefaultRouteGeneratorTest extends \PHPUnit_Framework_TestCase
|
|
|
*/
|
|
|
public function testGenerateUrl($expected, $name, array $parameters)
|
|
|
{
|
|
|
+ $childCollection = new RouteCollection('base.Code.Foo|base.Code.Bar', 'admin_acme_child', '/foo/', 'BundleName:ControllerName');
|
|
|
+ $childCollection->add('bar');
|
|
|
+
|
|
|
+ $collection = new RouteCollection('base.Code.Foo', 'admin_acme', '/', 'BundleName:ControllerName');
|
|
|
+ $collection->add('foo');
|
|
|
+ $collection->addCollection($childCollection);
|
|
|
+
|
|
|
$admin = $this->getMock('Sonata\AdminBundle\Admin\AdminInterface');
|
|
|
- $admin->expects($this->once())->method('isChild')->will($this->returnValue(false));
|
|
|
- $admin->expects($this->any())->method('getCode')->will($this->returnValue('base.Code.Route'));
|
|
|
+ $admin->expects($this->any())->method('isChild')->will($this->returnValue(false));
|
|
|
+ $admin->expects($this->any())->method('getCode')->will($this->returnValue('base.Code.Foo'));
|
|
|
$admin->expects($this->once())->method('hasParentFieldDescription')->will($this->returnValue(false));
|
|
|
$admin->expects($this->once())->method('hasRequest')->will($this->returnValue(true));
|
|
|
$admin->expects($this->any())->method('getUniqid')->will($this->returnValue('foo_uniqueid'));
|
|
|
$admin->expects($this->any())->method('getCode')->will($this->returnValue('foo_code'));
|
|
|
$admin->expects($this->once())->method('getPersistentParameters')->will($this->returnValue(array('abc'=>'a123', 'efg'=>'e456')));
|
|
|
+ $admin->expects($this->any())->method('getRoutes')->will($this->returnValue($collection));
|
|
|
|
|
|
- $route1 = new Route('/foo');
|
|
|
- $route1->setDefault('_sonata_name', 'admin_acme_foo');
|
|
|
-
|
|
|
- $route2 = new Route('/foo/bar');
|
|
|
- $route2->setDefault('_sonata_name', 'admin_acme_bar');
|
|
|
-
|
|
|
- $admin->expects($this->once())
|
|
|
- ->method('getRoute')
|
|
|
- ->will($this->returnCallback(function($name) use ($route1, $route2) {
|
|
|
- switch ($name) {
|
|
|
- case 'base.Code.Route.foo':
|
|
|
- return $route1;
|
|
|
- case 'base.Code.Route|foo.bar':
|
|
|
- return $route2;
|
|
|
- }
|
|
|
-
|
|
|
- return false;
|
|
|
- }));
|
|
|
-
|
|
|
- $router = $this->getMock('\Symfony\Component\Routing\RouterInterface');
|
|
|
+ $router = $this->getMock('Symfony\Component\Routing\RouterInterface');
|
|
|
$router->expects($this->once())
|
|
|
->method('generate')
|
|
|
->will($this->returnCallback(function($name, array $parameters = array()) {
|
|
@@ -71,14 +73,16 @@ class DefaultRouteGeneratorTest extends \PHPUnit_Framework_TestCase
|
|
|
switch ($name) {
|
|
|
case 'admin_acme_foo':
|
|
|
return '/foo'.$params;
|
|
|
- case 'admin_acme_bar':
|
|
|
+ case 'admin_acme_child_bar':
|
|
|
return '/foo/bar'.$params;
|
|
|
}
|
|
|
|
|
|
return null;
|
|
|
}));
|
|
|
|
|
|
- $generator = new DefaultRouteGenerator($router);
|
|
|
+ $cache = new RoutesCache($this->cacheTempFolder);
|
|
|
+
|
|
|
+ $generator = new DefaultRouteGenerator($router, $cache);
|
|
|
|
|
|
$this->assertEquals($expected, $generator->generateUrl($admin, $name, $parameters));
|
|
|
}
|
|
@@ -87,7 +91,7 @@ class DefaultRouteGeneratorTest extends \PHPUnit_Framework_TestCase
|
|
|
{
|
|
|
return array(
|
|
|
array('/foo?abc=a123&efg=e456&default_param=default_val', 'foo', array('default_param'=>'default_val')),
|
|
|
- array('/foo/bar?abc=a123&efg=e456&default_param=default_val', 'foo.bar', array('default_param'=>'default_val')),
|
|
|
+ array('/foo/bar?abc=a123&efg=e456&default_param=default_val', 'base.Code.Bar.bar', array('default_param'=>'default_val')),
|
|
|
);
|
|
|
}
|
|
|
|
|
@@ -96,40 +100,55 @@ class DefaultRouteGeneratorTest extends \PHPUnit_Framework_TestCase
|
|
|
$this->setExpectedException('RuntimeException', 'unable to find the route `base.Code.Route.foo`');
|
|
|
|
|
|
$admin = $this->getMock('Sonata\AdminBundle\Admin\AdminInterface');
|
|
|
- $admin->expects($this->once())->method('isChild')->will($this->returnValue(false));
|
|
|
+ $admin->expects($this->any())->method('isChild')->will($this->returnValue(false));
|
|
|
$admin->expects($this->any())->method('getCode')->will($this->returnValue('base.Code.Route'));
|
|
|
$admin->expects($this->once())->method('hasParentFieldDescription')->will($this->returnValue(false));
|
|
|
$admin->expects($this->once())->method('hasRequest')->will($this->returnValue(true));
|
|
|
$admin->expects($this->once())->method('getPersistentParameters')->will($this->returnValue(array()));
|
|
|
- $admin->expects($this->once())->method('getRoute')->will($this->returnValue(false));
|
|
|
+ $admin->expects($this->exactly(2))->method('getRoutes')->will($this->returnValue(new RouteCollection('base.Code.Route', 'baseRouteName', 'baseRoutePattern', 'BundleName:ControllerName')));
|
|
|
|
|
|
- $router = $this->getMock('\Symfony\Component\Routing\RouterInterface');
|
|
|
+ $router = $this->getMock('Symfony\Component\Routing\RouterInterface');
|
|
|
|
|
|
- $generator = new DefaultRouteGenerator($router);
|
|
|
+ $cache = new RoutesCache($this->cacheTempFolder);
|
|
|
+
|
|
|
+ $generator = new DefaultRouteGenerator($router, $cache);
|
|
|
$generator->generateUrl($admin, 'foo', array());
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* @dataProvider getGenerateUrlChildTests
|
|
|
*/
|
|
|
- public function testGenerateUrlChild($expected, $name, array $parameters)
|
|
|
+ public function testGenerateUrlChild($type, $expected, $name, array $parameters)
|
|
|
{
|
|
|
+ $childCollection = new RouteCollection('base.Code.Parent|base.Code.Child', 'admin_acme_child', '/foo/', 'BundleName:ControllerName');
|
|
|
+ $childCollection->add('bar');
|
|
|
+
|
|
|
+ $collection = new RouteCollection('base.Code.Parent', 'admin_acme', '/', 'BundleName:ControllerName');
|
|
|
+ $collection->add('foo');
|
|
|
+ $collection->addCollection($childCollection);
|
|
|
+
|
|
|
$admin = $this->getMock('Sonata\AdminBundle\Admin\AdminInterface');
|
|
|
- $admin->expects($this->once())->method('isChild')->will($this->returnValue(true));
|
|
|
- $admin->expects($this->any())->method('getCode')->will($this->returnValue('base.Code.Route'));
|
|
|
- $admin->expects($this->any())->method('getBaseCodeRoute')->will($this->returnValue('baseChild.Code.Route'));
|
|
|
+ $admin->expects($this->any())->method('isChild')->will($this->returnValue(true));
|
|
|
+ $admin->expects($this->any())->method('getCode')->will($this->returnValue('base.Code.Child'));
|
|
|
+ $admin->expects($this->any())->method('getBaseCodeRoute')->will($this->returnValue('base.Code.Parent|base.Code.Child'));
|
|
|
$admin->expects($this->any())->method('getIdParameter')->will($this->returnValue('id'));
|
|
|
- $admin->expects($this->once())->method('hasParentFieldDescription')->will($this->returnValue(false));
|
|
|
- $admin->expects($this->once())->method('hasRequest')->will($this->returnValue(true));
|
|
|
+ $admin->expects($this->any())->method('hasParentFieldDescription')->will($this->returnValue(false));
|
|
|
+ $admin->expects($this->any())->method('hasRequest')->will($this->returnValue(true));
|
|
|
$admin->expects($this->any())->method('getUniqid')->will($this->returnValue('foo_uniqueid'));
|
|
|
$admin->expects($this->any())->method('getCode')->will($this->returnValue('foo_code'));
|
|
|
- $admin->expects($this->once())->method('getPersistentParameters')->will($this->returnValue(array('abc'=>'a123', 'efg'=>'e456')));
|
|
|
+ $admin->expects($this->any())->method('getPersistentParameters')->will($this->returnValue(array('abc'=>'a123', 'efg'=>'e456')));
|
|
|
+ $admin->expects($this->any())->method('getRoutes')->will($this->returnValue($childCollection));
|
|
|
|
|
|
$parentAdmin = $this->getMock('Sonata\AdminBundle\Admin\AdminInterface');
|
|
|
$parentAdmin->expects($this->any())->method('getIdParameter')->will($this->returnValue('childId'));
|
|
|
+ $parentAdmin->expects($this->any())->method('getRoutes')->will($this->returnValue($collection));
|
|
|
+ $parentAdmin->expects($this->any())->method('getCode')->will($this->returnValue('base.Code.Parent'));
|
|
|
+
|
|
|
+ // no request attached in this test, so this will not be used
|
|
|
+ $parentAdmin->expects($this->never())->method('getPersistentParameters')->will($this->returnValue(array('from'=>'parent')));
|
|
|
|
|
|
$request = $this->getMock('Symfony\Component\HttpFoundation\Request');
|
|
|
- $request->expects($this->once())
|
|
|
+ $request->expects($this->any())
|
|
|
->method('get')
|
|
|
->will($this->returnCallback(function($key) {
|
|
|
if ($key == 'childId') {
|
|
@@ -138,29 +157,10 @@ class DefaultRouteGeneratorTest extends \PHPUnit_Framework_TestCase
|
|
|
|
|
|
return null;
|
|
|
}));
|
|
|
- $admin->expects($this->any())->method('getRequest')->will($this->returnValue($request));
|
|
|
|
|
|
+ $admin->expects($this->any())->method('getRequest')->will($this->returnValue($request));
|
|
|
$admin->expects($this->any())->method('getParent')->will($this->returnValue($parentAdmin));
|
|
|
|
|
|
- $route1 = new Route('/foo');
|
|
|
- $route1->setDefault('_sonata_name', 'admin_acme_foo');
|
|
|
-
|
|
|
- $route2 = new Route('/foo/bar');
|
|
|
- $route2->setDefault('_sonata_name', 'admin_acme_bar');
|
|
|
-
|
|
|
- $admin->expects($this->once())
|
|
|
- ->method('getRoute')
|
|
|
- ->will($this->returnCallback(function($name) use ($route1, $route2) {
|
|
|
- switch ($name) {
|
|
|
- case 'baseChild.Code.Route.foo':
|
|
|
- return $route1;
|
|
|
- case 'baseChild.Code.Route.foo.bar':
|
|
|
- return $route2;
|
|
|
- }
|
|
|
-
|
|
|
- return false;
|
|
|
- }));
|
|
|
-
|
|
|
$router = $this->getMock('\Symfony\Component\Routing\RouterInterface');
|
|
|
$router->expects($this->once())
|
|
|
->method('generate')
|
|
@@ -173,23 +173,26 @@ class DefaultRouteGeneratorTest extends \PHPUnit_Framework_TestCase
|
|
|
switch ($name) {
|
|
|
case 'admin_acme_foo':
|
|
|
return '/foo'.$params;
|
|
|
- case 'admin_acme_bar':
|
|
|
+ case 'admin_acme_child_bar':
|
|
|
return '/foo/bar'.$params;
|
|
|
}
|
|
|
|
|
|
return null;
|
|
|
}));
|
|
|
|
|
|
- $generator = new DefaultRouteGenerator($router);
|
|
|
+ $cache = new RoutesCache(sys_get_temp_dir());
|
|
|
|
|
|
- $this->assertEquals($expected, $generator->generateUrl($admin, $name, $parameters));
|
|
|
+ $generator = new DefaultRouteGenerator($router, $cache);
|
|
|
+
|
|
|
+ $this->assertEquals($expected, $generator->generateUrl($type == 'child' ? $admin : $parentAdmin, $name, $parameters));
|
|
|
}
|
|
|
|
|
|
public function getGenerateUrlChildTests()
|
|
|
{
|
|
|
return array(
|
|
|
- array('/foo?abc=a123&efg=e456&default_param=default_val&childId=987654', 'foo', array('id'=>123, 'default_param'=>'default_val')),
|
|
|
- array('/foo/bar?abc=a123&efg=e456&default_param=default_val&childId=987654', 'foo.bar', array('id'=>123, 'default_param'=>'default_val')),
|
|
|
+ array('parent', '/foo?id=123&default_param=default_val', 'foo', array('id'=>123, 'default_param'=>'default_val')),
|
|
|
+ array('parent', '/foo/bar?id=123&default_param=default_val', 'base.Code.Child.bar', array('id'=>123, 'default_param'=>'default_val')),
|
|
|
+ array('child', '/foo/bar?abc=a123&efg=e456&default_param=default_val&childId=987654', 'bar', array('id'=>123, 'default_param'=>'default_val')),
|
|
|
);
|
|
|
}
|
|
|
|
|
@@ -198,35 +201,25 @@ class DefaultRouteGeneratorTest extends \PHPUnit_Framework_TestCase
|
|
|
*/
|
|
|
public function testGenerateUrlParentFieldDescription($expected, $name, array $parameters)
|
|
|
{
|
|
|
+ $childCollection = new RouteCollection('base.Code.Parent|base.Code.Child', 'admin_acme_child', '/foo/', 'BundleName:ControllerName');
|
|
|
+ $childCollection->add('bar');
|
|
|
+
|
|
|
+ $collection = new RouteCollection('base.Code.Parent', 'admin_acme', '/', 'BundleName:ControllerName');
|
|
|
+ $collection->add('foo');
|
|
|
+ $collection->addCollection($childCollection);
|
|
|
+
|
|
|
$admin = $this->getMock('Sonata\AdminBundle\Admin\AdminInterface');
|
|
|
- $admin->expects($this->once())->method('isChild')->will($this->returnValue(false));
|
|
|
- $admin->expects($this->any())->method('getCode')->will($this->returnValue('base.Code.Route'));
|
|
|
+ $admin->expects($this->any())->method('isChild')->will($this->returnValue(false));
|
|
|
+ $admin->expects($this->any())->method('getCode')->will($this->returnValue('base.Code.Parent'));
|
|
|
+ // embeded admin (not nested ...)
|
|
|
$admin->expects($this->once())->method('hasParentFieldDescription')->will($this->returnValue(true));
|
|
|
$admin->expects($this->once())->method('hasRequest')->will($this->returnValue(true));
|
|
|
$admin->expects($this->any())->method('getUniqid')->will($this->returnValue('foo_uniqueid'));
|
|
|
$admin->expects($this->any())->method('getCode')->will($this->returnValue('foo_code'));
|
|
|
$admin->expects($this->once())->method('getPersistentParameters')->will($this->returnValue(array('abc'=>'a123', 'efg'=>'e456')));
|
|
|
|
|
|
- $route1 = new Route('/foo');
|
|
|
- $route1->setDefault('_sonata_name', 'admin_acme_foo');
|
|
|
-
|
|
|
- $route2 = new Route('/foo/bar');
|
|
|
- $route2->setDefault('_sonata_name', 'admin_acme_bar');
|
|
|
-
|
|
|
- $admin->expects($this->once())
|
|
|
- ->method('getRoute')
|
|
|
- ->will($this->returnCallback(function($name) use ($route1, $route2) {
|
|
|
- switch ($name) {
|
|
|
- case 'base.Code.Route.foo':
|
|
|
- return $route1;
|
|
|
- case 'base.Code.Route|foo.bar':
|
|
|
- return $route2;
|
|
|
- }
|
|
|
-
|
|
|
- return false;
|
|
|
- }));
|
|
|
|
|
|
- $router = $this->getMock('\Symfony\Component\Routing\RouterInterface');
|
|
|
+ $router = $this->getMock('Symfony\Component\Routing\RouterInterface');
|
|
|
$router->expects($this->once())
|
|
|
->method('generate')
|
|
|
->will($this->returnCallback(function($name, array $parameters = array()) {
|
|
@@ -238,7 +231,7 @@ class DefaultRouteGeneratorTest extends \PHPUnit_Framework_TestCase
|
|
|
switch ($name) {
|
|
|
case 'admin_acme_foo':
|
|
|
return '/foo'.$params;
|
|
|
- case 'admin_acme_bar':
|
|
|
+ case 'admin_acme_child_bar':
|
|
|
return '/foo/bar'.$params;
|
|
|
}
|
|
|
|
|
@@ -255,7 +248,9 @@ class DefaultRouteGeneratorTest extends \PHPUnit_Framework_TestCase
|
|
|
$fieldDescription->expects($this->any())->method('getAdmin')->will($this->returnValue($parentAdmin));
|
|
|
$admin->expects($this->any())->method('getParentFieldDescription')->will($this->returnValue($fieldDescription));
|
|
|
|
|
|
- $generator = new DefaultRouteGenerator($router);
|
|
|
+ $cache = new RoutesCache(sys_get_temp_dir());
|
|
|
+
|
|
|
+ $generator = new DefaultRouteGenerator($router, $cache);
|
|
|
|
|
|
$this->assertEquals($expected, $generator->generateUrl($admin, $name, $parameters));
|
|
|
}
|
|
@@ -263,8 +258,9 @@ class DefaultRouteGeneratorTest extends \PHPUnit_Framework_TestCase
|
|
|
public function getGenerateUrlParentFieldDescriptionTests()
|
|
|
{
|
|
|
return array(
|
|
|
- array('/foo?abc=a123&efg=e456&default_param=default_val&uniqid=foo_uniqueid&code=base.Code.Route&pcode=parent_foo_code&puniqid=parent_foo_uniqueid', 'foo', array('default_param'=>'default_val')),
|
|
|
- array('/foo/bar?abc=a123&efg=e456&default_param=default_val&uniqid=foo_uniqueid&code=base.Code.Route&pcode=parent_foo_code&puniqid=parent_foo_uniqueid', 'foo.bar', array('default_param'=>'default_val')),
|
|
|
+ array('/foo?abc=a123&efg=e456&default_param=default_val&uniqid=foo_uniqueid&code=base.Code.Parent&pcode=parent_foo_code&puniqid=parent_foo_uniqueid', 'foo', array('default_param'=>'default_val')),
|
|
|
+ // this second test does not make sense as we cannot have embeded admin with nested admin....
|
|
|
+ array('/foo/bar?abc=a123&efg=e456&default_param=default_val&uniqid=foo_uniqueid&code=base.Code.Parent&pcode=parent_foo_code&puniqid=parent_foo_uniqueid', 'base.Code.Child.bar', array('default_param'=>'default_val')),
|
|
|
);
|
|
|
}
|
|
|
}
|