mirror of
https://github.com/PHPOffice/PhpSpreadsheet.git
synced 2026-08-25 13:18:22 +00:00
f575d2b8b2
With the deprecation of `auto_detect_line_endings` in Php8.1, there have been some tickets (issue #2609 and PR #2438). Although the deprecation message is suppressed, users with a homegrown error handler may still see it. I am not very concerned about that symptom, but I imagine that there will be more similar tickets in future. This PR adds a new property/method to Reader/CSV to allow the user to avoid the deprecated code, at the negligible cost of being unable to read a CSV with Mac line endings even on a Php version that could support it.
73 lines
2.4 KiB
PHP
73 lines
2.4 KiB
PHP
<?php
|
|
|
|
namespace PhpOffice\PhpSpreadsheetTests\Reader\Csv;
|
|
|
|
use PhpOffice\PhpSpreadsheet\Reader\Csv;
|
|
use PhpOffice\PhpSpreadsheet\Shared\File;
|
|
use PHPUnit\Framework\TestCase;
|
|
|
|
class CsvLineEndingTest extends TestCase
|
|
{
|
|
/** @var string */
|
|
private $tempFile = '';
|
|
|
|
protected function tearDown(): void
|
|
{
|
|
if ($this->tempFile !== '') {
|
|
unlink($this->tempFile);
|
|
$this->tempFile = '';
|
|
}
|
|
}
|
|
|
|
/**
|
|
* @dataProvider providerEndings
|
|
*/
|
|
public function testEndings(string $ending): void
|
|
{
|
|
$this->tempFile = $filename = File::temporaryFilename();
|
|
$data = ['123', '456', '789'];
|
|
file_put_contents($filename, implode($ending, $data));
|
|
$reader = new Csv();
|
|
$spreadsheet = $reader->load($filename);
|
|
$sheet = $spreadsheet->getActiveSheet();
|
|
self::assertEquals($data[0], $sheet->getCell('A1')->getValue());
|
|
self::assertEquals($data[1], $sheet->getCell('A2')->getValue());
|
|
self::assertEquals($data[2], $sheet->getCell('A3')->getValue());
|
|
$spreadsheet->disconnectWorksheets();
|
|
}
|
|
|
|
/**
|
|
* @dataProvider providerEndings
|
|
*/
|
|
public function testEndingsNoDetect(string $ending): void
|
|
{
|
|
$this->tempFile = $filename = File::temporaryFilename();
|
|
$data = ['123', '456', '789'];
|
|
file_put_contents($filename, implode($ending, $data));
|
|
$reader = new Csv();
|
|
$reader->setTestAutoDetect(false);
|
|
$spreadsheet = $reader->load($filename);
|
|
$sheet = $spreadsheet->getActiveSheet();
|
|
if ($ending === "\r") {
|
|
// Can't handle Mac line endings without autoDetect
|
|
self::assertEquals(implode("\n", $data), $sheet->getCell('A1')->getValue());
|
|
self::assertNull($sheet->getCell('A2')->getValue());
|
|
self::assertNull($sheet->getCell('A3')->getValue());
|
|
} else {
|
|
self::assertEquals($data[0], $sheet->getCell('A1')->getValue());
|
|
self::assertEquals($data[1], $sheet->getCell('A2')->getValue());
|
|
self::assertEquals($data[2], $sheet->getCell('A3')->getValue());
|
|
}
|
|
$spreadsheet->disconnectWorksheets();
|
|
}
|
|
|
|
public function providerEndings(): array
|
|
{
|
|
return [
|
|
'Unix endings' => ["\n"],
|
|
'Mac endings' => ["\r"],
|
|
'Windows endings' => ["\r\n"],
|
|
];
|
|
}
|
|
}
|