StringUtilTest.php 2.8 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\Component\Form\Tests\Util;
  11. use PHPUnit\Framework\TestCase;
  12. use Symfony\Component\Form\Util\StringUtil;
  13. class StringUtilTest extends TestCase
  14. {
  15. public function testTrim()
  16. {
  17. $data = ' Foo! ';
  18. $this->assertEquals('Foo!', StringUtil::trim($data));
  19. }
  20. /**
  21. * @dataProvider spaceProvider
  22. */
  23. public function testTrimUtf8Separators($hex)
  24. {
  25. // Convert hexadecimal representation into binary
  26. // H: hex string, high nibble first (UCS-2BE)
  27. // *: repeat until end of string
  28. $binary = pack('H*', $hex);
  29. // Convert UCS-2BE to UTF-8
  30. $symbol = mb_convert_encoding($binary, 'UTF-8', 'UCS-2BE');
  31. $symbol .= "ab\ncd".$symbol;
  32. $this->assertSame("ab\ncd", StringUtil::trim($symbol));
  33. }
  34. public function spaceProvider()
  35. {
  36. return array(
  37. // separators
  38. array('0020'),
  39. array('00A0'),
  40. array('1680'),
  41. // array('180E'),
  42. array('2000'),
  43. array('2001'),
  44. array('2002'),
  45. array('2003'),
  46. array('2004'),
  47. array('2005'),
  48. array('2006'),
  49. array('2007'),
  50. array('2008'),
  51. array('2009'),
  52. array('200A'),
  53. array('2028'),
  54. array('2029'),
  55. array('202F'),
  56. array('205F'),
  57. array('3000'),
  58. // controls
  59. array('0009'),
  60. array('000A'),
  61. array('000B'),
  62. array('000C'),
  63. array('000D'),
  64. array('0085'),
  65. // zero width space
  66. // array('200B'),
  67. );
  68. }
  69. /**
  70. * @dataProvider fqcnToBlockPrefixProvider
  71. */
  72. public function testFqcnToBlockPrefix($fqcn, $expectedBlockPrefix)
  73. {
  74. $blockPrefix = StringUtil::fqcnToBlockPrefix($fqcn);
  75. $this->assertSame($expectedBlockPrefix, $blockPrefix);
  76. }
  77. public function fqcnToBlockPrefixProvider()
  78. {
  79. return array(
  80. array('TYPE', 'type'),
  81. array('\Type', 'type'),
  82. array('\UserType', 'user'),
  83. array('UserType', 'user'),
  84. array('Vendor\Name\Space\Type', 'type'),
  85. array('Vendor\Name\Space\UserForm', 'user_form'),
  86. array('Vendor\Name\Space\UserType', 'user'),
  87. array('Vendor\Name\Space\usertype', 'user'),
  88. array('Symfony\Component\Form\Form', 'form'),
  89. array('Vendor\Name\Space\BarTypeBazType', 'bar_type_baz'),
  90. array('FooBarBazType', 'foo_bar_baz'),
  91. );
  92. }
  93. }