AbstractThemeTest.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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. abstract class AbstractThemeTest extends \PHPUnit_Framework_TestCase
  13. {
  14. private $theme;
  15. public function setUp()
  16. {
  17. $this->theme = $this->createTheme();
  18. }
  19. abstract protected function createTheme();
  20. public function testTextWidgetDefault()
  21. {
  22. $input = $this->renderAsDomElement('text', 'widget', array(
  23. 'id' => 'foo',
  24. 'name' => 'foo',
  25. 'value' => '',
  26. 'class' => '',
  27. 'max_length' => null,
  28. 'disabled' => false,
  29. 'required' => false,
  30. 'size' => null
  31. ));
  32. $this->assertEquals('input', $input->tagName);
  33. $this->assertTrue($input->hasAttribute('id'), "Has id attribute");
  34. $this->assertEquals('foo', $input->getAttribute('id'), "Id attribute is set to field name.");
  35. $this->assertEquals('foo', $input->getAttribute('name'), "Field name translated to input name");
  36. $this->assertEquals('', $input->getAttribute('value'), "By default value is empty");
  37. $this->assertFalse($input->hasAttribute('class'), "No class attribute by default.");
  38. $this->assertFalse($input->hasAttribute('maxlength'), "has no maxlength attribute.");
  39. $this->assertFalse($input->hasAttribute('size'), "has no size attribute");
  40. $this->assertFalse($input->hasAttribute('disabled'));
  41. $this->assertFalse($input->hasAttribute('required'));
  42. }
  43. public function testTextWidgetFull()
  44. {
  45. $input = $this->renderAsDomElement('text', 'widget', array(
  46. 'id' => 'foo',
  47. 'name' => 'foo',
  48. 'value' => 'test',
  49. 'class' => 'foo',
  50. 'max_length' => 128,
  51. 'disabled' => true,
  52. 'required' => true,
  53. 'size' => 20
  54. ));
  55. $this->assertEquals('test', $input->getAttribute('value'));
  56. $this->assertEquals('foo', $input->getAttribute('class'));
  57. $this->assertEquals('128', $input->getAttribute('maxlength'));
  58. $this->assertTrue($input->hasAttribute('disabled'));
  59. $this->assertTrue($input->hasAttribute('required'));
  60. $this->assertEquals('20', $input->getAttribute('size'));
  61. }
  62. public function testChoiceWidgetDefaults()
  63. {
  64. $choiceList = new \Symfony\Component\Form\ChoiceList\DefaultChoiceList(array(
  65. 'foo' => 'Foo',
  66. 'bar' => 'Bar',
  67. 'baz' => 'Baz',
  68. ), array('baz'));
  69. $input = $this->renderAsDomElement('choice', 'widget', array(
  70. 'id' => 'foo',
  71. 'name' => 'foo',
  72. 'value' => 'foo',
  73. 'class' => 'foo',
  74. 'disabled' => false,
  75. 'required' => false,
  76. 'expanded' => false,
  77. 'multiple' => true,
  78. 'preferred_choices' => $choiceList->getPreferredChoices(),
  79. 'choices' => $choiceList->getOtherChoices(),
  80. 'choice_list' => $choiceList,
  81. 'separator' => '---'
  82. ));
  83. $this->assertXpathNodeValue($input, '//select/option[@selected="selected"]', 'Foo');
  84. $this->assertXpathMatches($input, '//select/option', 4);
  85. $this->assertXpathNodeValue($input, '//select/option[@disabled="disabled"]', '---');
  86. $this->assertXpathMatches($input, '//select[@multiple="multiple"]', 1);
  87. }
  88. protected function assertXpathNodeValue($element, $expression, $nodeValue)
  89. {
  90. $xpath = new \DOMXPath($element->ownerDocument);
  91. $nodeList = $xpath->evaluate($expression);
  92. $this->assertEquals(1, $nodeList->length);
  93. $this->assertEquals($nodeValue, $nodeList->item(0)->nodeValue);
  94. }
  95. protected function assertXpathMatches($element, $expression, $matches)
  96. {
  97. $xpath = new \DOMXPath($element->ownerDocument);
  98. $nodeList = $xpath->evaluate($expression);
  99. $this->assertEquals($matches, $nodeList->length);
  100. }
  101. protected function renderAsDomElement($field, $section, $parameters)
  102. {
  103. $html = $this->theme->render($field, $section, $parameters);
  104. $dom = new \DomDocument('UTF-8');
  105. $dom->loadXml($html);
  106. return $dom->documentElement;
  107. }
  108. }