PhpSpreadsheet/tests/PhpSpreadsheetTests/Reader/CsvTest.php

279 lines
8.8 KiB
PHP
Raw Normal View History

<?php
namespace PhpOffice\PhpSpreadsheetTests\Reader;
use PhpOffice\PhpSpreadsheet\Reader\Csv;
use PhpOffice\PhpSpreadsheet\Reader\Exception as ReaderException;
use PHPUnit\Framework\TestCase;
class CsvTest extends TestCase
{
/**
* @dataProvider providerDelimiterDetection
*
* @param string $filename
* @param string $expectedDelimiter
* @param string $cell
* @param float|int|string $expectedValue
*/
2020-05-18 04:49:57 +00:00
public function testDelimiterDetection($filename, $expectedDelimiter, $cell, $expectedValue): void
{
$reader = new Csv();
self::assertNull($reader->getDelimiter());
$spreadsheet = $reader->load($filename);
self::assertSame($expectedDelimiter, $reader->getDelimiter(), 'should be able to infer the delimiter');
$actual = $spreadsheet->getActiveSheet()->getCell($cell)->getValue();
self::assertSame($expectedValue, $actual, 'should be able to retrieve correct value');
}
public function providerDelimiterDetection()
{
return [
[
'tests/data/Reader/CSV/enclosure.csv',
',',
'C4',
'username2',
],
[
'tests/data/Reader/CSV/semicolon_separated.csv',
';',
'C2',
'25,5',
],
[
'tests/data/Reader/CSV/line_break_in_enclosure.csv',
',',
'A3',
'Test',
],
[
'tests/data/Reader/CSV/line_break_in_enclosure_with_escaped_quotes.csv',
',',
'A3',
'Test',
],
[
'tests/data/Reader/HTML/csv_with_angle_bracket.csv',
',',
'B1',
'Number of items with weight <= 50kg',
],
[
'samples/Reader/sampleData/example1.csv',
',',
'I4',
'100%',
],
[
'samples/Reader/sampleData/example2.csv',
',',
'D8',
-58.373161,
],
[
'tests/data/Reader/CSV/empty.csv',
',',
'A1',
null,
],
[
'tests/data/Reader/CSV/no_delimiter.csv',
',',
'A1',
'SingleLine',
],
];
}
/**
* @dataProvider providerCanLoad
*
* @param bool $expected
* @param string $filename
*/
2020-05-18 04:49:57 +00:00
public function testCanLoad($expected, $filename): void
{
$reader = new Csv();
self::assertSame($expected, $reader->canRead($filename));
}
public function providerCanLoad()
{
return [
[false, 'tests/data/Reader/Ods/data.ods'],
Improving Coverage for Excel2003 XML Reader (#1557) * Improving Coverage for Excel2003 XML Reader Reader/Xml is now 100% covered. File templates/Excel2003XMLTest.xml, used in some tests, is *not* readable by a current version of Excel. I have substituted a new file excel2003.xml to be used in its place. I have not deleted the original in case someone in future (possibly me) wants to see what it needs to make it usable. There are minimal code changes. - Unused protected functions pixel2WidthUnits and widthUnits2Pixel are deleted. - One regex looking to convert hex characters is changed from a-z to a-f, and made case insensitive. - No calculation performed for "error" cell (previously calculation was attempted and threw exception). - Empty relative row/cell is now handled correctly. - Style applied to empty cell when appropriate. - Support added for textRotation. - Support added for border styles. - Support added for diagonal borders. - Support added for superscript and subscript. - Support added for fill patterns. In theory, encodings other than UTF-8 were supported. In fact, I was unable to get SecurityScanner to pass *any* xml which is not UTF-8. Eliminating the assumption that strings might not be UTF-8 allowed much of the code to be greatly simplified. After that, I added some code that would permit the use of some ASCII-compatible encodings (there is a test of ISO-8859-1). It would be more difficult to handle other encodings (such as UTF-16). I am not convinced that even the ISO-8859 effort is worth it, but am willing to investigate either expanding or eliminating non-UTF8 support. I added a number of tests, creating an Xml directory, and moving XmlTest to that directory. Pull Request had problems reading old invalid sample in the code coverage phase, not in any of the other test phases, and not in the code coverage phase on my local machine. As it turns out, aside from being invalid, the sample is much larger than any of the other samples. Tests have been adjusted accordingly. * Smaller Test File Should eliminate need to avoid test during xml coverage. * Break Up Style Test into Multiple Tests Per suggestion from Mark Baker. * Integrate AddressHelper Change The introduction of AddressHelper introduced a conflict which needed to be resolved. I wanted to test it locally before resolving. This required me to add (unchanged) AddressHelper to my local copy. I hope this is an okay manner of resolving the conflict. * Weird Travis Error XmlOddTest works just fine on my local machine, but Travis failed it. Even worse, the lines which Travis flags don't even make any sense (one was the empty line between two methods!). This test is not essential to the rest of the change. I am removing it from the package, and will attempt to re-add it when I have a chance to sync up my fork with the main project.
2020-10-11 11:26:56 +00:00
[false, 'samples/templates/excel2003.xml'],
[true, 'tests/data/Reader/CSV/enclosure.csv'],
[true, 'tests/data/Reader/CSV/semicolon_separated.csv'],
[true, 'tests/data/Reader/CSV/contains_html.csv'],
[true, 'tests/data/Reader/CSV/csv_without_extension'],
[true, 'tests/data/Reader/HTML/csv_with_angle_bracket.csv'],
[true, 'tests/data/Reader/CSV/empty.csv'],
[true, 'samples/Reader/sampleData/example1.csv'],
[true, 'samples/Reader/sampleData/example2.csv'],
];
}
2020-05-18 04:49:57 +00:00
public function testEscapeCharacters(): void
{
$reader = (new Csv())->setEscapeCharacter('"');
$worksheet = $reader->load('tests/data/Reader/CSV/backslash.csv')
->getActiveSheet();
$expected = [
['field 1', 'field 2\\'],
['field 3\\', 'field 4'],
];
2020-05-18 04:49:57 +00:00
self::assertSame('"', $reader->getEscapeCharacter());
self::assertSame($expected, $worksheet->toArray());
}
/**
* @dataProvider providerEncodings
*
* @param string $filename
* @param string $encoding
*/
2020-05-18 04:49:57 +00:00
public function testEncodings($filename, $encoding): void
{
$reader = new Csv();
$reader->setInputEncoding($encoding);
$spreadsheet = $reader->load($filename);
$sheet = $spreadsheet->getActiveSheet();
self::assertEquals('Å', $sheet->getCell('A1')->getValue());
}
2020-05-18 04:49:57 +00:00
public function testInvalidWorkSheetInfo(): void
{
$this->expectException(ReaderException::class);
$reader = new Csv();
$reader->listWorksheetInfo('');
}
/**
* @dataProvider providerEncodings
*
* @param string $filename
* @param string $encoding
*/
2020-05-18 04:49:57 +00:00
public function testWorkSheetInfo($filename, $encoding): void
{
$reader = new Csv();
$reader->setInputEncoding($encoding);
$info = $reader->listWorksheetInfo($filename);
self::assertEquals('Worksheet', $info[0]['worksheetName']);
self::assertEquals('B', $info[0]['lastColumnLetter']);
self::assertEquals(1, $info[0]['lastColumnIndex']);
self::assertEquals(2, $info[0]['totalRows']);
self::assertEquals(2, $info[0]['totalColumns']);
}
public function providerEncodings()
{
return [
['tests/data/Reader/CSV/encoding.iso88591.csv', 'ISO-8859-1'],
['tests/data/Reader/CSV/encoding.utf8.csv', 'UTF-8'],
['tests/data/Reader/CSV/encoding.utf8bom.csv', 'UTF-8'],
['tests/data/Reader/CSV/encoding.utf16be.csv', 'UTF-16BE'],
['tests/data/Reader/CSV/encoding.utf16le.csv', 'UTF-16LE'],
['tests/data/Reader/CSV/encoding.utf32be.csv', 'UTF-32BE'],
['tests/data/Reader/CSV/encoding.utf32le.csv', 'UTF-32LE'],
];
}
2020-05-18 04:49:57 +00:00
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::assertEquals($expected, $sheet->getCell('B3')->getValue());
}
public function testLineBreakEscape(): void
{
$reader = new Csv();
$spreadsheet = $reader->load('tests/data/Reader/CSV/line_break_in_enclosure_with_escaped_quotes.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::assertEquals($expected, $sheet->getCell('B3')->getValue());
}
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::assertEquals($expected, $sheet->getCell('B3')->getValue());
}
2020-05-18 04:49:57 +00:00
public function testSeparatorLine(): void
{
$reader = new Csv();
$reader->setSheetIndex(3);
$spreadsheet = $reader->load('tests/data/Reader/CSV/sep.csv');
self::assertEquals(';', $reader->getDelimiter());
$sheet = $spreadsheet->getActiveSheet();
self::assertEquals(3, $reader->getSheetIndex());
self::assertEquals(3, $spreadsheet->getActiveSheetIndex());
self::assertEquals('A', $sheet->getCell('A1')->getValue());
self::assertEquals(1, $sheet->getCell('B1')->getValue());
self::assertEquals(2, $sheet->getCell('A2')->getValue());
self::assertEquals(3, $sheet->getCell('B2')->getValue());
}
2020-05-18 04:49:57 +00:00
public function testDefaultSettings(): void
{
$reader = new Csv();
self::assertEquals('UTF-8', $reader->getInputEncoding());
self::assertEquals('"', $reader->getEnclosure());
$reader->setEnclosure('\'');
self::assertEquals('\'', $reader->getEnclosure());
$reader->setEnclosure('');
self::assertEquals('"', $reader->getEnclosure());
// following tests from BaseReader
self::assertTrue($reader->getReadEmptyCells());
self::assertFalse($reader->getIncludeCharts());
self::assertNull($reader->getLoadSheetsOnly());
}
2020-05-18 04:49:57 +00:00
public function testReadEmptyFileName(): void
{
$this->expectException(ReaderException::class);
$reader = new Csv();
$filename = '';
$reader->load($filename);
}
2020-05-18 04:49:57 +00:00
public function testReadNonexistentFileName(): void
{
$this->expectException(ReaderException::class);
$reader = new Csv();
$reader->load('tests/data/Reader/CSV/encoding.utf8.csvxxx');
}
}