FormChoiceWidgetTest.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. <?php
  2. /*
  3. * This file is part of the Sonata Project package.
  4. *
  5. * (c) Thomas Rabaix <thomas.rabaix@sonata-project.org>
  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 Sonata\AdminBundle\Tests\Form\Widget;
  11. class FormChoiceWidgetTest extends BaseWidgetTest
  12. {
  13. protected $type = 'form';
  14. public function setUp()
  15. {
  16. parent::setUp();
  17. }
  18. public function testLabelRendering()
  19. {
  20. $choices = array('some', 'choices');
  21. if (!method_exists('Symfony\Component\Form\FormTypeInterface', 'setDefaultOptions')) {
  22. $choices = array_flip($choices);
  23. }
  24. $choice = $this->factory->create(
  25. $this->getChoiceClass(),
  26. null,
  27. $this->getDefaultOption() + array(
  28. 'multiple' => true,
  29. 'expanded' => true,
  30. ) + compact('choices')
  31. );
  32. $html = $this->renderWidget($choice->createView());
  33. $this->assertContains(
  34. '<li><label><input type="checkbox" id="choice_0" name="choice[]" value="0" />[trans]some[/trans]</label></li>',
  35. $this->cleanHtmlWhitespace($html)
  36. );
  37. }
  38. public function testBootstrapLabelRendering()
  39. {
  40. $sonataAdmin = $this->getSonataAdmin();
  41. $sonataAdmin['options']['use_icheck'] = false;
  42. $this->environment->addGlobal('sonata_admin', $sonataAdmin);
  43. $choices = array('some', 'choices');
  44. if (!method_exists('Symfony\Component\Form\FormTypeInterface', 'setDefaultOptions')) {
  45. $choices = array_flip($choices);
  46. }
  47. $choice = $this->factory->create(
  48. $this->getChoiceClass(),
  49. null,
  50. $this->getDefaultOption() + array(
  51. 'multiple' => true,
  52. 'expanded' => true,
  53. ) + compact('choices')
  54. );
  55. $html = $this->renderWidget($choice->createView());
  56. $this->assertContains(
  57. '<li><div class="checkbox"><label><input type="checkbox" id="choice_0" name="choice[]" value="0" />[trans]some[/trans]</label></div></li>',
  58. $this->cleanHtmlWhitespace($html)
  59. );
  60. }
  61. public function testDefaultValueRendering()
  62. {
  63. $choice = $this->factory->create(
  64. $this->getChoiceClass(),
  65. null,
  66. $this->getDefaultOption()
  67. );
  68. $html = $this->renderWidget($choice->createView());
  69. $this->assertContains(
  70. '<option value="" selected="selected">[trans]Choose an option[/trans]</option>',
  71. $this->cleanHtmlWhitespace($html)
  72. );
  73. }
  74. public function testRequiredIsDisabledForEmptyPlaceholder()
  75. {
  76. $choice = $this->factory->create(
  77. $this->getChoiceClass(),
  78. null,
  79. $this->getRequiredOption()
  80. );
  81. $html = $this->renderWidget($choice->createView());
  82. $this->assertNotContains(
  83. 'required="required"',
  84. $this->cleanHtmlWhitespace($html)
  85. );
  86. }
  87. public function testRequiredIsEnabledIfPlaceholderIsSet()
  88. {
  89. $choice = $this->factory->create(
  90. $this->getChoiceClass(),
  91. null,
  92. array_merge($this->getRequiredOption(), $this->getDefaultOption())
  93. );
  94. $html = $this->renderWidget($choice->createView());
  95. $this->assertContains(
  96. 'required="required"',
  97. $this->cleanHtmlWhitespace($html)
  98. );
  99. }
  100. protected function getRequiredOption()
  101. {
  102. return array('required' => true);
  103. }
  104. protected function getChoiceClass()
  105. {
  106. return
  107. method_exists('Symfony\Component\Form\FormTypeInterface', 'setDefaultOptions') ?
  108. 'choice' :
  109. 'Symfony\Component\Form\Extension\Core\Type\ChoiceType';
  110. }
  111. /**
  112. * For SF < 2.6, we use 'empty_data' to provide default empty value.
  113. * For SF >= 2.6, we must use 'placeholder' to achieve the same.
  114. */
  115. protected function getDefaultOption()
  116. {
  117. if (method_exists(
  118. 'Symfony\Component\Form\Tests\AbstractLayoutTest',
  119. 'testSingleChoiceNonRequiredWithPlaceholder'
  120. )) {
  121. return array(
  122. 'placeholder' => 'Choose an option',
  123. );
  124. } else {
  125. return array(
  126. 'empty_value' => 'Choose an option',
  127. );
  128. }
  129. }
  130. }