Skip to content

Commit

Permalink
[Form] Improve unit tests for rendering
Browse files Browse the repository at this point in the history
  • Loading branch information
vicb committed Jun 22, 2011
1 parent 1cb2129 commit a43fad4
Show file tree
Hide file tree
Showing 11 changed files with 138 additions and 73 deletions.
Expand Up @@ -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');
Expand Down
Expand Up @@ -24,7 +24,7 @@
use Symfony\Component\Templating\Loader\FilesystemLoader;
use Symfony\Tests\Component\Form\AbstractDivLayoutTest;

class FormHelperTest extends AbstractDivLayoutTest
class FormHelperDivLayoutTest extends AbstractDivLayoutTest
{
protected $helper;

Expand All @@ -33,8 +33,8 @@ protected function setUp()
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);

Expand Down Expand Up @@ -80,4 +80,23 @@ protected function renderRest(FormView $view, array $vars = array())
{
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'))
);
}
}
Expand Up @@ -24,7 +24,7 @@
use Symfony\Component\Templating\Loader\FilesystemLoader;
use Symfony\Tests\Component\Form\AbstractTableLayoutTest;

class FormHelperTableTest extends AbstractTableLayoutTest
class FormHelperTableLayoutTest extends AbstractTableLayoutTest
{
protected $helper;

Expand All @@ -33,8 +33,8 @@ protected function setUp()
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);

Expand Down Expand Up @@ -83,4 +83,9 @@ protected function renderRest(FormView $view, array $vars = array())
{
return (string)$this->helper->rest($view, $vars);
}

protected function setTheme(FormView $view, array $themes)
{
$this->helper->setTheme($view, $themes);
}
}
@@ -0,0 +1 @@
<label>child</label>
@@ -0,0 +1 @@
<label>parent</label>
@@ -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" />
Expand Up @@ -23,6 +23,8 @@

class FormExtensionDivLayoutTest extends AbstractDivLayoutTest
{
protected $extension;

protected function setUp()
{
if (!class_exists('Twig_Environment')) {
Expand Down Expand Up @@ -55,29 +57,14 @@ protected function tearDown()
$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
->createNamed('email', 'name')
->createView()
;

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

$this->assertMatchesXpath(
$this->renderWidget($view),
Expand All @@ -92,56 +79,14 @@ public function testThemeBlockInheritanceUsingExtend()
->createView()
;

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

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

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);
Expand Down Expand Up @@ -171,4 +116,23 @@ protected function renderRest(FormView $view, array $vars = array())
{
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'))
);
}
}
Expand Up @@ -23,6 +23,8 @@

class FormExtensionTableLayoutTest extends AbstractTableLayoutTest
{
protected $extension;

protected function setUp()
{
if (!class_exists('Twig_Environment')) {
Expand Down Expand Up @@ -84,4 +86,9 @@ protected function renderRest(FormView $view, array $vars = array())
{
return (string)$this->extension->renderRest($view, $vars);
}

protected function setTheme(FormView $view, array $themes)
{
$this->extension->setTheme($view, $themes);
}
}
63 changes: 63 additions & 0 deletions tests/Symfony/Tests/Component/Form/AbstractDivLayoutTest.php
Expand Up @@ -401,6 +401,69 @@ public function testLabelHasNoId()
./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"]
]
]
]
]
'
);
}
Expand Down
2 changes: 2 additions & 0 deletions tests/Symfony/Tests/Component/Form/AbstractLayoutTest.php
Expand Up @@ -107,6 +107,8 @@ abstract protected function renderRow(FormView $view, array $vars = array());

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(
Expand Down

0 comments on commit a43fad4

Please sign in to comment.