introduced row objects

This commit is contained in:
madflow 2017-05-24 18:42:37 +02:00
parent 4d6437fa77
commit 33c28dbf6c
4 changed files with 244 additions and 1 deletions

View File

@ -3,6 +3,7 @@
namespace Box\Spout\Writer\Common\Entity; namespace Box\Spout\Writer\Common\Entity;
use Box\Spout\Writer\Common\Helper\CellHelper; use Box\Spout\Writer\Common\Helper\CellHelper;
use Box\Spout\Writer\Common\Entity\Style\Style;
/** /**
* Class Cell * Class Cell
@ -54,13 +55,23 @@ class Cell
*/ */
protected $type = null; protected $type = null;
/**
* The cell style
* @var Style|null
*/
protected $style = null;
/** /**
* Cell constructor. * Cell constructor.
* @param $value mixed * @param $value mixed
* @param $style Style
*/ */
public function __construct($value) public function __construct($value, Style $style = null)
{ {
$this->setValue($value); $this->setValue($value);
if ($style) {
$this->setStyle($style);
}
} }
/** /**
@ -80,6 +91,22 @@ class Cell
return $this->value; 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 * @return int|null
*/ */

View File

@ -0,0 +1,80 @@
<?php
namespace Box\Spout\Writer\Common\Entity;
use Box\Spout\Writer\Common\Entity\Style\Style;
class Row
{
/**
* The cells in this row
* @var array
*/
protected $cells = [];
/**
* The row style
* @var null|Style
*/
protected $style = null;
/**
* Row constructor.
* @param array $cells
* @param Style|null $style
*/
public function __construct(array $cells = [], Style $style = null)
{
$this
->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;
}
}

View File

@ -0,0 +1,56 @@
<?php
namespace Box\Spout\Writer\Common\Entity;
use PHPUnit\Framework\TestCase;
class CellTest extends TestCase
{
protected function styleMock()
{
$styleMock = $this
->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());
}
}

View File

@ -0,0 +1,80 @@
<?php
namespace Box\Spout\Writer\Common\Entity;
use PHPUnit\Framework\TestCase;
class RowTest extends TestCase
{
protected function styleMock()
{
$styleMock = $this
->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));
}
}