瀏覽代碼

[Form] Added tests for table_layout.html.twig

Bernhard Schussek 14 年之前
父節點
當前提交
602846f386

+ 21 - 1
src/Symfony/Bundle/TwigBundle/Resources/views/table_layout.html.twig

@@ -1,6 +1,7 @@
 {% extends "widgets.html.twig" %}
 
 {% block field__row %}
+{% spaceless %}
     <tr>
         <td>
             {{ form_label(context) }}
@@ -10,23 +11,42 @@
             {{ form_widget(context) }}
         </td>
     </tr>
+{% endspaceless %}
 {% endblock field__row %}
 
 {% block form__errors %}
+{% spaceless %}
+    {% if errors|length > 0 %}
     <tr>
         <td colspan="2">
             {{ block('field__errors') }}
         </td>
     </tr>
+    {% endif %}
+{% endspaceless %}
 {% endblock form__errors %}
 
+{% block hidden__row %}
+{% spaceless %}
+    <tr style="display: none">
+        <td colspan="2">
+            {{ form_widget(context) }}
+        </td>
+    </tr>
+{% endspaceless %}
+{% endblock hidden__row %}
+
 {% block repeated__errors %}
+{% spaceless %}
     {{ block('form__errors') }}
+{% endspaceless %}
 {% endblock repeated__errors %}
 
 {% block form__widget %}
-    <table>
+{% spaceless %}
+    <table {{ block('attributes') }}>
         {{ block('field__rows') }}
         {{ form_rest(context) }}
     </table>
+{% endspaceless %}
 {% endblock form__widget %}

+ 6 - 0
src/Symfony/Bundle/TwigBundle/Resources/views/widgets.html.twig

@@ -1,8 +1,10 @@
 {% block field__rows %}
+{% spaceless %}
     {{ form_errors(context) }}
     {% for context in context.children %}
         {{ form_row(context) }}
     {% endfor %}
+{% endspaceless %}
 {% endblock field__rows %}
 
 {% block field__enctype %}
@@ -12,6 +14,7 @@
 {% endblock field__enctype %}
 
 {% block field__errors %}
+{% spaceless %}
     {% if errors|length > 0 %}
     <ul>
         {% for error in errors %}
@@ -19,14 +22,17 @@
         {% endfor %}
     </ul>
     {% endif %}
+{% endspaceless %}
 {% endblock field__errors %}
 
 {% block field__rest %}
+{% spaceless %}
     {% for context in context.children %}
         {% if not context.rendered %}
             {{ form_row(context) }}
         {% endif %}
     {% endfor %}
+{% endspaceless %}
 {% endblock field__rest %}
 
 {% block field__label %}

+ 35 - 0
tests/Symfony/Tests/Bridge/Twig/Extension/Fixtures/StubTranslator.php

@@ -0,0 +1,35 @@
+<?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\Bridge\Twig\Extension\Fixtures;
+
+use Symfony\Component\Translation\TranslatorInterface;
+
+class StubTranslator implements TranslatorInterface
+{
+    public function trans($id, array $parameters = array(), $domain = null, $locale = null)
+    {
+        return '[trans]'.$id.'[/trans]';
+    }
+
+    public function transChoice($id, $number, array $parameters = array(), $domain = null, $locale = null)
+    {
+        return '[trans]'.$id.'[/trans]';
+    }
+
+    public function setLocale($locale)
+    {
+    }
+
+    public function getLocale()
+    {
+    }
+}

+ 3 - 23
tests/Symfony/Tests/Bridge/Twig/Extension/FormExtensionDivLayoutTest.php

@@ -11,11 +11,13 @@
 
 namespace Symfony\Tests\Bridge\Twig\Extension;
 
+require_once __DIR__.'/Fixtures/StubTranslator.php';
+
 use Symfony\Component\Form\FormInterface;
-use Symfony\Component\Translation\TranslatorInterface;
 use Symfony\Bridge\Twig\Extension\FormExtension;
 use Symfony\Bridge\Twig\Extension\TranslationExtension;
 use Symfony\Tests\Component\Form\AbstractDivLayoutTest;
+use Symfony\Tests\Bridge\Twig\Extension\Fixtures\StubTranslator;
 
 class FormExtensionDivLayoutTest extends AbstractDivLayoutTest
 {
@@ -66,25 +68,3 @@ class FormExtensionDivLayoutTest extends AbstractDivLayoutTest
         return (string)$this->extension->renderRest($form->getContext(), $vars);
     }
 }
