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;
|
|
|
|
use PHPUnit_Framework_TestCase;
|
2017-04-03 02:52:35 +00:00
|
|
|
|
2017-05-17 22:02:17 +00:00
|
|
|
class CsvTest extends PHPUnit_Framework_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();
|
|
|
|
$this->assertSame($value, $actual, 'should be able to write and read strings with multiples quotes');
|
|
|
|
}
|
2017-04-17 16:51:53 +00:00
|
|
|
|
|
|
|
public function testDelimiterDetection()
|
|
|
|
{
|
2017-05-17 22:02:17 +00:00
|
|
|
$reader = new ReaderCsv();
|
2017-04-17 16:51:53 +00:00
|
|
|
$this->assertNull($reader->getDelimiter());
|
|
|
|
|
|
|
|
$filename = __DIR__ . '/../../data/Reader/CSV/semicolon_separated.csv';
|
|
|
|
$spreadsheet = $reader->load($filename);
|
|
|
|
|
|
|
|
$this->assertSame(';', $reader->getDelimiter(), 'should be able to infer the delimiter');
|
|
|
|
|
|
|
|
$actual = $spreadsheet->getActiveSheet()->getCell('C2')->getValue();
|
|
|
|
$this->assertSame('25,5', $actual, 'should be able to retrieve values with commas');
|
|
|
|
}
|
2017-04-03 02:52:35 +00:00
|
|
|
}
|