Files
oleibman 645d9fea64 Phpstan Level 10 Prep Penultimate
Everything except Statistical.
2025-05-18 18:04:25 -07:00

87 lines
3.3 KiB
PHP

<?php
declare(strict_types=1);
namespace PhpOffice\PhpSpreadsheetTests\Calculation\Engine;
use PhpOffice\PhpSpreadsheet\Calculation\Engine\FormattedNumber;
use PhpOffice\PhpSpreadsheet\Shared\StringHelper;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\TestCase;
class FormattedNumberSlashTest extends TestCase
{
protected function tearDown(): void
{
StringHelper::setCurrencyCode(null);
StringHelper::setDecimalSeparator(null);
StringHelper::setThousandsSeparator(null);
}
#[DataProvider('providerNumbers')]
public function testNumber(mixed $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', ',', '.', '/'],
];
}
}