Various improvements

This commit is contained in:
Adrien Loison 2017-10-21 17:14:27 +02:00
parent 13eb5f0560
commit 10fed91ac1
21 changed files with 349 additions and 300 deletions

View File

@ -56,7 +56,7 @@ class Cell
/** /**
* The cell style * The cell style
* @var Style|null * @var Style
*/ */
protected $style; protected $style;
@ -66,9 +66,8 @@ class Cell
protected $styleMerger; protected $styleMerger;
/** /**
* Cell constructor.
* @param $value mixed * @param $value mixed
* @param $style|null Style * @param Style|null $style
*/ */
public function __construct($value, Style $style = null) public function __construct($value, Style $style = null)
{ {
@ -78,7 +77,7 @@ class Cell
} }
/** /**
* @param $value mixed|null * @param mixed|null $value
*/ */
public function setValue($value) public function setValue($value)
{ {
@ -95,22 +94,18 @@ class Cell
} }
/** /**
* @param Style $style|null * @param Style|null $style
*/ */
public function setStyle(Style $style = null) public function setStyle($style)
{ {
$this->style = $style; $this->style = $style ?: new Style();
} }
/** /**
* @return Style|null * @return Style
*/ */
public function getStyle() public function getStyle()
{ {
if (!isset($this->style)) {
$this->setStyle(new Style());
}
return $this->style; return $this->style;
} }
@ -124,6 +119,7 @@ class Cell
/** /**
* Get the current value type * Get the current value type
*
* @param mixed|null $value * @param mixed|null $value
* @return int * @return int
*/ */
@ -163,6 +159,7 @@ class Cell
/** /**
* Not used at the moment * Not used at the moment
*
* @return bool * @return bool
*/ */
public function isFormula() public function isFormula()
@ -203,15 +200,16 @@ class Cell
} }
/** /**
* @param Style $style|null * @param Style|null $style
* @return Cell * @return Cell
*/ */
public function applyStyle(Style $style = null) public function applyStyle($style)
{ {
if ($style === null) { if ($style === null) {
return $this; return $this;
} }
$mergedStyle = $this->styleMerger->merge($this->getStyle(), $style);
$mergedStyle = $this->styleMerger->merge($this->style, $style);
$this->setStyle($mergedStyle); $this->setStyle($mergedStyle);
return $this; return $this;

View File

@ -9,13 +9,13 @@ class Row
{ {
/** /**
* The cells in this row * The cells in this row
* @var array * @var Cell[]
*/ */
protected $cells = []; protected $cells = [];
/** /**
* The row style * The row style
* @var Style|null * @var Style
*/ */
protected $style; protected $style;
@ -31,7 +31,7 @@ class Row
* @param Style|null $style * @param Style|null $style
* @param RowManager $rowManager * @param RowManager $rowManager
*/ */
public function __construct(array $cells = [], Style $style = null, RowManager $rowManager) public function __construct(array $cells, $style, RowManager $rowManager)
{ {
$this $this
->setCells($cells) ->setCells($cells)
@ -49,7 +49,7 @@ class Row
} }
/** /**
* @param array $cells * @param Cell[] $cells
* @return $this * @return $this
*/ */
public function setCells(array $cells) public function setCells(array $cells)
@ -67,29 +67,25 @@ class Row
*/ */
public function getStyle() public function getStyle()
{ {
if (!isset($this->style)) { return $this->style;
$this->setStyle(new Style());
} }
return $this->style; /**
* @param Style|null $style
* @return Row
*/
public function setStyle($style)
{
$this->style = $style ?: new Style();
return $this;
} }
/** /**
* @param Style $style * @param Style $style
* @return Row * @return Row
*/ */
public function setStyle($style) public function applyStyle($style)
{
$this->style = $style;
return $this;
}
/**
* @param Style $style|null
* @return Row
*/
public function applyStyle(Style $style = null)
{ {
$this->rowManager->applyStyle($this, $style); $this->rowManager->applyStyle($this, $style);
@ -107,6 +103,16 @@ class Row
return $this; return $this;
} }
/**
* Returns whether a row has cells
*
* @return bool
*/
public function hasCells()
{
return $this->rowManager->hasCells($this);
}
/** /**
* Detect whether this row is considered empty. * Detect whether this row is considered empty.
* An empty row has either no cells at all - or only empty cells * An empty row has either no cells at all - or only empty cells

View File

@ -14,7 +14,6 @@ class CellManager
protected $styleMerger; protected $styleMerger;
/** /**
* CellManager constructor.
* @param StyleMerger $styleMerger * @param StyleMerger $styleMerger
*/ */
public function __construct(StyleMerger $styleMerger) public function __construct(StyleMerger $styleMerger)
@ -23,7 +22,7 @@ class CellManager
} }
/** /**
* Merges a Style into a cells Style. * Merges a Style into a cell's Style.
* *
* @param Cell $cell * @param Cell $cell
* @param Style $style * @param Style $style

View File

@ -14,7 +14,6 @@ class RowManager
protected $styleMerger; protected $styleMerger;
/** /**
* RowManager constructor.
* @param StyleMerger $styleMerger * @param StyleMerger $styleMerger
*/ */
public function __construct(StyleMerger $styleMerger) public function __construct(StyleMerger $styleMerger)
@ -23,6 +22,7 @@ class RowManager
} }
/** /**
* @param Row $row
* @param Style $style * @param Style $style
* @return $this * @return $this
*/ */
@ -32,6 +32,17 @@ class RowManager
$row->setStyle($mergedStyle); $row->setStyle($mergedStyle);
} }
/**
* Returns whether a row has cells
*
* @param Row $row
* @return bool
*/
public function hasCells(Row $row)
{
return count($row->getCells()) !== 0;
}
/** /**
* Detect whether a row is considered empty. * Detect whether a row is considered empty.
* An empty row has either no cells at all - or only one empty cell * An empty row has either no cells at all - or only one empty cell

View File

@ -73,14 +73,17 @@ class StyleManager implements StyleManagerInterface
*/ */
protected function applyWrapTextIfCellContainsNewLine(Cell $cell) protected function applyWrapTextIfCellContainsNewLine(Cell $cell)
{ {
$cellStyle = $cell->getStyle();
// if the "wrap text" option is already set, no-op // if the "wrap text" option is already set, no-op
if ($cell->getStyle()->hasSetWrapText()) { if ($cellStyle->hasSetWrapText()) {
return $cell->getStyle(); return $cellStyle;
}
if ($cell->isString() && strpos($cell->getValue(), "\n") !== false) {
$cell->getStyle()->setShouldWrapText();
} }
return $cell->getStyle(); if ($cell->isString() && strpos($cell->getValue(), "\n") !== false) {
$cellStyle->setShouldWrapText();
}
return $cellStyle;
} }
} }

View File

