AbstractThemeTest.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Tests\Component\Form\Renderer\Theme;
  11. use Symfony\Component\Form\Renderer\Theme\PhpTheme;
  12. use Symfony\Component\Form\Renderer\ThemeRenderer;
  13. use Symfony\Component\Form\ChoiceList\DefaultChoiceList;
  14. abstract class AbstractThemeTest extends \PHPUnit_Framework_TestCase
  15. {
  16. private $themeFactory;
  17. public function setUp()
  18. {
  19. $this->themeFactory = $this->createThemeFactory();
  20. }
  21. abstract protected function createThemeFactory();
  22. public function testTextWidgetDefault()
  23. {
  24. $input = $this->renderAsDomElement('text', 'widget', array(
  25. 'id' => 'foo',
  26. 'name' => 'foo',
  27. 'value' => '',
  28. 'class' => '',
  29. 'max_length' => null,
  30. 'read_only' => false,
  31. 'required' => false,
  32. 'size' => null
  33. ));
  34. $this->assertEquals('input', $input->tagName);
  35. $this->assertTrue($input->hasAttribute('id'), "Has id attribute");
  36. $this->assertEquals('foo', $input->getAttribute('id'), "Id attribute is set to field name.");
  37. $this->assertEquals('foo', $input->getAttribute('name'), "Field name translated to input name");
  38. $this->assertEquals('', $input->getAttribute('value'), "By default value is empty");
  39. $this->assertFalse($input->hasAttribute('class'), "No class attribute by default.");
  40. $this->assertFalse($input->hasAttribute('maxlength'), "has no maxlength attribute.");
  41. $this->assertFalse($input->hasAttribute('size'), "has no size attribute");
  42. $this->assertFalse($input->hasAttribute('read_only'));
  43. $this->assertFalse($input->hasAttribute('required'));
  44. }
  45. public function testTextWidgetFull()
  46. {
  47. $input = $this->renderAsDomElement('text', 'widget', array(
  48. 'id' => 'foo',
  49. 'name' => 'foo',
  50. 'value' => 'test',
  51. 'class' => 'foo',
  52. 'max_length' => 128,
  53. 'read_only' => true,
  54. 'required' => true,
  55. 'size' => 20,
  56. 'attr' => array('accesskey' => 'G', 'title' => 'Foo'),
  57. 'renderer' => new ThemeRenderer($this->themeFactory, null),
  58. ));
  59. $this->assertEquals('test', $input->getAttribute('value'));
  60. $this->assertEquals('foo', $input->getAttribute('class'));
  61. $this->assertEquals('128', $input->getAttribute('maxlength'));
  62. $this->assertTrue($input->hasAttribute('disabled'));
  63. $this->assertTrue($input->hasAttribute('required'));
  64. $this->assertEquals('20', $input->getAttribute('size'));
  65. $this->assertTrue($input->hasAttribute('accesskey'));
  66. $this->assertEquals('G', $input->getAttribute('accesskey'));
  67. $this->assertTrue($input->hasAttribute('title'));
  68. $this->assertEquals('Foo', $input->getAttribute('title'));
  69. }
  70. public function testChoiceWidgetDefaults()
  71. {
  72. $factory = $this->getMock('Symfony\Component\Form\Renderer\Theme\ThemeFactoryInterface');
  73. $renderer = new ThemeRenderer($factory);
  74. $renderer->setVar('choices', array(
  75. 'foo' => 'Foo',
  76. 'bar' => 'Bar',
  77. ));
  78. $renderer->setVar('preferred_choices', array(
  79. 'baz' => 'Baz',
  80. ));
  81. $renderer->setVar('value', 'foo');
  82. $input = $this->renderAsDomElement('choice', 'widget', array(
  83. 'id' => 'foo',
  84. 'name' => 'foo',
  85. 'value' => 'foo',
  86. 'class' => 'foo',
  87. 'read_only' => false,
  88. 'required' => false,
  89. 'empty_value' => '---',
  90. 'expanded' => false,
  91. 'multiple' => true,
  92. 'renderer' => $renderer,
  93. 'choices' => $renderer->getVar('choices'),
  94. 'preferred_choices' => $renderer->getVar('preferred_choices'),
  95. 'separator' => '---',
  96. ));
  97. $this->assertXpathNodeValue($input, '//select/option[@selected="selected"]', 'Foo');
  98. $this->assertXpathMatches($input, '//select/option', 5);
  99. $this->assertXpathNodeValue($input, '//select/option[@disabled="disabled"]', '---');
  100. $this->assertXpathMatches($input, '//select[@multiple="multiple"]', 1);
  101. }
  102. protected function assertXpathNodeValue($element, $expression, $nodeValue)
  103. {
  104. $xpath = new \DOMXPath($element->ownerDocument);
  105. $nodeList = $xpath->evaluate($expression);
  106. $this->assertEquals(1, $nodeList->length);
  107. $this->assertEquals($nodeValue, $nodeList->item(0)->nodeValue);
  108. }
  109. protected function assertXpathMatches($element, $expression, $matches)
  110. {
  111. $xpath = new \DOMXPath($element->ownerDocument);
  112. $nodeList = $xpath->evaluate($expression);
  113. $this->assertEquals($matches, $nodeList->length);
  114. }
  115. protected function renderAsDomElement($block, $section, $parameters)
  116. {
  117. $html = $this->themeFactory->create()->render(array($block), $section, $parameters);
  118. $dom = new \DomDocument('UTF-8');
  119. $dom->loadXml($html);
  120. return $dom->documentElement;
  121. }
  122. }