Jelajahi Sumber

[Form] Improve unit tests for rendering

Victor Berchet 14 tahun lalu
induk
melakukan
a43fad409b

+ 9 - 8
src/Symfony/Bundle/FrameworkBundle/Tests/Templating/Helper/Fixtures/StubTemplateNameParser.php

@@ -18,23 +18,24 @@ class StubTemplateNameParser implements TemplateNameParserInterface
 {
     private $root;
 
-    private $rootCustom;
+    private $rootTheme;
 
-    public function __construct($root, $rootCustom)
+    public function __construct($root, $rootTheme)
     {
         $this->root = $root;
-        $this->rootCustom = $rootCustom;
+        $this->rootTheme = $rootTheme;
     }
 
     public function parse($name)
     {
-        $parts = explode(':', $name);
-        $name = $parts[count($parts) - 1];
+        list($bundle, $controller, $template) = explode(':', $name);
 
-        if ($name[0] == '_') {
-            $path = $this->rootCustom.'/'.$name;
+        if ($template[0] == '_') {
+            $path = $this->rootTheme.'/Custom/'.$template;
+        } elseif ($bundle === 'TestBundle') {
+            $path = $this->rootTheme.'/'.$controller.'/'.$template;
         } else {
-            $path = $this->root.'/'.$parts[count($parts) - 2].'/'.$name;
+            $path = $this->root.'/'.$controller.'/'.$template;
         }
 
         return new TemplateReference($path, 'php');

+ 22 - 3
src/Symfony/Bundle/FrameworkBundle/Tests/Templating/Helper/FormHelperTest.php

@@ -24,7 +24,7 @@ use Symfony\Component\Templating\TemplateNameParser;
 use Symfony\Component\Templating\Loader\FilesystemLoader;
 use Symfony\Tests\Component\Form\AbstractDivLayoutTest;
 
-class FormHelperTest extends AbstractDivLayoutTest
+class FormHelperDivLayoutTest extends AbstractDivLayoutTest
 {
     protected $helper;
 
@@ -33,8 +33,8 @@ class FormHelperTest extends AbstractDivLayoutTest
         parent::setUp();
 
         $root = realpath(__DIR__.'/../../../Resources/views');
-        $rootCustom = realpath(__DIR__.'/Resources');
-        $templateNameParser = new StubTemplateNameParser($root, $rootCustom);
+        $rootTheme = realpath(__DIR__.'/Resources');
+        $templateNameParser = new StubTemplateNameParser($root, $rootTheme);
         $loader = new FilesystemLoader(array());
         $engine = new PhpEngine($templateNameParser, $loader);
 
@@ -80,4 +80,23 @@ class FormHelperTest extends AbstractDivLayoutTest
     {
         return (string)$this->helper->rest($view, $vars);
     }
+
+    protected function setTheme(FormView $view, array $themes)
+    {
+        $this->helper->setTheme($view, $themes);
+    }
+
+    static public function themeBlockInheritanceProvider()
+    {
+        return array(
+            array(array('TestBundle:Parent'))
+        );
+    }
+
+    static public function themeInheritanceProvider()
+    {
+        return array(
+            array(array('TestBundle:Parent'), array('TestBundle:Child'))
+        );
+    }
 }

+ 8 - 3
src/Symfony/Bundle/FrameworkBundle/Tests/Templating/Helper/FormHelperTableTest.php

@@ -24,7 +24,7 @@ use Symfony\Component\Templating\TemplateNameParser;
 use Symfony\Component\Templating\Loader\FilesystemLoader;
 use Symfony\Tests\Component\Form\AbstractTableLayoutTest;
 
-class FormHelperTableTest extends AbstractTableLayoutTest
+class FormHelperTableLayoutTest extends AbstractTableLayoutTest
 {
     protected $helper;
 
@@ -33,8 +33,8 @@ class FormHelperTableTest extends AbstractTableLayoutTest
         parent::setUp();
 
         $root = realpath(__DIR__.'/../../../Resources/views');
-        $rootCustom = realpath(__DIR__.'/Resources');
-        $templateNameParser = new StubTemplateNameParser($root, $rootCustom);
+        $rootTheme = realpath(__DIR__.'/Resources');
+        $templateNameParser = new StubTemplateNameParser($root, $rootTheme);
         $loader = new FilesystemLoader(array());
         $engine = new PhpEngine($templateNameParser, $loader);
 
@@ -83,4 +83,9 @@ class FormHelperTableTest extends AbstractTableLayoutTest
     {
         return (string)$this->helper->rest($view, $vars);
     }
+
+    protected function setTheme(FormView $view, array $themes)
+    {
+        $this->helper->setTheme($view, $themes);
+    }
 }

+ 1 - 0
src/Symfony/Bundle/FrameworkBundle/Tests/Templating/Helper/Resources/Child/field_label.html.php

@@ -0,0 +1 @@
+<label>child</label>

src/Symfony/Bundle/FrameworkBundle/Tests/Templating/Helper/Resources/_text_id_widget.html.php → src/Symfony/Bundle/FrameworkBundle/Tests/Templating/Helper/Resources/Custom/_text_id_widget.html.php


+ 1 - 0
src/Symfony/Bundle/FrameworkBundle/Tests/Templating/Helper/Resources/Parent/field_label.html.php

@@ -0,0 +1 @@
+<label>parent</label>

+ 2 - 0
src/Symfony/Bundle/FrameworkBundle/Tests/Templating/Helper/Resources/Parent/field_widget.html.php

@@ -0,0 +1,2 @@
+<?php $type = isset($type) ? $type : 'text' ?>
+<input type="<?php echo $type ?>" <?php $view['form']->renderBlock('attributes') ?> value="<?php echo $value ?>" rel="theme" />

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

@@ -23,6 +23,8 @@ use Symfony\Tests\Bridge\Twig\Extension\Fixtures\StubFilesystemLoader;
 
 class FormExtensionDivLayoutTest extends AbstractDivLayoutTest
 {
+    protected $extension;
+
     protected function setUp()
     {
         if (!class_exists('Twig_Environment')) {
@@ -55,21 +57,6 @@ class FormExtensionDivLayoutTest extends AbstractDivLayoutTest
         $this->extension = null;
     }
 
-    public function testThemeBlockInheritance()
-    {
-        $view = $this->factory
-            ->createNamed('email', 'name')
-            ->createView()
-        ;
-
-        $this->extension->setTheme($view, array('theme.html.twig'));
-
-        $this->assertMatchesXpath(
-            $this->renderWidget($view),
-            '/input[@type="email"][@rel="theme"]'
-        );
-    }
-
     public function testThemeBlockInheritanceUsingUse()
     {
         $view = $this->factory
@@ -77,7 +64,7 @@ class FormExtensionDivLayoutTest extends AbstractDivLayoutTest
             ->createView()
         ;
 
-        $this->extension->setTheme($view, array('theme_use.html.twig'));
+        $this->setTheme($view, array('theme_use.html.twig'));
 
         $this->assertMatchesXpath(
             $this->renderWidget($view),
@@ -92,7 +79,7 @@ class FormExtensionDivLayoutTest extends AbstractDivLayoutTest
             ->createView()
         ;
 
-        $this->extension->setTheme($view, array('theme_extends.html.twig'));
+        $this->setTheme($view, array('theme_extends.html.twig'));
 
         $this->assertMatchesXpath(
             $this->renderWidget($view),
@@ -100,48 +87,6 @@ class FormExtensionDivLayoutTest extends AbstractDivLayoutTest
         );
     }
 
-    public function testThemeInheritance()
-    {
-        $child = $this->factory->createNamedBuilder('form', 'child')
-            ->add('field', 'text')
-            ->getForm();
-
-        $view = $this->factory->createNamedBuilder('form', 'parent')
-            ->add('field', 'text')
-            ->getForm()
-            ->add($child)
-            ->createView()
-        ;
-
-        $this->extension->setTheme($view, array('parent_label.html.twig'));
-        $this->extension->setTheme($view['child'], array('child_label.html.twig'));
-
-        $this->assertWidgetMatchesXpath($view, array(),
-'/div
-    [
-        ./input[@type="hidden"]
-        /following-sibling::div
-            [
-                ./label[.="parent"]
-                /following-sibling::input[@type="text"]
-            ]
-        /following-sibling::div
-            [
-                ./label
-                /following-sibling::div
-                    [
-                        ./div
-                            [
-                                ./label[.="child"]
-                                /following-sibling::input[@type="text"]
-                            ]
-                    ]
-            ]
-    ]
-'
-        );
-    }
-
     protected function renderEnctype(FormView $view)
     {
         return (string)$this->extension->renderEnctype($view);
@@ -171,4 +116,23 @@ class FormExtensionDivLayoutTest extends AbstractDivLayoutTest
     {
         return (string)$this->extension->renderRest($view, $vars);
     }
+
+    protected function setTheme(FormView $view, array $themes)
+    {
+        $this->extension->setTheme($view, $themes);
+    }
+
+    static public function themeBlockInheritanceProvider()
+    {
+        return array(
+            array(array('theme.html.twig'))
+        );
+    }
+
+    static public function themeInheritanceProvider()
+    {
+        return array(
+            array(array('parent_label.html.twig'), array('child_label.html.twig'))
+        );
+    }
 }

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

@@ -23,6 +23,8 @@ use Symfony\Tests\Bridge\Twig\Extension\Fixtures\StubFilesystemLoader;
 
 class FormExtensionTableLayoutTest extends AbstractTableLayoutTest
 {
+    protected $extension;
+
     protected function setUp()
     {
         if (!class_exists('Twig_Environment')) {
@@ -84,4 +86,9 @@ class FormExtensionTableLayoutTest extends AbstractTableLayoutTest
     {
         return (string)$this->extension->renderRest($view, $vars);
     }
+
+    protected function setTheme(FormView $view, array $themes)
+    {
+        $this->extension->setTheme($view, $themes);
+    }
 }

+ 63 - 0
tests/Symfony/Tests/Component/Form/AbstractDivLayoutTest.php

@@ -401,6 +401,69 @@ abstract class AbstractDivLayoutTest extends AbstractLayoutTest
         ./label[@for="name"][not(@id)]
         /following-sibling::input[@id="name"]
     ]
+'
+        );
+    }
+
+    /**
+     * @dataProvider themeBlockInheritanceProvider
+     */
+    public function testThemeBlockInheritance($theme)
+    {
+        $view = $this->factory
+            ->createNamed('email', 'name')
+            ->createView()
+        ;
+
+        $this->setTheme($view, $theme);
+
+        $this->assertMatchesXpath(
+            $this->renderWidget($view),
+            '/input[@type="email"][@rel="theme"]'
+        );
+    }
+
+    /**
+     * @dataProvider themeInheritanceProvider
+     */
+    public function testThemeInheritance($parentTheme, $childTheme)
+    {
+        $child = $this->factory->createNamedBuilder('form', 'child')
+            ->add('field', 'text')
+            ->getForm();
+
+        $view = $this->factory->createNamedBuilder('form', 'parent')
+            ->add('field', 'text')
+            ->getForm()
+            ->add($child)
+            ->createView()
+        ;
+
+        $this->setTheme($view, $parentTheme);
+        $this->setTheme($view['child'], $childTheme);
+
+        $this->assertWidgetMatchesXpath($view, array(),
+'/div
+    [
+        ./input[@type="hidden"]
+        /following-sibling::div
+            [
+                ./label[.="parent"]
+                /following-sibling::input[@type="text"]
+            ]
+        /following-sibling::div
+            [
+                ./label
+                /following-sibling::div
+                    [
+                        ./div
+                            [
+                                ./label[.="child"]
+                                /following-sibling::input[@type="text"]
+                            ]
+                    ]
+            ]
+    ]
 '
         );
     }

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

@@ -107,6 +107,8 @@ abstract class AbstractLayoutTest extends \PHPUnit_Framework_TestCase
 
     abstract protected function renderRest(FormView $view, array $vars = array());
 
+    abstract protected function setTheme(FormView $view, array $themes);
+
     public function testEnctype()
     {
         $form = $this->factory->createNamedBuilder('form', 'na&me', null, array(