first shot an row RowManager and make CSV tests pass

This commit is contained in:
madflow 2017-07-30 21:09:55 +02:00
parent 968ca198cc
commit 23de4b4117
6 changed files with 103 additions and 22 deletions

View File

@ -2,7 +2,9 @@
namespace Box\Spout\Writer\Common\Creator; namespace Box\Spout\Writer\Common\Creator;
use Box\Spout\Writer\Common\Entity\Row;
use Box\Spout\Writer\Common\Entity\Sheet; use Box\Spout\Writer\Common\Entity\Sheet;
use Box\Spout\Writer\Common\Entity\Style\Style;
use Box\Spout\Writer\Common\Entity\Workbook; use Box\Spout\Writer\Common\Entity\Workbook;
use Box\Spout\Writer\Common\Entity\Worksheet; use Box\Spout\Writer\Common\Entity\Worksheet;
@ -55,4 +57,15 @@ class EntityFactory
$sheetManager = $this->managerFactory->createSheetManager(); $sheetManager = $this->managerFactory->createSheetManager();
return new Sheet($sheetIndex, $associatedWorkbookId, $sheetManager); return new Sheet($sheetIndex, $associatedWorkbookId, $sheetManager);
} }
/**
* @param array $cells
* @param Style|null $style
* @return Row
*/
public function createRow(array $cells, Style $style = null)
{
$rowManager = $this->managerFactory->createRowManager();
return new Row($cells, $style, $rowManager);
}
} }

View File

@ -3,7 +3,10 @@
namespace Box\Spout\Writer\Common\Creator; namespace Box\Spout\Writer\Common\Creator;
use Box\Spout\Common\Helper\StringHelper; use Box\Spout\Common\Helper\StringHelper;
use Box\Spout\Writer\Common\Manager\CellManager;
use Box\Spout\Writer\Common\Manager\RowManager;
use Box\Spout\Writer\Common\Manager\SheetManager; use Box\Spout\Writer\Common\Manager\SheetManager;
use Box\Spout\Writer\Common\Manager\Style\StyleMerger;
/** /**
* Class ManagerFactory * Class ManagerFactory
@ -13,6 +16,24 @@ use Box\Spout\Writer\Common\Manager\SheetManager;
*/ */
class ManagerFactory class ManagerFactory
{ {
/**
* @return CellManager
*/
public function createCellManager()
{
$styleMerger = new StyleMerger();
return new CellManager($styleMerger);
}
/**
* @return RowManager
*/
public function createRowManager()
{
$styleMerger = new StyleMerger();
return new RowManager($styleMerger);
}
/** /**
* @return SheetManager * @return SheetManager
*/ */

View File

@ -3,6 +3,7 @@
namespace Box\Spout\Writer\Common\Entity; namespace Box\Spout\Writer\Common\Entity;
use Box\Spout\Writer\Common\Entity\Style\Style; use Box\Spout\Writer\Common\Entity\Style\Style;
use Box\Spout\Writer\Common\Manager\RowManager;
use Box\Spout\Writer\Common\Manager\Style\StyleMerger; use Box\Spout\Writer\Common\Manager\Style\StyleMerger;
class Row class Row
@ -20,22 +21,24 @@ class Row
protected $style = null; protected $style = null;
/** /**
* @var StyleMerger * Thw row manager
* @var RowManager
*/ */
protected $styleMerger; protected $rowManager;
/** /**
* Row constructor. * Row constructor.
* @param Cell[] $cells * @param Cell[] $cells
* @param Style|null $style * @param Style|null $style
* @param RowManager $rowManager
*/ */
public function __construct(array $cells = [], Style $style = null) public function __construct(array $cells = [], Style $style = null, RowManager $rowManager)
{ {
$this $this
->setCells($cells) ->setCells($cells)
->setStyle($style); ->setStyle($style);
$this->styleMerger = new StyleMerger(); $this->rowManager = $rowManager;
} }
/** /**
@ -86,11 +89,7 @@ class Row
*/ */
public function applyStyle(Style $style = null) public function applyStyle(Style $style = null)
{ {
if ($style === null) { $this->rowManager->applyStyle($this, $style);
return $this;
}
$merged = $this->styleMerger->merge($this->getStyle(), $style);
$this->setStyle($merged);
return $this; return $this;
} }
@ -112,6 +111,6 @@ class Row
*/ */
public function isEmpty() public function isEmpty()
{ {
return count($this->cells) === 0 || (count($this->cells) === 1 && $this->cells[0]->isEmpty()); return $this->rowManager->isEmpty($this);
} }
} }

View File

@ -31,4 +31,17 @@ class RowManager
$mergedStyle = $this->styleMerger->merge($row->getStyle(), $style); $mergedStyle = $this->styleMerger->merge($row->getStyle(), $style);
$row->setStyle($mergedStyle); $row->setStyle($mergedStyle);
} }
/**
* Detect whether a row is considered empty.
* An empty row has either no cells at all - or only one empty cell
*
* @param Row $row
* @return bool
*/
public function isEmpty(Row $row)
{
$cells = $row->getCells();
return count($cells) === 0 || (count($cells) === 1 && $cells[0]->isEmpty());
}
} }

View File

