AbstractThemeTest.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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. $choiceList = new DefaultChoiceList(array(
  73. 'foo' => 'Foo',
  74. 'bar' => 'Bar',
  75. 'baz' => 'Baz',
  76. ), array('baz'));
  77. $input = $this->renderAsDomElement('choice', 'widget', array(
  78. 'id' => 'foo',
  79. 'name' => 'foo',
  80. 'value' => 'foo',
  81. 'class' => 'foo',
  82. 'read_only' => false,
  83. 'required' => false,
  84. 'empty_value' => '---',
  85. 'expanded' => false,
  86. 'multiple' => true,
  87. 'preferred_choices' => $choiceList->getPreferredChoices(),
  88. 'choices' => $choiceList->getOtherChoices(),
  89. 'choice_list' => $choiceList,
  90. 'separator' => '---',
  91. ));
  92. $this->assertXpathNodeValue($input, '//select/option[@selected="selected"]', 'Foo');
  93. $this->assertXpathMatches($input, '//select/option', 5);
  94. $this->assertXpathNodeValue($input, '//select/option[@disabled="disabled"]', '---');
  95. $this->assertXpathMatches($input, '//select[@multiple="multiple"]', 1);
  96. }
  97. protected function assertXpathNodeValue($element, $expression, $nodeValue)
  98. {
  99. $xpath = new \DOMXPath($element->ownerDocument);
  100. $nodeList = $xpath->evaluate($expression);
  101. $this->assertEquals(1, $nodeList->length);
  102. $this->assertEquals($nodeValue, $nodeList->item(0)->nodeValue);
  103. }
  104. protected function assertXpathMatches($element, $expression, $matches)
  105. {
  106. $xpath = new \DOMXPath($element->ownerDocument);
  107. $nodeList = $xpath->evaluate($expression);
  108. $this->assertEquals($matches, $nodeList->length);
  109. }
  110. protected function renderAsDomElement($block, $section, $parameters)
  111. {
  112. $html = $this->themeFactory->create()->render(array($block), $section, $parameters);
  113. $dom = new \DomDocument('UTF-8');
  114. $dom->loadXml($html);
  115. return $dom->documentElement;
  116. }
  117. }