From 4f0ee67bd2a1c9536a27871494358bc2f48284d5 Mon Sep 17 00:00:00 2001 From: DonCallisto Date: Sat, 22 Jul 2017 17:58:13 +0200 Subject: [PATCH] Added VectorTest --- Grid/Source/Source.php | 8 +- Grid/Source/Vector.php | 51 ++++--- Tests/Grid/Source/VectorTest.php | 255 +++++++++++++++++++++++++++++++ 3 files changed, 290 insertions(+), 24 deletions(-) create mode 100644 Tests/Grid/Source/VectorTest.php diff --git a/Grid/Source/Source.php b/Grid/Source/Source.php index 0e99b6eb..214dcc07 100755 --- a/Grid/Source/Source.php +++ b/Grid/Source/Source.php @@ -217,7 +217,7 @@ protected function getItemsFromData($columns) || is_callable([$itemEntity, $fullFunctionName = 'is' . $functionName])) { $fieldValue = call_user_func([$itemEntity, $fullFunctionName]); } else { - throw new PropertyAccessDeniedException(sprintf('Property "%s" is not public or has no accessor.', $fieldName)); + throw new PropertyAccessDeniedExceptio(sprintf('Property "%s" is not public or has no accessor.', $fieldName)); } } elseif (isset($item[$fieldName])) { $fieldValue = $item[$fieldName]; @@ -236,8 +236,9 @@ protected function getItemsFromData($columns) * @param \APY\DataGridBundle\Grid\Column\Column[] $columns * @param int $page * @param int $limit + * @param int $maxResults * - * @return \APY\DataGridBundle\DataGrid\Rows + * @return Rows */ public function executeFromData($columns, $page = 0, $limit = 0, $maxResults = null) { @@ -246,7 +247,6 @@ public function executeFromData($columns, $page = 0, $limit = 0, $maxResults = n $serializeColumns = []; foreach ($this->data as $key => $item) { - $keep = true; foreach ($columns as $column) { $fieldName = $column->getField(); @@ -443,7 +443,7 @@ public function executeFromData($columns, $page = 0, $limit = 0, $maxResults = n $row = new Row(); if ($this instanceof Vector) { - $row->setPrimaryField($this->id); + $row->setPrimaryField($this->getId()); } foreach ($item as $fieldName => $fieldValue) { diff --git a/Grid/Source/Vector.php b/Grid/Source/Vector.php index f3738dcb..8a06bdd4 100644 --- a/Grid/Source/Vector.php +++ b/Grid/Source/Vector.php @@ -12,7 +12,15 @@ namespace APY\DataGridBundle\Grid\Source; -use APY\DataGridBundle\Grid\Column; +use APY\DataGridBundle\Grid\Column\ArrayColumn; +use APY\DataGridBundle\Grid\Column\BooleanColumn; +use APY\DataGridBundle\Grid\Column\Column; +use APY\DataGridBundle\Grid\Column\DateColumn; +use APY\DataGridBundle\Grid\Column\DateTimeColumn; +use APY\DataGridBundle\Grid\Column\NumberColumn; +use APY\DataGridBundle\Grid\Column\TextColumn; +use APY\DataGridBundle\Grid\Column\UntypedColumn; +use APY\DataGridBundle\Grid\Rows; /** * Vector is really an Array. @@ -45,6 +53,7 @@ class Vector extends Source * Creates the Vector and sets its data. * * @param array $data + * @param array $columns */ public function __construct(array $data, array $columns = []) { @@ -78,7 +87,7 @@ protected function guessColumns() 'visible' => true, 'field' => $id, ]; - $guessedColumns[] = new Column\UntypedColumn($params); + $guessedColumns[] = new UntypedColumn($params); } } @@ -88,7 +97,7 @@ protected function guessColumns() $iteration = min(10, count($this->data)); foreach ($this->columns as $c) { - if (!$c instanceof Column\UntypedColumn) { + if (!$c instanceof UntypedColumn) { continue; } @@ -152,26 +161,26 @@ public function getColumns($columns) $token = empty($this->id); //makes the first column primary by default foreach ($this->columns as $c) { - if ($c instanceof Column\UntypedColumn) { + if ($c instanceof UntypedColumn) { switch ($c->getType()) { case 'date': - $column = new Column\DateColumn($c->getParams()); + $column = new DateColumn($c->getParams()); break; case 'datetime': - $column = new Column\DateTimeColumn($c->getParams()); + $column = new DateTimeColumn($c->getParams()); break; case 'boolean': - $column = new Column\BooleanColumn($c->getParams()); + $column = new BooleanColumn($c->getParams()); break; case 'number': - $column = new Column\NumberColumn($c->getParams()); + $column = new NumberColumn($c->getParams()); break; case 'array': - $column = new Column\ArrayColumn($c->getParams()); + $column = new ArrayColumn($c->getParams()); break; case 'text': default: - $column = new Column\TextColumn($c->getParams()); + $column = new TextColumn($c->getParams()); break; } } else { @@ -192,9 +201,10 @@ public function getColumns($columns) * @param \APY\DataGridBundle\Grid\Column\Column[] $columns * @param int $page Page Number * @param int $limit Rows Per Page + * @param int $maxResults Max rows * @param int $gridDataJunction Grid data junction * - * @return \APY\DataGridBundle\Grid\Rows + * @return Rows */ public function execute($columns, $page = 0, $limit = 0, $maxResults = null, $gridDataJunction = Column::DATA_CONJUNCTION) { @@ -226,6 +236,14 @@ public function setId($id) $this->id = $id; } + /** + * @return mixed + */ + public function getId() + { + return $this->id; + } + /** * Set a two-dimentional array. * @@ -241,12 +259,14 @@ public function setData($data) throw new \InvalidArgumentException('Data should be an array with content'); } + // This seems to exclude ... if (is_object(reset($this->data))) { foreach ($this->data as $key => $object) { $this->data[$key] = (array) $object; } } + // ... this other (or vice versa) $firstRaw = reset($this->data); if (!is_array($firstRaw) || empty($firstRaw)) { throw new \InvalidArgumentException('Data should be a two-dimentional array'); @@ -272,13 +292,4 @@ protected function hasColumn($id) return false; } - - protected function getColumn($id) - { - foreach ($this->columns as $c) { - if ($id === $c->getId()) { - return $c; - } - } - } } diff --git a/Tests/Grid/Source/VectorTest.php b/Tests/Grid/Source/VectorTest.php new file mode 100644 index 00000000..1d77d829 --- /dev/null +++ b/Tests/Grid/Source/VectorTest.php @@ -0,0 +1,255 @@ +assertAttributeEmpty('data', $this->vector); + } + + public function testRaiseExceptionDuringVectorCreationWhenDataIsNotAVector() + { + $this->expectException(\InvalidArgumentException::class); + + new Vector(['notAnArray'], []); + } + + public function testRaiseExceptionDuringVectorCreationWhenEmptyVector() + { + $this->expectException(\InvalidArgumentException::class); + + new Vector([[]], []); + } + + public function testCreateVectorWithColumns() + { + $column = $this->createMock(Column::class); + $column2 = $this->createMock(Column::class); + $columns = [$column, $column2]; + + $vector = new Vector([], $columns); + + $this->assertAttributeEquals($columns, 'columns', $vector); + } + + public function testInitialiseWithoutData() + { + $this->vector->initialise($this->createMock(Container::class)); + + $this->assertAttributeEmpty('columns', $this->vector); + } + + public function testInizialiseWithGuessedColumnsMergedToAlreadySettedColumns() + { + $columnId = 'cId'; + $column = $this->createMock(Column::class); + $column + ->method('getId') + ->willReturn($columnId); + + $column2Id = 'c2Id'; + $column2 = $this->createMock(Column::class); + $column2 + ->method('getId') + ->willReturn($column2Id); + + $vector = new Vector([['c3Id' => 'c3', 'c4Id' => 'c4']], [$column, $column2]); + + $uc1 = new UntypedColumn([ + 'id' => 'c3Id', + 'title' => 'c3Id', + 'source' => true, + 'filterable' => true, + 'sortable' => true, + 'visible' => true, + 'field' => 'c3Id', + ]); + $uc1->setType('text'); + + $uc2 = new UntypedColumn([ + 'id' => 'c4Id', + 'title' => 'c4Id', + 'source' => true, + 'filterable' => true, + 'sortable' => true, + 'visible' => true, + 'field' => 'c4Id', + ]); + $uc2->setType('text'); + + $vector->initialise($this->createMock(Container::class)); + + $this->assertAttributeEquals([$column, $column2, $uc1, $uc2], 'columns', $vector); + } + + public function testInizialiseWithoutGuessedColumns() + { + $columnId = 'cId'; + $column = $this->createMock(Column::class); + $column + ->method('getId') + ->willReturn($columnId); + + $column2Id = 'c2Id'; + $column2 = $this->createMock(Column::class); + $column2 + ->method('getId') + ->willReturn($column2Id); + + $vector = new Vector([[$columnId => 'c1', $column2Id => 'c2']], [$column, $column2]); + + $vector->initialise($this->createMock(Container::class)); + + $this->assertAttributeEquals([$column, $column2], 'columns', $vector); + } + + /** + * @dataProvider guessedColumnProvider + */ + public function testInizializeWithGuessedColumn($vectorValue, UntypedColumn $untypedColumn, $columnType) + { + $untypedColumn->setType($columnType); + + $vector = new Vector($vectorValue); + $vector->initialise($this->createMock(Container::class)); + + $this->assertAttributeEquals([$untypedColumn], 'columns', $vector); + } + + public function testExecute() + { + $rows = [new Row(), new Row()]; + $columns = $this->createMock(Columns::class); + + $vector = $this->createPartialMock(Vector::class, ['executeFromData']); + $vector + ->method('executeFromData') + ->with($columns, 0, null, null) + ->willReturn($rows); + + $this->assertEquals($rows, $vector->execute($columns, 0, null, null)); + } + + public function testPopulateSelectFilters() + { + $columns = $this->createMock(Columns::class); + + $vector = $this->createPartialMock(Vector::class, ['populateSelectFiltersFromData']); + $vector + ->expects($this->once()) + ->method('populateSelectFiltersFromData') + ->with($columns, false); + + $vector->populateSelectFilters($columns); + } + + public function testGetTotalCount() + { + $maxResults = 10; + + $vector = $this->createPartialMock(Vector::class, ['getTotalCountFromData']); + $vector + ->method('getTotalCountFromData') + ->with($maxResults) + ->willReturn(8); + + $this->assertEquals(8, $vector->getTotalCount($maxResults)); + } + + public function testGetHash() + { + $idCol1 = 'idCol1'; + $column1 = $this->createMock(Column::class); + $column1 + ->method('getId') + ->willReturn($idCol1); + + $idCol2 = 'idCol2'; + $column2 = $this->createMock(Column::class); + $column2 + ->method('getId') + ->willReturn($idCol2); + + $vector = new Vector([], [$column1, $column2]); + + $this->assertEquals('APY\DataGridBundle\Grid\Source\Vector' . md5($idCol1.$idCol2), $vector->getHash()); + } + + public function testSetId() + { + $id = 'id'; + $this->vector->setId($id); + + $this->assertAttributeEquals($id, 'id', $this->vector); + } + + public function testGetId() + { + $id = 'id'; + $this->vector->setId($id); + + $this->assertEquals($id, $this->vector->getId()); + } + + public function guessedColumnProvider() + { + $uc = new UntypedColumn([ + 'id' => 'c1Id', + 'title' => 'c1Id', + 'source' => true, + 'filterable' => true, + 'sortable' => true, + 'visible' => true, + 'field' => 'c1Id', + ]); + + $date = new \DateTime(); + $date->setTime(0, 0, 0); + + return [ + 'Empty' => [[['c1Id' => '']], $uc, 'text'], + 'Null' => [[['c1Id' => null]], $uc, 'text'], + 'Array' => [[['c1Id' => []]], $uc, 'array'], + 'Datetime' => [[['c1Id' => new \DateTime()]], $uc, 'datetime'], + 'Date' => [[['c1Id' => $date]], $uc, 'date'], + 'String but not date' => [[['c1Id' => 'thisIsAString']], $uc, 'text'], + 'Date string' => [[['c1Id' => '2017-07-22']], $uc, 'date'], + 'Datetime string' => [[['c1Id' => '2017-07-22 12:00:00']], $uc, 'datetime'], + 'True value' => [[['c1Id' => true]], $uc, 'boolean'], + 'False value' => [[['c1Id' => true]], $uc, 'boolean'], + 'True int value' => [[['c1Id' => 1]], $uc, 'boolean'], + 'False int value' => [[['c1Id' => 0]], $uc, 'boolean'], + 'True string value' => [[['c1Id' => '1']], $uc, 'boolean'], + 'False string value' => [[['c1Id' => '0']], $uc, 'boolean'], + 'Number' => [[['c1Id' => 12]], $uc, 'number'], + 'Boolean and not number' => [[['c1Id' => true], ['c1Id' => '2017-07-22']], $uc, 'text'], + 'Boolean and number' => [[['c1Id' => true], ['c1Id' => 20]], $uc, 'number'], + 'Date and not date time' => [[['c1Id' => '2017-07-22'], ['c1Id' => 20]], $uc, 'text'], + 'Date and time' => [[['c1Id' => '2017-07-22'], ['c1Id' => '2017-07-22 11:00:00']], $uc, 'datetime'] + ]; + } + + public function setUp() + { + $this->vector = new Vector([], []); + } +} + +class VectorObj +{ +} \ No newline at end of file