Prechádzať zdrojové kódy

[Test] add AdminHelper tests

Thomas Rabaix 13 rokov pred
rodič
commit
5c6af34859

+ 7 - 7
Admin/AdminHelper.php

@@ -45,15 +45,14 @@ class AdminHelper
     }
 
     /**
-     * @throws \RuntimeException
-     * @param \Symfony\Component\Form\FormView $formBuilder
-     * @param  $elementId
-     * @return \Symfony\Component\Form\FormView
+     * @param \Symfony\Component\Form\FormView $formView
+     * @param $elementId
+     * @return null|\Symfony\Component\Form\FormView
      */
     public function getChildFormView(FormView $formView, $elementId)
     {
         foreach (new \RecursiveIteratorIterator(new FormViewIterator($formView), \RecursiveIteratorIterator::SELF_FIRST) as $name => $formView) {
-            if ($name == $elementId) {
+            if ($name === $elementId) {
                 return $formView;
             }
         }
@@ -62,6 +61,7 @@ class AdminHelper
     }
 
     /**
+     * @deprecated
      * @param string $code
      * @return \Sonata\AdminBundle\Admin\AdminInterface
      */
@@ -78,8 +78,8 @@ class AdminHelper
      *
      * @throws \RuntimeException
      * @param \Sonata\AdminBundle\Admin\AdminInterface $admin
-     * @param  $elementId
-     * @return void
+     * @param sting $elementId
+     * @return array
      */
     public function appendFormFieldElement(AdminInterface $admin, $elementId)
     {

+ 5 - 0
Admin/AdminInterface.php

@@ -353,4 +353,9 @@ interface AdminInterface
      * @return array|null
      */
     function getChild($code);
+
+    /**
+     * @return object a new object instance
+     */
+    function getNewInstance();
 }

+ 83 - 0
Tests/Admin/AdminHelperTest.php

@@ -0,0 +1,83 @@
+<?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\AdminHelper;
+use Sonata\AdminBundle\Admin\Pool;
+use Symfony\Component\Form\FormBuilder;
+use Symfony\Component\DependencyInjection\ContainerInterface;
+use Symfony\Component\Form\FormFactoryInterface;
+use Symfony\Component\EventDispatcher\EventDispatcherInterface;
+use Sonata\AdminBundle\Admin\FieldDescriptionInterface;
+use Sonata\AdminBundle\Admin\AdminInterface;
+use Symfony\Component\Form\FormView;
+
+class AdminHelperTest extends \PHPUnit_Framework_TestCase
+{
+
+    public function testgetChildFormBuilder()
+    {
+        $container = $this->getMock('Symfony\Component\DependencyInjection\ContainerInterface');
+
+        $pool = new Pool($container, 'title', 'logo.png');
+        $helper = new AdminHelper($pool);
+
+        $formFactory = $this->getMock('Symfony\Component\Form\FormFactoryInterface');
+        $eventDispatcher = $this->getMock('Symfony\Component\EventDispatcher\EventDispatcherInterface');
+
+        $formBuilder = new FormBuilder('test', $formFactory, $eventDispatcher);
+
+        $childFormBuilder = new FormBuilder('elementId', $formFactory, $eventDispatcher);
+        $formBuilder->add($childFormBuilder);
+
+        $this->assertNull($helper->getChildFormBuilder($formBuilder, 'foo'));
+        $this->isInstanceOf('Symfony\Component\Form\FormBuilder', $helper->getChildFormBuilder($formBuilder, 'test_elementId'));
+    }
+
+    public function testgetChildFormView()
+    {
+        $container = $this->getMock('Symfony\Component\DependencyInjection\ContainerInterface');
+
+        $pool = new Pool($container, 'title', 'logo.png');
+        $helper = new AdminHelper($pool);
+
+        $formView = new FormView();
+        $formView->set('id', 'test');
+        $child = new FormView();
+        $child->set('id', 'test_elementId');
+        $formView->setChildren(array('elementId' => $child));
+
+        $this->assertNull($helper->getChildFormView($formView, 'foo'));
+        $this->isInstanceOf('Symfony\Component\Form\FormView', $helper->getChildFormView($formView, 'test_elementId'));
+    }
+
+    public function testaddNewInstance()
+    {
+        $container = $this->getMock('Symfony\Component\DependencyInjection\ContainerInterface');
+
+        $pool = new Pool($container, 'title', 'logo.png');
+        $helper = new AdminHelper($pool);
+
+        $admin = $this->getMock('Sonata\AdminBundle\Admin\AdminInterface');
+        $admin->expects($this->once())->method('getNewInstance')->will($this->returnValue(new \stdClass()));
+
+        $fieldDescription = $this->getMock('Sonata\AdminBundle\Admin\FieldDescriptionInterface');
+        $fieldDescription->expects($this->once())->method('getAssociationAdmin')->will($this->returnValue($admin));
+        $fieldDescription->expects($this->once())->method('getAssociationMapping')->will($this->returnValue(array('fieldName' => 'fooBar')));
+
+        $object = $this->getMock('sdtClass', array('addFooBar'));
+        $object->expects($this->once())->method('addFooBar');
+
+        $helper->addNewInstance($object, $fieldDescription);
+
+    }
+}