mirror of
https://github.com/PHPOffice/PhpSpreadsheet.git
synced 2026-09-14 20:16:34 +00:00
38a8330748
A follow-up to PR #4837. PR #4827, to which I am not necessarily committed, shows that there could be a use case for letting a user perform customized logic to convert a non-UTF8-encoded CSV. In addition, several other CSV Reader properties require the use of static or locale properties, which is somewhat problematic; this PR allows them to be set as instance variables, falling back to static/locale only when unset.
147 lines
4.3 KiB
PHP
147 lines
4.3 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace PhpOffice\PhpSpreadsheetTests\Reader\Csv;
|
|
|
|
use PhpOffice\PhpSpreadsheet\Cell\DataType;
|
|
use PhpOffice\PhpSpreadsheet\Reader\Csv;
|
|
use PhpOffice\PhpSpreadsheet\Shared\StringHelper;
|
|
use PHPUnit\Framework\Attributes\DataProvider;
|
|
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();
|
|
StringHelper::setCurrencyCode(null);
|
|
StringHelper::setThousandsSeparator(null);
|
|
StringHelper::setDecimalSeparator(null);
|
|
}
|
|
|
|
protected function tearDown(): void
|
|
{
|
|
StringHelper::setCurrencyCode(null);
|
|
StringHelper::setThousandsSeparator(null);
|
|
StringHelper::setDecimalSeparator(null);
|
|
if ($this->localeAdjusted && is_string($this->currentLocale)) {
|
|
setlocale(LC_ALL, $this->currentLocale);
|
|
}
|
|
}
|
|
|
|
#[DataProvider('providerNumberFormatNoConversionTest')]
|
|
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');
|
|
$spreadsheet->disconnectWorksheets();
|
|
}
|
|
|
|
public static function providerNumberFormatNoConversionTest(): array
|
|
{
|
|
return [
|
|
[
|
|
-123,
|
|
'-123',
|
|
'A1',
|
|
],
|
|
[
|
|
'12.345,67',
|
|
'12.345,67',
|
|
'C1',
|
|
],
|
|
[
|
|
'-1.234,567',
|
|
'-1.234,567',
|
|
'A3',
|
|
],
|
|
];
|
|
}
|
|
|
|
#[DataProvider('providerNumberValueConversionTest')]
|
|
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');
|
|
$spreadsheet->disconnectWorksheets();
|
|
}
|
|
|
|
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',
|
|
],
|
|
];
|
|
}
|
|
}
|