Skip to content

Commit a43fad4

Browse files
committed
[Form] Improve unit tests for rendering
1 parent 1cb2129 commit a43fad4

File tree

11 files changed

+138
-73
lines changed

11 files changed

+138
-73
lines changed

src/Symfony/Bundle/FrameworkBundle/Tests/Templating/Helper/Fixtures/StubTemplateNameParser.php

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,23 +18,24 @@ class StubTemplateNameParser implements TemplateNameParserInterface
1818
{
1919
private $root;
2020

21-
private $rootCustom;
21+
private $rootTheme;
2222

23-
public function __construct($root, $rootCustom)
23+
public function __construct($root, $rootTheme)
2424
{
2525
$this->root = $root;
26-
$this->rootCustom = $rootCustom;
26+
$this->rootTheme = $rootTheme;
2727
}
2828

2929
public function parse($name)
3030
{
31-
$parts = explode(':', $name);
32-
$name = $parts[count($parts) - 1];
31+
list($bundle, $controller, $template) = explode(':', $name);
3332

34-
if ($name[0] == '_') {
35-
$path = $this->rootCustom.'/'.$name;
33+
if ($template[0] == '_') {
34+
$path = $this->rootTheme.'/Custom/'.$template;
35+
} elseif ($bundle === 'TestBundle') {
36+
$path = $this->rootTheme.'/'.$controller.'/'.$template;
3637
} else {
37-
$path = $this->root.'/'.$parts[count($parts) - 2].'/'.$name;
38+
$path = $this->root.'/'.$controller.'/'.$template;
3839
}
3940

4041
return new TemplateReference($path, 'php');

src/Symfony/Bundle/FrameworkBundle/Tests/Templating/Helper/FormHelperTest.php renamed to src/Symfony/Bundle/FrameworkBundle/Tests/Templating/Helper/FormHelperDivLayoutTest.php

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
use Symfony\Component\Templating\Loader\FilesystemLoader;
2525
use Symfony\Tests\Component\Form\AbstractDivLayoutTest;
2626

27-
class FormHelperTest extends AbstractDivLayoutTest
27+
class FormHelperDivLayoutTest extends AbstractDivLayoutTest
2828
{
2929
protected $helper;
3030

@@ -33,8 +33,8 @@ protected function setUp()
3333
parent::setUp();
3434

3535
$root = realpath(__DIR__.'/../../../Resources/views');
36-
$rootCustom = realpath(__DIR__.'/Resources');
37-
$templateNameParser = new StubTemplateNameParser($root, $rootCustom);
36+
$rootTheme = realpath(__DIR__.'/Resources');
37+
$templateNameParser = new StubTemplateNameParser($root, $rootTheme);
3838
$loader = new FilesystemLoader(array());
3939
$engine = new PhpEngine($templateNameParser, $loader);
4040

@@ -80,4 +80,23 @@ protected function renderRest(FormView $view, array $vars = array())
8080
{
8181
return (string)$this->helper->rest($view, $vars);
8282
}
83+
84+
protected function setTheme(FormView $view, array $themes)
85+
{
86+
$this->helper->setTheme($view, $themes);
87+
}
88+
89+
static public function themeBlockInheritanceProvider()
90+
{
91+
return array(
92+
array(array('TestBundle:Parent'))
93+
);
94+
}
95+
96+
static public function themeInheritanceProvider()
97+
{
98+
return array(
99+
array(array('TestBundle:Parent'), array('TestBundle:Child'))
100+
);
101+
}
83102
}

src/Symfony/Bundle/FrameworkBundle/Tests/Templating/Helper/FormHelperTableTest.php renamed to src/Symfony/Bundle/FrameworkBundle/Tests/Templating/Helper/FormHelperTableLayoutTest.php

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
use Symfony\Component\Templating\Loader\FilesystemLoader;
2525
use Symfony\Tests\Component\Form\AbstractTableLayoutTest;
2626

27-
class FormHelperTableTest extends AbstractTableLayoutTest
27+
class FormHelperTableLayoutTest extends AbstractTableLayoutTest
2828
{
2929
protected $helper;
3030

@@ -33,8 +33,8 @@ protected function setUp()
3333
parent::setUp();
3434

3535
$root = realpath(__DIR__.'/../../../Resources/views');
36-
$rootCustom = realpath(__DIR__.'/Resources');
37-
$templateNameParser = new StubTemplateNameParser($root, $rootCustom);
36+
$rootTheme = realpath(__DIR__.'/Resources');
37+
$templateNameParser = new StubTemplateNameParser($root, $rootTheme);
3838
$loader = new FilesystemLoader(array());
3939
$engine = new PhpEngine($templateNameParser, $loader);
4040

@@ -83,4 +83,9 @@ protected function renderRest(FormView $view, array $vars = array())
8383
{
8484
return (string)$this->helper->rest($view, $vars);
8585
}
86+
87+
protected function setTheme(FormView $view, array $themes)
88+
{
89+
$this->helper->setTheme($view, $themes);
90+
}
8691
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<label>child</label>
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<label>parent</label>
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
<?php $type = isset($type) ? $type : 'text' ?>
2+
<input type="<?php echo $type ?>" <?php $view['form']->renderBlock('attributes') ?> value="<?php echo $value ?>" rel="theme" />

tests/Symfony/Tests/Bridge/Twig/Extension/FormExtensionDivLayoutTest.php

Lines changed: 23 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323

2424
class FormExtensionDivLayoutTest extends AbstractDivLayoutTest
2525
{
26+
protected $extension;
27+
2628
protected function setUp()
2729
{
2830
if (!class_exists('Twig_Environment')) {
@@ -55,29 +57,14 @@ protected function tearDown()
5557
$this->extension = null;
5658
}
5759

58-
public function testThemeBlockInheritance()
59-
{
60-
$view = $this->factory
61-
->createNamed('email', 'name')
62-
->createView()
63-
;
64-
65-
$this->extension->setTheme($view, array('theme.html.twig'));
66-
67-
$this->assertMatchesXpath(
68-
$this->renderWidget($view),
69-
'/input[@type="email"][@rel="theme"]'
70-
);
71-
}
72-
7360
public function testThemeBlockInheritanceUsingUse()
7461
{
7562
$view = $this->factory
7663
->createNamed('email', 'name')
7764
->createView()
7865
;
7966

80-
$this->extension->setTheme($view, array('theme_use.html.twig'));
67+
$this->setTheme($view, array('theme_use.html.twig'));
8168

8269
$this->assertMatchesXpath(
8370
$this->renderWidget($view),
@@ -92,56 +79,14 @@ public function testThemeBlockInheritanceUsingExtend()
9279
->createView()
9380
;
9481

95-
$this->extension->setTheme($view, array('theme_extends.html.twig'));
82+
$this->setTheme($view, array('theme_extends.html.twig'));
9683

9784
$this->assertMatchesXpath(
9885
$this->renderWidget($view),
9986
'/input[@type="email"][@rel="theme"]'
10087
);
10188
}
10289

103-
public function testThemeInheritance()
104-
{
105-
$child = $this->factory->createNamedBuilder('form', 'child')
106-
->add('field', 'text')
107-
->getForm();
108-
109-
$view = $this->factory->createNamedBuilder('form', 'parent')
110-
->add('field', 'text')
111-
->getForm()
112-
->add($child)
113-
->createView()
114-
;
115-
116-
$this->extension->setTheme($view, array('parent_label.html.twig'));
117-
$this->extension->setTheme($view['child'], array('child_label.html.twig'));
118-
119-
$this->assertWidgetMatchesXpath($view, array(),
120-
'/div
121-
[
122-
./input[@type="hidden"]
123-
/following-sibling::div
124-
[
125-
./label[.="parent"]
126-
/following-sibling::input[@type="text"]
127-
]
128-
/following-sibling::div
129-
[
130-
./label
131-
/following-sibling::div
132-
[
133-
./div
134-
[
135-
./label[.="child"]
136-
/following-sibling::input[@type="text"]
137-
]
138-
]
139-
]
140-
]
141-
'
142-
);
143-
}
144-
14590
protected function renderEnctype(FormView $view)
14691
{
14792
return (string)$this->extension->renderEnctype($view);
@@ -171,4 +116,23 @@ protected function renderRest(FormView $view, array $vars = array())
171116
{
172117
return (string)$this->extension->renderRest($view, $vars);
173118
}
119+
120+
protected function setTheme(FormView $view, array $themes)
121+
{
122+
$this->extension->setTheme($view, $themes);
123+
}
124+
125+
static public function themeBlockInheritanceProvider()
126+
{
127+
return array(
128+
array(array('theme.html.twig'))
129+
);
130+
}
131+
132+
static public function themeInheritanceProvider()
133+
{
134+
return array(
135+
array(array('parent_label.html.twig'), array('child_label.html.twig'))
136+
);
137+
}
174138
}

tests/Symfony/Tests/Bridge/Twig/Extension/FormExtensionTableLayoutTest.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323

2424
class FormExtensionTableLayoutTest extends AbstractTableLayoutTest
2525
{
26+
protected $extension;
27+
2628
protected function setUp()
2729
{
2830
if (!class_exists('Twig_Environment')) {
@@ -84,4 +86,9 @@ protected function renderRest(FormView $view, array $vars = array())
8486
{
8587
return (string)$this->extension->renderRest($view, $vars);
8688
}
89+
90+
protected function setTheme(FormView $view, array $themes)
91+
{
92+
$this->extension->setTheme($view, $themes);
93+
}
8794
}

tests/Symfony/Tests/Component/Form/AbstractDivLayoutTest.php

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -401,6 +401,69 @@ public function testLabelHasNoId()
401401
./label[@for="name"][not(@id)]
402402
/following-sibling::input[@id="name"]
403403
]
404+
'
405+
);
406+
}
407+
408+
/**
409+
* @dataProvider themeBlockInheritanceProvider
410+
*/
411+
public function testThemeBlockInheritance($theme)
412+
{
413+
$view = $this->factory
414+
->createNamed('email', 'name')
415+
->createView()
416+
;
417+
418+
$this->setTheme($view, $theme);
419+
420+
$this->assertMatchesXpath(
421+
$this->renderWidget($view),
422+
'/input[@type="email"][@rel="theme"]'
423+
);
424+
}
425+
426+
/**
427+
* @dataProvider themeInheritanceProvider
428+
*/
429+
public function testThemeInheritance($parentTheme, $childTheme)
430+
{
431+
$child = $this->factory->createNamedBuilder('form', 'child')
432+
->add('field', 'text')
433+
->getForm();
434+
435+
$view = $this->factory->createNamedBuilder('form', 'parent')
436+
->add('field', 'text')
437+
->getForm()
438+
->add($child)
439+
->createView()
440+
;
441+
442+
$this->setTheme($view, $parentTheme);
443+
$this->setTheme($view['child'], $childTheme);
444+
445+
$this->assertWidgetMatchesXpath($view, array(),
446+
'/div
447+
[
448+
./input[@type="hidden"]
449+
/following-sibling::div
450+
[
451+
./label[.="parent"]
452+
/following-sibling::input[@type="text"]
453+
]
454+
/following-sibling::div
455+
[
456+
./label
457+
/following-sibling::div
458+
[
459+
./div
460+
[
461+
./label[.="child"]
462+
/following-sibling::input[@type="text"]
463+
]
464+
]
465+
]
466+
]
404467
'
405468
);
406469
}

tests/Symfony/Tests/Component/Form/AbstractLayoutTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,8 @@ abstract protected function renderRow(FormView $view, array $vars = array());
107107

108108
abstract protected function renderRest(FormView $view, array $vars = array());
109109

110+
abstract protected function setTheme(FormView $view, array $themes);
111+
110112
public function testEnctype()
111113
{
112114
$form = $this->factory->createNamedBuilder('form', 'na&me', null, array(

0 commit comments

Comments
 (0)