Explorar o código

add more test : Admin + ModelManager

Thomas Rabaix %!s(int64=13) %!d(string=hai) anos
pai
achega
4be0c95682

+ 5 - 1
Admin/Admin.php

@@ -1132,7 +1132,7 @@ abstract class Admin implements AdminInterface, DomainObjectInterface
      */
     public function createQuery($context = 'list')
     {
-        return $this->modelManager->createQuery($this->class);
+        return $this->getModelManager()->createQuery($this->class);
     }
 
     /**
@@ -2103,6 +2103,10 @@ abstract class Admin implements AdminInterface, DomainObjectInterface
         return $this->securityHandler->isGranted($this, $name, $object ?: $this);
     }
 
+    /**
+     * @param $entity
+     * @return mixed
+     */
     public function getNormalizedIdentifier($entity)
     {
         return $this->getModelManager()->getNormalizedIdentifier($entity);

+ 6 - 0
Model/ModelManagerInterface.php

@@ -129,6 +129,12 @@ interface ModelManagerInterface
      */
     function getIdentifierFieldNames($class);
 
+    /**
+     * @abstract
+     * @param $entity
+     */
+    function getNormalizedIdentifier($entity);
+
     /**
      * @abstract
      * @param string $class

+ 85 - 0
Tests/Admin/BaseAdminModelManagerTest.php

@@ -0,0 +1,85 @@
+<?php
+
+/*
+ * This file is part of the Sonata package.
+ *
+ * (c) Thomas Rabaix <thomas.rabaix@sonata-project.org>
+ *
+ * For the full copyright and license information, please view the LICENSE
+ * file that was distributed with this source code.
+ */
+
+namespace Sonata\AdminBundle\Tests\Admin;
+
+use Sonata\AdminBundle\Admin\Admin;
+use Sonata\AdminBundle\Route\RouteCollection;
+use Sonata\AdminBundle\Model\ModelManagerInterface;
+
+class Admin_BaseAdminModelManagerTest extends Admin
+{
+
+}
+
+class BaseAdminModelManagerTest extends \PHPUnit_Framework_TestCase
+{
+    public function testHook()
+    {
+        $modelManager = $this->getMock('Sonata\AdminBundle\Model\ModelManagerInterface');
+        $modelManager->expects($this->once())->method('create');
+        $modelManager->expects($this->once())->method('update');
+        $modelManager->expects($this->once())->method('delete');
+
+        $admin = new Admin_BaseAdminModelManagerTest('code', 'class', 'controller');
+        $admin->setModelManager($modelManager);
+
+        $t = new \stdClass();
+
+        $admin->update($t);
+        $admin->create($t);
+        $admin->delete($t);
+    }
+
+    public function testObject()
+    {
+        $modelManager = $this->getMock('Sonata\AdminBundle\Model\ModelManagerInterface');
+        $modelManager->expects($this->once())->method('find')->will($this->returnCallback(function($class, $id) {
+            if ($class != 'class') {
+                throw new \RuntimeException('Invalid class argument');
+            }
+
+            if ($id != 10) {
+                throw new \RuntimeException('Invalid id argument');
+            }
+        }));
+
+        $admin = new Admin_BaseAdminModelManagerTest('code', 'class', 'controller');
+        $admin->setModelManager($modelManager);
+        $admin->getObject(10);
+    }
+
+    public function testCreateQuery()
+    {
+        $modelManager = $this->getMock('Sonata\AdminBundle\Model\ModelManagerInterface');
+        $modelManager->expects($this->once())->method('createQuery')->will($this->returnCallback(function($class) {
+            if ($class != 'class') {
+                throw new \RuntimeException('Invalid class argument');
+            }
+        }));
+
+        $admin = new Admin_BaseAdminModelManagerTest('code', 'class', 'controller');
+        $admin->setModelManager($modelManager);
+        $admin->createQuery();
+    }
+
+    public function testId()
+    {
+        $modelManager = $this->getMock('Sonata\AdminBundle\Model\ModelManagerInterface');
+        $modelManager->expects($this->exactly(2))->method('getNormalizedIdentifier');
+
+        $admin = new Admin_BaseAdminModelManagerTest('code', 'class', 'controller');
+        $admin->setModelManager($modelManager);
+
+        $admin->id('Entity');
+        $admin->getNormalizedIdentifier('Entity');
+    }
+}

+ 3 - 3
Tests/Admin/AdminTest.php

@@ -59,7 +59,7 @@ class CommentAdmin extends Admin
     }
 }
 
-class AdminTest extends \PHPUnit_Framework_TestCase
+class BaseAdminTest extends \PHPUnit_Framework_TestCase
 {
     /**
      * @covers Sonata\AdminBundle\Admin\Admin::__construct
@@ -114,7 +114,7 @@ class AdminTest extends \PHPUnit_Framework_TestCase
 
         $admin->initialize();
         $this->assertFalse($admin->getUniqid() == "");
-        $this->assertEquals('post', $admin->getClassnameLabel());
+        $this->assertEquals('Post', $admin->getClassnameLabel());
 
 
         $admin = new CommentAdmin('sonata.post.admin.comment', 'Application\Sonata\NewsBundle\Entity\Comment', 'SonataNewsBundle:CommentAdmin');
@@ -219,6 +219,6 @@ class AdminTest extends \PHPUnit_Framework_TestCase
         $this->assertEquals('', $admin->toString($s));
 
         $s = new FooTest_AdminTest;
-        $this->assertEquals('Salut', $admin->toString($s));
+        $this->assertEquals('salut', $admin->toString($s));
     }
 }

+ 2 - 1
phpunit.xml.dist

@@ -13,7 +13,7 @@
 >
     <testsuites>
         <testsuite name="AdminBundle Test Suite">
-            <directory>./Tests/</directory>
+            <directory>./Tests</directory>
         </testsuite>
     </testsuites>
 
@@ -21,6 +21,7 @@
         <whitelist>
             <directory>./</directory>
             <exclude>
+                <directory>./Tests/tests</directory>
                 <directory>./DataFixtures/</directory>
                 <directory>./Resources/</directory>
                 <directory>./DependencyInjection/</directory>