Browse Source

tweak subclass management

Thomas Rabaix 11 years ago
parent
commit
bc8f65abf6
3 changed files with 23 additions and 6 deletions
  1. 7 3
      Admin/Admin.php
  2. 0 1
      Builder/BuilderInterface.php
  3. 16 2
      Tests/Admin/BaseAdminTest.php

+ 7 - 3
Admin/Admin.php

@@ -948,6 +948,10 @@ abstract class Admin implements AdminInterface, DomainObjectInterface
             return $this->class;
         }
 
+        if ($this->getParentFieldDescription() && $this->hasActiveSubClass()) {
+            throw new \RuntimeException('Feature not implemented: an embedded admin cannot have subclass');
+        }
+
         $subClass = $this->getRequest()->query->get('subclass');
 
         return $this->getSubClass($subClass);
@@ -982,7 +986,7 @@ abstract class Admin implements AdminInterface, DomainObjectInterface
             return $this->subClasses[$name];
         }
 
-        return null;
+        throw new \RuntimeException(sprintf('Unable to find the subclass `%s` for admin `%s`', $name, get_class($this)));
     }
 
     /**
@@ -998,7 +1002,7 @@ abstract class Admin implements AdminInterface, DomainObjectInterface
      */
     public function hasActiveSubClass()
     {
-        if ($this->request) {
+        if (count($this->subClasses) > 1 && $this->request) {
             return null !== $this->getRequest()->query->get('subclass');
         }
 
@@ -1028,7 +1032,7 @@ abstract class Admin implements AdminInterface, DomainObjectInterface
 
         $subClass = $this->getRequest()->query->get('subclass');
 
-        if(! $this->hasSubClass($subClass)){
+        if (!$this->hasSubClass($subClass)) {
             return null;
         }
 

+ 0 - 1
Builder/BuilderInterface.php

@@ -24,5 +24,4 @@ interface BuilderInterface
      * @return void
      */
     public function fixFieldDescription(AdminInterface $admin, FieldDescriptionInterface $fieldDescription);
-
 }

+ 16 - 2
Tests/Admin/BaseAdminTest.php

@@ -354,9 +354,23 @@ class BaseAdminTest extends \PHPUnit_Framework_TestCase
         $this->assertEquals('NewsBundle\Entity\PostExtended1', $admin->getActiveSubClass());
         $this->assertEquals('extended1', $admin->getActiveSubclassCode());
         $this->assertEquals('NewsBundle\Entity\PostExtended1', $admin->getClass());
-        
+
         $request->query->set('subclass', 'inject');
-        $this->assertNull($admin->getActiveSubClass());
         $this->assertNull($admin->getActiveSubclassCode());
     }
+
+    /**
+     * @expectedException RuntimeException
+     */
+    public function testNonExistantSubclass()
+    {
+        $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin');
+        $admin->setRequest(new \Symfony\Component\HttpFoundation\Request(array('subclass' => 'inject')));
+
+        $admin->setSubClasses(array('extended1' => 'NewsBundle\Entity\PostExtended1', 'extended2' => 'NewsBundle\Entity\PostExtended2'));
+
+        $this->assertTrue($admin->hasActiveSubClass());
+
+        $admin->getActiveSubClass();
+    }
 }