FormUtilTest.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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. array(true, new \SplFixedArray(1)),
  57. );
  58. }
  59. /**
  60. * @dataProvider isChoiceGroupProvider
  61. */
  62. public function testIsChoiceGroup($expected, $value)
  63. {
  64. $this->assertSame($expected, FormUtil::isChoiceGroup($value));
  65. }
  66. public function isChoiceSelectedProvider()
  67. {
  68. return array(
  69. array(true, 0, 0),
  70. array(true, '0', 0),
  71. array(true, '1', 1),
  72. array(true, false, 0),
  73. array(true, true, 1),
  74. array(true, '', ''),
  75. array(true, null, ''),
  76. array(true, '1.23', '1.23'),
  77. array(true, 'foo', 'foo'),
  78. array(true, 'foo10', 'foo10'),
  79. array(true, 'foo', array(1, 'foo', 'foo10')),
  80. array(false, 10, array(1, 'foo', 'foo10')),
  81. array(false, 0, array(1, 'foo', 'foo10')),
  82. );
  83. }
  84. /**
  85. * @dataProvider isChoiceSelectedProvider
  86. */
  87. public function testIsChoiceSelected($expected, $choice, $value)
  88. {
  89. $this->assertSame($expected, FormUtil::isChoiceSelected($choice, $value));
  90. }
  91. }