AbstractDivLayoutTest.php 4.3 KB

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