瀏覽代碼

Added DefaultRouterTest

Andrej Hudec 11 年之前
父節點
當前提交
d10d549cec
共有 5 個文件被更改,包括 298 次插入16 次删除
  1. 6 15
      Admin/Admin.php
  2. 41 0
      Admin/AdminInterface.php
  3. 7 0
      CHANGELOG.md
  4. 1 1
      Route/DefaultRouteGenerator.php
  5. 243 0
      Tests/Route/DefaultRouteGeneratorTest.php

+ 6 - 15
Admin/Admin.php

@@ -1077,9 +1077,7 @@ abstract class Admin implements AdminInterface, DomainObjectInterface
     }
 
     /**
-     * Returns the parameter representing request id, ie: id or childId
-     *
-     * @return string
+     * {@inheritdoc}
      */
     public function getIdParameter()
     {
@@ -1574,8 +1572,7 @@ abstract class Admin implements AdminInterface, DomainObjectInterface
     }
 
     /**
-     *
-     * @return \Sonata\AdminBundle\Admin\FieldDescriptionInterface the parent field description
+     * {@inheritdoc}
      */
     public function getParentFieldDescription()
     {
@@ -1583,9 +1580,7 @@ abstract class Admin implements AdminInterface, DomainObjectInterface
     }
 
     /**
-     * Returns true if the Admin is linked to a parent FieldDescription
-     *
-     * @return bool
+     * {@inheritdoc}
      */
     public function hasParentFieldDescription()
     {
@@ -1893,9 +1888,7 @@ abstract class Admin implements AdminInterface, DomainObjectInterface
     }
 
     /**
-     * Returns the uniqid
-     *
-     * @return integer
+     * {@inheritdoc}
      */
     public function getUniqid()
     {
@@ -1913,9 +1906,7 @@ abstract class Admin implements AdminInterface, DomainObjectInterface
     }
 
     /**
-     * Returns an array of persistent parameters
-     *
-     * @return array
+     * {@inheritdoc}
      */
     public function getPersistentParameters()
     {
@@ -2274,7 +2265,7 @@ abstract class Admin implements AdminInterface, DomainObjectInterface
     }
 
     /**
-     * @return string
+     * {@inheritdoc}
      */
     public function getBaseCodeRoute()
     {

+ 41 - 0
Admin/AdminInterface.php

@@ -229,6 +229,12 @@ interface AdminInterface
      */
     public function getCode();
 
+    /**
+     *
+     * @return string
+     */
+    public function getBaseCodeRoute();
+
     /**
      * Return the roles and permissions per role
      * - different permissions per role for the acl handler
@@ -246,6 +252,20 @@ interface AdminInterface
      */
     public function setParentFieldDescription(FieldDescriptionInterface $parentFieldDescription);
 
+    /**
+     * Get parent field description
+     *
+     * @return \Sonata\AdminBundle\Admin\FieldDescriptionInterface The parent field description
+     */
+    public function getParentFieldDescription();
+
+    /**
+     * Returns true if the Admin is linked to a parent FieldDescription
+     *
+     * @return bool
+     */
+    public function hasParentFieldDescription();
+
     /**
      * translate a message id
      *
@@ -272,6 +292,13 @@ interface AdminInterface
      */
     public function getRouterIdParameter();
 
+    /**
+     * Returns the parameter representing request id, ie: id or childId
+     *
+     * @return string
+     */
+    public function getIdParameter();
+
     /**
      * Returns true if the admin has a FieldDescription with the given $name
      *
@@ -536,6 +563,13 @@ interface AdminInterface
      */
     public function setUniqid($uniqId);
 
+    /**
+     * Returns the uniqid
+     *
+     * @return integer
+     */
+    public function getUniqid();
+
     /**
      * @param mixed $id
      *
@@ -831,6 +865,13 @@ interface AdminInterface
      */
     public function getLabel();
 
+    /**
+     * Returns an array of persistent parameters
+     *
+     * @return array
+     */
+    public function getPersistentParameters();
+
     /**
      * Get breadcrumbs for $action
      *

+ 7 - 0
CHANGELOG.md

@@ -1,6 +1,13 @@
 CHANGELOG
 =========
 
+### 2013-09-27
+
+* [BC BREAK] added ``hasParentFieldDescription``, ``getPersistentParameters``,
+  ``getParentFieldDescription``, ``getUniqid``, ``getBaseCodeRoute``,
+  ``getIdParameter`` to the AdminInterface
+  If you do not extend the Admin class, you need to add these methods to
+
 ### 2013-09-23
 
 * change list's action buttons to use ``btn-small`` from twitter bootstrap

+ 1 - 1
Route/DefaultRouteGenerator.php

@@ -58,7 +58,7 @@ class DefaultRouteGenerator implements RouteGeneratorInterface
             }
         }
         // if the admin is a child we automatically append the parent's id
-        else if ($admin->isChild()) {
+        else {
             $name = $admin->getBaseCodeRoute().'.'.$name;
 
             // twig template does not accept variable hash key ... so cannot use admin.idparameter ...

+ 243 - 0
Tests/Route/DefaultRouteGeneratorTest.php

@@ -12,6 +12,7 @@
 namespace Sonata\AdminBundle\Tests\Route;
 
 use Sonata\AdminBundle\Route\DefaultRouteGenerator;
+use Symfony\Component\Routing\Route;
 
 class DefaultRouteGeneratorTest extends \PHPUnit_Framework_TestCase
 {
@@ -24,4 +25,246 @@ class DefaultRouteGeneratorTest extends \PHPUnit_Framework_TestCase
 
         $this->assertEquals('/foo/bar', $generator->generate('foo_bar'));
     }
+
+    /**
+     * @dataProvider getGenerateUrlTests
+     */
+    public function testGenerateUrl($expected, $name, array $parameters)
+    {
+        $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->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')));
+
+        $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->expects($this->once())
+            ->method('generate')
+            ->will($this->returnCallback(function($name, array $parameters = array())  {
+                $params = '';
+                if (!empty($parameters)) {
+                    $params .= '?'.http_build_query($parameters);
+                }
+
+                switch ($name) {
+                    case 'admin_acme_foo':
+                        return '/foo'.$params;
+                    case 'admin_acme_bar':
+                        return '/foo/bar'.$params;
+                }
+
+                return null;
+            }));
+
+        $generator = new DefaultRouteGenerator($router);
+
+        $this->assertEquals($expected, $generator->generateUrl($admin, $name, $parameters));
+    }
+
+    public function getGenerateUrlTests()
+    {
+        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')),
+        );
+    }
+
+    public function testGenerateUrlWithException()
+    {
+        $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('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));
+
+        $router = $this->getMock('\Symfony\Component\Routing\RouterInterface');
+
+        $generator = new DefaultRouteGenerator($router);
+        $generator->generateUrl($admin, 'foo', array());
+    }
+
+    /**
+     * @dataProvider getGenerateUrlChildTests
+     */
+    public function testGenerateUrlChild($expected, $name, array $parameters)
+    {
+        $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('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('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')));
+
+        $parentAdmin = $this->getMock('Sonata\AdminBundle\Admin\AdminInterface');
+        $parentAdmin->expects($this->any())->method('getIdParameter')->will($this->returnValue('childId'));
+
+        $request = $this->getMock('Symfony\Component\HttpFoundation\Request');
+        $request->expects($this->once())
+            ->method('get')
+            ->will($this->returnCallback(function($key) {
+                if ($key == 'childId') {
+                    return '987654';
+                }
+
+                return null;
+            }));
+        $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')
+            ->will($this->returnCallback(function($name, array $parameters = array())  {
+                $params = '';
+                if (!empty($parameters)) {
+                    $params .= '?'.http_build_query($parameters);
+                }
+
+                switch ($name) {
+                    case 'admin_acme_foo':
+                        return '/foo'.$params;
+                    case 'admin_acme_bar':
+                        return '/foo/bar'.$params;
+                }
+
+                return null;
+            }));
+
+        $generator = new DefaultRouteGenerator($router);
+
+        $this->assertEquals($expected, $generator->generateUrl($admin, $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')),
+        );
+    }
+
+    /**
+     * @dataProvider getGenerateUrlParentFieldDescriptionTests
+     */
+    public function testGenerateUrlParentFieldDescription($expected, $name, array $parameters)
+    {
+        $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->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->expects($this->once())
+            ->method('generate')
+            ->will($this->returnCallback(function($name, array $parameters = array())  {
+                $params = '';
+                if (!empty($parameters)) {
+                    $params .= '?'.http_build_query($parameters);
+                }
+
+                switch ($name) {
+                    case 'admin_acme_foo':
+                        return '/foo'.$params;
+                    case 'admin_acme_bar':
+                        return '/foo/bar'.$params;
+                }
+
+                return null;
+            }));
+
+        $fieldDescription = $this->getMock('Sonata\AdminBundle\Admin\FieldDescriptionInterface');
+        $fieldDescription->expects($this->once())->method('getOption')->will($this->returnValue(array()));
+
+        $parentAdmin = $this->getMock('Sonata\AdminBundle\Admin\AdminInterface');
+        $parentAdmin->expects($this->any())->method('getUniqid')->will($this->returnValue('parent_foo_uniqueid'));
+        $parentAdmin->expects($this->any())->method('getCode')->will($this->returnValue('parent_foo_code'));
+
+        $fieldDescription->expects($this->any())->method('getAdmin')->will($this->returnValue($parentAdmin));
+        $admin->expects($this->any())->method('getParentFieldDescription')->will($this->returnValue($fieldDescription));
+
+        $generator = new DefaultRouteGenerator($router);
+
+        $this->assertEquals($expected, $generator->generateUrl($admin, $name, $parameters));
+    }
+
+    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')),
+        );
+    }
 }