mirror of
https://github.com/PHPOffice/PhpSpreadsheet.git
synced 2026-09-11 18:46:49 +00:00
b8191244d5
PR #3513, developed by @SaidkhojaIftikhor, has been stuck for some time awaiting tests. This is the first of three PRs to replace that one. This PR also allows the use of slash as a thousands separator or decimal separator or currency symbol. These are, of course, very unusual situations; the main reason to support them is so that PhpSpreadsheet code will not crash when users set those options. New Calculation/Engine and AdvancedValueBinder tests confirm these. While making these changes, a few errors were found in AdvancedValueBinder and Calculation/Engine/FormattedNumber, e.g. currencies weren't parsed correctly when period was used as the group separator and comma as the decimal separator. Tests have been added for those situations.
105 lines
3.8 KiB
PHP
105 lines
3.8 KiB
PHP
<?php
|
|
|
|
namespace PhpOffice\PhpSpreadsheetTests\Calculation\Engine;
|
|
|
|
use PhpOffice\PhpSpreadsheet\Calculation\Engine\FormattedNumber;
|
|
use PhpOffice\PhpSpreadsheet\Shared\StringHelper;
|
|
use PHPUnit\Framework\TestCase;
|
|
|
|
class FormattedNumberSlashTest extends TestCase
|
|
{
|
|
private string $originalCurrencyCode;
|
|
|
|
private string $originalDecimalSeparator;
|
|
|
|
private string $originalThousandsSeparator;
|
|
|
|
protected function setUp(): void
|
|
{
|
|
$this->originalCurrencyCode = StringHelper::getCurrencyCode();
|
|
$this->originalDecimalSeparator = StringHelper::getDecimalSeparator();
|
|
$this->originalThousandsSeparator = StringHelper::getThousandsSeparator();
|
|
}
|
|
|
|
protected function tearDown(): void
|
|
{
|
|
StringHelper::setCurrencyCode($this->originalCurrencyCode);
|
|
StringHelper::setDecimalSeparator($this->originalDecimalSeparator);
|
|
StringHelper::setThousandsSeparator($this->originalThousandsSeparator);
|
|
}
|
|
|
|
/**
|
|
* @dataProvider providerNumbers
|
|
*
|
|
* @param mixed $expected
|
|
*/
|
|
public function testNumber($expected, string $value, string $thousandsSeparator = ',', string $decimalSeparator = '.'): void
|
|
{
|
|
StringHelper::setThousandsSeparator($thousandsSeparator);
|
|
StringHelper::setDecimalSeparator($decimalSeparator);
|
|
$result = FormattedNumber::convertToNumberIfFormatted($value);
|
|
self::assertTrue($result);
|
|
self::assertSame($expected, $value);
|
|
}
|
|
|
|
public static function providerNumbers(): array
|
|
{
|
|
return [
|
|
'normal' => [1234.5, '1,234.5'],
|
|
'slash as thousands separator' => [-1234.5, '- 1/234.5', '/', '.'],
|
|
'slash as decimal separator' => [-1234.5, '- 1,234/5', ',', '/'],
|
|
];
|
|
}
|
|
|
|
/**
|
|
* @dataProvider providerPercentages
|
|
*/
|
|
public function testPercentage(string $expected, string $value, string $thousandsSeparator = ',', string $decimalSeparator = '.'): void
|
|
{
|
|
$originalValue = $value;
|
|
StringHelper::setThousandsSeparator($thousandsSeparator);
|
|
StringHelper::setDecimalSeparator($decimalSeparator);
|
|
$result = FormattedNumber::convertToNumberIfPercent($value);
|
|
self::assertTrue($result);
|
|
self::assertSame($expected, (string) $value);
|
|
self::assertNotEquals($value, $originalValue);
|
|
}
|
|
|
|
public static function providerPercentages(): array
|
|
{
|
|
return [
|
|
'normal' => ['21.5034', '2,150.34%'],
|
|
'slash as thousands separator' => ['21.5034', '2/150.34%', '/', '.'],
|
|
'slash as decimal separator' => ['21.5034', '2,150/34%', ',', '/'],
|
|
];
|
|
}
|
|
|
|
/**
|
|
* @dataProvider providerCurrencies
|
|
*/
|
|
public function testCurrencies(string $expected, string $value, string $thousandsSeparator = ',', string $decimalSeparator = '.', ?string $currencyCode = null): void
|
|
{
|
|
$originalValue = $value;
|
|
StringHelper::setThousandsSeparator($thousandsSeparator);
|
|
StringHelper::setDecimalSeparator($decimalSeparator);
|
|
if ($currencyCode !== null) {
|
|
StringHelper::setCurrencyCode($currencyCode);
|
|
}
|
|
$result = FormattedNumber::convertToNumberIfCurrency($value);
|
|
self::assertTrue($result);
|
|
self::assertSame($expected, (string) $value);
|
|
self::assertNotEquals($value, $originalValue);
|
|
}
|
|
|
|
public static function providerCurrencies(): array
|
|
{
|
|
return [
|
|
'switched delimiters' => ['2134.56', '$2.134,56', '.', ','],
|
|
'normal' => ['2134.56', '$2,134.56'],
|
|
'slash as thousands separator' => ['2134.56', '$2/134.56', '/', '.'],
|
|
'slash as decimal separator' => ['2134.56', '$2,134/56', ',', '/'],
|
|
'slash as currency code' => ['2134.56', '/2,134.56', ',', '.', '/'],
|
|
];
|
|
}
|
|
}
|