TextTest.php 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. <?php
  2. /**
  3. * This file is part of PHPOffice Common
  4. *
  5. * PHPOffice Common is free software distributed under the terms of the GNU Lesser
  6. * General Public License version 3 as published by the Free Software Foundation.
  7. *
  8. * For the full copyright and license information, please read the LICENSE
  9. * file that was distributed with this source code. For the full list of
  10. * contributors, visit https://github.com/PHPOffice/Common/contributors.
  11. *
  12. * @link https://github.com/PHPOffice/Common
  13. * @copyright 2009-2016 PHPOffice Common contributors
  14. * @license http://www.gnu.org/licenses/lgpl.txt LGPL version 3
  15. */
  16. namespace PhpOffice\Common\Tests;
  17. use PhpOffice\Common\Text;
  18. /**
  19. * Test class for Text
  20. *
  21. * @coversDefaultClass PhpOffice\Common\Text
  22. */
  23. class TextTest extends \PHPUnit\Framework\TestCase
  24. {
  25. /**
  26. */
  27. public function testControlCharacters()
  28. {
  29. $this->assertEquals('', Text::controlCharacterPHP2OOXML());
  30. $this->assertEquals('aeiou', Text::controlCharacterPHP2OOXML('aeiou'));
  31. $this->assertEquals('àéîöù', Text::controlCharacterPHP2OOXML('àéîöù'));
  32. $value = rand(0, 8);
  33. $this->assertEquals('_x'.sprintf('%04s', strtoupper(dechex($value))).'_', Text::controlCharacterPHP2OOXML(chr($value)));
  34. $this->assertEquals('', Text::controlCharacterOOXML2PHP(''));
  35. $this->assertEquals(chr(0x08), Text::controlCharacterOOXML2PHP('_x0008_'));
  36. }
  37. public function testNumberFormat()
  38. {
  39. $this->assertEquals('2.1', Text::numberFormat('2.06', 1));
  40. $this->assertEquals('2.1', Text::numberFormat('2.12', 1));
  41. $this->assertEquals('1234.0', Text::numberFormat(1234, 1));
  42. }
  43. public function testChr()
  44. {
  45. $this->assertEquals('A', Text::chr(65));
  46. $this->assertEquals('A', Text::chr(0x41));
  47. $this->assertEquals('é', Text::chr(233));
  48. $this->assertEquals('é', Text::chr(0xE9));
  49. $this->assertEquals('⼳', Text::chr(12083));
  50. $this->assertEquals('⼳', Text::chr(0x2F33));
  51. $this->assertEquals('🌃', Text::chr(127747));
  52. $this->assertEquals('🌃', Text::chr(0x1F303));
  53. $this->assertEquals('', Text::chr(2097152));
  54. }
  55. /**
  56. * Is UTF8
  57. */
  58. public function testIsUTF8()
  59. {
  60. $this->assertTrue(Text::isUTF8(''));
  61. $this->assertTrue(Text::isUTF8('éééé'));
  62. $this->assertFalse(Text::isUTF8(utf8_decode('éééé')));
  63. }
  64. /**
  65. * Test unicode conversion
  66. */
  67. public function testToUnicode()
  68. {
  69. $this->assertEquals('a', Text::toUnicode('a'));
  70. $this->assertEquals('\uc0{\u8364}', Text::toUnicode('€'));
  71. $this->assertEquals('\uc0{\u233}', Text::toUnicode('é'));
  72. }
  73. /**
  74. * Test remove underscore prefix
  75. */
  76. public function testRemoveUnderscorePrefix()
  77. {
  78. $this->assertEquals('item', Text::removeUnderscorePrefix('_item'));
  79. }
  80. }