AbstractDivLayoutTest.php 4.2 KB

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