@ -69,8 +69,6 @@ class ManagerFactory implements ManagerFactoryInterface
$stringsHelper = $this->helperFactory->createStringHelper(); $stringsHelper = $this->helperFactory->createStringHelper();
return new WorksheetManager($styleManager, $stringsEscaper, $stringsHelper); return new WorksheetManager($styleManager, $stringsEscaper, $stringsHelper);
return new WorksheetManager($stringsEscaper, $stringsHelper, $this->entityFactory);
} }
/** /**

View File

@ -8,8 +8,6 @@ use Box\Spout\Common\Exception\IOException;
use Box\Spout\Common\Exception\SpoutException; use Box\Spout\Common\Exception\SpoutException;
use Box\Spout\Common\Helper\GlobalFunctionsHelper; use Box\Spout\Common\Helper\GlobalFunctionsHelper;
use Box\Spout\Common\Manager\OptionsManagerInterface; use Box\Spout\Common\Manager\OptionsManagerInterface;
use Box\Spout\Writer\Common\Creator\EntityFactory;
use Box\Spout\Writer\Common\Entity\Cell;
use Box\Spout\Writer\Common\Entity\Options; use Box\Spout\Writer\Common\Entity\Options;
use Box\Spout\Writer\Common\Entity\Row; use Box\Spout\Writer\Common\Entity\Row;
use Box\Spout\Writer\Common\Entity\Style\Style; use Box\Spout\Writer\Common\Entity\Style\Style;
@ -119,6 +117,8 @@ abstract class WriterAbstract implements WriterInterface
} }
/** /**
* @codeCoverageIgnore
*
* {@inheritdoc} * {@inheritdoc}
*/ */
public function openToBrowser($outputFileName) public function openToBrowser($outputFileName)
@ -187,7 +187,7 @@ abstract class WriterAbstract implements WriterInterface
public function addRow(Row $row) public function addRow(Row $row)
{ {
if ($this->isWriterOpened) { if ($this->isWriterOpened) {
if (!$row->isEmpty()) { if ($row->hasCells()) {
try { try {
$this->applyDefaultRowStyle($row); $this->applyDefaultRowStyle($row);
$this->addRowToWriter($row); $this->addRowToWriter($row);
@ -195,6 +195,7 @@ abstract class WriterAbstract implements WriterInterface
// if an exception occurs while writing data, // if an exception occurs while writing data,
// close the writer and remove all files created so far. // close the writer and remove all files created so far.
$this->closeAndAttemptToCleanupAllFiles(); $this->closeAndAttemptToCleanupAllFiles();
// re-throw the exception to alert developers of the error // re-throw the exception to alert developers of the error
throw $e; throw $e;
} }
@ -206,14 +207,6 @@ abstract class WriterAbstract implements WriterInterface
return $this; return $this;
} }
/**
* {@inheritdoc}
*/
public function withRow(\Closure $callback)
{
return $this->addRow($callback(EntityFactory::createRow([])));
}
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
@ -231,28 +224,6 @@ abstract class WriterAbstract implements WriterInterface
return $this; return $this;
} }
/**
* @param array $dataRow
* @param Style|null $style
* @return Row
*/
protected function createRowFromArray(array $dataRow, Style $style = null)
{
$row = EntityFactory::createRow(array_map(function ($value) {
if ($value instanceof Cell) {
return $value;
}
return new Cell($value);
}, $dataRow));
if ($style !== null) {
$row->setStyle($style);
}
return $row;
}
/** /**
* @TODO: Move this into styleMerger * @TODO: Move this into styleMerger
* *
@ -265,6 +236,7 @@ abstract class WriterAbstract implements WriterInterface
if ($defaultRowStyle === null) { if ($defaultRowStyle === null) {
return $this; return $this;
} }
$mergedStyle = $this->styleMerger->merge($row->getStyle(), $defaultRowStyle); $mergedStyle = $this->styleMerger->merge($row->getStyle(), $defaultRowStyle);
$row->setStyle($mergedStyle); $row->setStyle($mergedStyle);
} }
@ -273,7 +245,6 @@ abstract class WriterAbstract implements WriterInterface
* Closes the writer. This will close the streamer as well, preventing new data * Closes the writer. This will close the streamer as well, preventing new data
* to be written to the file. * to be written to the file.
* *
* @api
* @return void * @return void
*/ */
public function close() public function close()

View File

@ -37,14 +37,6 @@ interface WriterInterface
*/ */
public function addRow(Row $row); public function addRow(Row $row);
/**
* Write given data to the output with a closure function. New data will be appended to the end of the stream.
*
* @param \Closure $callback A callback returning a Row object. A new Row object is injected into the callback.
* @return WriterInterface
*/
public function withRow(\Closure $callback);
/** /**
* Write a given array of rows to the output. New data will be appended to the end of the stream. * Write a given array of rows to the output. New data will be appended to the end of the stream.
* *

View File

@ -127,7 +127,6 @@ EOD;
* @throws IOException If the data cannot be written * @throws IOException If the data cannot be written
* @throws InvalidArgumentException If a cell value's type is not supported * @throws InvalidArgumentException If a cell value's type is not supported
* @return void * @return void
* @return void
*/ */
public function addRow(Worksheet $worksheet, Row $row) public function addRow(Worksheet $worksheet, Row $row)
{ {
@ -145,8 +144,6 @@ EOD;
* @throws \Box\Spout\Common\Exception\IOException If the data cannot be written * @throws \Box\Spout\Common\Exception\IOException If the data cannot be written
* @throws \Box\Spout\Common\Exception\InvalidArgumentException If a cell value's type is not supported * @throws \Box\Spout\Common\Exception\InvalidArgumentException If a cell value's type is not supported
* @return void * @return void
*
* @return void
*/ */
private function addNonEmptyRow(Worksheet $worksheet, Row $row) private function addNonEmptyRow(Worksheet $worksheet, Row $row)
{ {

View File

@ -3,6 +3,7 @@
namespace Box\Spout\Reader\XLSX\Helper; namespace Box\Spout\Reader\XLSX\Helper;
use Box\Spout\Common\Helper\Escaper; use Box\Spout\Common\Helper\Escaper;
use Box\Spout\Reader\XLSX\Manager\StyleManager;
/** /**
* Class CellValueFormatterTest * Class CellValueFormatterTest
@ -39,7 +40,7 @@ class CellValueFormatterTest extends \PHPUnit_Framework_TestCase
*/ */
public function testExcelDate($cellType, $nodeValue, $expectedDateAsString) public function testExcelDate($cellType, $nodeValue, $expectedDateAsString)
{ {
$nodeListMock = $this->getMockBuilder('DOMNodeList')->disableOriginalConstructor()->getMock(); $nodeListMock = $this->createMock('DOMNodeList');
$nodeListMock $nodeListMock
->expects($this->atLeastOnce()) ->expects($this->atLeastOnce())
@ -47,7 +48,7 @@ class CellValueFormatterTest extends \PHPUnit_Framework_TestCase
->with(0) ->with(0)
->will($this->returnValue((object) ['nodeValue' => $nodeValue])); ->will($this->returnValue((object) ['nodeValue' => $nodeValue]));
$nodeMock = $this->getMockBuilder('DOMElement')->disableOriginalConstructor()->getMock(); $nodeMock = $this->createMock('DOMElement');
$nodeMock $nodeMock
->expects($this->atLeastOnce()) ->expects($this->atLeastOnce())
@ -64,7 +65,7 @@ class CellValueFormatterTest extends \PHPUnit_Framework_TestCase
->will($this->returnValue($nodeListMock)); ->will($this->returnValue($nodeListMock));
/** @var \Box\Spout\Reader\XLSX\Manager\StyleManager|\PHPUnit_Framework_MockObject_MockObject $styleManagerMock */ /** @var \Box\Spout\Reader\XLSX\Manager\StyleManager|\PHPUnit_Framework_MockObject_MockObject $styleManagerMock */
$styleManagerMock = $this->getMockBuilder('Box\Spout\Reader\XLSX\Manager\StyleManager')->disableOriginalConstructor()->getMock(); $styleManagerMock = $this->createMock(StyleManager::class);
$styleManagerMock $styleManagerMock
->expects($this->once()) ->expects($this->once())
@ -120,7 +121,7 @@ class CellValueFormatterTest extends \PHPUnit_Framework_TestCase
public function testFormatNumericCellValueWithNumbers($value, $expectedFormattedValue, $expectedType) public function testFormatNumericCellValueWithNumbers($value, $expectedFormattedValue, $expectedType)
{ {
/** @var \Box\Spout\Reader\XLSX\Manager\StyleManager|\PHPUnit_Framework_MockObject_MockObject $styleManagerMock */ /** @var \Box\Spout\Reader\XLSX\Manager\StyleManager|\PHPUnit_Framework_MockObject_MockObject $styleManagerMock */
$styleManagerMock = $this->getMockBuilder('Box\Spout\Reader\XLSX\Manager\StyleManager')->disableOriginalConstructor()->getMock(); $styleManagerMock = $this->createMock(StyleManager::class);
$styleManagerMock $styleManagerMock
->expects($this->once()) ->expects($this->once())
->method('shouldFormatNumericValueAsDate') ->method('shouldFormatNumericValueAsDate')
@ -155,14 +156,14 @@ class CellValueFormatterTest extends \PHPUnit_Framework_TestCase
*/ */
public function testFormatInlineStringCellValue($value, $expectedFormattedValue) public function testFormatInlineStringCellValue($value, $expectedFormattedValue)
{ {
$nodeListMock = $this->getMockBuilder('DOMNodeList')->disableOriginalConstructor()->getMock(); $nodeListMock = $this->createMock('DOMNodeList');
$nodeListMock $nodeListMock
->expects($this->atLeastOnce()) ->expects($this->atLeastOnce())
->method('item') ->method('item')
->with(0) ->with(0)
->will($this->returnValue((object) ['nodeValue' => $value])); ->will($this->returnValue((object) ['nodeValue' => $value]));
$nodeMock = $this->getMockBuilder('DOMElement')->disableOriginalConstructor()->getMock(); $nodeMock = $this->createMock('DOMElement');
$nodeMock $nodeMock
->expects($this->atLeastOnce()) ->expects($this->atLeastOnce())
->method('getElementsByTagName') ->method('getElementsByTagName')

View File

@ -6,7 +6,6 @@ use Box\Spout\Common\Helper\EncodingHelper;
use Box\Spout\Common\Type; use Box\Spout\Common\Type;
use Box\Spout\TestUsingResource; use Box\Spout\TestUsingResource;
use Box\Spout\Writer\Common\Creator\EntityFactory; use Box\Spout\Writer\Common\Creator\EntityFactory;
use Box\Spout\Writer\Common\Entity\Cell;
use Box\Spout\Writer\WriterFactory; use Box\Spout\Writer\WriterFactory;
/** /**
@ -28,8 +27,8 @@ class WriterTest extends \PHPUnit_Framework_TestCase
$writer = WriterFactory::create(Type::CSV); $writer = WriterFactory::create(Type::CSV);
@$writer->openToFile($filePath); @$writer->openToFile($filePath);
$row = EntityFactory::createRow([ $row = EntityFactory::createRow([
new Cell('csv--11'), EntityFactory::createCell('csv--11'),
new Cell('csv--12'), EntityFactory::createCell('csv--12'),
]); ]);
$writer->addRow($row); $writer->addRow($row);
$writer->close(); $writer->close();
@ -42,8 +41,8 @@ class WriterTest extends \PHPUnit_Framework_TestCase
{ {
$writer = WriterFactory::create(Type::CSV); $writer = WriterFactory::create(Type::CSV);
$row = EntityFactory::createRow([ $row = EntityFactory::createRow([
new Cell('csv--11'), EntityFactory::createCell('csv--11'),
new Cell('csv--12'), EntityFactory::createCell('csv--12'),
]); ]);
$writer->addRow($row); $writer->addRow($row);
$writer->close(); $writer->close();
@ -56,8 +55,8 @@ class WriterTest extends \PHPUnit_Framework_TestCase
{ {
$writer = WriterFactory::create(Type::CSV); $writer = WriterFactory::create(Type::CSV);
$row = EntityFactory::createRow([ $row = EntityFactory::createRow([
new Cell('csv--11'), EntityFactory::createCell('csv--11'),
new Cell('csv--12'), EntityFactory::createCell('csv--12'),
]); ]);
$writer->addRows([$row]); $writer->addRows([$row]);
$writer->close(); $writer->close();
@ -196,7 +195,7 @@ class WriterTest extends \PHPUnit_Framework_TestCase
public function testWriteShouldAcceptCellObjects() public function testWriteShouldAcceptCellObjects()
{ {
$allRows = [ $allRows = [
[new Cell('String Value'), new Cell(1)], [EntityFactory::createCell('String Value'), EntityFactory::createCell(1)],
]; ];
$writtenContent = $this->writeToCsvFileAndReturnWrittenContent($allRows, 'csv_with_cell_objects.csv'); $writtenContent = $this->writeToCsvFileAndReturnWrittenContent($allRows, 'csv_with_cell_objects.csv');
$writtenContent = $this->trimWrittenContent($writtenContent); $writtenContent = $this->trimWrittenContent($writtenContent);
@ -225,12 +224,11 @@ class WriterTest extends \PHPUnit_Framework_TestCase
$writer->openToFile($resourcePath); $writer->openToFile($resourcePath);
$writer->addRows(array_map(function ($oneRow) { $writer->addRows(array_map(function ($oneRow) {
$row = EntityFactory::createRow(array_map(function ($value) { return EntityFactory::createRow(array_map(function ($value) {
return new Cell($value); return EntityFactory::createCell($value);
}, $oneRow)); }, $oneRow));
return $row;
}, $allRows)); }, $allRows));
$writer->close(); $writer->close();
return file_get_contents($resourcePath); return file_get_contents($resourcePath);

View File

@ -2,54 +2,65 @@
namespace Box\Spout\Writer\Common\Entity; namespace Box\Spout\Writer\Common\Entity;
use Box\Spout\Writer\Common\Entity\Style\Style;
use PHPUnit\Framework\TestCase; use PHPUnit\Framework\TestCase;
class CellTest extends TestCase class CellTest extends TestCase
{ {
protected function styleMock() /**
{ * @return void
$styleMock = $this */
->getMockBuilder('Box\Spout\Writer\Common\Entity\Style\Style');
return $styleMock;
}
public function testValidInstance() public function testValidInstance()
{ {
$this->assertInstanceOf('Box\Spout\Writer\Common\Entity\Cell', new Cell('cell')); $this->assertInstanceOf(Cell::class, new Cell('cell'));
$this->assertInstanceOf( $this->assertInstanceOf(Cell::class, new Cell('cell-with-style', $this->createMock(Style::class)));
'Box\Spout\Writer\Common\Entity\Cell',
new Cell('cell-with-style', $this->styleMock()->getMock())
);
} }
/**
* @return void
*/
public function testCellTypeNumeric() public function testCellTypeNumeric()
{ {
$this->assertTrue((new Cell(0))->isNumeric()); $this->assertTrue((new Cell(0))->isNumeric());
$this->assertTrue((new Cell(1))->isNumeric()); $this->assertTrue((new Cell(1))->isNumeric());
} }
/**
* @return void
*/
public function testCellTypeString() public function testCellTypeString()
{ {
$this->assertTrue((new Cell('String!'))->isString()); $this->assertTrue((new Cell('String!'))->isString());
} }
/**
* @return void
*/
public function testCellTypeEmptyString() public function testCellTypeEmptyString()
{ {
$this->assertTrue((new Cell(''))->isEmpty()); $this->assertTrue((new Cell(''))->isEmpty());
} }
/**
* @return void
*/
public function testCellTypeEmptyNull() public function testCellTypeEmptyNull()
{ {
$this->assertTrue((new Cell(null))->isEmpty()); $this->assertTrue((new Cell(null))->isEmpty());
} }
/**
* @return void
*/
public function testCellTypeBool() public function testCellTypeBool()
{ {
$this->assertTrue((new Cell(true))->isBoolean()); $this->assertTrue((new Cell(true))->isBoolean());
$this->assertTrue((new Cell(false))->isBoolean()); $this->assertTrue((new Cell(false))->isBoolean());
} }
/**
* @return void
*/
public function testCellTypeError() public function testCellTypeError()
{ {
$this->assertTrue((new Cell([]))->isError()); $this->assertTrue((new Cell([]))->isError());

View File

@ -2,88 +2,113 @@
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\Manager\RowManager;
use PHPUnit\Framework\TestCase; use PHPUnit\Framework\TestCase;
class RowTest extends TestCase class RowTest extends TestCase
{ {
protected function styleMock() /**
* @return \PHPUnit_Framework_MockObject_MockObject|Style
*/
private function getStyleMock()
{ {
$styleMock = $this return $this->createMock(Style::class);
->getMockBuilder('Box\Spout\Writer\Common\Entity\Style\Style');
return $styleMock;
} }
protected function cellMock() /**
* @return \PHPUnit_Framework_MockObject_MockObject|Cell
*/
private function getCellMock()
{ {
$cellMock = $this return $this->createMock(Cell::class);
->getMockBuilder('Box\Spout\Writer\Common\Entity\Cell')
->disableOriginalConstructor();
return $cellMock;
} }
protected function rowManagerMock() /**
* @return \PHPUnit_Framework_MockObject_MockObject|RowManager
*/
private function getRowManagerMock()
{ {
$rowManagerMock = $this return $this->createMock(RowManager::class);
->getMockBuilder('Box\Spout\Writer\Common\Manager\RowManager')
->disableOriginalConstructor();
return $rowManagerMock;
} }
/**
* @return void
*/
public function testValidInstance() public function testValidInstance()
{ {
$this->assertInstanceOf( $this->assertInstanceOf(
'Box\Spout\Writer\Common\Entity\Row', Row::class,
new Row( new Row([], null, $this->getRowManagerMock())
[],
null,
$this->rowManagerMock()->getMock()
)
); );
} }
/**
* @return void
*/
public function testSetCells() public function testSetCells()
{ {
$o = new Row([], null, $this->rowManagerMock()->getMock()); $row = new Row([], null, $this->getRowManagerMock());
$o->setCells([$this->cellMock()->getMock(), $this->cellMock()->getMock()]); $row->setCells([$this->getCellMock(), $this->getCellMock()]);
$this->assertEquals(2, count($o->getCells()));
$this->assertEquals(2, count($row->getCells()));
} }
/**
* @return void
*/
public function testSetCellsResets() public function testSetCellsResets()
{ {
$o = new Row([], null, $this->rowManagerMock()->getMock()); $row = new Row([], null, $this->getRowManagerMock());
$o->setCells([$this->cellMock()->getMock(), $this->cellMock()->getMock()]); $row->setCells([$this->getCellMock(), $this->getCellMock()]);
$this->assertEquals(2, count($o->getCells()));
$o->setCells([$this->cellMock()->getMock()]); $this->assertEquals(2, count($row->getCells()));
$this->assertEquals(1, count($o->getCells()));
$row->setCells([$this->getCellMock()]);
$this->assertEquals(1, count($row->getCells()));
} }
/**
* @return void
*/
public function testGetCells() public function testGetCells()
{ {
$o = new Row([], null, $this->rowManagerMock()->getMock()); $row = new Row([], null, $this->getRowManagerMock());
$this->assertEquals(0, count($o->getCells()));
$o->setCells([$this->cellMock()->getMock(), $this->cellMock()->getMock()]); $this->assertEquals(0, count($row->getCells()));
$this->assertEquals(2, count($o->getCells()));
$row->setCells([$this->getCellMock(), $this->getCellMock()]);
$this->assertEquals(2, count($row->getCells()));
} }
/**
* @return void
*/
public function testAddCell() public function testAddCell()
{ {
$o = new Row([], null, $this->rowManagerMock()->getMock()); $row = new Row([], null, $this->getRowManagerMock());
$o->setCells([$this->cellMock()->getMock(), $this->cellMock()->getMock()]); $row->setCells([$this->getCellMock(), $this->getCellMock()]);
$this->assertEquals(2, count($o->getCells()));
$o->addCell($this->cellMock()->getMock()); $this->assertEquals(2, count($row->getCells()));
$this->assertEquals(3, count($o->getCells()));
$row->addCell($this->getCellMock());
$this->assertEquals(3, count($row->getCells()));
} }
/**
* @return void
*/
public function testFluentInterface() public function testFluentInterface()
{ {
$o = new Row([], null, $this->rowManagerMock()->getMock()); $row = new Row([], null, $this->getRowManagerMock());
$o $row
->addCell($this->cellMock()->getMock()) ->addCell($this->getCellMock())
->setStyle($this->styleMock()->getMock()) ->setStyle($this->getStyleMock())
->setCells([]); ->setCells([]);
$this->assertTrue(is_object($o));
$this->assertTrue(is_object($row));
} }
} }

View File

@ -0,0 +1,28 @@
<?php
namespace Spout\Writer\Common\Manager;
use Box\Spout\Writer\Common\Creator\Style\StyleBuilder;
use Box\Spout\Writer\Common\Entity\Cell;
use Box\Spout\Writer\Common\Manager\CellManager;
use Box\Spout\Writer\Common\Manager\Style\StyleMerger;
use PHPUnit\Framework\TestCase;
class CellManagerTest extends TestCase
{
/**
* @return void
*/
public function testApplyStyle()
{
$cellManager = new CellManager(new StyleMerger());
$cell = new Cell('test');
$this->assertFalse($cell->getStyle()->isFontBold());
$style = (new StyleBuilder())->setFontBold()->build();
$cellManager->applyStyle($cell, $style);
$this->assertTrue($cell->getStyle()->isFontBold());
}
}

View File

@ -2,6 +2,7 @@
namespace Spout\Writer\Common\Manager; namespace Spout\Writer\Common\Manager;
use Box\Spout\Writer\Common\Creator\Style\StyleBuilder;
use Box\Spout\Writer\Common\Entity\Cell; use Box\Spout\Writer\Common\Entity\Cell;
use Box\Spout\Writer\Common\Entity\Row; use Box\Spout\Writer\Common\Entity\Row;
use Box\Spout\Writer\Common\Manager\RowManager; use Box\Spout\Writer\Common\Manager\RowManager;
@ -11,31 +12,75 @@ use PHPUnit\Framework\TestCase;
class RowManagerTest extends TestCase class RowManagerTest extends TestCase
{ {
/** /**
* @var RowManager * @return void
*/ */
protected $rowManager; public function testApplyStyle()
public function setUp()
{ {
$this->rowManager = new RowManager(new StyleMerger()); $rowManager = new RowManager(new StyleMerger());
parent::setUp(); $row = new Row([new Cell('test')], null, $rowManager);
$this->assertFalse($row->getStyle()->isFontBold());
$style = (new StyleBuilder())->setFontBold()->build();
$rowManager->applyStyle($row, $style);
$this->assertTrue($row->getStyle()->isFontBold());
} }
public function testIsEmptyRow() /**
* @return array
*/
public function dataProviderForTestHasCells()
{ {
$row = new Row([], null, $this->rowManager); return [
$this->assertTrue($this->rowManager->isEmpty($row)); // cells, expected hasCells
[[], false],
[[new Cell('')], true],
[[new Cell(null)], true],
[[new Cell('test')], true],
];
}
$row = new Row([ /**
new Cell(''), * @dataProvider dataProviderForTestHasCells
], null, $this->rowManager); *
$this->assertTrue($this->rowManager->isEmpty($row)); * @param array $cells
* @param bool $expectedHasCells
* @return void
*/
public function testHasCells(array $cells, $expectedHasCells)
{
$rowManager = new RowManager(new StyleMerger());
$row = new Row([ $row = new Row($cells, null, $rowManager);
new Cell(''), $this->assertEquals($expectedHasCells, $rowManager->hasCells($row));
new Cell(''), }
new Cell('Okay'),
], null, $this->rowManager); /**
$this->assertFalse($this->rowManager->isEmpty($row)); * @return array
*/
public function dataProviderForTestIsEmptyRow()
{
return [
// cells, expected isEmpty
[[], true],
[[new Cell('')], true],
[[new Cell(''), new Cell(''), new Cell('Okay')], false],
];
}
/**
* @dataProvider dataProviderForTestIsEmptyRow
*
* @param array $cells
* @param bool $expectedIsEmpty
* @return void
*/
public function testIsEmptyRow(array $cells, $expectedIsEmpty)
{
$rowManager = new RowManager(new StyleMerger());
$row = new Row($cells, null, $rowManager);
$this->assertEquals($expectedIsEmpty, $rowManager->isEmpty($row));
} }
} }

View File

@ -5,7 +5,6 @@ namespace Box\Spout\Writer\ODS;
use Box\Spout\Common\Type; use Box\Spout\Common\Type;
use Box\Spout\TestUsingResource; use Box\Spout\TestUsingResource;
use Box\Spout\Writer\Common\Creator\EntityFactory; use Box\Spout\Writer\Common\Creator\EntityFactory;
use Box\Spout\Writer\Common\Entity\Cell;
use Box\Spout\Writer\Common\Entity\Sheet; use Box\Spout\Writer\Common\Entity\Sheet;
use Box\Spout\Writer\WriterFactory; use Box\Spout\Writer\WriterFactory;
@ -94,8 +93,8 @@ class SheetTest extends \PHPUnit_Framework_TestCase
$sheet->setName($sheetName); $sheet->setName($sheetName);
$row = EntityFactory::createRow([ $row = EntityFactory::createRow([
new Cell('ods--11'), EntityFactory::createCell('ods--11'),
new Cell('ods--12'), EntityFactory::createCell('ods--12'),
]); ]);
$writer->addRow($row); $writer->addRow($row);
$writer->close(); $writer->close();
@ -115,17 +114,17 @@ class SheetTest extends \PHPUnit_Framework_TestCase
$writer->openToFile($resourcePath); $writer->openToFile($resourcePath);
$row = EntityFactory::createRow([ $row = EntityFactory::createRow([
new Cell('ods--sheet1--11'), EntityFactory::createCell('ods--sheet1--11'),
new Cell('ods--sheet1--12'), EntityFactory::createCell('ods--sheet1--12'),
]); ]);
$writer->addRow($row); $writer->addRow($row);
$writer->addNewSheetAndMakeItCurrent(); $writer->addNewSheetAndMakeItCurrent();
$row = EntityFactory::createRow([ $row = EntityFactory::createRow([
new Cell('ods--sheet2--11'), EntityFactory::createCell('ods--sheet2--11'),
new Cell('ods--sheet2--12'), EntityFactory::createCell('ods--sheet2--12'),
new Cell('ods--sheet2--13'), EntityFactory::createCell('ods--sheet2--13'),
]); ]);
$writer->addRow($row); $writer->addRow($row);

View File

@ -52,8 +52,8 @@ class WriterTest extends \PHPUnit_Framework_TestCase
{ {
$writer = WriterFactory::create(Type::ODS); $writer = WriterFactory::create(Type::ODS);
$row = $this->entityFactory->createRow([ $row = $this->entityFactory->createRow([
new Cell('csv--11'), EntityFactory::createCell('csv--11'),
new Cell('csv--12'), EntityFactory::createCell('csv--12'),
]); ]);
$writer->addRow($row); $writer->addRow($row);
} }
@ -65,8 +65,8 @@ class WriterTest extends \PHPUnit_Framework_TestCase
{ {
$writer = WriterFactory::create(Type::ODS); $writer = WriterFactory::create(Type::ODS);
$row = $this->entityFactory->createRow([ $row = $this->entityFactory->createRow([
new Cell('csv--11'), EntityFactory::createCell('csv--11'),
new Cell('csv--12'), EntityFactory::createCell('csv--12'),
]); ]);
$writer->addRows([$row]); $writer->addRows([$row]);
} }
@ -339,11 +339,9 @@ class WriterTest extends \PHPUnit_Framework_TestCase
{ {
$arrayToRows = function (array $allRows) { $arrayToRows = function (array $allRows) {
return array_map(function ($oneRow) { return array_map(function ($oneRow) {
$row = $this->entityFactory->createRow(array_map(function ($value) { return $this->entityFactory->createRow(array_map(function ($value) {
return new Cell($value); return EntityFactory::createCell($value);
}, $oneRow)); }, $oneRow));
return $row;
}, $allRows); }, $allRows);
}; };
@ -505,8 +503,8 @@ class WriterTest extends \PHPUnit_Framework_TestCase
{ {
$fileName = 'test_writer_should_accept_cell_objects.ods'; $fileName = 'test_writer_should_accept_cell_objects.ods';
$dataRows = [ $dataRows = [
[new Cell('ods--11'), new Cell('ods--12')], [EntityFactory::createCell('ods--11'), EntityFactory::createCell('ods--12')],
[new Cell('ods--21'), new Cell('ods--22'), new Cell('ods--23')], [EntityFactory::createCell('ods--21'), EntityFactory::createCell('ods--22'), EntityFactory::createCell('ods--23')],
]; ];
$this->writeToODSFile($dataRows, $fileName); $this->writeToODSFile($dataRows, $fileName);
@ -526,7 +524,7 @@ class WriterTest extends \PHPUnit_Framework_TestCase
{ {
$fileName = 'test_writer_should_accept_cell_objects_with_types.ods'; $fileName = 'test_writer_should_accept_cell_objects_with_types.ods';
$dataRows = [ $dataRows = [
[new Cell('i am a string'), new Cell(51465), new Cell(true), new Cell(51465.5)], [EntityFactory::createCell('i am a string'), EntityFactory::createCell(51465), EntityFactory::createCell(true), EntityFactory::createCell(51465.5)],
]; ];
$this->writeToODSFile($dataRows, $fileName); $this->writeToODSFile($dataRows, $fileName);
@ -556,15 +554,14 @@ class WriterTest extends \PHPUnit_Framework_TestCase
$writer->openToFile($resourcePath); $writer->openToFile($resourcePath);
$writer->addRows(array_map(function ($oneRow) { $writer->addRows(array_map(function ($oneRow) {
$row = $this->entityFactory->createRow(array_map(function ($value) { return $this->entityFactory->createRow(array_map(function ($value) {
// @TODO: always pass a Cell instance!
if (!$value instanceof Cell) { if (!$value instanceof Cell) {
return new Cell($value); return EntityFactory::createCell($value);
} else { } else {
return $value; return $value;
} }
}, $oneRow)); }, $oneRow));
return $row;
}, $allRows)); }, $allRows));
$writer->close(); $writer->close();
@ -589,21 +586,17 @@ class WriterTest extends \PHPUnit_Framework_TestCase
$writer->openToFile($resourcePath); $writer->openToFile($resourcePath);
$writer->addRows(array_map(function ($oneRow) { $writer->addRows(array_map(function ($oneRow) {
$row = $this->entityFactory->createRow(array_map(function ($value) { return $this->entityFactory->createRow(array_map(function ($value) {
return new Cell($value); return EntityFactory::createCell($value);
}, $oneRow)); }, $oneRow));
return $row;
}, $allRows)); }, $allRows));
for ($i = 1; $i < $numSheets; $i++) { for ($i = 1; $i < $numSheets; $i++) {
$writer->addNewSheetAndMakeItCurrent(); $writer->addNewSheetAndMakeItCurrent();
$writer->addRows(array_map(function ($oneRow) { $writer->addRows(array_map(function ($oneRow) {
$row = $this->entityFactory->createRow(array_map(function ($value) { return $this->entityFactory->createRow(array_map(function ($value) {
return new Cell($value); return EntityFactory::createCell($value);
}, $oneRow)); }, $oneRow));
return $row;
}, $allRows)); }, $allRows));
} }

View File

@ -39,8 +39,8 @@ class WriterWithStyleTest extends \PHPUnit_Framework_TestCase
{ {
$writer = WriterFactory::create(Type::ODS); $writer = WriterFactory::create(Type::ODS);
$row = EntityFactory::createRow([ $row = EntityFactory::createRow([
new Cell('ods--11'), EntityFactory::createCell('ods--11'),
new Cell('ods--12'), EntityFactory::createCell('ods--12'),
], $this->defaultStyle); ], $this->defaultStyle);
$writer->addRow($row); $writer->addRow($row);
} }
@ -52,8 +52,8 @@ class WriterWithStyleTest extends \PHPUnit_Framework_TestCase
{ {
$writer = WriterFactory::create(Type::ODS); $writer = WriterFactory::create(Type::ODS);
$row = EntityFactory::createRow([ $row = EntityFactory::createRow([
new Cell('ods--11'), EntityFactory::createCell('ods--11'),
new Cell('ods--12'), EntityFactory::createCell('ods--12'),
], $this->defaultStyle); ], $this->defaultStyle);
$writer->addRows([$row]); $writer->addRows([$row]);
} }
@ -70,17 +70,14 @@ class WriterWithStyleTest extends \PHPUnit_Framework_TestCase
} }
/** /**
* @requires PHP 7
* @dataProvider dataProviderForInvalidStyle * @dataProvider dataProviderForInvalidStyle
* *
* @param \Box\Spout\Writer\Common\Entity\Style\Style $style * @param \Box\Spout\Writer\Common\Entity\Style\Style $style
*/ */
public function testAddRowWithStyleShouldThrowExceptionIfInvalidStyleGiven($style) public function testAddRowWithStyleShouldThrowExceptionIfInvalidStyleGiven($style)
{ {
if (version_compare(PHP_VERSION, '7.0.0') >= 0) {
$this->expectException(\TypeError::class); $this->expectException(\TypeError::class);
} else {
$this->markTestSkipped('PHP > 7.0 only');
}
$fileName = 'test_add_row_with_style_should_throw_exception.ods'; $fileName = 'test_add_row_with_style_should_throw_exception.ods';
$this->createGeneratedFolderIfNeeded($fileName); $this->createGeneratedFolderIfNeeded($fileName);
@ -89,24 +86,21 @@ class WriterWithStyleTest extends \PHPUnit_Framework_TestCase
$writer = WriterFactory::create(Type::ODS); $writer = WriterFactory::create(Type::ODS);
$writer->openToFile($resourcePath); $writer->openToFile($resourcePath);
$row = EntityFactory::createRow([ $row = EntityFactory::createRow([
new Cell('ods--11'), EntityFactory::createCell('ods--11'),
new Cell('ods--12'), EntityFactory::createCell('ods--12'),
], $style); ], $style);
$writer->addRow($row); $writer->addRow($row);
} }
/** /**
* @requires PHP 7
* @dataProvider dataProviderForInvalidStyle * @dataProvider dataProviderForInvalidStyle
* *
* @param \Box\Spout\Writer\Common\Entity\Style\Style $style * @param \Box\Spout\Writer\Common\Entity\Style\Style $style
*/ */
public function testAddRowsWithStyleShouldThrowExceptionIfInvalidStyleGiven($style) public function testAddRowsWithStyleShouldThrowExceptionIfInvalidStyleGiven($style)
{ {
if (version_compare(PHP_VERSION, '7.0.0') >= 0) {
$this->expectException(\TypeError::class); $this->expectException(\TypeError::class);
} else {
$this->markTestSkipped('PHP > 7.0 only');
}
$fileName = 'test_add_row_with_style_should_throw_exception.ods'; $fileName = 'test_add_row_with_style_should_throw_exception.ods';
$this->createGeneratedFolderIfNeeded($fileName); $this->createGeneratedFolderIfNeeded($fileName);
@ -115,8 +109,8 @@ class WriterWithStyleTest extends \PHPUnit_Framework_TestCase
$writer = WriterFactory::create(Type::ODS); $writer = WriterFactory::create(Type::ODS);
$writer->openToFile($resourcePath); $writer->openToFile($resourcePath);
$row = EntityFactory::createRow([ $row = EntityFactory::createRow([
new Cell('ods--11'), EntityFactory::createCell('ods--11'),
new Cell('ods--12'), EntityFactory::createCell('ods--12'),
], $style); ], $style);
$writer->addRows([[$row]]); $writer->addRows([[$row]]);
} }
@ -356,7 +350,7 @@ class WriterWithStyleTest extends \PHPUnit_Framework_TestCase
{ {
$fileName = 'test_set_default_row_style.ods'; $fileName = 'test_set_default_row_style.ods';
$row = EntityFactory::createRow([ $row = EntityFactory::createRow([
new Cell('ods--11'), EntityFactory::createCell('ods--11'),
]); ]);
$dataRows = [$row]; $dataRows = [$row];
@ -379,11 +373,9 @@ class WriterWithStyleTest extends \PHPUnit_Framework_TestCase
{ {
$arrayToRows = function (array $allRows) use ($style) { $arrayToRows = function (array $allRows) use ($style) {
return array_map(function ($oneRow) use ($style) { return array_map(function ($oneRow) use ($style) {
$row = EntityFactory::createRow(array_map(function ($value) { return EntityFactory::createRow(array_map(function ($value) {
return new Cell($value); return EntityFactory::createCell($value);
}, $oneRow), $style); }, $oneRow), $style);
return $row;
}, $allRows); }, $allRows);
}; };
@ -444,7 +436,7 @@ class WriterWithStyleTest extends \PHPUnit_Framework_TestCase
$currentRow = $allRows[$i]; $currentRow = $allRows[$i];
$currentStyle = $styles[$i]; $currentStyle = $styles[$i];
$row = EntityFactory::createRow(array_map(function ($value) { $row = EntityFactory::createRow(array_map(function ($value) {
return new Cell($value); return EntityFactory::createCell($value);
}, $currentRow), $currentStyle); }, $currentRow), $currentStyle);
$writer->addRow($row); $writer->addRow($row);
} }

View File

@ -5,7 +5,6 @@ namespace Box\Spout\Writer\XLSX;
use Box\Spout\Common\Type; use Box\Spout\Common\Type;
use Box\Spout\TestUsingResource; use Box\Spout\TestUsingResource;
use Box\Spout\Writer\Common\Creator\EntityFactory; use Box\Spout\Writer\Common\Creator\EntityFactory;
use Box\Spout\Writer\Common\Entity\Cell;
use Box\Spout\Writer\Common\Entity\Sheet; use Box\Spout\Writer\Common\Entity\Sheet;
use Box\Spout\Writer\WriterFactory; use Box\Spout\Writer\WriterFactory;
@ -94,8 +93,8 @@ class SheetTest extends \PHPUnit_Framework_TestCase
$sheet->setName($sheetName); $sheet->setName($sheetName);
$row = EntityFactory::createRow([ $row = EntityFactory::createRow([
new Cell('xlsx--11'), EntityFactory::createCell('xlsx--11'),
new Cell('xlsx--12'), EntityFactory::createCell('xlsx--12'),
]); ]);
$writer->addRow($row); $writer->addRow($row);
$writer->close(); $writer->close();
@ -117,17 +116,17 @@ class SheetTest extends \PHPUnit_Framework_TestCase
$writer->openToFile($resourcePath); $writer->openToFile($resourcePath);
$row = EntityFactory::createRow([ $row = EntityFactory::createRow([
new Cell('xlsx--sheet1--11'), EntityFactory::createCell('xlsx--sheet1--11'),
new Cell('xlsx--sheet1--12'), EntityFactory::createCell('xlsx--sheet1--12'),
]); ]);
$writer->addRow($row); $writer->addRow($row);
$writer->addNewSheetAndMakeItCurrent(); $writer->addNewSheetAndMakeItCurrent();
$row = EntityFactory::createRow([ $row = EntityFactory::createRow([
new Cell('xlsx--sheet2--11'), EntityFactory::createCell('xlsx--sheet2--11'),
new Cell('xlsx--sheet2--12'), EntityFactory::createCell('xlsx--sheet2--12'),
new Cell('xlsx--sheet2--13'), EntityFactory::createCell('xlsx--sheet2--13'),
]); ]);
$writer->addRow($row); $writer->addRow($row);

View File

@ -51,9 +51,9 @@ class WriterTest extends \PHPUnit_Framework_TestCase
{ {
$writer = WriterFactory::create(Type::XLSX); $writer = WriterFactory::create(Type::XLSX);
$row = $this->entityFactory->createRow([ $row = EntityFactory::createRow([
new Cell('xlsx--11'), EntityFactory::createCell('xlsx--11'),
new Cell('xlsx--12'), EntityFactory::createCell('xlsx--12'),
]); ]);
$writer->addRow($row); $writer->addRow($row);
} }
@ -64,9 +64,9 @@ class WriterTest extends \PHPUnit_Framework_TestCase
public function testAddRowShouldThrowExceptionIfCalledBeforeOpeningWriter() public function testAddRowShouldThrowExceptionIfCalledBeforeOpeningWriter()
{ {
$writer = WriterFactory::create(Type::XLSX); $writer = WriterFactory::create(Type::XLSX);
$row = $this->entityFactory->createRow([ $row = EntityFactory::createRow([
new Cell('xlsx--11'), EntityFactory::createCell('xlsx--11'),
new Cell('xlsx--12'), EntityFactory::createCell('xlsx--12'),
]); ]);
$writer->addRows([$row]); $writer->addRows([$row]);
} }
@ -344,7 +344,6 @@ class WriterTest extends \PHPUnit_Framework_TestCase
*/ */
public function testAddRowShouldNotWriteEmptyRows() public function testAddRowShouldNotWriteEmptyRows()
{ {
$this->markTestIncomplete('Unsure why this does not pass');
$fileName = 'test_add_row_should_not_write_empty_rows.xlsx'; $fileName = 'test_add_row_should_not_write_empty_rows.xlsx';
$dataRows = [ $dataRows = [
[''], [''],
@ -388,11 +387,9 @@ class WriterTest extends \PHPUnit_Framework_TestCase
{ {
$arrayToRows = function (array $allRows) { $arrayToRows = function (array $allRows) {
return array_map(function ($oneRow) { return array_map(function ($oneRow) {
$row = $this->entityFactory->createRow(array_map(function ($value) { return EntityFactory::createRow(array_map(function ($value) {
return new Cell($value); return EntityFactory::createCell($value);
}, $oneRow)); }, $oneRow));
return $row;
}, $allRows); }, $allRows);
}; };
@ -548,8 +545,8 @@ class WriterTest extends \PHPUnit_Framework_TestCase
{ {
$fileName = 'test_writer_should_accept_cell_objects.xlsx'; $fileName = 'test_writer_should_accept_cell_objects.xlsx';
$dataRows = [ $dataRows = [
[new Cell('xlsx--11'), new Cell('xlsx--12')], [EntityFactory::createCell('xlsx--11'), EntityFactory::createCell('xlsx--12')],
[new Cell('xlsx--21'), new Cell('xlsx--22'), new Cell('xlsx--23')], [EntityFactory::createCell('xlsx--21'), EntityFactory::createCell('xlsx--22'), EntityFactory::createCell('xlsx--23')],
]; ];
$this->writeToXLSXFile($dataRows, $fileName, $shouldUseInlineStrings = false); $this->writeToXLSXFile($dataRows, $fileName, $shouldUseInlineStrings = false);
@ -570,10 +567,10 @@ class WriterTest extends \PHPUnit_Framework_TestCase
$fileName = 'test_writer_should_accept_cell_objects_with_types.xlsx'; $fileName = 'test_writer_should_accept_cell_objects_with_types.xlsx';
$dataRowsShared = [ $dataRowsShared = [
[new Cell('i am a string')], [EntityFactory::createCell('i am a string')],
]; ];
$dataRowsInline = [ $dataRowsInline = [
[new Cell(51465), new Cell(true), new Cell(51465.5)], [EntityFactory::createCell(51465), EntityFactory::createCell(true), EntityFactory::createCell(51465.5)],
]; ];
$dataRows = array_merge($dataRowsShared, $dataRowsInline); $dataRows = array_merge($dataRowsShared, $dataRowsInline);
@ -614,15 +611,13 @@ class WriterTest extends \PHPUnit_Framework_TestCase
$writer->openToFile($resourcePath); $writer->openToFile($resourcePath);
$writer->addRows(array_map(function ($oneRow) { $writer->addRows(array_map(function ($oneRow) {
$row = $this->entityFactory->createRow(array_map(function ($value) { return EntityFactory::createRow(array_map(function ($value) {
if (!$value instanceof Cell) { if (!$value instanceof Cell) {
return new Cell($value); return EntityFactory::createCell($value);
} else { } else {
return $value; return $value;
} }
}, $oneRow)); }, $oneRow));
return $row;
}, $allRows)); }, $allRows));
$writer->close(); $writer->close();
@ -649,21 +644,17 @@ class WriterTest extends \PHPUnit_Framework_TestCase
$writer->openToFile($resourcePath); $writer->openToFile($resourcePath);
$writer->addRows(array_map(function ($oneRow) { $writer->addRows(array_map(function ($oneRow) {
$row = $this->entityFactory->createRow(array_map(function ($value) { return EntityFactory::createRow(array_map(function ($value) {
return new Cell($value); return EntityFactory::createCell($value);
}, $oneRow)); }, $oneRow));
return $row;
}, $allRows)); }, $allRows));
for ($i = 1; $i < $numSheets; $i++) { for ($i = 1; $i < $numSheets; $i++) {
$writer->addNewSheetAndMakeItCurrent(); $writer->addNewSheetAndMakeItCurrent();
$writer->addRows(array_map(function ($oneRow) { $writer->addRows(array_map(function ($oneRow) {
$row = $this->entityFactory->createRow(array_map(function ($value) { return EntityFactory::createRow(array_map(function ($value) {
return new Cell($value); return EntityFactory::createCell($value);
}, $oneRow)); }, $oneRow));
return $row;
}, $allRows)); }, $allRows));
} }

View File

@ -41,8 +41,8 @@ class WriterWithStyleTest extends \PHPUnit_Framework_TestCase
{ {
$writer = WriterFactory::create(Type::XLSX); $writer = WriterFactory::create(Type::XLSX);
$row = EntityFactory::createRow([ $row = EntityFactory::createRow([
new Cell('xlsx--11'), EntityFactory::createCell('xlsx--11'),
new Cell('xlsx--12'), EntityFactory::createCell('xlsx--12'),
], $this->defaultStyle); ], $this->defaultStyle);
$writer->addRow($row); $writer->addRow($row);
} }
@ -54,8 +54,8 @@ class WriterWithStyleTest extends \PHPUnit_Framework_TestCase
{ {
$writer = WriterFactory::create(Type::XLSX); $writer = WriterFactory::create(Type::XLSX);
$row = EntityFactory::createRow([ $row = EntityFactory::createRow([
new Cell('xlsx--11'), EntityFactory::createCell('xlsx--11'),
new Cell('xlsx--12'), EntityFactory::createCell('xlsx--12'),
], $this->defaultStyle); ], $this->defaultStyle);
$writer->addRows([$row]); $writer->addRows([$row]);
} }
@ -72,17 +72,14 @@ class WriterWithStyleTest extends \PHPUnit_Framework_TestCase
} }
/** /**
* @requires PHP 7
* @dataProvider dataProviderForInvalidStyle * @dataProvider dataProviderForInvalidStyle
* *
* @param \Box\Spout\Writer\Common\Entity\Style\Style $style * @param \Box\Spout\Writer\Common\Entity\Style\Style $style
*/ */
public function testAddRowWithStyleShouldThrowExceptionIfInvalidStyleGiven($style) public function testAddRowWithStyleShouldThrowExceptionIfInvalidStyleGiven($style)
{ {
if (version_compare(PHP_VERSION, '7.0.0') >= 0) {
$this->expectException(\TypeError::class); $this->expectException(\TypeError::class);
} else {
$this->markTestSkipped('PHP > 7.0 only');
}
$fileName = 'test_add_row_with_style_should_throw_exception.xlsx'; $fileName = 'test_add_row_with_style_should_throw_exception.xlsx';
$this->createGeneratedFolderIfNeeded($fileName); $this->createGeneratedFolderIfNeeded($fileName);
@ -91,24 +88,21 @@ class WriterWithStyleTest extends \PHPUnit_Framework_TestCase
$writer = WriterFactory::create(Type::XLSX); $writer = WriterFactory::create(Type::XLSX);
$writer->openToFile($resourcePath); $writer->openToFile($resourcePath);
$row = EntityFactory::createRow([ $row = EntityFactory::createRow([
new Cell('xlsx--11'), EntityFactory::createCell('xlsx--11'),
new Cell('xlsx--12'), EntityFactory::createCell('xlsx--12'),
], $style); ], $style);
$writer->addRow($row); $writer->addRow($row);
} }
/** /**
* @requires PHP 7
* @dataProvider dataProviderForInvalidStyle * @dataProvider dataProviderForInvalidStyle
* *
* @param \Box\Spout\Writer\Common\Entity\Style\Style $style * @param \Box\Spout\Writer\Common\Entity\Style\Style $style
*/ */
public function testAddRowsWithStyleShouldThrowExceptionIfInvalidStyleGiven($style) public function testAddRowsWithStyleShouldThrowExceptionIfInvalidStyleGiven($style)
{ {
if (version_compare(PHP_VERSION, '7.0.0') >= 0) {
$this->expectException(\TypeError::class); $this->expectException(\TypeError::class);
} else {
$this->markTestSkipped('PHP > 7.0 only');
}
$fileName = 'test_add_row_with_style_should_throw_exception.xlsx'; $fileName = 'test_add_row_with_style_should_throw_exception.xlsx';
$this->createGeneratedFolderIfNeeded($fileName); $this->createGeneratedFolderIfNeeded($fileName);
@ -117,8 +111,8 @@ class WriterWithStyleTest extends \PHPUnit_Framework_TestCase
$writer = WriterFactory::create(Type::XLSX); $writer = WriterFactory::create(Type::XLSX);
$writer->openToFile($resourcePath); $writer->openToFile($resourcePath);
$row = EntityFactory::createRow([ $row = EntityFactory::createRow([
new Cell('xlsx--11'), EntityFactory::createCell('xlsx--11'),
new Cell('xlsx--12'), EntityFactory::createCell('xlsx--12'),
], $style); ], $style);
$writer->addRows([$row]); $writer->addRows([$row]);
} }
@ -458,7 +452,7 @@ class WriterWithStyleTest extends \PHPUnit_Framework_TestCase
$fileName = 'test_set_default_row_style.xlsx'; $fileName = 'test_set_default_row_style.xlsx';
$row = EntityFactory::createRow([ $row = EntityFactory::createRow([
new Cell('xlsx--11'), EntityFactory::createCell('xlsx--11'),
]); ]);
$dataRows = [$row]; $dataRows = [$row];
@ -554,11 +548,9 @@ class WriterWithStyleTest extends \PHPUnit_Framework_TestCase
{ {
$arrayToRows = function (array $allRows) use ($style) { $arrayToRows = function (array $allRows) use ($style) {
return array_map(function ($oneRow) use ($style) { return array_map(function ($oneRow) use ($style) {
$row = EntityFactory::createRow(array_map(function ($value) { return EntityFactory::createRow(array_map(function ($value) {
return new Cell($value); return EntityFactory::createCell($value);
}, $oneRow), $style); }, $oneRow), $style);
return $row;
}, $allRows); }, $allRows);
}; };
@ -622,7 +614,7 @@ class WriterWithStyleTest extends \PHPUnit_Framework_TestCase
$currentRow = $allRows[$i]; $currentRow = $allRows[$i];
$currentStyle = $styles[$i]; $currentStyle = $styles[$i];
$row = EntityFactory::createRow(array_map(function ($value) { $row = EntityFactory::createRow(array_map(function ($value) {
return new Cell($value); return EntityFactory::createCell($value);
}, $currentRow), $currentStyle); }, $currentRow), $currentStyle);
$writer->addRow($row); $writer->addRow($row);
} }