浏览代码

Added doc & unit tests

Hugo Briand 11 年之前
父节点
当前提交
9c88fcc10a
共有 2 个文件被更改,包括 19 次插入0 次删除
  1. 9 0
      Resources/doc/reference/form_types.rst
  2. 10 0
      Tests/Admin/BaseFieldDescriptionTest.php

+ 9 - 0
Resources/doc/reference/form_types.rst

@@ -275,6 +275,15 @@ General
         <?php
         <?php
         $form->add('status', null, array('label' => false);
         $form->add('status', null, array('label' => false);
 
 
+- ``code``: You can set the ``code`` option to a closure if you want to customize the value returned (will take the object as a parameter).
+
+.. code-block:: php
+
+        <?php
+        $form->add('status', null, array('code' => function ($objectWithStatus) {
+            return sprintf("Status: %s", $objectWithStatus->getStatus());
+        });
+
 .. _`Symfony field types`: http://symfony.com/doc/current/book/forms.html#built-in-field-types
 .. _`Symfony field types`: http://symfony.com/doc/current/book/forms.html#built-in-field-types
 .. _`Symfony choice Field Type docs`: http://symfony.com/doc/current/reference/forms/types/choice.html
 .. _`Symfony choice Field Type docs`: http://symfony.com/doc/current/reference/forms/types/choice.html
 .. _`Symfony PropertyPath`: http://api.symfony.com/2.0/Symfony/Component/Form/Util/PropertyPath.html
 .. _`Symfony PropertyPath`: http://api.symfony.com/2.0/Symfony/Component/Form/Util/PropertyPath.html

+ 10 - 0
Tests/Admin/BaseFieldDescriptionTest.php

@@ -95,6 +95,16 @@ class BaseFieldDescriptionTest extends \PHPUnit_Framework_TestCase
     }
     }
 
 
     public function testGetValue()
     public function testGetValue()
+    {
+        $description = new FieldDescription();
+
+        $mock = $this->getMock('stdClass', array('getFake'));
+        $mock->expects($this->once())->method('getFake')->will($this->returnValue(42));
+
+        $this->assertEquals(42, $description->getFieldValue($mock, 'fake'));
+    }
+
+    public function testGetValueCode()
     {
     {
         $description = new FieldDescription();
         $description = new FieldDescription();
         $description->setOption('code', 'getFoo');
         $description->setOption('code', 'getFoo');