7517cdd008
I believe that both CSV Reader and Writer are 100% covered now. There were some errors uncovered during development. The reader specifically permits encodings other than UTF-8 to be used. However, fgetcsv will not properly handle other encodings. I tried replacing it with fgets/iconv/strgetcsv, but that could not handle line breaks within a cell, even for UTF-8. This is, I'm sure, a very rare use case. I eventually handled it by using php://memory to hold the translated file contents for non-UTF8. There were no tests for this situation, and now there are (probably too many). "Contiguous" read was not handle correctly. There is a file in samples which uses it. It was designed to read a large sheet, and split it into three. The first sheet was corrrect, but the second and third were almost entirely empty. This has been corrected, and the sample code was adapted into a formal test with assertions to confirm that it works as designed. I made a minor documentation change. Unlike HTML, where you never need a BOM because you can declare the encoding in the file, a CSV with non-ASCII characters must explicitly include a BOM for Excel to handle it correctly. This was explained in the Reading CSV section, but was glossed over in the Writing CSV section, which I have updated.
61 lines
2.3 KiB
PHP
61 lines
2.3 KiB
PHP
<?php
|
|
|
|
namespace PhpOffice\PhpSpreadsheetTests\Functional;
|
|
|
|
use PhpOffice\PhpSpreadsheet\Reader\Csv as CsvReader;
|
|
use PhpOffice\PhpSpreadsheet\Shared\File;
|
|
use PhpOffice\PhpSpreadsheet\Spreadsheet;
|
|
use PhpOffice\PhpSpreadsheet\Writer\Csv as CsvWriter;
|
|
use PhpOffice\PhpSpreadsheet\Writer\Exception as WriterException;
|
|
use PhpOffice\PhpSpreadsheetTests\Functional;
|
|
|
|
class CsvWriteTest extends Functional\AbstractFunctional
|
|
{
|
|
public function testNotFirstSheet()
|
|
{
|
|
$spreadsheet = new Spreadsheet();
|
|
$sheet = $spreadsheet->getActiveSheet();
|
|
$sheet->setCellValue('A1', 'First Sheet');
|
|
$sheet = $spreadsheet->createSheet();
|
|
$sheet->setCellValue('A1', 'Second Sheet');
|
|
$sheet = $spreadsheet->createSheet();
|
|
$sheet->setCellValue('A1', 'Third Sheet');
|
|
$writer = new CsvWriter($spreadsheet);
|
|
$writer->setSheetIndex(1);
|
|
self::assertEquals(1, $writer->getSheetIndex());
|
|
$filename = tempnam(File::sysGetTempDir(), 'phpspreadsheet-test');
|
|
$writer->save($filename);
|
|
$reader = new CsvReader();
|
|
$newspreadsheet = $reader->load($filename);
|
|
unlink($filename);
|
|
$sheet = $newspreadsheet->getActiveSheet();
|
|
self::assertEquals('Second Sheet', $sheet->getCell('A1')->getValue());
|
|
self::assertEquals(0, $newspreadsheet->getActiveSheetIndex());
|
|
}
|
|
|
|
public function testWriteEmptyFileName()
|
|
{
|
|
$this->expectException(WriterException::class);
|
|
$spreadsheet = new Spreadsheet();
|
|
$writer = new CsvWriter($spreadsheet);
|
|
$filename = '';
|
|
$writer->save($filename);
|
|
}
|
|
|
|
public function testDefaultSettings()
|
|
{
|
|
$spreadsheet = new Spreadsheet();
|
|
$writer = new CsvWriter($spreadsheet);
|
|
self::assertEquals('"', $writer->getEnclosure());
|
|
$writer->setEnclosure('\'');
|
|
self::assertEquals('\'', $writer->getEnclosure());
|
|
$writer->setEnclosure('');
|
|
self::assertEquals('"', $writer->getEnclosure());
|
|
self::assertEquals(PHP_EOL, $writer->getLineEnding());
|
|
self::assertFalse($writer->getUseBOM());
|
|
self::assertFalse($writer->getIncludeSeparatorLine());
|
|
self::assertFalse($writer->getExcelCompatibility());
|
|
self::assertEquals(0, $writer->getSheetIndex());
|
|
}
|
|
}
|