diff --git a/src/Spout/Reader/Common/Creator/ReaderFactory.php b/src/Spout/Reader/Common/Creator/ReaderFactory.php index 6b10175..c16d469 100644 --- a/src/Spout/Reader/Common/Creator/ReaderFactory.php +++ b/src/Spout/Reader/Common/Creator/ReaderFactory.php @@ -28,6 +28,16 @@ use Box\Spout\Reader\XLSX\Reader as XLSXReader; */ class ReaderFactory { + /** + * File extensions and readers mapped + */ + protected const MAPPED_EXTENSIONS = [ + 'xlsx' => 'xlsx', + 'csv' => 'csv', + 'ods' => 'ods', + 'xlsm' => 'xlsx', + ]; + /** * Creates a reader by file extension * @@ -42,6 +52,15 @@ class ReaderFactory return self::createFromType($extension); } + public static function getMappedFormats(string $fileExtension) + { + if (!(array_key_exists($fileExtension, self::MAPPED_EXTENSIONS))) { + throw new UnsupportedTypeException('No readers supporting the given type: ' . $fileExtension); + } + + return self::MAPPED_EXTENSIONS[$fileExtension]; + } + /** * This creates an instance of the appropriate reader, given the type of the file to be read * @@ -51,13 +70,10 @@ class ReaderFactory */ public static function createFromType($readerType) { - switch ($readerType) { - case Type::CSV: return self::createCSVReader(); - case Type::XLSX: return self::createXLSXReader(); - case Type::ODS: return self::createODSReader(); - default: - throw new UnsupportedTypeException('No readers supporting the given type: ' . $readerType); - } + $readerType = self::getMappedFormats($readerType); + $methodName = 'create' . strtoupper($readerType) . 'Reader'; + + return self::$methodName(); } /** diff --git a/tests/Spout/Reader/XLSM/ReaderTest.php b/tests/Spout/Reader/XLSM/ReaderTest.php new file mode 100644 index 0000000..6e15382 --- /dev/null +++ b/tests/Spout/Reader/XLSM/ReaderTest.php @@ -0,0 +1,73 @@ +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; + } +} diff --git a/tests/Spout/TestUsingResource.php b/tests/Spout/TestUsingResource.php index 551f86b..defebc0 100644 --- a/tests/Spout/TestUsingResource.php +++ b/tests/Spout/TestUsingResource.php @@ -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'; diff --git a/tests/resources/xlsm/simple-xlsm-sample.xlsm b/tests/resources/xlsm/simple-xlsm-sample.xlsm new file mode 100644 index 0000000..028b1fb Binary files /dev/null and b/tests/resources/xlsm/simple-xlsm-sample.xlsm differ