AbstractDivLayoutTest.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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 testRepeatedRow()
  33. {
  34. $form = $this->factory->createNamed('repeated', 'name');
  35. $form->addError(new FormError('Error!'));
  36. $view = $form->createView();
  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->createNamedBuilder('form', 'name')
  58. ->add('field1', 'text')
  59. ->add('field2', 'repeated')
  60. ->add('field3', 'text')
  61. ->add('field4', 'text')
  62. ->getForm()
  63. ->createView();
  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->createNamed('collection', 'name', array('a', 'b'), array(
  94. 'type' => 'text',
  95. ));
  96. $this->assertWidgetMatchesXpath($form->createView(), array(),
  97. '/div
  98. [
  99. ./div[./input[@type="text"][@value="a"]]
  100. /following-sibling::div[./input[@type="text"][@value="b"]]
  101. ]
  102. [count(./div[./input])=2]
  103. '
  104. );
  105. }
  106. public function testForm()
  107. {
  108. $form = $this->factory->createNamedBuilder('form', 'name')
  109. ->add('firstName', 'text')
  110. ->add('lastName', 'text')
  111. ->getForm();
  112. $this->assertWidgetMatchesXpath($form->createView(), array(),
  113. '/div
  114. [
  115. ./input[@type="hidden"][@id="name__token"]
  116. /following-sibling::div
  117. [
  118. ./label[@for="name_firstName"]
  119. /following-sibling::input[@type="text"][@id="name_firstName"]
  120. ]
  121. /following-sibling::div
  122. [
  123. ./label[@for="name_lastName"]
  124. /following-sibling::input[@type="text"][@id="name_lastName"]
  125. ]
  126. ]
  127. [count(.//input)=3]
  128. '
  129. );
  130. }
  131. public function testRepeated()
  132. {
  133. $form = $this->factory->createNamed('repeated', 'name', 'foobar', array(
  134. 'type' => 'text',
  135. ));
  136. $this->assertWidgetMatchesXpath($form->createView(), array(),
  137. '/div
  138. [
  139. ./div
  140. [
  141. ./label[@for="name_first"]
  142. /following-sibling::input[@type="text"][@id="name_first"]
  143. ]
  144. /following-sibling::div
  145. [
  146. ./label[@for="name_second"]
  147. /following-sibling::input[@type="text"][@id="name_second"]
  148. ]
  149. ]
  150. [count(.//input)=2]
  151. '
  152. );
  153. }
  154. }