-
-class StubTranslator implements TranslatorInterface
-{
-    public function trans($id, array $parameters = array(), $domain = null, $locale = null)
-    {
-        return '[trans]'.$id.'[/trans]';
-    }
-
-    public function transChoice($id, $number, array $parameters = array(), $domain = null, $locale = null)
-    {
-        return '[trans]'.$id.'[/trans]';
-    }
-
-    public function setLocale($locale)
-    {
-    }
-
-    public function getLocale()
-    {
-    }
-
-}

+ 70 - 0
tests/Symfony/Tests/Bridge/Twig/Extension/FormExtensionTableLayoutTest.php

@@ -0,0 +1,70 @@
+<?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\Bridge\Twig\Extension;
+
+require_once __DIR__.'/Fixtures/StubTranslator.php';
+
+use Symfony\Component\Form\FormInterface;
+use Symfony\Bridge\Twig\Extension\FormExtension;
+use Symfony\Bridge\Twig\Extension\TranslationExtension;
+use Symfony\Tests\Component\Form\AbstractTableLayoutTest;
+use Symfony\Tests\Bridge\Twig\Extension\Fixtures\StubTranslator;
+
+class FormExtensionTableLayoutTest extends AbstractTableLayoutTest
+{
+    protected function setUp()
+    {
+        parent::setUp();
+
+        $loader = new \Twig_Loader_Filesystem(array(
+            __DIR__.'/../../../../../../src/Symfony/Bundle/TwigBundle/Resources/views',
+        ));
+
+        $this->extension = new FormExtension(array('table_layout.html.twig'));
+
+        $environment = new \Twig_Environment($loader);
+        $environment->addExtension($this->extension);
+        $environment->addExtension(new TranslationExtension(new StubTranslator()));
+
+        $this->extension->initRuntime($environment);
+    }
+
+    protected function renderEnctype(FormInterface $form)
+    {
+        return (string)$this->extension->renderEnctype($form->getContext());
+    }
+
+    protected function renderLabel(FormInterface $form, $label = null)
+    {
+        return (string)$this->extension->renderLabel($form->getContext(), $label);
+    }
+
+    protected function renderErrors(FormInterface $form)
+    {
+        return (string)$this->extension->renderErrors($form->getContext());
+    }
+
+    protected function renderWidget(FormInterface $form, array $vars = array())
+    {
+        return (string)$this->extension->renderWidget($form->getContext(), $vars);
+    }
+
+    protected function renderRow(FormInterface $form, array $vars = array())
+    {
+        return (string)$this->extension->renderRow($form->getContext(), $vars);
+    }
+
+    protected function renderRest(FormInterface $form, array $vars = array())
+    {
+        return (string)$this->extension->renderRest($form->getContext(), $vars);
+    }
+}

+ 5 - 4
tests/Symfony/Tests/Component/Form/AbstractDivLayoutTest.php

@@ -81,9 +81,6 @@ abstract class AbstractDivLayoutTest extends AbstractLayoutTest
 '/input
     [@type="hidden"]
     [@id="name__token"]
