StringHelperTest.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. <?php
  2. namespace PhpOffice\PhpSpreadsheetTests\Shared;
  3. use PhpOffice\PhpSpreadsheet\Shared\StringHelper;
  4. use PHPUnit\Framework\TestCase;
  5. class StringHelperTest extends TestCase
  6. {
  7. /**
  8. * @var string
  9. */
  10. private $currencyCode;
  11. /**
  12. * @var string
  13. */
  14. private $decimalSeparator;
  15. /**
  16. * @var string
  17. */
  18. private $thousandsSeparator;
  19. protected function setUp(): void
  20. {
  21. parent::setUp();
  22. $this->currencyCode = StringHelper::getCurrencyCode();
  23. $this->decimalSeparator = StringHelper::getDecimalSeparator();
  24. $this->thousandsSeparator = StringHelper::getThousandsSeparator();
  25. }
  26. protected function tearDown(): void
  27. {
  28. StringHelper::setCurrencyCode($this->currencyCode);
  29. StringHelper::setDecimalSeparator($this->decimalSeparator);
  30. StringHelper::setThousandsSeparator($this->thousandsSeparator);
  31. }
  32. public function testGetIsIconvEnabled(): void
  33. {
  34. $result = StringHelper::getIsIconvEnabled();
  35. self::assertTrue($result);
  36. }
  37. public function testGetDecimalSeparator(): void
  38. {
  39. $localeconv = localeconv();
  40. $expectedResult = (!empty($localeconv['decimal_point'])) ? $localeconv['decimal_point'] : ',';
  41. $result = StringHelper::getDecimalSeparator();
  42. self::assertEquals($expectedResult, $result);
  43. }
  44. public function testSetDecimalSeparator(): void
  45. {
  46. $expectedResult = ',';
  47. StringHelper::setDecimalSeparator($expectedResult);
  48. $result = StringHelper::getDecimalSeparator();
  49. self::assertEquals($expectedResult, $result);
  50. }
  51. public function testGetThousandsSeparator(): void
  52. {
  53. $localeconv = localeconv();
  54. $expectedResult = (!empty($localeconv['thousands_sep'])) ? $localeconv['thousands_sep'] : ',';
  55. $result = StringHelper::getThousandsSeparator();
  56. self::assertEquals($expectedResult, $result);
  57. }
  58. public function testSetThousandsSeparator(): void
  59. {
  60. $expectedResult = ' ';
  61. StringHelper::setThousandsSeparator($expectedResult);
  62. $result = StringHelper::getThousandsSeparator();
  63. self::assertEquals($expectedResult, $result);
  64. }
  65. public function testGetCurrencyCode(): void
  66. {
  67. $localeconv = localeconv();
  68. $expectedResult = (!empty($localeconv['currency_symbol']) ? $localeconv['currency_symbol'] : (!empty($localeconv['int_curr_symbol']) ? $localeconv['int_curr_symbol'] : '$'));
  69. $result = StringHelper::getCurrencyCode();
  70. self::assertEquals($expectedResult, $result);
  71. }
  72. public function testSetCurrencyCode(): void
  73. {
  74. $expectedResult = '£';
  75. StringHelper::setCurrencyCode($expectedResult);
  76. $result = StringHelper::getCurrencyCode();
  77. self::assertEquals($expectedResult, $result);
  78. }
  79. public function testControlCharacterPHP2OOXML(): void
  80. {
  81. $expectedResult = 'foo_x000B_bar';
  82. $result = StringHelper::controlCharacterPHP2OOXML('foo' . chr(11) . 'bar');
  83. self::assertEquals($expectedResult, $result);
  84. }
  85. public function testControlCharacterOOXML2PHP(): void
  86. {
  87. $expectedResult = 'foo' . chr(11) . 'bar';
  88. $result = StringHelper::controlCharacterOOXML2PHP('foo_x000B_bar');
  89. self::assertEquals($expectedResult, $result);
  90. }
  91. public function testSYLKtoUTF8(): void
  92. {
  93. $expectedResult = 'foo' . chr(11) . 'bar';
  94. $result = StringHelper::SYLKtoUTF8("foo\x1B ;bar");
  95. self::assertEquals($expectedResult, $result);
  96. }
  97. /**
  98. * @dataProvider providerFractions
  99. */
  100. public function testFraction(string $expected, string $value): void
  101. {
  102. $originalValue = $value;
  103. $result = StringHelper::convertToNumberIfFraction($value);
  104. if ($result === false) {
  105. self::assertSame($expected, $originalValue);
  106. self::assertSame($expected, $value);
  107. } else {
  108. self::assertSame($expected, (string) $value);
  109. self::assertNotEquals($value, $originalValue);
  110. }
  111. }
  112. public function providerFractions(): array
  113. {
  114. return [
  115. 'non-fraction' => ['1', '1'],
  116. 'common fraction' => ['1.5', '1 1/2'],
  117. 'fraction between -1 and 0' => ['-0.5', '-1/2'],
  118. 'fraction between -1 and 0 with space' => ['-0.5', ' - 1/2'],
  119. 'fraction between 0 and 1' => ['0.75', '3/4 '],
  120. 'fraction between 0 and 1 with space' => ['0.75', ' 3/4'],
  121. 'improper fraction' => ['1.75', '7/4'],
  122. ];
  123. }
  124. }