AbstractThemeTest.php 5.0 KB

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