Enable support to XLSM spreadsheets

This commit is contained in:
Marcos Teodoro committed 2019-11-24 02:32:38 -03:00
1 parent eb88bb4c3a
commit f8d14d3f05
4 files changed
+97 -8

No files matched your search

+73
View File
@@ -0,0 +1,73 @@
<?php
use Box\Spout\Reader\Common\Creator\ReaderEntityFactory;
use Box\Spout\TestUsingResource;
use PHPUnit\Framework\TestCase;
/**
* Class ReaderTest
*/
class ReaderTest extends TestCase
{
use TestUsingResource;
/**
* @return void
*/
public function testReaderFactoryShouldReturnXLSXReader()
{
$resourcePath = $this->getResourcePath('simple-xlsm-sample.xlsm');
$reader = ReaderEntityFactory::createReaderFromFile($resourcePath);
$this->assertInstanceOf(Box\Spout\Reader\ReaderInterface::class, $reader);
}
/**
* @return void
*/
public function testReadXLSMSpreadsheetShouldReturnData()
{
$this->assertNotEmpty($this->getAllRowsForFile('simple-xlsm-sample.xlsm'));
}
/**
* @return void
*/
public function testSpreadsheetRowsShouldMatch()
{
$expectedRows = [
['a1', 'b1', 'c1'],
['a2', 'b2', 'c2'],
['a3', 'b3', 'c3'],
];
$this->assertEquals($expectedRows, $this->getAllRowsForFile('simple-xlsm-sample.xlsm'));
}
/**
* @param string $fileName
* @param bool $shouldFormatDates
* @param bool $shouldPreserveEmptyRows
* @return array All the read rows the given file
*/
private function getAllRowsForFile($fileName, $shouldFormatDates = false, $shouldPreserveEmptyRows = false)
{
$allRows = [];
$resourcePath = $this->getResourcePath($fileName);
$reader = ReaderEntityFactory::createXLSXReader();
$reader->setShouldFormatDates($shouldFormatDates);
$reader->setShouldPreserveEmptyRows($shouldPreserveEmptyRows);
$reader->open($resourcePath);
foreach ($reader->getSheetIterator() as $sheetIndex => $sheet) {
foreach ($sheet->getRowIterator() as $rowIndex => $row) {
$allRows[] = $row->toArray();
}
}
$reader->close();
return $allRows;
}
}
+1 -1
View File
@@ -8,7 +8,7 @@ namespace Box\Spout;
trait TestUsingResource
{
/** @var string Path to the test resources folder */
private $resourcesPath = 'tests/resources';
private $resourcesPath = __DIR__ . '/../../tests/resources';
/** @var string Path to the test generated resources folder */
private $generatedResourcesPath = 'tests/resources/generated';