mirror of
https://github.com/PHPOffice/PhpSpreadsheet.git
synced 2026-09-20 15:16:36 +00:00
e9cf27354d
Successor to PR #3523. There are 494 single-line changes (`public function provider` to `public static function provider`) in this PR. None of these were made manually; they were all created with the following script (adapted from https://stackoverflow.com/questions/25909820/how-to-recursively-iterate-through-files-in-php): ```php $dir = 'C:/git/unit10prep2/tests/PhpSpreadsheetTests'; $it = new RecursiveDirectoryIterator($dir); // Loop through files foreach(new RecursiveIteratorIterator($it) as $file) { if ($file->getExtension() === 'php') { $contents = file_get_contents($file); $new = preg_replace('/public function (\\w*)([Pp])rovider/', 'public static function $1$2rovider', $contents); if ($new !== $contents) { echo "changing $file\n"; file_put_contents($file, $new); } } } ``` After this PR, there will be one more, with a small number of test changes, and enabling PhpUnit 10 for Php 8.1+.
174 lines
4.4 KiB
PHP
174 lines
4.4 KiB
PHP
<?php
|
|
|
|
namespace PhpOffice\PhpSpreadsheetTests\Reader\Csv;
|
|
|
|
use PhpOffice\PhpSpreadsheet\Cell\DataType;
|
|
use PhpOffice\PhpSpreadsheet\Reader\Csv;
|
|
use PHPUnit\Framework\TestCase;
|
|
|
|
class CsvNumberFormatTest extends TestCase
|
|
{
|
|
/**
|
|
* @var string
|
|
*/
|
|
protected $filename;
|
|
|
|
/**
|
|
* @var Csv
|
|
*/
|
|
protected $csvReader;
|
|
|
|
protected function setUp(): void
|
|
{
|
|
$this->filename = 'tests/data/Reader/CSV/NumberFormatTest.csv';
|
|
$this->csvReader = new Csv();
|
|
}
|
|
|
|
/**
|
|
* @dataProvider providerNumberFormatNoConversionTest
|
|
*
|
|
* @param mixed $expectedValue
|
|
*/
|
|
public function testNumberFormatNoConversion($expectedValue, string $expectedFormat, string $cellAddress): void
|
|
{
|
|
$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',
|
|
],
|
|
];
|
|
}
|
|
|
|
/**
|
|
* @dataProvider providerNumberValueConversionTest
|
|
*
|
|
* @param mixed $expectedValue
|
|
*/
|
|
public function testNumberValueConversion($expectedValue, string $cellAddress): void
|
|
{
|
|
$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',
|
|
],
|
|
'B3' => [
|
|
1234.567,
|
|
'B3',
|
|
],
|
|
];
|
|
}
|
|
|
|
/**
|
|
* @dataProvider providerNumberFormatConversionTest
|
|
*
|
|
* @param mixed $expectedValue
|
|
*/
|
|
public function testNumberFormatConversion($expectedValue, string $expectedFormat, string $cellAddress): void
|
|
{
|
|
$this->csvReader->castFormattedNumberToNumeric(true, 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');
|
|
self::assertSame($expectedFormat, $cell->getFormattedValue(), 'Format mask check');
|
|
}
|
|
|
|
public static function providerNumberFormatConversionTest(): array
|
|
{
|
|
return [
|
|
'A1' => [
|
|
-123,
|
|
'-123',
|
|
'A1',
|
|
],
|
|
'B1' => [
|
|
1234,
|
|
'1,234',
|
|
'B1',
|
|
],
|
|
'C1' => [
|
|
12345.67,
|
|
'12,345.67',
|
|
'C1',
|
|
],
|
|
'A2' => [
|
|
123.4567,
|
|
'123.4567',
|
|
'A2',
|
|
],
|
|
'B2' => [
|
|
123.456789012,
|
|
'123.456789',
|
|
'B2',
|
|
],
|
|
'A3' => [
|
|
-1234.567,
|
|
'-1,234.567',
|
|
'A3',
|
|
],
|
|
'B3' => [
|
|
1234.567,
|
|
'1234.567',
|
|
'B3',
|
|
],
|
|
];
|
|
}
|
|
}
|