2012-06-18 20:35:21 +00:00
|
|
|
<?php
|
|
|
|
|
2016-08-31 17:18:12 +00:00
|
|
|
namespace PhpOffice\PhpSpreadsheetTests\Shared;
|
2012-06-18 20:35:21 +00:00
|
|
|
|
2016-08-31 17:18:12 +00:00
|
|
|
use PhpOffice\PhpSpreadsheet\Shared\StringHelper;
|
2017-11-08 15:48:01 +00:00
|
|
|
use PHPUnit\Framework\TestCase;
|
2016-08-14 04:08:43 +00:00
|
|
|
|
2017-11-08 15:48:01 +00:00
|
|
|
class StringHelperTest extends TestCase
|
2012-06-18 20:35:21 +00:00
|
|
|
{
|
Restoring State After Static Changes in Tests (#1571)
This request does not change any source code, only tests.
For a change on which I was working, a test passed when run on its own,
but failed when run as part of the full test suite. It turned out that
an existing test had changed a static value,
thousands separator in this case, and failed to restore it.
The test turned out to be AdvancedBinderTest.
The search for the offending test was more difficult than it should have
been because 26 test scripts which had nothing to do with thousands
separator nevertheless changed that value. They all changed
decimal separator, currency code, and compatibility mode as well,
again for no reason. I changed all of those to eliminate those operations.
I changed the following tests, which actually do change the static
properties identified above for a reason, to restore them as part of teardown.
- CalculationTest sets compatibilityMode and locale
- DayTest sets compatibilityMode, returnDateType, and excelCalendar
- CountTest sets compatibilityMode
- FunctionsTest sets compatibilityMode and returnDateType
- AdvancedValueBinderTest sets currencyCode, decimalSeparator, thousandsSeparator
- StringHelperTest sets currencyCode, decimalSeparator, thousandsSeparator
- NumberFormatTest sets currencyCode, decimalSeparator, thousandsSeparator
- HtmlNumberFormatTest sets currencyCode, decimalSeparator, thousandsSeparator
2020-07-15 11:23:00 +00:00
|
|
|
private $currencyCode;
|
|
|
|
|
|
|
|
private $decimalSeparator;
|
|
|
|
|
|
|
|
private $thousandsSeparator;
|
|
|
|
|
2020-04-27 10:28:36 +00:00
|
|
|
protected function setUp(): void
|
2016-05-18 11:37:00 +00:00
|
|
|
{
|
|
|
|
parent::setUp();
|
Restoring State After Static Changes in Tests (#1571)
This request does not change any source code, only tests.
For a change on which I was working, a test passed when run on its own,
but failed when run as part of the full test suite. It turned out that
an existing test had changed a static value,
thousands separator in this case, and failed to restore it.
The test turned out to be AdvancedBinderTest.
The search for the offending test was more difficult than it should have
been because 26 test scripts which had nothing to do with thousands
separator nevertheless changed that value. They all changed
decimal separator, currency code, and compatibility mode as well,
again for no reason. I changed all of those to eliminate those operations.
I changed the following tests, which actually do change the static
properties identified above for a reason, to restore them as part of teardown.
- CalculationTest sets compatibilityMode and locale
- DayTest sets compatibilityMode, returnDateType, and excelCalendar
- CountTest sets compatibilityMode
- FunctionsTest sets compatibilityMode and returnDateType
- AdvancedValueBinderTest sets currencyCode, decimalSeparator, thousandsSeparator
- StringHelperTest sets currencyCode, decimalSeparator, thousandsSeparator
- NumberFormatTest sets currencyCode, decimalSeparator, thousandsSeparator
- HtmlNumberFormatTest sets currencyCode, decimalSeparator, thousandsSeparator
2020-07-15 11:23:00 +00:00
|
|
|
$this->currencyCode = StringHelper::getCurrencyCode();
|
|
|
|
$this->decimalSeparator = StringHelper::getDecimalSeparator();
|
|
|
|
$this->thousandsSeparator = StringHelper::getThousandsSeparator();
|
2016-05-18 11:37:00 +00:00
|
|
|
|
|
|
|
// Reset Currency Code
|
2016-08-26 06:39:29 +00:00
|
|
|
StringHelper::setCurrencyCode(null);
|
2016-05-18 11:37:00 +00:00
|
|
|
}
|
|
|
|
|
Restoring State After Static Changes in Tests (#1571)
This request does not change any source code, only tests.
For a change on which I was working, a test passed when run on its own,
but failed when run as part of the full test suite. It turned out that
an existing test had changed a static value,
thousands separator in this case, and failed to restore it.
The test turned out to be AdvancedBinderTest.
The search for the offending test was more difficult than it should have
been because 26 test scripts which had nothing to do with thousands
separator nevertheless changed that value. They all changed
decimal separator, currency code, and compatibility mode as well,
again for no reason. I changed all of those to eliminate those operations.
I changed the following tests, which actually do change the static
properties identified above for a reason, to restore them as part of teardown.
- CalculationTest sets compatibilityMode and locale
- DayTest sets compatibilityMode, returnDateType, and excelCalendar
- CountTest sets compatibilityMode
- FunctionsTest sets compatibilityMode and returnDateType
- AdvancedValueBinderTest sets currencyCode, decimalSeparator, thousandsSeparator
- StringHelperTest sets currencyCode, decimalSeparator, thousandsSeparator
- NumberFormatTest sets currencyCode, decimalSeparator, thousandsSeparator
- HtmlNumberFormatTest sets currencyCode, decimalSeparator, thousandsSeparator
2020-07-15 11:23:00 +00:00
|
|
|
protected function tearDown(): void
|
|
|
|
{
|
|
|
|
StringHelper::setCurrencyCode($this->currencyCode);
|
|
|
|
StringHelper::setDecimalSeparator($this->decimalSeparator);
|
|
|
|
StringHelper::setThousandsSeparator($this->thousandsSeparator);
|
|
|
|
}
|
|
|
|
|
2020-05-18 04:49:57 +00:00
|
|
|
public function testGetIsIconvEnabled(): void
|
2015-05-17 13:00:02 +00:00
|
|
|
{
|
2016-08-26 06:39:29 +00:00
|
|
|
$result = StringHelper::getIsIconvEnabled();
|
2017-09-20 05:55:42 +00:00
|
|
|
self::assertTrue($result);
|
2015-05-17 13:00:02 +00:00
|
|
|
}
|
|
|
|
|
2020-05-18 04:49:57 +00:00
|
|
|
public function testGetDecimalSeparator(): void
|
2015-05-17 13:00:02 +00:00
|
|
|
{
|
|
|
|
$localeconv = localeconv();
|
|
|
|
|
|
|
|
$expectedResult = (!empty($localeconv['decimal_point'])) ? $localeconv['decimal_point'] : ',';
|
2016-08-26 06:39:29 +00:00
|
|
|
$result = StringHelper::getDecimalSeparator();
|
2017-09-20 05:55:42 +00:00
|
|
|
self::assertEquals($expectedResult, $result);
|
2015-05-17 13:00:02 +00:00
|
|
|
}
|
|
|
|
|
2020-05-18 04:49:57 +00:00
|
|
|
public function testSetDecimalSeparator(): void
|
2015-05-17 13:00:02 +00:00
|
|
|
{
|
|
|
|
$expectedResult = ',';
|
2016-08-26 06:39:29 +00:00
|
|
|
StringHelper::setDecimalSeparator($expectedResult);
|
2015-05-17 13:00:02 +00:00
|
|
|
|
2016-08-26 06:39:29 +00:00
|
|
|
$result = StringHelper::getDecimalSeparator();
|
2017-09-20 05:55:42 +00:00
|
|
|
self::assertEquals($expectedResult, $result);
|
2015-05-17 13:00:02 +00:00
|
|
|
}
|
|
|
|
|
2020-05-18 04:49:57 +00:00
|
|
|
public function testGetThousandsSeparator(): void
|
2015-05-17 13:00:02 +00:00
|
|
|
{
|
|
|
|
$localeconv = localeconv();
|
|
|
|
|
|
|
|
$expectedResult = (!empty($localeconv['thousands_sep'])) ? $localeconv['thousands_sep'] : ',';
|
2016-08-26 06:39:29 +00:00
|
|
|
$result = StringHelper::getThousandsSeparator();
|
2017-09-20 05:55:42 +00:00
|
|
|
self::assertEquals($expectedResult, $result);
|
2015-05-17 13:00:02 +00:00
|
|
|
}
|
|
|
|
|
2020-05-18 04:49:57 +00:00
|
|
|
public function testSetThousandsSeparator(): void
|
2015-05-17 13:00:02 +00:00
|
|
|
{
|
|
|
|
$expectedResult = ' ';
|
2016-08-26 06:39:29 +00:00
|
|
|
StringHelper::setThousandsSeparator($expectedResult);
|
2015-05-17 13:00:02 +00:00
|
|
|
|
2016-08-26 06:39:29 +00:00
|
|
|
$result = StringHelper::getThousandsSeparator();
|
2017-09-20 05:55:42 +00:00
|
|
|
self::assertEquals($expectedResult, $result);
|
2015-05-17 13:00:02 +00:00
|
|
|
}
|
|
|
|
|
2020-05-18 04:49:57 +00:00
|
|
|
public function testGetCurrencyCode(): void
|
2015-05-17 13:00:02 +00:00
|
|
|
{
|
|
|
|
$localeconv = localeconv();
|
2016-12-22 14:43:37 +00:00
|
|
|
$expectedResult = (!empty($localeconv['currency_symbol']) ? $localeconv['currency_symbol'] : (!empty($localeconv['int_curr_symbol']) ? $localeconv['int_curr_symbol'] : '$'));
|
2016-08-26 06:39:29 +00:00
|
|
|
$result = StringHelper::getCurrencyCode();
|
2017-09-20 05:55:42 +00:00
|
|
|
self::assertEquals($expectedResult, $result);
|
2015-05-17 13:00:02 +00:00
|
|
|
}
|
|
|
|
|
2020-05-18 04:49:57 +00:00
|
|
|
public function testSetCurrencyCode(): void
|
2015-05-17 13:00:02 +00:00
|
|
|
{
|
|
|
|
$expectedResult = '£';
|
2016-08-26 06:39:29 +00:00
|
|
|
StringHelper::setCurrencyCode($expectedResult);
|
2015-05-17 13:00:02 +00:00
|
|
|
|
2016-08-26 06:39:29 +00:00
|
|
|
$result = StringHelper::getCurrencyCode();
|
2017-09-20 05:55:42 +00:00
|
|
|
self::assertEquals($expectedResult, $result);
|
2015-05-17 13:00:02 +00:00
|
|
|
}
|
2017-09-09 10:29:08 +00:00
|
|
|
|
2020-05-18 04:49:57 +00:00
|
|
|
public function testControlCharacterPHP2OOXML(): void
|
2017-09-09 10:29:08 +00:00
|
|
|
{
|
|
|
|
$expectedResult = 'foo_x000B_bar';
|
|
|
|
$result = StringHelper::controlCharacterPHP2OOXML('foo' . chr(11) . 'bar');
|
|
|
|
|
2017-09-20 05:55:42 +00:00
|
|
|
self::assertEquals($expectedResult, $result);
|
2017-09-09 10:29:08 +00:00
|
|
|
}
|
|
|
|
|
2020-05-18 04:49:57 +00:00
|
|
|
public function testControlCharacterOOXML2PHP(): void
|
2017-09-09 10:29:08 +00:00
|
|
|
{
|
|
|
|
$expectedResult = 'foo' . chr(11) . 'bar';
|
|
|
|
$result = StringHelper::controlCharacterOOXML2PHP('foo_x000B_bar');
|
|
|
|
|
2017-09-20 05:55:42 +00:00
|
|
|
self::assertEquals($expectedResult, $result);
|
2017-09-09 10:29:08 +00:00
|
|
|
}
|
|
|
|
|
2020-05-18 04:49:57 +00:00
|
|
|
public function testSYLKtoUTF8(): void
|
2017-09-09 10:29:08 +00:00
|
|
|
{
|
|
|
|
$expectedResult = 'foo' . chr(11) . 'bar';
|
|
|
|
$result = StringHelper::SYLKtoUTF8("foo\x1B ;bar");
|
|
|
|
|
2017-09-20 05:55:42 +00:00
|
|
|
self::assertEquals($expectedResult, $result);
|
2017-09-09 10:29:08 +00:00
|
|
|
}
|
2012-06-18 20:35:21 +00:00
|
|
|
}
|