From 33c28dbf6c13198d589ca40ec98d3c3c974967cf Mon Sep 17 00:00:00 2001 From: madflow Date: Wed, 24 May 2017 18:42:37 +0200 Subject: [PATCH] introduced row objects --- src/Spout/Writer/Common/Entity/Cell.php | 29 ++++++- src/Spout/Writer/Common/Entity/Row.php | 80 +++++++++++++++++++ tests/Spout/Writer/Common/Entity/CellTest.php | 56 +++++++++++++ tests/Spout/Writer/Common/Entity/RowTest.php | 80 +++++++++++++++++++ 4 files changed, 244 insertions(+), 1 deletion(-) create mode 100644 src/Spout/Writer/Common/Entity/Row.php create mode 100644 tests/Spout/Writer/Common/Entity/CellTest.php create mode 100644 tests/Spout/Writer/Common/Entity/RowTest.php diff --git a/src/Spout/Writer/Common/Entity/Cell.php b/src/Spout/Writer/Common/Entity/Cell.php index ab9f2ff..fc8112f 100644 --- a/src/Spout/Writer/Common/Entity/Cell.php +++ b/src/Spout/Writer/Common/Entity/Cell.php @@ -3,6 +3,7 @@ namespace Box\Spout\Writer\Common\Entity; use Box\Spout\Writer\Common\Helper\CellHelper; +use Box\Spout\Writer\Common\Entity\Style\Style; /** * Class Cell @@ -54,13 +55,23 @@ class Cell */ protected $type = null; + /** + * The cell style + * @var Style|null + */ + protected $style = null; + /** * Cell constructor. * @param $value mixed + * @param $style Style */ - public function __construct($value) + public function __construct($value, Style $style = null) { $this->setValue($value); + if ($style) { + $this->setStyle($style); + } } /** @@ -80,6 +91,22 @@ class Cell return $this->value; } + /** + * @param Style $style + */ + public function setStyle(Style $style) + { + $this->style = $style; + } + + /** + * @return Style|null + */ + public function getStyle() + { + return $this->style; + } + /** * @return int|null */ diff --git a/src/Spout/Writer/Common/Entity/Row.php b/src/Spout/Writer/Common/Entity/Row.php new file mode 100644 index 0000000..5aa1c0c --- /dev/null +++ b/src/Spout/Writer/Common/Entity/Row.php @@ -0,0 +1,80 @@ +setCells($cells) + ->setStyle($style); + } + + /** + * @return array + */ + public function getCells() + { + return $this->cells; + } + + /** + * @param array $cells + * @return Row + */ + public function setCells($cells) + { + $this->cells = []; + foreach ($cells as $cell) { + $this->addCell($cell); + } + return $this; + } + + /** + * @return Style + */ + public function getStyle() + { + return $this->style; + } + + /** + * @param Style $style + * @return Row + */ + public function setStyle($style) + { + $this->style = $style; + return $this; + } + + /** + * @param Cell $cell + */ + public function addCell(Cell $cell) + { + $this->cells[] = $cell; + return $this; + } +} diff --git a/tests/Spout/Writer/Common/Entity/CellTest.php b/tests/Spout/Writer/Common/Entity/CellTest.php new file mode 100644 index 0000000..b5257bc --- /dev/null +++ b/tests/Spout/Writer/Common/Entity/CellTest.php @@ -0,0 +1,56 @@ +getMockBuilder('Box\Spout\Writer\Common\Entity\Style\Style'); + return $styleMock; + } + + public function testValidInstance() + { + $this->assertInstanceOf('Box\Spout\Writer\Common\Entity\Cell', new Cell('cell')); + $this->assertInstanceOf( + 'Box\Spout\Writer\Common\Entity\Cell', + new Cell('cell-with-style', $this->styleMock()->getMock()) + ); + } + + public function testCellTypeNumeric() + { + $this->assertTrue((new Cell(0))->isNumeric()); + $this->assertTrue((new Cell(1))->isNumeric()); + } + + public function testCellTypeString() + { + $this->assertTrue((new Cell('String!'))->isString()); + } + + public function testCellTypeEmptyString() + { + $this->assertTrue((new Cell(''))->isEmpty()); + } + + public function testCellTypeEmptyNull() + { + $this->assertTrue((new Cell(null))->isEmpty()); + } + + public function testCellTypeBool() + { + $this->assertTrue((new Cell(true))->isBoolean()); + $this->assertTrue((new Cell(false))->isBoolean()); + } + + public function testCellTypeError() + { + $this->assertTrue((new Cell([]))->isError()); + } +} diff --git a/tests/Spout/Writer/Common/Entity/RowTest.php b/tests/Spout/Writer/Common/Entity/RowTest.php new file mode 100644 index 0000000..3f6304d --- /dev/null +++ b/tests/Spout/Writer/Common/Entity/RowTest.php @@ -0,0 +1,80 @@ +getMockBuilder('Box\Spout\Writer\Common\Entity\Style\Style'); + return $styleMock; + } + + protected function cellMock() + { + $cellMock = $this + ->getMockBuilder('Box\Spout\Writer\Common\Entity\Cell') + ->disableOriginalConstructor(); + return $cellMock; + } + + public function testValidInstance() + { + $this->assertInstanceOf('Box\Spout\Writer\Common\Entity\Row', new Row()); + $this->assertInstanceOf('Box\Spout\Writer\Common\Entity\Row', new Row([])); + $this->assertInstanceOf('Box\Spout\Writer\Common\Entity\Row', new Row([], $this->styleMock()->getMock())); + $this->assertInstanceOf('Box\Spout\Writer\Common\Entity\Row', new Row([$this->cellMock()->getMock()])); + } + + public function testInvalidInstanceCellType() + { + $this->expectException('TypeError'); + $this->assertInstanceOf('Box\Spout\Writer\Common\Entity\Row', new Row(['string'])); + } + + public function testSetCells() + { + $o = new Row(); + $o->setCells([$this->cellMock()->getMock(), $this->cellMock()->getMock()]); + $this->assertEquals(2, count($o->getCells())); + } + + public function testSetCellsResets() + { + $o = new Row(); + $o->setCells([$this->cellMock()->getMock(), $this->cellMock()->getMock()]); + $this->assertEquals(2, count($o->getCells())); + $o->setCells([$this->cellMock()->getMock()]); + $this->assertEquals(1, count($o->getCells())); + } + + public function testGetCells() + { + $o = new Row(); + $this->assertEquals(0, count($o->getCells())); + $o->setCells([$this->cellMock()->getMock(), $this->cellMock()->getMock()]); + $this->assertEquals(2, count($o->getCells())); + } + + public function testAddCell() + { + $o = new Row(); + $o->setCells([$this->cellMock()->getMock(), $this->cellMock()->getMock()]); + $this->assertEquals(2, count($o->getCells())); + $o->addCell($this->cellMock()->getMock()); + $this->assertEquals(3, count($o->getCells())); + } + + public function testFluentInterface() + { + $o = new Row(); + $o + ->addCell($this->cellMock()->getMock()) + ->setStyle($this->styleMock()->getMock()) + ->setCells([]); + $this->assertTrue(is_object($o)); + } +}