-    [count(../div)=2]
-    [count(..//label)=2]
-    [count(..//input)=3]
 /following-sibling::div
     [
         ./label[@for="name_field1"]
@@ -94,6 +91,9 @@ abstract class AbstractDivLayoutTest extends AbstractLayoutTest
         ./label[@for="name_field4"]
         /following-sibling::input[@type="text"][@id="name_field4"]
     ]
+    [count(../div)=2]
+    [count(..//label)=2]
+    [count(..//input)=3]
 '
         );
     }
@@ -107,7 +107,8 @@ abstract class AbstractDivLayoutTest extends AbstractLayoutTest
 
         $this->assertWidgetMatchesXpath($form, array(),
 '/div
-    [./div[./input[@type="text"][@value="a"]]
+    [
+        ./div[./input[@type="text"][@value="a"]]
         /following-sibling::div[./input[@type="text"][@value="b"]]
     ]
     [count(./div[./input])=2]

+ 224 - 0
tests/Symfony/Tests/Component/Form/AbstractTableLayoutTest.php

@@ -0,0 +1,224 @@
+<?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;
+
+use Symfony\Component\Form\FormError;
+
+abstract class AbstractTableLayoutTest extends AbstractLayoutTest
+{
+    public function testRow()
+    {
+        $form = $this->factory->create('text', 'name');
+        $form->addError(new FormError('Error!'));
+        $html = $this->renderRow($form);
+
+        $this->assertMatchesXpath($html,
+'/tr
+    [
+        ./td
+            [./label[@for="name"]]
+        /following-sibling::td
+            [
+                ./ul
+                    [./li[.="[trans]Error![/trans]"]]
+                    [count(./li)=1]
+                /following-sibling::input[@id="name"]
+            ]
+    ]
+'
+        );
+    }
+
+    public function testRepeatedRow()
+    {
+        $form = $this->factory->create('repeated', 'name');
+        $html = $this->renderRow($form);
+
+        $this->assertMatchesXpath($html,
+'/tr
+    [
+        ./td
+            [./label[@for="name_first"]]
+        /following-sibling::td
+            [./input[@id="name_first"]]
+    ]
+/following-sibling::tr
+    [
+        ./td
+            [./label[@for="name_second"]]
+        /following-sibling::td
+            [./input[@id="name_second"]]
+    ]
+    [count(../tr)=2]
+'
+        );
+    }
+
+    public function testRepeatedRowWithErrors()
+    {
+        $form = $this->factory->create('repeated', 'name');
+        $form->addError(new FormError('Error!'));
+        $html = $this->renderRow($form);
+
+        $this->assertMatchesXpath($html,
+'/tr
+    [./td[@colspan="2"]/ul
+        [./li[.="[trans]Error![/trans]"]]
+    ]
+/following-sibling::tr
+    [
+        ./td
+            [./label[@for="name_first"]]
+        /following-sibling::td
+            [./input[@id="name_first"]]
+    ]
+/following-sibling::tr
+    [
+        ./td
+            [./label[@for="name_second"]]
+        /following-sibling::td
+            [./input[@id="name_second"]]
+    ]
+    [count(../tr)=3]
+'
+        );
+    }
+
+    public function testRest()
+    {
+        $form = $this->factory->createBuilder('form', 'name')
+            ->add('field1', 'text')
+            ->add('field2', 'repeated')
+            ->add('field3', 'text')
+            ->add('field4', 'text')
+            ->getForm();
+
+        // Render field2 row -> does not implicitely call renderWidget because
+        // it is a repeated field!
+        $this->renderRow($form['field2']);
+
+        // Render field3 widget
+        $this->renderWidget($form['field3']);
+
+        // Rest should only contain field1 and field4
+        $html = $this->renderRest($form);
+
+        $this->assertMatchesXpath($html,
+'/tr[@style="display: none"]
+    [./td[@colspan="2"]/input
+        [@type="hidden"]
+        [@id="name__token"]
+    ]
+/following-sibling::tr
+    [
+        ./td
+            [./label[@for="name_field1"]]
+        /following-sibling::td
+            [./input[@id="name_field1"]]
+    ]
+/following-sibling::tr
+    [
+        ./td
+            [./label[@for="name_field4"]]
+        /following-sibling::td
+            [./input[@id="name_field4"]]
+    ]
+    [count(../tr)=3]
+    [count(..//label)=2]
+    [count(..//input)=3]
+'
+        );
+    }
+
+    public function testCollection()
+    {
+        $form = $this->factory->create('collection', 'name', array(
+            'type' => 'text',
+            'data' => array('a', 'b'),
+        ));
+
+        $this->assertWidgetMatchesXpath($form, array(),
+'/table
+    [
+        ./tr[./td/input[@type="text"][@value="a"]]
+        /following-sibling::tr[./td/input[@type="text"][@value="b"]]
+    ]
+    [count(./tr[./td/input])=2]
+'
+        );
+    }
+
+    public function testForm()
+    {
+        $form = $this->factory->createBuilder('form', 'name')
+            ->add('firstName', 'text')
+            ->add('lastName', 'text')
+            ->getForm();
+
+        $this->assertWidgetMatchesXpath($form, array(),
+'/table
+    [
+        ./tr[@style="display: none"]
+            [./td[@colspan="2"]/input
+                [@type="hidden"]
+                [@id="name__token"]
+            ]
+        /following-sibling::tr
+            [
+                ./td
+                    [./label[@for="name_firstName"]]
+                /following-sibling::td
+                    [./input[@id="name_firstName"]]
+            ]
+        /following-sibling::tr
+            [
+                ./td
+                    [./label[@for="name_lastName"]]
+                /following-sibling::td
+                    [./input[@id="name_lastName"]]
+            ]
+    ]
+    [count(.//input)=3]
+'
+        );
+    }
+
+    public function testRepeated()
+    {
+        $form = $this->factory->create('repeated', 'name', array(
+            'type' => 'text',
+            'data' => 'foobar',
+        ));
+
+        $this->assertWidgetMatchesXpath($form, array(),
+'/table
+    [
+        ./tr
+            [
+                ./td
+                    [./label[@for="name_first"]]
+                /following-sibling::td
+                    [./input[@id="name_first"]]
+            ]
+        /following-sibling::tr
+            [
+                ./td
+                    [./label[@for="name_second"]]
+                /following-sibling::td
+                    [./input[@id="name_second"]]
+            ]
+    ]
+    [count(.//input)=2]
+'
+        );
+    }
+}