Files
PhpSpreadsheet/tests/PhpSpreadsheetTests/Reader/Csv/CsvNumberFormatLocaleTest.php
T
oleibman d647fe7ee7 Use Php Attributes Rather than Annotations for PhpUnit
With PhpUnit 10 came the ability to use Php attributes rather than doc-block annotations for things like "data provider". PhpUnit 11 deprecates the use of annotations, and PhpUnit 12 will not not permit their use. Since PhpUnit 11 requires Php8.2+, we cannot adopt it as long as we support Php8.1, which will continue to be the case for some time. However, there is no penalty for early adoption.

Php-cs-fixer can use:
```
'php_unit_attributes' => ['keep_annotations' => false],
```
This allows us to run `composer fix` to automate all the needed changes. No manual changes were needed for any of the test members.

With this change, PhpUnit 9 can no longer be used with the test suite. File composer.json is updated to reflect that reality, and phpunit9.xml.dist, which has been supplied in case anyone needed to use PhpUnit 9, is no longer required, and is thus deleted. For now, PhpUnit 11 is not being added as a possibility.

No source code is changed in this PR.
2024-11-23 20:56:26 -08:00

139 lines
4.0 KiB
PHP

<?php
declare(strict_types=1);
namespace PhpOffice\PhpSpreadsheetTests\Reader\Csv;
use PhpOffice\PhpSpreadsheet\Cell\DataType;
use PhpOffice\PhpSpreadsheet\Reader\Csv;
use PHPUnit\Framework\TestCase;
class CsvNumberFormatLocaleTest extends TestCase
{
private bool $localeAdjusted;
/**
* @var false|string
*/
private $currentLocale;
protected string $filename;
protected Csv $csvReader;
protected function setUp(): void
{
$this->currentLocale = setlocale(LC_ALL, '0');
if (!setlocale(LC_ALL, 'de_DE.UTF-8', 'deu_deu.utf8')) {
$this->localeAdjusted = false;
return;
}
$this->localeAdjusted = true;
$this->filename = 'tests/data/Reader/CSV/NumberFormatTest.de.csv';
$this->csvReader = new Csv();
}
protected function tearDown(): void
{
if ($this->localeAdjusted && is_string($this->currentLocale)) {
setlocale(LC_ALL, $this->currentLocale);
}
}
#[\PHPUnit\Framework\Attributes\DataProvider('providerNumberFormatNoConversionTest')]
#[\PHPUnit\Framework\Attributes\RunInSeparateProcess]
public function testNumberFormatNoConversion(mixed $expectedValue, string $expectedFormat, string $cellAddress): void
{
if (!$this->localeAdjusted) {
self::markTestSkipped('Unable to set locale for testing.');
}
$localeconv = localeconv();
self::assertSame(',', $localeconv['decimal_point'], 'unexpected change to German decimal separator');
self::assertSame('.', $localeconv['thousands_sep'], 'unexpected change to German thousands separator');
$spreadsheet = $this->csvReader->load($this->filename);
$worksheet = $spreadsheet->getActiveSheet();
$cell = $worksheet->getCell($cellAddress);
self::assertSame($expectedValue, $cell->getValue(), 'Expected value check');
self::assertSame($expectedFormat, $cell->getFormattedValue(), 'Format mask check');
}
public static function providerNumberFormatNoConversionTest(): array
{
return [
[
-123,
'-123',
'A1',
],
[
'12.345,67',
'12.345,67',
'C1',
],
[
'-1.234,567',
'-1.234,567',
'A3',
],
];
}
#[\PHPUnit\Framework\Attributes\DataProvider('providerNumberValueConversionTest')]
#[\PHPUnit\Framework\Attributes\RunInSeparateProcess]
public function testNumberValueConversion(mixed $expectedValue, string $cellAddress): void
{
if (!$this->localeAdjusted) {
self::markTestSkipped('Unable to set locale for testing.');
}
$localeconv = localeconv();
self::assertSame(',', $localeconv['decimal_point'], 'unexpected change to German decimal separator');
self::assertSame('.', $localeconv['thousands_sep'], 'unexpected change to German thousands separator');
$this->csvReader->castFormattedNumberToNumeric(true);
$spreadsheet = $this->csvReader->load($this->filename);
$worksheet = $spreadsheet->getActiveSheet();
$cell = $worksheet->getCell($cellAddress);
self::assertSame(DataType::TYPE_NUMERIC, $cell->getDataType(), 'Datatype check');
self::assertSame($expectedValue, $cell->getValue(), 'Expected value check');
}
public static function providerNumberValueConversionTest(): array
{
return [
'A1' => [
-123,
'A1',
],
'B1' => [
1234,
'B1',
],
'C1' => [
12345.67,
'C1',
],
'A2' => [
123.4567,
'A2',
],
'B2' => [
123.456789012,
'B2',
],
'A3' => [
-1234.567,
'A3',
],
];
}
}