Преглед изворни кода

[Form] Added tests for blocks/templates in the format _<ID>_(widget|row|label|...)

Bernhard Schussek пре 14 година
родитељ
комит
38098604af

+ 2 - 3
src/Symfony/Bridge/Twig/Extension/FormExtension.php

@@ -156,9 +156,8 @@ class FormExtension extends \Twig_Extension
     {
         $templates = $this->getTemplates($view);
         $blocks = $view->get('types');
-        if ('widget' === $section || 'row' === $section) {
-            array_unshift($blocks, '_'.$view->get('id'));
-        }
+        array_unshift($blocks, '_'.$view->get('id'));
+
         foreach ($blocks as &$block) {
             $block = $block.'_'.$section;
 

+ 2 - 3
src/Symfony/Bundle/FrameworkBundle/Templating/Helper/FormHelper.php

@@ -101,9 +101,8 @@ class FormHelper extends Helper
     {
         $template = null;
         $blocks = $view->get('types');
-        if ('widget' === $section || 'row' === $section) {
-            array_unshift($blocks, '_'.$view->get('id'));
-        }
+        array_unshift($blocks, '_'.$view->get('id'));
+
         foreach ($blocks as &$block) {
             $block = $block.'_'.$section;
             $template = $this->lookupTemplate($block);

+ 7 - 2
src/Symfony/Bundle/FrameworkBundle/Tests/Templating/Helper/Fixtures/StubTemplateNameParser.php

@@ -18,9 +18,12 @@ class StubTemplateNameParser implements TemplateNameParserInterface
 {
     private $root;
 
-    public function __construct($root)
+    private $rootCustom;
+
+    public function __construct($root, $rootCustom)
     {
         $this->root = $root;
+        $this->rootCustom = $rootCustom;
     }
 
     public function parse($name)
@@ -28,6 +31,8 @@ class StubTemplateNameParser implements TemplateNameParserInterface
         $parts = explode(':', $name);
         $name = $parts[count($parts)-1];
 
-        return new TemplateReference($this->root.'/'.$name, 'php');
+        $path = ($name{0} === '_' ? $this->rootCustom : $this->root).'/'.$name;
+
+        return new TemplateReference($path, 'php');
     }
 }

+ 2 - 1
src/Symfony/Bundle/FrameworkBundle/Tests/Templating/Helper/FormHelperTest.php

@@ -33,7 +33,8 @@ class FormHelperTest extends AbstractDivLayoutTest
         parent::setUp();
 
         $root = realpath(__DIR__.'/../../../Resources/views/Form');
-        $templateNameParser = new StubTemplateNameParser($root);
+        $rootCustom = realpath(__DIR__.'/Resources');
+        $templateNameParser = new StubTemplateNameParser($root, $rootCustom);
         $loader = new FilesystemLoader(array());
         $engine = new PhpEngine($templateNameParser, $loader);
 

+ 3 - 0
src/Symfony/Bundle/FrameworkBundle/Tests/Templating/Helper/Resources/_text_id_widget.html.php

@@ -0,0 +1,3 @@
+<div id="container">
+    <?php echo $view['form']->render('FrameworkBundle:Form:text_widget.html.php') ?>
+</div>

+ 5 - 1
tests/Symfony/Tests/Bridge/Twig/Extension/FormExtensionDivLayoutTest.php

@@ -29,9 +29,13 @@ class FormExtensionDivLayoutTest extends AbstractDivLayoutTest
 
         $loader = new StubFilesystemLoader(array(
             __DIR__.'/../../../../../../src/Symfony/Bundle/TwigBundle/Resources/views/Form',
+            __DIR__,
         ));
 
-        $this->extension = new FormExtension(array('div_layout.html.twig'));
+        $this->extension = new FormExtension(array(
+            'div_layout.html.twig',
+            'custom_widgets.html.twig',
+        ));
 
         $environment = new \Twig_Environment($loader);
         $environment->addExtension($this->extension);

+ 5 - 1
tests/Symfony/Tests/Bridge/Twig/Extension/FormExtensionTableLayoutTest.php

@@ -29,9 +29,13 @@ class FormExtensionTableLayoutTest extends AbstractTableLayoutTest
 
         $loader = new StubFilesystemLoader(array(
             __DIR__.'/../../../../../../src/Symfony/Bundle/TwigBundle/Resources/views/Form',
+            __DIR__,
         ));
 
-        $this->extension = new FormExtension(array('table_layout.html.twig'));
+        $this->extension = new FormExtension(array(
+            'table_layout.html.twig',
+            'custom_widgets.html.twig',
+        ));
 
         $environment = new \Twig_Environment($loader);
         $environment->addExtension($this->extension);

+ 6 - 0
tests/Symfony/Tests/Bridge/Twig/Extension/custom_widgets.html.twig

@@ -0,0 +1,6 @@
+{% block _text_id_widget %}
+<div id="container">
+    {# TODO find a smarter way to render the parent type #}
+    <input type="text" id="{{ id }}" value="{{ value }}" />
+</div>
+{% endblock _text_id_widget %}

+ 17 - 0
tests/Symfony/Tests/Component/Form/AbstractLayoutTest.php

@@ -192,6 +192,23 @@ abstract class AbstractLayoutTest extends \PHPUnit_Framework_TestCase
         );
     }
 
+    public function testWidgetById()
+    {
+        $form = $this->factory->createNamed('text', 'text_id');
+        $html = $this->renderWidget($form->createView());
+
+        $this->assertMatchesXpath($html,
+'/div
+    [
+        ./input
+        [@type="text"]
+        [@id="text_id"]
+    ]
+    [@id="container"]
+'
+        );
+    }
+
     public function testCheckedCheckbox()
     {
         $form = $this->factory->createNamed('checkbox', 'na&me', true, array(