@ -54,7 +54,8 @@ abstract class WriterAbstract implements WriterInterface
OptionsManagerInterface $optionsManager, OptionsManagerInterface $optionsManager,
StyleMerger $styleMerger, StyleMerger $styleMerger,
GlobalFunctionsHelper $globalFunctionsHelper GlobalFunctionsHelper $globalFunctionsHelper
) { )
{
$this->optionsManager = $optionsManager; $this->optionsManager = $optionsManager;
$this->styleMerger = $styleMerger; $this->styleMerger = $styleMerger;
$this->globalFunctionsHelper = $globalFunctionsHelper; $this->globalFunctionsHelper = $globalFunctionsHelper;

View File

@ -5,6 +5,8 @@ namespace Box\Spout\Writer\CSV;
use Box\Spout\TestUsingResource; use Box\Spout\TestUsingResource;
use Box\Spout\Common\Type; use Box\Spout\Common\Type;
use Box\Spout\Common\Helper\EncodingHelper; use Box\Spout\Common\Helper\EncodingHelper;
use Box\Spout\Writer\Common\Creator\EntityFactory;
use Box\Spout\Writer\Common\Creator\ManagerFactory;
use Box\Spout\Writer\Common\Entity\Cell; use Box\Spout\Writer\Common\Entity\Cell;
use Box\Spout\Writer\WriterFactory; use Box\Spout\Writer\WriterFactory;
@ -17,6 +19,17 @@ class WriterTest extends \PHPUnit_Framework_TestCase
{ {
use TestUsingResource; use TestUsingResource;
/**
* @var EntityFactory
*/
protected $entityFactory;
protected function setUp()
{
$this->entityFactory = new EntityFactory(new ManagerFactory());
parent::setUp();
}
/** /**
* @expectedException \Box\Spout\Common\Exception\IOException * @expectedException \Box\Spout\Common\Exception\IOException
*/ */
@ -28,7 +41,11 @@ class WriterTest extends \PHPUnit_Framework_TestCase
$writer = WriterFactory::create(Type::CSV); $writer = WriterFactory::create(Type::CSV);
@$writer->openToFile($filePath); @$writer->openToFile($filePath);
$writer->addRow(['csv--11', 'csv--12']); $row = $this->entityFactory->createRow([
new Cell('csv--11'),
new Cell('csv--12')
]);
$writer->addRow($row);
$writer->close(); $writer->close();
} }
@ -38,7 +55,11 @@ class WriterTest extends \PHPUnit_Framework_TestCase
public function testWriteShouldThrowExceptionIfCallAddRowBeforeOpeningWriter() public function testWriteShouldThrowExceptionIfCallAddRowBeforeOpeningWriter()
{ {
$writer = WriterFactory::create(Type::CSV); $writer = WriterFactory::create(Type::CSV);
$writer->addRow(['csv--11', 'csv--12']); $row = $this->entityFactory->createRow([
new Cell('csv--11'),
new Cell('csv--12')
]);
$writer->addRow($row);
$writer->close(); $writer->close();
} }
@ -48,17 +69,24 @@ class WriterTest extends \PHPUnit_Framework_TestCase
public function testWriteShouldThrowExceptionIfCallAddRowsBeforeOpeningWriter() public function testWriteShouldThrowExceptionIfCallAddRowsBeforeOpeningWriter()
{ {
$writer = WriterFactory::create(Type::CSV); $writer = WriterFactory::create(Type::CSV);
$writer->addRows([['csv--11', 'csv--12']]); $row = $this->entityFactory->createRow([
new Cell('csv--11'),
new Cell('csv--12')
]);
$writer->addRows([$row]);
$writer->close(); $writer->close();
} }
/** public function testAddRowsShouldThrowExceptionIfRowsAreNotArrayOfRows()
* @expectedException \Box\Spout\Common\Exception\InvalidArgumentException
*/
public function testAddRowsShouldThrowExceptionIfRowsAreNotArrayOfArrays()
{ {
if (version_compare(PHP_VERSION, '7.0.0') >= 0) {
$this->expectException(\TypeError::class);
} else {
$this->markTestSkipped('PHP > 7.0 only');
}
$writer = WriterFactory::create(Type::CSV); $writer = WriterFactory::create(Type::CSV);
$writer->addRows(['csv--11', 'csv--12']); $row = new \stdClass();
$writer->addRows([$row]);
$writer->close(); $writer->close();
} }
@ -192,11 +220,11 @@ class WriterTest extends \PHPUnit_Framework_TestCase
} }
/** /**
* @param array $allRows * @param array $allRows
* @param string $fileName * @param string $fileName
* @param string $fieldDelimiter * @param string $fieldDelimiter
* @param string $fieldEnclosure * @param string $fieldEnclosure
* @param bool $shouldAddBOM * @param bool $shouldAddBOM
* @return null|string * @return null|string
*/ */
private function writeToCsvFileAndReturnWrittenContent($allRows, $fileName, $fieldDelimiter = ',', $fieldEnclosure = '"', $shouldAddBOM = true) private function writeToCsvFileAndReturnWrittenContent($allRows, $fileName, $fieldDelimiter = ',', $fieldEnclosure = '"', $shouldAddBOM = true)
@ -211,7 +239,13 @@ class WriterTest extends \PHPUnit_Framework_TestCase
$writer->setShouldAddBOM($shouldAddBOM); $writer->setShouldAddBOM($shouldAddBOM);
$writer->openToFile($resourcePath); $writer->openToFile($resourcePath);
$writer->addRows($allRows);
$writer->addRows(array_map(function ($oneRow) {
$row = $this->entityFactory->createRow(array_map(function ($value) {
return new Cell($value);
}, $oneRow));
return $row;
}, $allRows));
$writer->close(); $writer->close();
return file_get_contents($resourcePath); return file_get_contents($resourcePath);