mirror of
https://github.com/PHPOffice/PhpSpreadsheet.git
synced 2026-08-24 22:08:19 +00:00
9bef9c90ce
This was suggested by the investigation of issue #3811. No fix is necessary for the issue. However, two possible code solutions (Php setlocale, which comes with certain design flaws, and StringHelper set(Decimal/Thousands)Separator were suggested, and neither is adequately tested. This PR adds such tests. Unusually, getting StringHelper Decimal Separator, Thousands Separator, and Currency Code can result in a change to those properties. So, the existing design in several tests where those properties are captured in Setup and restored in Teardown do not work quite as designed. Instead, the ability to set those properties to their default value (null) is added, and the tests re-done to restore the default in Teardown. The two methods yield the same results when parsing input. However, they diverge when examining output fields through `getFormattedValue`. Such output is currently correct (usually) when using setlocale, but not when using StringHelper. The former works through the 'trick' of using `sprintf(%f)`, which generates a locale-aware string. However, using non-locale-aware `sprintf(%F)` followed by `str_replace` will produce the correct result for both setlocale and StringHelper. One place in the code uses a cast to string, which is incorrect for both methods. Following that up with the same str_replace makes it correct for both. These changes permit, but do not require, the user to avoid setlocale altogether. It remains an open question whether Settings/Calculation::setLocale should set DecimalSeparator, CurrencySeparator, and CurrencyCode. That makes logical sense, but it would be a breaking change, and having to explicitly set those values when using setLocale does not seem especially burdensome. For now, such a change will not be made.
65 lines
1.9 KiB
PHP
65 lines
1.9 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace PhpOffice\PhpSpreadsheetTests\Calculation\Functions\TextData;
|
|
|
|
use PhpOffice\PhpSpreadsheet\Calculation\Calculation;
|
|
use PhpOffice\PhpSpreadsheet\Shared\StringHelper;
|
|
|
|
class ValueTest extends AllSetupTeardown
|
|
{
|
|
protected function tearDown(): void
|
|
{
|
|
parent::tearDown();
|
|
StringHelper::setCurrencyCode(null);
|
|
StringHelper::setDecimalSeparator(null);
|
|
StringHelper::setThousandsSeparator(null);
|
|
}
|
|
|
|
/**
|
|
* @dataProvider providerVALUE
|
|
*/
|
|
public function testVALUE(mixed $expectedResult, mixed $value = 'omitted'): void
|
|
{
|
|
StringHelper::setDecimalSeparator('.');
|
|
StringHelper::setThousandsSeparator(' ');
|
|
StringHelper::setCurrencyCode('$');
|
|
|
|
$this->mightHaveException($expectedResult);
|
|
$sheet = $this->getSheet();
|
|
if ($value === 'omitted') {
|
|
$sheet->getCell('B1')->setValue('=VALUE()');
|
|
} else {
|
|
$this->setCell('A1', $value);
|
|
$sheet->getCell('B1')->setValue('=VALUE(A1)');
|
|
}
|
|
$result = $sheet->getCell('B1')->getCalculatedValue();
|
|
self::assertEqualsWithDelta($expectedResult, $result, 1E-8);
|
|
}
|
|
|
|
public static function providerVALUE(): array
|
|
{
|
|
return require 'tests/data/Calculation/TextData/VALUE.php';
|
|
}
|
|
|
|
/**
|
|
* @dataProvider providerValueArray
|
|
*/
|
|
public function testValueArray(array $expectedResult, string $argument): void
|
|
{
|
|
$calculation = Calculation::getInstance();
|
|
|
|
$formula = "=VALUE({$argument})";
|
|
$result = $calculation->_calculateFormulaValue($formula);
|
|
self::assertEqualsWithDelta($expectedResult, $result, 1.0e-14);
|
|
}
|
|
|
|
public static function providerValueArray(): array
|
|
{
|
|
return [
|
|
'row vector' => [[[44604, -1234.567]], '{"12-Feb-2022", "$ -1,234.567"}'],
|
|
];
|
|
}
|
|
}
|