FormUtilTest.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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\Util;
  11. use Symfony\Component\Form\Util\FormUtil;
  12. class FormUtilTest extends \PHPUnit_Framework_TestCase
  13. {
  14. public function toArrayKeyProvider()
  15. {
  16. return array(
  17. array(0, 0),
  18. array('0', 0),
  19. array('1', 1),
  20. array(false, 0),
  21. array(true, 1),
  22. array('', ''),
  23. array(null, ''),
  24. array('1.23', '1.23'),
  25. array('foo', 'foo'),
  26. array('foo10', 'foo10'),
  27. );
  28. }
  29. /**
  30. * @dataProvider toArrayKeyProvider
  31. */
  32. public function testToArrayKey($in, $out)
  33. {
  34. $this->assertSame($out, FormUtil::toArrayKey($in));
  35. }
  36. public function testToArrayKeys()
  37. {
  38. $in = $out = array();
  39. foreach ($this->toArrayKeyProvider() as $call) {
  40. $in[] = $call[0];
  41. $out[] = $call[1];
  42. }
  43. $this->assertSame($out, FormUtil::toArrayKeys($in));
  44. }
  45. public function isChoiceGroupProvider()
  46. {
  47. return array(
  48. array(false, 0),
  49. array(false, '0'),
  50. array(false, '1'),
  51. array(false, 1),
  52. array(false, ''),
  53. array(false, null),
  54. array(false, true),
  55. array(true, array()),
  56. );
  57. }
  58. /**
  59. * @dataProvider isChoiceGroupProvider
  60. */
  61. public function testIsChoiceGroup($expected, $value)
  62. {
  63. $this->assertSame($expected, FormUtil::isChoiceGroup($value));
  64. }
  65. public function testIsChoiceGroupPart2()
  66. {
  67. if (version_compare(PHP_VERSION, '5.3.2') <= 0) {
  68. $this->markTestSkipped('PHP prior to 5.3.3 has issue with SplFixedArrays - https://bugs.php.net/bug.php?id=50481');
  69. }
  70. $this->assertSame(true, FormUtil::isChoiceGroup(new \SplFixedArray(1)));
  71. }
  72. public function isChoiceSelectedProvider()
  73. {
  74. return array(
  75. array(true, 0, 0),
  76. array(true, '0', 0),
  77. array(true, '1', 1),
  78. array(true, false, 0),
  79. array(true, true, 1),
  80. array(true, '', ''),
  81. array(true, null, ''),
  82. array(true, '1.23', '1.23'),
  83. array(true, 'foo', 'foo'),
  84. array(true, 'foo10', 'foo10'),
  85. array(true, 'foo', array(1, 'foo', 'foo10')),
  86. array(false, 10, array(1, 'foo', 'foo10')),
  87. array(false, 0, array(1, 'foo', 'foo10')),
  88. );
  89. }
  90. /**
  91. * @dataProvider isChoiceSelectedProvider
  92. */
  93. public function testIsChoiceSelected($expected, $choice, $value)
  94. {
  95. $this->assertSame($expected, FormUtil::isChoiceSelected($choice, $value));
  96. }
  97. }