2017-04-03 02:52:35 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace PhpOffice\PhpSpreadsheetTests\Reader;
|
|
|
|
|
2017-05-17 22:02:17 +00:00
|
|
|
use PhpOffice\PhpSpreadsheet\Reader\Csv as ReaderCsv;
|
2017-04-03 02:52:35 +00:00
|
|
|
use PhpOffice\PhpSpreadsheet\Shared\File;
|
|
|
|
use PhpOffice\PhpSpreadsheet\Spreadsheet;
|
2017-05-17 22:02:17 +00:00
|
|
|
use PhpOffice\PhpSpreadsheet\Writer\Csv as WriterCsv;
|
2017-11-08 15:48:01 +00:00
|
|
|
use PHPUnit\Framework\TestCase;
|
2017-04-03 02:52:35 +00:00
|
|
|
|
2017-11-08 15:48:01 +00:00
|
|
|
class CsvTest extends TestCase
|
2017-04-03 02:52:35 +00:00
|
|
|
{
|
|
|
|
public function testEnclosure()
|
|
|
|
{
|
|
|
|
$value = '<img alt="" src="http://example.com/image.jpg" />';
|
|
|
|
$filename = tempnam(File::sysGetTempDir(), 'phpspreadsheet');
|
|
|
|
|
|
|
|
// Write temp file with value
|
|
|
|
$spreadsheet = new Spreadsheet();
|
|
|
|
$spreadsheet->getActiveSheet()->getCell('A1')->setValue($value);
|
2017-05-17 22:02:17 +00:00
|
|
|
$writer = new WriterCsv($spreadsheet);
|
2017-04-03 02:52:35 +00:00
|
|
|
$writer->save($filename);
|
|
|
|
|
|
|
|
// Read written file
|
2017-05-17 22:02:17 +00:00
|
|
|
$reader = new ReaderCsv();
|
2017-04-03 02:52:35 +00:00
|
|
|
$reloadedSpreadsheet = $reader->load($filename);
|
|
|
|
$actual = $reloadedSpreadsheet->getActiveSheet()->getCell('A1')->getCalculatedValue();
|
2017-09-20 05:55:42 +00:00
|
|
|
self::assertSame($value, $actual, 'should be able to write and read strings with multiples quotes');
|
2017-04-03 02:52:35 +00:00
|
|
|
}
|
2017-04-17 16:51:53 +00:00
|
|
|
|
|
|
|
public function testDelimiterDetection()
|
|
|
|
{
|
2017-05-17 22:02:17 +00:00
|
|
|
$reader = new ReaderCsv();
|
2017-09-20 05:55:42 +00:00
|
|
|
self::assertNull($reader->getDelimiter());
|
2017-04-17 16:51:53 +00:00
|
|
|
|
|
|
|
$filename = __DIR__ . '/../../data/Reader/CSV/semicolon_separated.csv';
|
|
|
|
$spreadsheet = $reader->load($filename);
|
|
|
|
|
2017-09-20 05:55:42 +00:00
|
|
|
self::assertSame(';', $reader->getDelimiter(), 'should be able to infer the delimiter');
|
2017-04-17 16:51:53 +00:00
|
|
|
|
|
|
|
$actual = $spreadsheet->getActiveSheet()->getCell('C2')->getValue();
|
2017-09-20 05:55:42 +00:00
|
|
|
self::assertSame('25,5', $actual, 'should be able to retrieve values with commas');
|
2017-04-17 16:51:53 +00:00
|
|
|
}
|
2017-04-03 02:52:35 +00:00
|
|
|
}
|