AbstractThemeTest.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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 $themeFactory;
  15. public function setUp()
  16. {
  17. $this->themeFactory = $this->createThemeFactory();
  18. }
  19. abstract protected function createThemeFactory();
  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. 'attr' => array('accesskey' => 'G', 'title' => 'Foo'),
  55. 'renderer' => new \Symfony\Component\Form\Renderer\ThemeRenderer($this->themeFactory, null),
  56. ));
  57. $this->assertEquals('test', $input->getAttribute('value'));
  58. $this->assertEquals('foo', $input->getAttribute('class'));
  59. $this->assertEquals('128', $input->getAttribute('maxlength'));
  60. $this->assertTrue($input->hasAttribute('disabled'));
  61. $this->assertTrue($input->hasAttribute('required'));
  62. $this->assertEquals('20', $input->getAttribute('size'));
  63. $this->assertTrue($input->hasAttribute('accesskey'));
  64. $this->assertEquals('G', $input->getAttribute('accesskey'));
  65. $this->assertTrue($input->hasAttribute('title'));
  66. $this->assertEquals('Foo', $input->getAttribute('title'));
  67. }
  68. public function testChoiceWidgetDefaults()
  69. {
  70. $choiceList = new \Symfony\Component\Form\ChoiceList\DefaultChoiceList(array(
  71. 'foo' => 'Foo',
  72. 'bar' => 'Bar',
  73. 'baz' => 'Baz',
  74. ), array('baz'));
  75. $input = $this->renderAsDomElement('choice', 'widget', array(
  76. 'id' => 'foo',
  77. 'name' => 'foo',
  78. 'value' => 'foo',
  79. 'class' => 'foo',
  80. 'disabled' => false,
  81. 'required' => false,
  82. 'empty_value' => '---',
  83. 'expanded' => false,
  84. 'multiple' => true,
  85. 'preferred_choices' => $choiceList->getPreferredChoices(),
  86. 'choices' => $choiceList->getOtherChoices(),
  87. 'choice_list' => $choiceList,
  88. 'separator' => '---',
  89. ));
  90. $this->assertXpathNodeValue($input, '//select/option[@selected="selected"]', 'Foo');
  91. $this->assertXpathMatches($input, '//select/option', 5);
  92. $this->assertXpathNodeValue($input, '//select/option[@disabled="disabled"]', '---');
  93. $this->assertXpathMatches($input, '//select[@multiple="multiple"]', 1);
  94. }
  95. protected function assertXpathNodeValue($element, $expression, $nodeValue)
  96. {
  97. $xpath = new \DOMXPath($element->ownerDocument);
  98. $nodeList = $xpath->evaluate($expression);
  99. $this->assertEquals(1, $nodeList->length);
  100. $this->assertEquals($nodeValue, $nodeList->item(0)->nodeValue);
  101. }
  102. protected function assertXpathMatches($element, $expression, $matches)
  103. {
  104. $xpath = new \DOMXPath($element->ownerDocument);
  105. $nodeList = $xpath->evaluate($expression);
  106. $this->assertEquals($matches, $nodeList->length);
  107. }
  108. protected function renderAsDomElement($form, $section, $parameters)
  109. {
  110. $html = $this->themeFactory->create()->render($form, $section, $parameters);
  111. $dom = new \DomDocument('UTF-8');
  112. $dom->loadXml($html);
  113. return $dom->documentElement;
  114. }
  115. }