AbstractTableLayoutTest.php 5.3 KB

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