AbstractDivLayoutTest.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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 testCollectionRow()
  120. {
  121. $collection = $this->factory->createNamedBuilder(
  122. 'collection',
  123. 'collection',
  124. array('a', 'b'),
  125. array('type' => 'text')
  126. );
  127. $form = $this->factory->createNamedBuilder('form', 'form')
  128. ->add($collection)
  129. ->getForm();
  130. $this->assertWidgetMatchesXpath($form->createView(), array(),
  131. '/div
  132. [
  133. ./input[@type="hidden"][@id="form__token"]
  134. /following-sibling::div
  135. [
  136. ./label[not(@for)]
  137. /following-sibling::div
  138. [
  139. ./div
  140. [
  141. ./label[@for="form_collection_0"]
  142. /following-sibling::input[@type="text"][@value="a"]
  143. ]
  144. /following-sibling::div
  145. [
  146. ./label[@for="form_collection_1"]
  147. /following-sibling::input[@type="text"][@value="b"]
  148. ]
  149. ]
  150. ]
  151. ]
  152. [count(.//input)=3]
  153. '
  154. );
  155. }
  156. public function testForm()
  157. {
  158. $form = $this->factory->createNamedBuilder('form', 'name')
  159. ->add('firstName', 'text')
  160. ->add('lastName', 'text')
  161. ->getForm();
  162. $this->assertWidgetMatchesXpath($form->createView(), array(),
  163. '/div
  164. [
  165. ./input[@type="hidden"][@id="name__token"]
  166. /following-sibling::div
  167. [
  168. ./label[@for="name_firstName"]
  169. /following-sibling::input[@type="text"][@id="name_firstName"]
  170. ]
  171. /following-sibling::div
  172. [
  173. ./label[@for="name_lastName"]
  174. /following-sibling::input[@type="text"][@id="name_lastName"]
  175. ]
  176. ]
  177. [count(.//input)=3]
  178. '
  179. );
  180. }
  181. public function testRepeated()
  182. {
  183. $form = $this->factory->createNamed('repeated', 'name', 'foobar', array(
  184. 'type' => 'text',
  185. ));
  186. $this->assertWidgetMatchesXpath($form->createView(), array(),
  187. '/div
  188. [
  189. ./div
  190. [
  191. ./label[@for="name_first"]
  192. /following-sibling::input[@type="text"][@id="name_first"]
  193. ]
  194. /following-sibling::div
  195. [
  196. ./label[@for="name_second"]
  197. /following-sibling::input[@type="text"][@id="name_second"]
  198. ]
  199. ]
  200. [count(.//input)=2]
  201. '
  202. );
  203. }
  204. }