FormChoiceWidgetTest.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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><div class="checkbox"><label><input type="checkbox" id="choice_0" name="choice[]" value="0" /><span class="control-label__text">[trans]some[/trans]</span></label></div></li>',
  35. $this->cleanHtmlWhitespace($html)
  36. );
  37. }
  38. public function testDefaultValueRendering()
  39. {
  40. $choice = $this->factory->create(
  41. $this->getChoiceClass(),
  42. null,
  43. $this->getDefaultOption()
  44. );
  45. $html = $this->renderWidget($choice->createView());
  46. $this->assertContains(
  47. '<option value="" selected="selected">[trans]Choose an option[/trans]</option>',
  48. $this->cleanHtmlWhitespace($html)
  49. );
  50. }
  51. public function testRequiredIsDisabledForEmptyPlaceholder()
  52. {
  53. $choice = $this->factory->create(
  54. $this->getChoiceClass(),
  55. null,
  56. $this->getRequiredOption()
  57. );
  58. $html = $this->renderWidget($choice->createView());
  59. $this->assertNotContains(
  60. 'required="required"',
  61. $this->cleanHtmlWhitespace($html)
  62. );
  63. }
  64. public function testRequiredIsEnabledIfPlaceholderIsSet()
  65. {
  66. $choice = $this->factory->create(
  67. $this->getChoiceClass(),
  68. null,
  69. array_merge($this->getRequiredOption(), $this->getDefaultOption())
  70. );
  71. $html = $this->renderWidget($choice->createView());
  72. $this->assertContains(
  73. 'required="required"',
  74. $this->cleanHtmlWhitespace($html)
  75. );
  76. }
  77. protected function getRequiredOption()
  78. {
  79. return array('required' => true);
  80. }
  81. protected function getChoiceClass()
  82. {
  83. return
  84. method_exists('Symfony\Component\Form\AbstractType', 'getBlockPrefix') ?
  85. 'Symfony\Component\Form\Extension\Core\Type\ChoiceType' :
  86. 'choice';
  87. }
  88. /**
  89. * For SF < 2.6, we use 'empty_data' to provide default empty value.
  90. * For SF >= 2.6, we must use 'placeholder' to achieve the same.
  91. */
  92. protected function getDefaultOption()
  93. {
  94. if (method_exists(
  95. 'Symfony\Component\Form\Tests\AbstractLayoutTest',
  96. 'testSingleChoiceNonRequiredWithPlaceholder'
  97. )) {
  98. return array(
  99. 'placeholder' => 'Choose an option',
  100. );
  101. }
  102. return array(
  103. 'empty_value' => 'Choose an option',
  104. );
  105. }
  106. }