AbstractDivLayoutTest.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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;
  11. use Symfony\Component\Form\FormError;
  12. abstract class AbstractDivLayoutTest extends AbstractLayoutTest
  13. {
  14. public function testRow()
  15. {
  16. $form = $this->factory->createNamed('text', 'name');
  17. $form->addError(new FormError('Error!'));
  18. $view = $form->createView();
  19. $html = $this->renderRow($view);
  20. $this->assertMatchesXpath($html,
  21. '/div
  22. [
  23. ./label[@for="name"]
  24. /following-sibling::ul
  25. [./li[.="[trans]Error![/trans]"]]
  26. [count(./li)=1]
  27. /following-sibling::input[@id="name"]
  28. ]
  29. '
  30. );
  31. }
  32. public function testRowForwardsVariables()
  33. {
  34. $view = $this->factory->createNamed('text', 'name')->createView();
  35. $html = $this->renderRow($view, array('label' => 'foo&bar'));
  36. $this->assertMatchesXpath($html,
  37. '/div
  38. [
  39. ./label[@for="name"][.="[trans]foo&bar[/trans]"]
  40. /following-sibling::input[@id="name"]
  41. ]
  42. '
  43. );
  44. }
  45. public function testRepeatedRow()
  46. {
  47. $form = $this->factory->createNamed('repeated', 'name');
  48. $form->addError(new FormError('Error!'));
  49. $view = $form->createView();
  50. $html = $this->renderRow($view);
  51. $this->assertMatchesXpath($html,
  52. '/ul
  53. [./li[.="[trans]Error![/trans]"]]
  54. [count(./li)=1]
  55. /following-sibling::div
  56. [
  57. ./label[@for="name_first"]
  58. /following-sibling::input[@id="name_first"]
  59. ]
  60. /following-sibling::div
  61. [
  62. ./label[@for="name_second"]
  63. /following-sibling::input[@id="name_second"]
  64. ]
  65. '
  66. );
  67. }
  68. public function testRest()
  69. {
  70. $view = $this->factory->createNamedBuilder('form', 'name')
  71. ->add('field1', 'text')
  72. ->add('field2', 'repeated')
  73. ->add('field3', 'text')
  74. ->add('field4', 'text')
  75. ->getForm()
  76. ->createView();
  77. // Render field2 row -> does not implicitely call renderWidget because
  78. // it is a repeated field!
  79. $this->renderRow($view['field2']);
  80. // Render field3 widget
  81. $this->renderWidget($view['field3']);
  82. // Rest should only contain field1 and field4
  83. $html = $this->renderRest($view);
  84. $this->assertMatchesXpath($html,
  85. '/input
  86. [@type="hidden"]
  87. [@id="name__token"]
  88. /following-sibling::div
  89. [
  90. ./label[@for="name_field1"]
  91. /following-sibling::input[@type="text"][@id="name_field1"]
  92. ]
  93. /following-sibling::div
  94. [
  95. ./label[@for="name_field4"]
  96. /following-sibling::input[@type="text"][@id="name_field4"]
  97. ]
  98. [count(../div)=2]
  99. [count(..//label)=2]
  100. [count(..//input)=3]
  101. '
  102. );
  103. }
  104. public function testCollection()
  105. {
  106. $form = $this->factory->createNamed('collection', 'name', array('a', 'b'), array(
  107. 'type' => 'text',
  108. ));
  109. $this->assertWidgetMatchesXpath($form->createView(), array(),
  110. '/div
  111. [
  112. ./div[./input[@type="text"][@value="a"]]
  113. /following-sibling::div[./input[@type="text"][@value="b"]]
  114. ]
  115. [count(./div[./input])=2]
  116. '
  117. );
  118. }
  119. public function testForm()
  120. {
  121. $form = $this->factory->createNamedBuilder('form', 'name')
  122. ->add('firstName', 'text')
  123. ->add('lastName', 'text')
  124. ->getForm();
  125. $this->assertWidgetMatchesXpath($form->createView(), array(),
  126. '/div
  127. [
  128. ./input[@type="hidden"][@id="name__token"]
  129. /following-sibling::div
  130. [
  131. ./label[@for="name_firstName"]
  132. /following-sibling::input[@type="text"][@id="name_firstName"]
  133. ]
  134. /following-sibling::div
  135. [
  136. ./label[@for="name_lastName"]
  137. /following-sibling::input[@type="text"][@id="name_lastName"]
  138. ]
  139. ]
  140. [count(.//input)=3]
  141. '
  142. );
  143. }
  144. public function testRepeated()
  145. {
  146. $form = $this->factory->createNamed('repeated', 'name', 'foobar', array(
  147. 'type' => 'text',
  148. ));
  149. $this->assertWidgetMatchesXpath($form->createView(), array(),
  150. '/div
  151. [
  152. ./div
  153. [
  154. ./label[@for="name_first"]
  155. /following-sibling::input[@type="text"][@id="name_first"]
  156. ]
  157. /following-sibling::div
  158. [
  159. ./label[@for="name_second"]
  160. /following-sibling::input[@type="text"][@id="name_second"]
  161. ]
  162. ]
  163. [count(.//input)=2]
  164. '
  165. );
  166. }
  167. }