浏览代码

[Form] Improved test coverage of PropertyPathMapper

Bernhard Schussek 14 年之前
父节点
当前提交
6825ea2489

+ 5 - 1
src/Symfony/Component/Form/FormInterface.php

@@ -40,6 +40,8 @@ interface FormInterface extends \ArrayAccess, \Traversable, \Countable
 
     function getErrors();
 
+    function setData($data);
+
     function getData();
 
     function getClientData();
@@ -47,7 +49,7 @@ interface FormInterface extends \ArrayAccess, \Traversable, \Countable
     function getRenderer();
 
     function isBound();
-    
+
     /**
      * Returns the name by which the form is identified in forms.
      *
@@ -100,6 +102,8 @@ interface FormInterface extends \ArrayAccess, \Traversable, \Countable
      */
     function isEmpty();
 
+    function isSynchronized();
+
     /**
      * Writes posted data into the form
      *

+ 85 - 0
tests/Symfony/Tests/Component/Form/DataMapper/PropertyPathMapperTest.php

@@ -0,0 +1,85 @@
+<?php
+
+/*
+ * This file is part of the Symfony package.
+ *
+ * (c) Fabien Potencier <fabien@symfony.com>
+ *
+ * For the full copyright and license information, please view the LICENSE
+ * file that was distributed with this source code.
+ */
+
+namespace Symfony\Tests\Component\Form\DataMapper;
+
+use Symfony\Component\Form\FormBuilder;
+use Symfony\Component\Form\PropertyPath;
+use Symfony\Component\Form\DataMapper\PropertyPathMapper;
+
+class PropertyPathMapperTest extends \PHPUnit_Framework_TestCase
+{
+    private $mapper;
+
+    private $propertyPath;
+
+    protected function setUp()
+    {
+        $this->mapper = new PropertyPathMapper();
+        $this->propertyPath = $this->getMockBuilder('Symfony\Component\Form\PropertyPath')
+            ->disableOriginalConstructor()
+            ->getMock();
+    }
+
+    private function getForm(PropertyPath $propertyPath = null)
+    {
+        $form = $this->getMock('Symfony\Tests\Component\Form\FormInterface');
+
+        $form->expects($this->any())
+            ->method('getAttribute')
+            ->with('property_path')
+            ->will($this->returnValue($propertyPath));
+
+        return $form;
+    }
+
+    public function testMapDataToForm()
+    {
+        $data = new \stdClass();
+
+        $this->propertyPath->expects($this->once())
+            ->method('getValue')
+            ->with($data)
+            ->will($this->returnValue('foobar'));
+
+        $form = $this->getForm($this->propertyPath);
+
+        $form->expects($this->once())
+            ->method('setData')
+            ->with('foobar');
+
+        $this->mapper->mapDataToForm($data, $form);
+    }
+
+    public function testMapDataToFormIgnoresEmptyPropertyPath()
+    {
+        $data = new \stdClass();
+
+        $form = $this->getForm(null);
+
+        $form->expects($this->never())
+            ->method('setData');
+
+        $this->mapper->mapDataToForm($data, $form);
+    }
+
+    public function testMapDataToFormIgnoresEmptyData()
+    {
+        $form = $this->getForm($this->propertyPath);
+
+        $form->expects($this->never())
+            ->method('setData');
+
+        $form->getAttribute('property_path'); // <- weird PHPUnit bug if I don't do this
+
+        $this->mapper->mapDataToForm(null, $form);
+    }
+}