Cell value objects (#383)

* first stab at cell objects, #182
* removed comment parameter, streamlined cell detection, more tests #182
* shorter constant names, missing isFormula() #182
* first batch of changes #182
* documentation #182
This commit is contained in:
madflow authored and Adrien Loison committed 2017-05-01 12:09:24 +02:00
1 parent 4e6db6a8a1
commit 4acd9ad087
6 files changed
+312 -18

No files matched your search

+14
View File
@@ -5,6 +5,7 @@ namespace Box\Spout\Writer\CSV;
use Box\Spout\TestUsingResource;
use Box\Spout\Common\Type;
use Box\Spout\Common\Helper\EncodingHelper;
use Box\Spout\Writer\Common\Cell;
use Box\Spout\Writer\WriterFactory;
/**
@@ -177,6 +178,19 @@ class WriterTest extends \PHPUnit_Framework_TestCase
$this->assertEquals('#This is, a comma#,csv--12,csv--13', $writtenContent, 'The fields should be enclosed with #');
}
/**
* @return void
*/
public function testWriteShouldAcceptCellObjects()
{
$allRows = [
[new Cell('String Value'), new Cell(1)]
];
$writtenContent = $this->writeToCsvFileAndReturnWrittenContent($allRows, 'csv_with_cell_objects.csv');
$writtenContent = $this->trimWrittenContent($writtenContent);
$this->assertEquals('"String Value",1', $writtenContent);
}
/**
* @param array $allRows
* @param string $fileName
+43
View File
@@ -6,6 +6,7 @@ use Box\Spout\Common\Exception\SpoutException;
use Box\Spout\Common\Type;
use Box\Spout\Reader\Wrapper\XMLReader;
use Box\Spout\TestUsingResource;
use Box\Spout\Writer\Common\Cell;
use Box\Spout\Writer\Common\Helper\ZipHelper;
use Box\Spout\Writer\WriterFactory;
@@ -463,6 +464,47 @@ class WriterTest extends \PHPUnit_Framework_TestCase
$this->assertEquals('application/vnd.oasis.opendocument.spreadsheet', $finfo->file($resourcePath));
}
/**
* @return void
*/
public function testWriteShouldAcceptCellObjects()
{
$fileName = 'test_writer_should_accept_cell_objects.ods';
$dataRows = [
[new Cell('ods--11'), new Cell('ods--12')],
[new Cell('ods--21'), new Cell('ods--22'), new Cell('ods--23')],
];
$this->writeToODSFile($dataRows, $fileName);
foreach ($dataRows as $dataRow) {
/** @var Cell $cell */
foreach ($dataRow as $cell) {
$this->assertValueWasWritten($fileName, $cell->getValue());
}
}
}
/**
* @return void
*/
public function testWriteShouldAcceptCellObjectsWithDifferentValueTypes()
{
$fileName = 'test_writer_should_accept_cell_objects_with_types.ods';
$dataRows = [
[new Cell('i am a string'), new Cell(51465), new Cell(true), new Cell(51465.5)],
];
$this->writeToODSFile($dataRows, $fileName);
foreach ($dataRows as $dataRow) {
/** @var Cell $cell */
foreach ($dataRow as $cell) {
$this->assertValueWasWritten($fileName, (string)$cell->getValue(), '', true);
}
}
}
/**
* @param array $allRows
* @param string $fileName
@@ -527,6 +569,7 @@ class WriterTest extends \PHPUnit_Framework_TestCase
$xmlContents = file_get_contents('zip://' . $pathToContentFile);
$this->assertContains($value, $xmlContents, $message);
}
/**
+55
View File
@@ -5,6 +5,7 @@ namespace Box\Spout\Writer\XLSX;
use Box\Spout\Common\Exception\SpoutException;
use Box\Spout\Common\Type;
use Box\Spout\TestUsingResource;
use Box\Spout\Writer\Common\Cell;
use Box\Spout\Writer\WriterFactory;
use Box\Spout\Writer\XLSX\Internal\Worksheet;
@@ -507,6 +508,60 @@ class WriterTest extends \PHPUnit_Framework_TestCase
$this->assertEquals('application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', $finfo->file($resourcePath));
}
/**
* @return void
*/
public function testWriterShouldAcceptCellObjects()
{
$fileName = 'test_writer_should_accept_cell_objects.xlsx';
$dataRows = [
[new Cell('xlsx--11'), new Cell('xlsx--12')],
[new Cell('xlsx--21'), new Cell('xlsx--22'), new Cell('xlsx--23')],
];
$this->writeToXLSXFile($dataRows, $fileName, $shouldUseInlineStrings = false);
foreach ($dataRows as $dataRow) {
/** @var Cell $cell */
foreach ($dataRow as $cell) {
$this->assertSharedStringWasWritten($fileName, $cell->getValue());
}
}
}
/**
* @return void
*/
public function testWriteShouldAcceptCellObjectsWithDifferentValueTypes()
{
$fileName = 'test_writer_should_accept_cell_objects_with_types.xlsx';
$dataRowsShared = [
[new Cell('i am a string')],
];
$dataRowsInline = [
[new Cell(51465), new Cell(true), new Cell(51465.5)]
];
$dataRows = array_merge($dataRowsShared, $dataRowsInline);
$this->writeToXLSXFile($dataRows, $fileName, $shouldUseInlineStrings = false);
foreach ($dataRowsShared as $dataRow) {
/** @var Cell $cell */
foreach ($dataRow as $cell) {
$this->assertSharedStringWasWritten($fileName, (string)$cell->getValue());
}
}
foreach ($dataRowsInline as $dataRow) {
/** @var Cell $cell */
foreach ($dataRow as $cell) {
$this->assertInlineDataWasWrittenToSheet($fileName, 1, $cell->getValue());
}
}
}
/**
* @param array $allRows
* @param string $fileName