AbstractTableLayoutTest.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  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 AbstractTableLayoutTest extends AbstractLayoutTest
  13. {
  14. public function testRow()
  15. {
  16. $form = $this->factory->create('text', 'name');
  17. $form->addError(new FormError('Error!'));
  18. $context = $form->getContext();
  19. $html = $this->renderRow($context);
  20. $this->assertMatchesXpath($html,
  21. '/tr
  22. [
  23. ./td
  24. [./label[@for="name"]]
  25. /following-sibling::td
  26. [
  27. ./ul
  28. [./li[.="[trans]Error![/trans]"]]
  29. [count(./li)=1]
  30. /following-sibling::input[@id="name"]
  31. ]
  32. ]
  33. '
  34. );
  35. }
  36. public function testRepeatedRow()
  37. {
  38. $form = $this->factory->create('repeated', 'name');
  39. $html = $this->renderRow($form->getContext());
  40. $this->assertMatchesXpath($html,
  41. '/tr
  42. [
  43. ./td
  44. [./label[@for="name_first"]]
  45. /following-sibling::td
  46. [./input[@id="name_first"]]
  47. ]
  48. /following-sibling::tr
  49. [
  50. ./td
  51. [./label[@for="name_second"]]
  52. /following-sibling::td
  53. [./input[@id="name_second"]]
  54. ]
  55. [count(../tr)=2]
  56. '
  57. );
  58. }
  59. public function testRepeatedRowWithErrors()
  60. {
  61. $form = $this->factory->create('repeated', 'name');
  62. $form->addError(new FormError('Error!'));
  63. $context = $form->getContext();
  64. $html = $this->renderRow($context);
  65. $this->assertMatchesXpath($html,
  66. '/tr
  67. [./td[@colspan="2"]/ul
  68. [./li[.="[trans]Error![/trans]"]]
  69. ]
  70. /following-sibling::tr
  71. [
  72. ./td
  73. [./label[@for="name_first"]]
  74. /following-sibling::td
  75. [./input[@id="name_first"]]
  76. ]
  77. /following-sibling::tr
  78. [
  79. ./td
  80. [./label[@for="name_second"]]
  81. /following-sibling::td
  82. [./input[@id="name_second"]]
  83. ]
  84. [count(../tr)=3]
  85. '
  86. );
  87. }
  88. public function testRest()
  89. {
  90. $context = $this->factory->createBuilder('form', 'name')
  91. ->add('field1', 'text')
  92. ->add('field2', 'repeated')
  93. ->add('field3', 'text')
  94. ->add('field4', 'text')
  95. ->getForm()
  96. ->getContext();
  97. // Render field2 row -> does not implicitely call renderWidget because
  98. // it is a repeated field!
  99. $this->renderRow($context['field2']);
  100. // Render field3 widget
  101. $this->renderWidget($context['field3']);
  102. // Rest should only contain field1 and field4
  103. $html = $this->renderRest($context);
  104. $this->assertMatchesXpath($html,
  105. '/tr[@style="display: none"]
  106. [./td[@colspan="2"]/input
  107. [@type="hidden"]
  108. [@id="name__token"]
  109. ]
  110. /following-sibling::tr
  111. [
  112. ./td
  113. [./label[@for="name_field1"]]
  114. /following-sibling::td
  115. [./input[@id="name_field1"]]
  116. ]
  117. /following-sibling::tr
  118. [
  119. ./td
  120. [./label[@for="name_field4"]]
  121. /following-sibling::td
  122. [./input[@id="name_field4"]]
  123. ]
  124. [count(../tr)=3]
  125. [count(..//label)=2]
  126. [count(..//input)=3]
  127. '
  128. );
  129. }
  130. public function testCollection()
  131. {
  132. $form = $this->factory->create('collection', 'name', array(
  133. 'type' => 'text',
  134. 'data' => array('a', 'b'),
  135. ));
  136. $this->assertWidgetMatchesXpath($form->getContext(), array(),
  137. '/table
  138. [
  139. ./tr[./td/input[@type="text"][@value="a"]]
  140. /following-sibling::tr[./td/input[@type="text"][@value="b"]]
  141. ]
  142. [count(./tr[./td/input])=2]
  143. '
  144. );
  145. }
  146. public function testForm()
  147. {
  148. $context = $this->factory->createBuilder('form', 'name')
  149. ->add('firstName', 'text')
  150. ->add('lastName', 'text')
  151. ->getForm()
  152. ->getContext();
  153. $this->assertWidgetMatchesXpath($context, array(),
  154. '/table
  155. [
  156. ./tr[@style="display: none"]
  157. [./td[@colspan="2"]/input
  158. [@type="hidden"]
  159. [@id="name__token"]
  160. ]
  161. /following-sibling::tr
  162. [
  163. ./td
  164. [./label[@for="name_firstName"]]
  165. /following-sibling::td
  166. [./input[@id="name_firstName"]]
  167. ]
  168. /following-sibling::tr
  169. [
  170. ./td
  171. [./label[@for="name_lastName"]]
  172. /following-sibling::td
  173. [./input[@id="name_lastName"]]
  174. ]
  175. ]
  176. [count(.//input)=3]
  177. '
  178. );
  179. }
  180. public function testRepeated()
  181. {
  182. $form = $this->factory->create('repeated', 'name', array(
  183. 'type' => 'text',
  184. 'data' => 'foobar',
  185. ));
  186. $this->assertWidgetMatchesXpath($form->getContext(), array(),
  187. '/table
  188. [
  189. ./tr
  190. [
  191. ./td
  192. [./label[@for="name_first"]]
  193. /following-sibling::td
  194. [./input[@id="name_first"]]
  195. ]
  196. /following-sibling::tr
  197. [
  198. ./td
  199. [./label[@for="name_second"]]
  200. /following-sibling::td
  201. [./input[@id="name_second"]]
  202. ]
  203. ]
  204. [count(.//input)=2]
  205. '
  206. );
  207. }
  208. }