Files
oleibman 38a8330748 More Extendability for CSV Reader
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.
2026-03-27 16:16:45 -07:00

80 lines
2.6 KiB
PHP

<?php
declare(strict_types=1);
namespace PhpOffice\PhpSpreadsheetTests\Reader\Csv;
use PhpOffice\PhpSpreadsheet\Reader\Csv;
use PHPUnit\Framework\TestCase;
class NonUtf8Test extends TestCase
{
public function testUtf16LineBreak(): void
{
$reader = new Csv();
$reader->setInputEncoding('UTF-16BE');
$spreadsheet = $reader->load('tests/data/Reader/CSV/utf16be.line_break_in_enclosure.csv');
$sheet = $spreadsheet->getActiveSheet();
$expected = <<<EOF
This is a test
with line breaks
that breaks the
delimiters
EOF;
self::assertSame($expected, $sheet->getCell('B3')->getValue());
$spreadsheet->disconnectWorksheets();
}
// Same as above, but with extending Reader class.
public function testUtf16LineBreak2(): void
{
$reader = new CsvIconv2();
$reader->setInputEncoding('UTF-16BE');
$spreadsheet = $reader->load('tests/data/Reader/CSV/utf16be.line_break_in_enclosure.csv');
$sheet = $spreadsheet->getActiveSheet();
$expected = <<<EOF
This is a test
with line breaks
that breaks the
delimiters
EOF;
self::assertSame($expected, $sheet->getCell('B3')->getValue());
$spreadsheet->disconnectWorksheets();
}
public function testUtf32LineBreakEscape(): void
{
$reader = new Csv();
$reader->setInputEncoding('UTF-32LE');
$spreadsheet = $reader->load('tests/data/Reader/CSV/line_break_escaped_32le.csv');
$sheet = $spreadsheet->getActiveSheet();
$expected = <<<EOF
This is a "test csv file"
with both "line breaks"
and "escaped
quotes" that breaks
the delimiters
EOF;
self::assertSame($expected, $sheet->getCell('B3')->getValue());
$spreadsheet->disconnectWorksheets();
}
// Same as above, but with extending Reader class.
public function testUtf32LineBreakEscape2(): void
{
$reader = new CsvIconv2();
$reader->setInputEncoding('UTF-32LE');
$spreadsheet = $reader->load('tests/data/Reader/CSV/line_break_escaped_32le.csv');
$sheet = $spreadsheet->getActiveSheet();
$expected = <<<EOF
This is a "test csv file"
with both "line breaks"
and "escaped
quotes" that breaks
the delimiters
EOF;
self::assertSame($expected, $sheet->getCell('B3')->getValue());
$spreadsheet->disconnectWorksheets();
}
}