Make XLSX reader return Row objects

This commit is contained in:
Adrien Loison committed 2017-11-18 20:53:22 +01:00
1 parent a665b974fa
commit 78b6639480
12 files changed
+272 -96

No files matched your search

@@ -9,29 +9,6 @@ use Box\Spout\Common\Exception\InvalidArgumentException;
*/
class CellHelperTest extends \PHPUnit_Framework_TestCase
{
/**
* @return array
*/
public function dataProviderForTestFillMissingArrayIndexes()
{
return [
[null, []],
[[], []],
[[1 => 1, 3 => 3], ['FILL', 1, 'FILL', 3]],
];
}
/**
* @dataProvider dataProviderForTestFillMissingArrayIndexes
* @param array $arrayToFill
* @param array $expectedFilledArray
*/
public function testFillMissingArrayIndexes($arrayToFill, array $expectedFilledArray)
{
$filledArray = CellHelper::fillMissingArrayIndexes($arrayToFill, 'FILL');
$this->assertEquals($expectedFilledArray, $filledArray);
}
/**
* @return array
*/
@@ -0,0 +1,90 @@
<?php
namespace Box\Spout\Reader\XLSX\Manager;
use Box\Spout\Reader\Common\Entity\Cell;
use Box\Spout\Reader\Common\Entity\Row;
use Box\Spout\Reader\XLSX\Creator\HelperFactory;
use Box\Spout\Reader\XLSX\Creator\InternalEntityFactory;
use Box\Spout\Reader\XLSX\Creator\ManagerFactory;
/**
* Class SharedStringsManagerTest
*/
class RowManagerTest extends \PHPUnit_Framework_TestCase
{
/**
* @return array
*/
public function dataProviderForTestFillMissingIndexesWithEmptyCells()
{
$cell1 = new Cell(1);
$cell3 = new Cell(3);
return [
[[], []],
[[1 => $cell1, 3 => $cell3], [new Cell(''), $cell1, new Cell(''), $cell3]],
];
}
/**
* @dataProvider dataProviderForTestFillMissingIndexesWithEmptyCells
*
* @param Cell[]|null $rowCells
* @param Cell[] $expectedFilledCells
*/
public function testFillMissingIndexesWithEmptyCells($rowCells, $expectedFilledCells)
{
$rowManager = $this->createRowManager();
$rowToFill = new Row([]);
foreach ($rowCells as $cellIndex => $cell) {
$rowToFill->setCellAtIndex($cell, $cellIndex);
}
$filledRow = $rowManager->fillMissingIndexesWithEmptyCells($rowToFill);
$this->assertEquals($expectedFilledCells, $filledRow->getCells());
}
/**
* @return array
*/
public function dataProviderForTestIsEmptyRow()
{
return [
// cells, expected isEmpty
[[], true],
[[new Cell('')], true],
[[new Cell(''), 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 = $this->createRowManager();
$row = new Row($cells);
$this->assertEquals($expectedIsEmpty, $rowManager->isEmpty($row));
}
/**
* @return RowManager
*/
private function createRowManager()
{
$entityFactory = new InternalEntityFactory(
$this->createMock(ManagerFactory::class),
$this->createMock(HelperFactory::class)
);
return new RowManager($entityFactory);
}
}
+7 -8
View File
@@ -237,7 +237,6 @@ class ReaderTest extends \PHPUnit_Framework_TestCase
'weird string', // valid 'str' string
null, // invalid date
],
['', '', '', '', '', '', '', '', ''],
];
$this->assertEquals($expectedRows, $allRows);
}
@@ -451,10 +450,10 @@ class ReaderTest extends \PHPUnit_Framework_TestCase
$this->assertEquals(6, count($allRows), 'There should be 6 rows');
$expectedRows = [
[''],
[],
['s1--A2', 's1--B2', 's1--C2'],
[''],
[''],
[],
[],
['s1--A5', 's1--B5', 's1--C5'],
['s1--A6', 's1--B6', 's1--C6'],
];
@@ -580,14 +579,14 @@ class ReaderTest extends \PHPUnit_Framework_TestCase
foreach ($reader->getSheetIterator() as $sheet) {
// this loop should only add the first row of the first sheet
foreach ($sheet->getRowIterator() as $row) {
$allRows[] = $row;
$allRows[] = $row->toArray();
break;
}
// this loop should rewind the iterator and restart reading from the 1st row again
// therefore, it should only add the first row of the first sheet
foreach ($sheet->getRowIterator() as $row) {
$allRows[] = $row;
$allRows[] = $row->toArray();
break;
}
@@ -598,7 +597,7 @@ class ReaderTest extends \PHPUnit_Framework_TestCase
foreach ($reader->getSheetIterator() as $sheet) {
// this loop should only add the first row of the current sheet
foreach ($sheet->getRowIterator() as $row) {
$allRows[] = $row;
$allRows[] = $row->toArray();
break;
}
@@ -708,7 +707,7 @@ class ReaderTest extends \PHPUnit_Framework_TestCase
foreach ($reader->getSheetIterator() as $sheetIndex => $sheet) {
foreach ($sheet->getRowIterator() as $rowIndex => $row) {
$allRows[] = $row;
$allRows[] = $row->toArray();
}
}