From 668c10a30d89e187e59ec13c7b89a1c64daf96c0 Mon Sep 17 00:00:00 2001 From: Adrien Loison Date: Sun, 27 Aug 2017 03:44:51 +0200 Subject: [PATCH] Introduce Managers for readers Some helper classes were more managers than helpers... --- src/Spout/Reader/ReaderFactory.php | 11 +- .../Reader/XLSX/Creator/EntityFactory.php | 32 ++-- .../Reader/XLSX/Creator/HelperFactory.php | 63 +------ .../Reader/XLSX/Creator/ManagerFactory.php | 68 ++++++++ .../Reader/XLSX/Helper/CellValueFormatter.php | 29 ++-- .../CachingStrategyFactory.php | 5 +- .../CachingStrategyInterface.php | 4 +- .../FileBasedStrategy.php | 6 +- .../SharedStringsCaching/InMemoryStrategy.php | 4 +- .../SharedStringsManager.php} | 14 +- .../SheetManager.php} | 22 +-- .../StyleManager.php} | 10 +- src/Spout/Reader/XLSX/Reader.php | 28 ++-- src/Spout/Reader/XLSX/SheetIterator.php | 10 +- .../XLSX/Helper/CellValueFormatterTest.php | 14 +- .../XLSX/Helper/SharedStringsHelperTest.php | 154 ----------------- .../CachingStrategyFactoryTest.php | 11 +- .../XLSX/Manager/SharedStringsManagerTest.php | 156 ++++++++++++++++++ .../StyleManagerTest.php} | 74 +++++---- 19 files changed, 376 insertions(+), 339 deletions(-) create mode 100644 src/Spout/Reader/XLSX/Creator/ManagerFactory.php rename src/Spout/Reader/XLSX/{Helper => Manager}/SharedStringsCaching/CachingStrategyFactory.php (97%) rename src/Spout/Reader/XLSX/{Helper => Manager}/SharedStringsCaching/CachingStrategyInterface.php (90%) rename src/Spout/Reader/XLSX/{Helper => Manager}/SharedStringsCaching/FileBasedStrategy.php (96%) rename src/Spout/Reader/XLSX/{Helper => Manager}/SharedStringsCaching/InMemoryStrategy.php (95%) rename src/Spout/Reader/XLSX/{Helper/SharedStringsHelper.php => Manager/SharedStringsManager.php} (96%) rename src/Spout/Reader/XLSX/{Helper/SheetHelper.php => Manager/SheetManager.php} (92%) rename src/Spout/Reader/XLSX/{Helper/StyleHelper.php => Manager/StyleManager.php} (98%) delete mode 100644 tests/Spout/Reader/XLSX/Helper/SharedStringsHelperTest.php rename tests/Spout/Reader/XLSX/{Helper => Manager}/SharedStringsCaching/CachingStrategyFactoryTest.php (89%) create mode 100644 tests/Spout/Reader/XLSX/Manager/SharedStringsManagerTest.php rename tests/Spout/Reader/XLSX/{Helper/StyleHelperTest.php => Manager/StyleManagerTest.php} (54%) diff --git a/src/Spout/Reader/ReaderFactory.php b/src/Spout/Reader/ReaderFactory.php index 39e6516..de39030 100644 --- a/src/Spout/Reader/ReaderFactory.php +++ b/src/Spout/Reader/ReaderFactory.php @@ -4,9 +4,8 @@ namespace Box\Spout\Reader; use Box\Spout\Common\Creator\HelperFactory; use Box\Spout\Common\Exception\UnsupportedTypeException; -use Box\Spout\Common\Helper\GlobalFunctionsHelper; use Box\Spout\Common\Type; -use Box\Spout\Reader\XLSX\Helper\SharedStringsCaching\CachingStrategyFactory; +use Box\Spout\Reader\XLSX\Manager\SharedStringsCaching\CachingStrategyFactory; /** * Class ReaderFactory @@ -56,12 +55,12 @@ class ReaderFactory private static function getXLSXReader() { $optionsManager = new XLSX\Manager\OptionsManager(); - $cachingStrategyFactory = new CachingStrategyFactory(); - $helperFactory = new XLSX\Creator\HelperFactory($cachingStrategyFactory); - $entityFactory = new XLSX\Creator\EntityFactory($helperFactory); + $helperFactory = new XLSX\Creator\HelperFactory(); + $managerFactory = new XLSX\Creator\ManagerFactory($helperFactory, new CachingStrategyFactory()); + $entityFactory = new XLSX\Creator\EntityFactory($managerFactory, $helperFactory); $globalFunctionsHelper = $helperFactory->createGlobalFunctionsHelper(); - return new XLSX\Reader($optionsManager, $globalFunctionsHelper, $entityFactory, $helperFactory); + return new XLSX\Reader($optionsManager, $globalFunctionsHelper, $entityFactory, $managerFactory); } /** diff --git a/src/Spout/Reader/XLSX/Creator/EntityFactory.php b/src/Spout/Reader/XLSX/Creator/EntityFactory.php index 54d0662..56b5b75 100644 --- a/src/Spout/Reader/XLSX/Creator/EntityFactory.php +++ b/src/Spout/Reader/XLSX/Creator/EntityFactory.php @@ -5,11 +5,12 @@ namespace Box\Spout\Reader\XLSX\Creator; use Box\Spout\Reader\Common\Creator\EntityFactoryInterface; use Box\Spout\Reader\Common\Entity\Options; use Box\Spout\Reader\Common\XMLProcessor; -use Box\Spout\Reader\XLSX\Helper\SharedStringsHelper; +use Box\Spout\Reader\XLSX\Manager\SharedStringsManager; use Box\Spout\Reader\XLSX\RowIterator; use Box\Spout\Reader\XLSX\Sheet; use Box\Spout\Reader\XLSX\SheetIterator; use Box\Spout\Reader\Wrapper\XMLReader; +use MongoDB\Driver\Manager; /** * Class EntityFactory @@ -22,24 +23,29 @@ class EntityFactory implements EntityFactoryInterface /** @var HelperFactory */ private $helperFactory; + /** @var ManagerFactory */ + private $managerFactory; + /** + * @param ManagerFactory $managerFactory * @param HelperFactory $helperFactory */ - public function __construct(HelperFactory $helperFactory) + public function __construct(ManagerFactory $managerFactory, HelperFactory $helperFactory) { + $this->managerFactory = $managerFactory; $this->helperFactory = $helperFactory; } /** * @param string $filePath Path of the file to be read * @param \Box\Spout\Common\Manager\OptionsManagerInterface $optionsManager Reader's options manager - * @param SharedStringsHelper $sharedStringsHelper Helper to work with shared strings + * @param SharedStringsManager $sharedStringsManager Manages shared strings * @return SheetIterator */ - public function createSheetIterator($filePath, $optionsManager, $sharedStringsHelper) + public function createSheetIterator($filePath, $optionsManager, $sharedStringsManager) { - $sheetHelper = $this->helperFactory->createSheetHelper($filePath, $optionsManager, $sharedStringsHelper, $this); - return new SheetIterator($sheetHelper); + $sheetManager = $this->managerFactory->createSheetManager($filePath, $optionsManager, $sharedStringsManager, $this); + return new SheetIterator($sheetManager); } /** @@ -49,7 +55,7 @@ class EntityFactory implements EntityFactoryInterface * @param string $sheetName Name of the sheet * @param bool $isSheetActive Whether the sheet was defined as active * @param \Box\Spout\Common\Manager\OptionsManagerInterface $optionsManager Reader's options manager - * @param SharedStringsHelper $sharedStringsHelper Helper to work with shared strings + * @param SharedStringsManager $sharedStringsManager Manages shared strings * @return Sheet */ public function createSheet( @@ -59,9 +65,9 @@ class EntityFactory implements EntityFactoryInterface $sheetName, $isSheetActive, $optionsManager, - $sharedStringsHelper) + $sharedStringsManager) { - $rowIterator = $this->createRowIterator($filePath, $sheetDataXMLFilePath, $optionsManager, $sharedStringsHelper); + $rowIterator = $this->createRowIterator($filePath, $sheetDataXMLFilePath, $optionsManager, $sharedStringsManager); return new Sheet($rowIterator, $sheetIndex, $sheetName, $isSheetActive); } @@ -69,17 +75,17 @@ class EntityFactory implements EntityFactoryInterface * @param string $filePath Path of the XLSX file being read * @param string $sheetDataXMLFilePath Path of the sheet data XML file as in [Content_Types].xml * @param \Box\Spout\Common\Manager\OptionsManagerInterface $optionsManager Reader's options manager - * @param SharedStringsHelper $sharedStringsHelper Helper to work with shared strings + * @param SharedStringsManager $sharedStringsManager Manages shared strings * @return RowIterator */ - private function createRowIterator($filePath, $sheetDataXMLFilePath, $optionsManager, $sharedStringsHelper) + private function createRowIterator($filePath, $sheetDataXMLFilePath, $optionsManager, $sharedStringsManager) { $xmlReader = $this->createXMLReader(); $xmlProcessor = $this->createXMLProcessor($xmlReader); - $styleHelper = $this->helperFactory->createStyleHelper($filePath, $this); + $styleManager = $this->managerFactory->createStyleManager($filePath, $this); $shouldFormatDates = $optionsManager->getOption(Options::SHOULD_FORMAT_DATES); - $cellValueFormatter = $this->helperFactory->createCellValueFormatter($sharedStringsHelper, $styleHelper, $shouldFormatDates); + $cellValueFormatter = $this->helperFactory->createCellValueFormatter($sharedStringsManager, $styleManager, $shouldFormatDates); $shouldPreserveEmptyRows = $optionsManager->getOption(Options::SHOULD_PRESERVE_EMPTY_ROWS); diff --git a/src/Spout/Reader/XLSX/Creator/HelperFactory.php b/src/Spout/Reader/XLSX/Creator/HelperFactory.php index 0494486..dbd1fd5 100644 --- a/src/Spout/Reader/XLSX/Creator/HelperFactory.php +++ b/src/Spout/Reader/XLSX/Creator/HelperFactory.php @@ -4,75 +4,30 @@ namespace Box\Spout\Reader\XLSX\Creator; use Box\Spout\Common\Helper\Escaper; use Box\Spout\Reader\XLSX\Helper\CellValueFormatter; -use Box\Spout\Reader\XLSX\Helper\SharedStringsCaching\CachingStrategyFactory; -use Box\Spout\Reader\XLSX\Helper\SharedStringsHelper; -use Box\Spout\Reader\XLSX\Helper\SheetHelper; -use Box\Spout\Reader\XLSX\Helper\StyleHelper; +use Box\Spout\Reader\XLSX\Manager\SharedStringsCaching\CachingStrategyFactory; +use Box\Spout\Reader\XLSX\Manager\SharedStringsManager; +use Box\Spout\Reader\XLSX\Manager\SheetManager; +use Box\Spout\Reader\XLSX\Manager\StyleManager; /** - * Class EntityFactory + * Class HelperFactory * Factory to create helpers * * @package Box\Spout\Reader\XLSX\Creator */ class HelperFactory extends \Box\Spout\Common\Creator\HelperFactory { - /** @var CachingStrategyFactory */ - private $cachingStrategyFactory; - /** - * @param CachingStrategyFactory $cachingStrategyFactory Factory to create shared strings caching strategies - */ - public function __construct(CachingStrategyFactory $cachingStrategyFactory) - { - $this->cachingStrategyFactory = $cachingStrategyFactory; - } - - /** - * @param string $filePath Path of the XLSX file being read - * @param string $tempFolder Temporary folder where the temporary files to store shared strings will be stored - * @param EntityFactory $entityFactory Factory to create entities - * @return SharedStringsHelper - */ - public function createSharedStringsHelper($filePath, $tempFolder, $entityFactory) - { - return new SharedStringsHelper($filePath, $tempFolder, $entityFactory, $this, $this->cachingStrategyFactory); - } - - /** - * @param string $filePath Path of the XLSX file being read - * @param \Box\Spout\Common\Manager\OptionsManagerInterface $optionsManager Reader's options manager - * @param \Box\Spout\Reader\XLSX\Helper\SharedStringsHelper Helper to work with shared strings - * @param EntityFactory $entityFactory Factory to create entities - * @return SheetHelper - */ - public function createSheetHelper($filePath, $optionsManager, $sharedStringsHelper, $entityFactory) - { - $escaper = $this->createStringsEscaper(); - return new SheetHelper($filePath, $optionsManager, $sharedStringsHelper, $escaper, $entityFactory); - } - - /** - * @param string $filePath Path of the XLSX file being read - * @param EntityFactory $entityFactory Factory to create entities - * @return StyleHelper - */ - public function createStyleHelper($filePath, $entityFactory) - { - return new StyleHelper($filePath, $entityFactory); - } - - /** - * @param SharedStringsHelper $sharedStringsHelper Helper to work with shared strings - * @param StyleHelper $styleHelper Helper to work with styles + * @param SharedStringsManager $sharedStringsManager Manages shared strings + * @param StyleManager $styleManager Manages styles * @param bool $shouldFormatDates Whether date/time values should be returned as PHP objects or be formatted as strings * @return CellValueFormatter */ - public function createCellValueFormatter($sharedStringsHelper, $styleHelper, $shouldFormatDates) + public function createCellValueFormatter($sharedStringsManager, $styleManager, $shouldFormatDates) { $escaper = $this->createStringsEscaper(); - return new CellValueFormatter($sharedStringsHelper, $styleHelper, $shouldFormatDates, $escaper); + return new CellValueFormatter($sharedStringsManager, $styleManager, $shouldFormatDates, $escaper); } /** diff --git a/src/Spout/Reader/XLSX/Creator/ManagerFactory.php b/src/Spout/Reader/XLSX/Creator/ManagerFactory.php new file mode 100644 index 0000000..08acaff --- /dev/null +++ b/src/Spout/Reader/XLSX/Creator/ManagerFactory.php @@ -0,0 +1,68 @@ +helperFactory = $helperFactory; + $this->cachingStrategyFactory = $cachingStrategyFactory; + } + + /** + * @param string $filePath Path of the XLSX file being read + * @param string $tempFolder Temporary folder where the temporary files to store shared strings will be stored + * @param EntityFactory $entityFactory Factory to create entities + * @return SharedStringsManager + */ + public function createSharedStringsManager($filePath, $tempFolder, $entityFactory) + { + return new SharedStringsManager($filePath, $tempFolder, $entityFactory, $this->helperFactory, $this->cachingStrategyFactory); + } + + /** + * @param string $filePath Path of the XLSX file being read + * @param \Box\Spout\Common\Manager\OptionsManagerInterface $optionsManager Reader's options manager + * @param \Box\Spout\Reader\XLSX\Manager\SharedStringsManager $sharedStringsManager Manages shared strings + * @param EntityFactory $entityFactory Factory to create entities + * @return SheetManager + */ + public function createSheetManager($filePath, $optionsManager, $sharedStringsManager, $entityFactory) + { + $escaper = $this->helperFactory->createStringsEscaper(); + return new SheetManager($filePath, $optionsManager, $sharedStringsManager, $escaper, $entityFactory); + } + + /** + * @param string $filePath Path of the XLSX file being read + * @param EntityFactory $entityFactory Factory to create entities + * @return StyleManager + */ + public function createStyleManager($filePath, $entityFactory) + { + return new StyleManager($filePath, $entityFactory); + } +} diff --git a/src/Spout/Reader/XLSX/Helper/CellValueFormatter.php b/src/Spout/Reader/XLSX/Helper/CellValueFormatter.php index 43300bc..200991c 100644 --- a/src/Spout/Reader/XLSX/Helper/CellValueFormatter.php +++ b/src/Spout/Reader/XLSX/Helper/CellValueFormatter.php @@ -2,6 +2,9 @@ namespace Box\Spout\Reader\XLSX\Helper; +use Box\Spout\Reader\XLSX\Manager\SharedStringsManager; +use Box\Spout\Reader\XLSX\Manager\StyleManager; + /** * Class CellValueFormatter * This class provides helper functions to format cell values @@ -38,11 +41,11 @@ class CellValueFormatter */ const ERRONEOUS_EXCEL_LEAP_YEAR_DAY = 60; - /** @var SharedStringsHelper Helper to work with shared strings */ - protected $sharedStringsHelper; + /** @var SharedStringsManager Manages shared strings */ + protected $sharedStringsManager; - /** @var StyleHelper Helper to work with styles */ - protected $styleHelper; + /** @var StyleManager Manages styles */ + protected $styleManager; /** @var bool Whether date/time values should be returned as PHP objects or be formatted as strings */ protected $shouldFormatDates; @@ -51,15 +54,15 @@ class CellValueFormatter protected $escaper; /** - * @param SharedStringsHelper $sharedStringsHelper Helper to work with shared strings - * @param StyleHelper $styleHelper Helper to work with styles + * @param SharedStringsManager $sharedStringsManager Manages shared strings + * @param StyleManager $styleManager Manages styles * @param bool $shouldFormatDates Whether date/time values should be returned as PHP objects or be formatted as strings * @param \Box\Spout\Common\Helper\Escaper\XLSX $escaper Used to unescape XML data */ - public function __construct($sharedStringsHelper, $styleHelper, $shouldFormatDates, $escaper) + public function __construct($sharedStringsManager, $styleManager, $shouldFormatDates, $escaper) { - $this->sharedStringsHelper = $sharedStringsHelper; - $this->styleHelper = $styleHelper; + $this->sharedStringsManager = $sharedStringsManager; + $this->styleManager = $styleManager; $this->shouldFormatDates = $shouldFormatDates; $this->escaper = $escaper; } @@ -139,7 +142,7 @@ class CellValueFormatter // shared strings are formatted this way: // [SHARED_STRING_INDEX] $sharedStringIndex = intval($nodeValue); - $escapedCellValue = $this->sharedStringsHelper->getStringAtIndex($sharedStringIndex); + $escapedCellValue = $this->sharedStringsManager->getStringAtIndex($sharedStringIndex); $cellValue = $this->escaper->unescape($escapedCellValue); return $cellValue; } @@ -169,7 +172,7 @@ class CellValueFormatter { // Numeric values can represent numbers as well as timestamps. // We need to look at the style of the cell to determine whether it is one or the other. - $shouldFormatAsDate = $this->styleHelper->shouldFormatNumericValueAsDate($cellStyleId); + $shouldFormatAsDate = $this->styleManager->shouldFormatNumericValueAsDate($cellStyleId); if ($shouldFormatAsDate) { return $this->formatExcelTimestampValue(floatval($nodeValue), $cellStyleId); @@ -227,7 +230,7 @@ class CellValueFormatter $dateObj->setTime($hours, $minutes, $seconds); if ($this->shouldFormatDates) { - $styleNumberFormatCode = $this->styleHelper->getNumberFormatCode($cellStyleId); + $styleNumberFormatCode = $this->styleManager->getNumberFormatCode($cellStyleId); $phpDateFormat = DateFormatHelper::toPHPDateFormat($styleNumberFormatCode); return $dateObj->format($phpDateFormat); } else { @@ -256,7 +259,7 @@ class CellValueFormatter $dateObj->modify('+' . $secondsRemainder . 'seconds'); if ($this->shouldFormatDates) { - $styleNumberFormatCode = $this->styleHelper->getNumberFormatCode($cellStyleId); + $styleNumberFormatCode = $this->styleManager->getNumberFormatCode($cellStyleId); $phpDateFormat = DateFormatHelper::toPHPDateFormat($styleNumberFormatCode); return $dateObj->format($phpDateFormat); } else { diff --git a/src/Spout/Reader/XLSX/Helper/SharedStringsCaching/CachingStrategyFactory.php b/src/Spout/Reader/XLSX/Manager/SharedStringsCaching/CachingStrategyFactory.php similarity index 97% rename from src/Spout/Reader/XLSX/Helper/SharedStringsCaching/CachingStrategyFactory.php rename to src/Spout/Reader/XLSX/Manager/SharedStringsCaching/CachingStrategyFactory.php index 7ba55e5..ff808e0 100644 --- a/src/Spout/Reader/XLSX/Helper/SharedStringsCaching/CachingStrategyFactory.php +++ b/src/Spout/Reader/XLSX/Manager/SharedStringsCaching/CachingStrategyFactory.php @@ -1,12 +1,13 @@ filePath = $filePath; $this->optionsManager = $optionsManager; - $this->sharedStringsHelper = $sharedStringsHelper; + $this->sharedStringsManager = $sharedStringsManager; $this->escaper = $escaper; $this->entityFactory = $entityFactory; } @@ -126,7 +126,7 @@ class SheetHelper $sheetName, $isSheetActive, $this->optionsManager, - $this->sharedStringsHelper + $this->sharedStringsManager ); } diff --git a/src/Spout/Reader/XLSX/Helper/StyleHelper.php b/src/Spout/Reader/XLSX/Manager/StyleManager.php similarity index 98% rename from src/Spout/Reader/XLSX/Helper/StyleHelper.php rename to src/Spout/Reader/XLSX/Manager/StyleManager.php index 2781577..23c1582 100644 --- a/src/Spout/Reader/XLSX/Helper/StyleHelper.php +++ b/src/Spout/Reader/XLSX/Manager/StyleManager.php @@ -1,16 +1,16 @@ helperFactory = $helperFactory; + $this->managerFactory = $managerFactory; } /** @@ -87,14 +87,14 @@ class Reader extends ReaderAbstract if ($this->zip->open($filePath) === true) { $tempFolder = $this->optionsManager->getOption(Options::TEMP_FOLDER); - $this->sharedStringsHelper = $this->helperFactory->createSharedStringsHelper($filePath, $tempFolder, $entityFactory); + $this->sharedStringsManager = $this->managerFactory->createSharedStringsManager($filePath, $tempFolder, $entityFactory); - if ($this->sharedStringsHelper->hasSharedStrings()) { + if ($this->sharedStringsManager->hasSharedStrings()) { // Extracts all the strings from the sheets for easy access in the future - $this->sharedStringsHelper->extractSharedStrings(); + $this->sharedStringsManager->extractSharedStrings(); } - $this->sheetIterator = $entityFactory->createSheetIterator($filePath, $this->optionsManager, $this->sharedStringsHelper, $this->globalFunctionsHelper); + $this->sheetIterator = $entityFactory->createSheetIterator($filePath, $this->optionsManager, $this->sharedStringsManager, $this->globalFunctionsHelper); } else { throw new IOException("Could not open $filePath for reading."); } @@ -121,8 +121,8 @@ class Reader extends ReaderAbstract $this->zip->close(); } - if ($this->sharedStringsHelper) { - $this->sharedStringsHelper->cleanup(); + if ($this->sharedStringsManager) { + $this->sharedStringsManager->cleanup(); } } } diff --git a/src/Spout/Reader/XLSX/SheetIterator.php b/src/Spout/Reader/XLSX/SheetIterator.php index 4f2b64d..d960a7a 100644 --- a/src/Spout/Reader/XLSX/SheetIterator.php +++ b/src/Spout/Reader/XLSX/SheetIterator.php @@ -3,9 +3,7 @@ namespace Box\Spout\Reader\XLSX; use Box\Spout\Reader\IteratorInterface; -use Box\Spout\Reader\XLSX\Creator\EntityFactory; -use Box\Spout\Reader\XLSX\Creator\HelperFactory; -use Box\Spout\Reader\XLSX\Helper\SheetHelper; +use Box\Spout\Reader\XLSX\Manager\SheetManager; use Box\Spout\Reader\Exception\NoSheetsFoundException; /** @@ -23,13 +21,13 @@ class SheetIterator implements IteratorInterface protected $currentSheetIndex; /** - * @param SheetHelper $sheetHelper Helper to work with sheets + * @param SheetManager $sheetManager Manages sheets * @throws \Box\Spout\Reader\Exception\NoSheetsFoundException If there are no sheets in the file */ - public function __construct($sheetHelper) + public function __construct($sheetManager) { // Fetch all available sheets - $this->sheets = $sheetHelper->getSheets(); + $this->sheets = $sheetManager->getSheets(); if (count($this->sheets) === 0) { throw new NoSheetsFoundException('The file must contain at least one sheet.'); diff --git a/tests/Spout/Reader/XLSX/Helper/CellValueFormatterTest.php b/tests/Spout/Reader/XLSX/Helper/CellValueFormatterTest.php index bef5512..558a725 100644 --- a/tests/Spout/Reader/XLSX/Helper/CellValueFormatterTest.php +++ b/tests/Spout/Reader/XLSX/Helper/CellValueFormatterTest.php @@ -65,15 +65,16 @@ class CellValueFormatterTest extends \PHPUnit_Framework_TestCase ->with(CellValueFormatter::XML_NODE_VALUE) ->will($this->returnValue($nodeListMock)); - $styleHelperMock = $this->getMockBuilder('Box\Spout\Reader\XLSX\Helper\StyleHelper')->disableOriginalConstructor()->getMock(); + /** @var \Box\Spout\Reader\XLSX\Manager\StyleManager|\PHPUnit_Framework_MockObject_MockObject $styleManagerMock */ + $styleManagerMock = $this->getMockBuilder('Box\Spout\Reader\XLSX\Manager\StyleManager')->disableOriginalConstructor()->getMock(); - $styleHelperMock + $styleManagerMock ->expects($this->once()) ->method('shouldFormatNumericValueAsDate') ->with(123) ->will($this->returnValue(true)); - $formatter = new CellValueFormatter(null, $styleHelperMock, false, new Escaper\XLSX()); + $formatter = new CellValueFormatter(null, $styleManagerMock, false, new Escaper\XLSX()); $result = $formatter->extractAndFormatNodeValue($nodeMock); if ($expectedDateAsString === null) { @@ -120,13 +121,14 @@ class CellValueFormatterTest extends \PHPUnit_Framework_TestCase */ public function testFormatNumericCellValueWithNumbers($value, $expectedFormattedValue, $expectedType) { - $styleHelperMock = $this->getMockBuilder('Box\Spout\Reader\XLSX\Helper\StyleHelper')->disableOriginalConstructor()->getMock(); - $styleHelperMock + /** @var \Box\Spout\Reader\XLSX\Manager\StyleManager|\PHPUnit_Framework_MockObject_MockObject $styleManagerMock */ + $styleManagerMock = $this->getMockBuilder('Box\Spout\Reader\XLSX\Manager\StyleManager')->disableOriginalConstructor()->getMock(); + $styleManagerMock ->expects($this->once()) ->method('shouldFormatNumericValueAsDate') ->will($this->returnValue(false)); - $formatter = new CellValueFormatter(null, $styleHelperMock, false, new Escaper\XLSX()); + $formatter = new CellValueFormatter(null, $styleManagerMock, false, new Escaper\XLSX()); $formattedValue = \ReflectionHelper::callMethodOnObject($formatter, 'formatNumericCellValue', $value, 0); $this->assertEquals($expectedFormattedValue, $formattedValue); diff --git a/tests/Spout/Reader/XLSX/Helper/SharedStringsHelperTest.php b/tests/Spout/Reader/XLSX/Helper/SharedStringsHelperTest.php deleted file mode 100644 index 41d48dc..0000000 --- a/tests/Spout/Reader/XLSX/Helper/SharedStringsHelperTest.php +++ /dev/null @@ -1,154 +0,0 @@ -sharedStringsHelper = null; - } - - /** - * @return void - */ - public function tearDown() - { - if ($this->sharedStringsHelper !== null) { - $this->sharedStringsHelper->cleanup(); - } - } - - /** - * @param string $resourceName - * @return SharedStringsHelper - */ - private function createSharedStringsHelper($resourceName = 'one_sheet_with_shared_strings.xlsx') - { - $resourcePath = $this->getResourcePath($resourceName); - $tempFolder = sys_get_temp_dir(); - $cachingStrategyFactory = new CachingStrategyFactory(); - $helperFactory = new HelperFactory($cachingStrategyFactory); - $entityFactory = new EntityFactory($helperFactory); - - $this->sharedStringsHelper = new SharedStringsHelper($resourcePath, $tempFolder, $entityFactory, $helperFactory, $cachingStrategyFactory); - - return $this->sharedStringsHelper; - } - - /** - * @expectedException \Box\Spout\Reader\Exception\SharedStringNotFoundException - * @return void - */ - public function testGetStringAtIndexShouldThrowExceptionIfStringNotFound() - { - $sharedStringsHelper = $this->createSharedStringsHelper(); - $sharedStringsHelper->extractSharedStrings(); - $sharedStringsHelper->getStringAtIndex(PHP_INT_MAX); - } - - /** - * @return void - */ - public function testGetStringAtIndexShouldReturnTheCorrectStringIfFound() - { - $sharedStringsHelper = $this->createSharedStringsHelper(); - $sharedStringsHelper->extractSharedStrings(); - - $sharedString = $sharedStringsHelper->getStringAtIndex(0); - $this->assertEquals('s1--A1', $sharedString); - - $sharedString = $sharedStringsHelper->getStringAtIndex(24); - $this->assertEquals('s1--E5', $sharedString); - - $usedCachingStrategy = \ReflectionHelper::getValueOnObject($sharedStringsHelper, 'cachingStrategy'); - $this->assertTrue($usedCachingStrategy instanceof InMemoryStrategy); - } - - /** - * @return void - */ - public function testGetStringAtIndexShouldWorkWithMultilineStrings() - { - $sharedStringsHelper = $this->createSharedStringsHelper('one_sheet_with_shared_multiline_strings.xlsx'); - - $sharedStringsHelper->extractSharedStrings(); - - $sharedString = $sharedStringsHelper->getStringAtIndex(0); - $this->assertEquals("s1\nA1", $sharedString); - - $sharedString = $sharedStringsHelper->getStringAtIndex(24); - $this->assertEquals("s1\nE5", $sharedString); - } - - /** - * @return void - */ - public function testGetStringAtIndexShouldWorkWithStringsContainingTextAndHyperlinkInSameCell() - { - $sharedStringsHelper = $this->createSharedStringsHelper('one_sheet_with_shared_strings_containing_text_and_hyperlink_in_same_cell.xlsx'); - - $sharedStringsHelper->extractSharedStrings(); - - $sharedString = $sharedStringsHelper->getStringAtIndex(0); - $this->assertEquals('go to https://github.com please', $sharedString); - } - - /** - * @return void - */ - public function testGetStringAtIndexShouldNotDoubleDecodeHTMLEntities() - { - $sharedStringsHelper = $this->createSharedStringsHelper('one_sheet_with_pre_encoded_html_entities.xlsx'); - - $sharedStringsHelper->extractSharedStrings(); - - $sharedString = $sharedStringsHelper->getStringAtIndex(0); - $this->assertEquals('quote: " - ampersand: &', $sharedString); - } - - /** - * @return void - */ - public function testGetStringAtIndexWithFileBasedStrategy() - { - // force the file-based strategy by setting no memory limit - $originalMemoryLimit = ini_get('memory_limit'); - ini_set('memory_limit', '-1'); - - $sharedStringsHelper = $this->createSharedStringsHelper('sheet_with_lots_of_shared_strings.xlsx'); - - $sharedStringsHelper->extractSharedStrings(); - - $sharedString = $sharedStringsHelper->getStringAtIndex(0); - $this->assertEquals('str', $sharedString); - - $sharedString = $sharedStringsHelper->getStringAtIndex(CachingStrategyFactory::MAX_NUM_STRINGS_PER_TEMP_FILE + 1); - $this->assertEquals('str', $sharedString); - - $usedCachingStrategy = \ReflectionHelper::getValueOnObject($sharedStringsHelper, 'cachingStrategy'); - $this->assertTrue($usedCachingStrategy instanceof FileBasedStrategy); - - ini_set('memory_limit', $originalMemoryLimit); - } -} diff --git a/tests/Spout/Reader/XLSX/Helper/SharedStringsCaching/CachingStrategyFactoryTest.php b/tests/Spout/Reader/XLSX/Manager/SharedStringsCaching/CachingStrategyFactoryTest.php similarity index 89% rename from tests/Spout/Reader/XLSX/Helper/SharedStringsCaching/CachingStrategyFactoryTest.php rename to tests/Spout/Reader/XLSX/Manager/SharedStringsCaching/CachingStrategyFactoryTest.php index 85ec84b..5323f01 100644 --- a/tests/Spout/Reader/XLSX/Helper/SharedStringsCaching/CachingStrategyFactoryTest.php +++ b/tests/Spout/Reader/XLSX/Manager/SharedStringsCaching/CachingStrategyFactoryTest.php @@ -1,12 +1,13 @@ getMockBuilder('\Box\Spout\Reader\XLSX\Helper\SharedStringsCaching\CachingStrategyFactory') + ->getMockBuilder('\Box\Spout\Reader\XLSX\Manager\SharedStringsCaching\CachingStrategyFactory') ->disableOriginalConstructor() ->setMethods(['getMemoryLimitInKB']) ->getMock(); @@ -49,7 +50,7 @@ class CachingStrategyFactoryTest extends \PHPUnit_Framework_TestCase $helperFactory = new HelperFactory($factoryStub); $strategy = $factoryStub->createBestCachingStrategy($sharedStringsUniqueCount, $tempFolder, $helperFactory); - $fullExpectedStrategyClassName = 'Box\Spout\Reader\XLSX\Helper\SharedStringsCaching\\' . $expectedStrategyClassName; + $fullExpectedStrategyClassName = 'Box\Spout\Reader\XLSX\Manager\SharedStringsCaching\\' . $expectedStrategyClassName; $this->assertEquals($fullExpectedStrategyClassName, get_class($strategy)); $strategy->clearCache(); @@ -86,7 +87,7 @@ class CachingStrategyFactoryTest extends \PHPUnit_Framework_TestCase { /** @var CachingStrategyFactory|\PHPUnit_Framework_MockObject_MockObject $factoryStub */ $factoryStub = $this - ->getMockBuilder('\Box\Spout\Reader\XLSX\Helper\SharedStringsCaching\CachingStrategyFactory') + ->getMockBuilder('\Box\Spout\Reader\XLSX\Manager\SharedStringsCaching\CachingStrategyFactory') ->disableOriginalConstructor() ->setMethods(['getMemoryLimitFromIni']) ->getMock(); diff --git a/tests/Spout/Reader/XLSX/Manager/SharedStringsManagerTest.php b/tests/Spout/Reader/XLSX/Manager/SharedStringsManagerTest.php new file mode 100644 index 0000000..51a4097 --- /dev/null +++ b/tests/Spout/Reader/XLSX/Manager/SharedStringsManagerTest.php @@ -0,0 +1,156 @@ +sharedStringsManager = null; + } + + /** + * @return void + */ + public function tearDown() + { + if ($this->sharedStringsManager !== null) { + $this->sharedStringsManager->cleanup(); + } + } + + /** + * @param string $resourceName + * @return SharedStringsManager + */ + private function createSharedStringsManager($resourceName = 'one_sheet_with_shared_strings.xlsx') + { + $resourcePath = $this->getResourcePath($resourceName); + $tempFolder = sys_get_temp_dir(); + $cachingStrategyFactory = new CachingStrategyFactory(); + $helperFactory = new HelperFactory(); + $managerFactory = new ManagerFactory($helperFactory, $cachingStrategyFactory); + $entityFactory = new EntityFactory($managerFactory, $helperFactory); + + $this->sharedStringsManager = new SharedStringsManager($resourcePath, $tempFolder, $entityFactory, $helperFactory, $cachingStrategyFactory); + + return $this->sharedStringsManager; + } + + /** + * @expectedException \Box\Spout\Reader\Exception\SharedStringNotFoundException + * @return void + */ + public function testGetStringAtIndexShouldThrowExceptionIfStringNotFound() + { + $sharedStringsManager = $this->createSharedStringsManager(); + $sharedStringsManager->extractSharedStrings(); + $sharedStringsManager->getStringAtIndex(PHP_INT_MAX); + } + + /** + * @return void + */ + public function testGetStringAtIndexShouldReturnTheCorrectStringIfFound() + { + $sharedStringsManager = $this->createSharedStringsManager(); + $sharedStringsManager->extractSharedStrings(); + + $sharedString = $sharedStringsManager->getStringAtIndex(0); + $this->assertEquals('s1--A1', $sharedString); + + $sharedString = $sharedStringsManager->getStringAtIndex(24); + $this->assertEquals('s1--E5', $sharedString); + + $usedCachingStrategy = \ReflectionHelper::getValueOnObject($sharedStringsManager, 'cachingStrategy'); + $this->assertTrue($usedCachingStrategy instanceof InMemoryStrategy); + } + + /** + * @return void + */ + public function testGetStringAtIndexShouldWorkWithMultilineStrings() + { + $sharedStringsManager = $this->createSharedStringsManager('one_sheet_with_shared_multiline_strings.xlsx'); + + $sharedStringsManager->extractSharedStrings(); + + $sharedString = $sharedStringsManager->getStringAtIndex(0); + $this->assertEquals("s1\nA1", $sharedString); + + $sharedString = $sharedStringsManager->getStringAtIndex(24); + $this->assertEquals("s1\nE5", $sharedString); + } + + /** + * @return void + */ + public function testGetStringAtIndexShouldWorkWithStringsContainingTextAndHyperlinkInSameCell() + { + $sharedStringsManager = $this->createSharedStringsManager('one_sheet_with_shared_strings_containing_text_and_hyperlink_in_same_cell.xlsx'); + + $sharedStringsManager->extractSharedStrings(); + + $sharedString = $sharedStringsManager->getStringAtIndex(0); + $this->assertEquals('go to https://github.com please', $sharedString); + } + + /** + * @return void + */ + public function testGetStringAtIndexShouldNotDoubleDecodeHTMLEntities() + { + $sharedStringsManager = $this->createSharedStringsManager('one_sheet_with_pre_encoded_html_entities.xlsx'); + + $sharedStringsManager->extractSharedStrings(); + + $sharedString = $sharedStringsManager->getStringAtIndex(0); + $this->assertEquals('quote: " - ampersand: &', $sharedString); + } + + /** + * @return void + */ + public function testGetStringAtIndexWithFileBasedStrategy() + { + // force the file-based strategy by setting no memory limit + $originalMemoryLimit = ini_get('memory_limit'); + ini_set('memory_limit', '-1'); + + $sharedStringsManager = $this->createSharedStringsManager('sheet_with_lots_of_shared_strings.xlsx'); + + $sharedStringsManager->extractSharedStrings(); + + $sharedString = $sharedStringsManager->getStringAtIndex(0); + $this->assertEquals('str', $sharedString); + + $sharedString = $sharedStringsManager->getStringAtIndex(CachingStrategyFactory::MAX_NUM_STRINGS_PER_TEMP_FILE + 1); + $this->assertEquals('str', $sharedString); + + $usedCachingStrategy = \ReflectionHelper::getValueOnObject($sharedStringsManager, 'cachingStrategy'); + $this->assertTrue($usedCachingStrategy instanceof FileBasedStrategy); + + ini_set('memory_limit', $originalMemoryLimit); + } +} diff --git a/tests/Spout/Reader/XLSX/Helper/StyleHelperTest.php b/tests/Spout/Reader/XLSX/Manager/StyleManagerTest.php similarity index 54% rename from tests/Spout/Reader/XLSX/Helper/StyleHelperTest.php rename to tests/Spout/Reader/XLSX/Manager/StyleManagerTest.php index efbaedd..c9834f1 100644 --- a/tests/Spout/Reader/XLSX/Helper/StyleHelperTest.php +++ b/tests/Spout/Reader/XLSX/Manager/StyleManagerTest.php @@ -1,37 +1,41 @@ getMockBuilder('\Box\Spout\Reader\XLSX\Helper\StyleHelper') - ->setConstructorArgs(['/path/to/file.xlsx', $entityFactory]) - ->setMethods(['getCustomNumberFormats', 'getStylesAttributes']) - ->getMock(); + /** @var StyleManager $styleManager */ + $styleManager = $this->getMockBuilder('\Box\Spout\Reader\XLSX\Manager\StyleManager') + ->setConstructorArgs(['/path/to/file.xlsx', $entityFactory]) + ->setMethods(['getCustomNumberFormats', 'getStylesAttributes']) + ->getMock(); - $styleHelper->method('getStylesAttributes')->willReturn($styleAttributes); - $styleHelper->method('getCustomNumberFormats')->willReturn($customNumberFormats); + $styleManager->method('getStylesAttributes')->willReturn($styleAttributes); + $styleManager->method('getCustomNumberFormats')->willReturn($customNumberFormats); - return $styleHelper; + return $styleManager; } /** @@ -39,8 +43,8 @@ class StyleHelperTest extends \PHPUnit_Framework_TestCase */ public function testShouldFormatNumericValueAsDateWithDefaultStyle() { - $styleHelper = $this->getStyleHelperMock(); - $shouldFormatAsDate = $styleHelper->shouldFormatNumericValueAsDate(0); + $styleManager = $this->getStyleManagerMock(); + $shouldFormatAsDate = $styleManager->shouldFormatNumericValueAsDate(0); $this->assertFalse($shouldFormatAsDate); } @@ -49,8 +53,8 @@ class StyleHelperTest extends \PHPUnit_Framework_TestCase */ public function testShouldFormatNumericValueAsDateWhenShouldNotApplyNumberFormat() { - $styleHelper = $this->getStyleHelperMock([[], ['applyNumberFormat' => false, 'numFmtId' => 14]]); - $shouldFormatAsDate = $styleHelper->shouldFormatNumericValueAsDate(1); + $styleManager = $this->getStyleManagerMock([[], ['applyNumberFormat' => false, 'numFmtId' => 14]]); + $shouldFormatAsDate = $styleManager->shouldFormatNumericValueAsDate(1); $this->assertFalse($shouldFormatAsDate); } @@ -59,8 +63,8 @@ class StyleHelperTest extends \PHPUnit_Framework_TestCase */ public function testShouldFormatNumericValueAsDateWithGeneralFormat() { - $styleHelper = $this->getStyleHelperMock([[], ['applyNumberFormat' => true, 'numFmtId' => 0]]); - $shouldFormatAsDate = $styleHelper->shouldFormatNumericValueAsDate(1); + $styleManager = $this->getStyleManagerMock([[], ['applyNumberFormat' => true, 'numFmtId' => 0]]); + $shouldFormatAsDate = $styleManager->shouldFormatNumericValueAsDate(1); $this->assertFalse($shouldFormatAsDate); } @@ -69,8 +73,8 @@ class StyleHelperTest extends \PHPUnit_Framework_TestCase */ public function testShouldFormatNumericValueAsDateWithNonDateBuiltinFormat() { - $styleHelper = $this->getStyleHelperMock([[], ['applyNumberFormat' => true, 'numFmtId' => 9]]); - $shouldFormatAsDate = $styleHelper->shouldFormatNumericValueAsDate(1); + $styleManager = $this->getStyleManagerMock([[], ['applyNumberFormat' => true, 'numFmtId' => 9]]); + $shouldFormatAsDate = $styleManager->shouldFormatNumericValueAsDate(1); $this->assertFalse($shouldFormatAsDate); } @@ -79,8 +83,8 @@ class StyleHelperTest extends \PHPUnit_Framework_TestCase */ public function testShouldFormatNumericValueAsDateWithNoNumFmtId() { - $styleHelper = $this->getStyleHelperMock([[], ['applyNumberFormat' => true, 'numFmtId' => null]]); - $shouldFormatAsDate = $styleHelper->shouldFormatNumericValueAsDate(1); + $styleManager = $this->getStyleManagerMock([[], ['applyNumberFormat' => true, 'numFmtId' => null]]); + $shouldFormatAsDate = $styleManager->shouldFormatNumericValueAsDate(1); $this->assertFalse($shouldFormatAsDate); } @@ -92,8 +96,8 @@ class StyleHelperTest extends \PHPUnit_Framework_TestCase $builtinNumFmtIdsForDate = [14, 15, 16, 17, 18, 19, 20, 21, 22, 45, 46, 47]; foreach ($builtinNumFmtIdsForDate as $builtinNumFmtIdForDate) { - $styleHelper = $this->getStyleHelperMock([[], ['applyNumberFormat' => true, 'numFmtId' => $builtinNumFmtIdForDate]]); - $shouldFormatAsDate = $styleHelper->shouldFormatNumericValueAsDate(1); + $styleManager = $this->getStyleManagerMock([[], ['applyNumberFormat' => true, 'numFmtId' => $builtinNumFmtIdForDate]]); + $shouldFormatAsDate = $styleManager->shouldFormatNumericValueAsDate(1); $this->assertTrue($shouldFormatAsDate); } @@ -104,8 +108,8 @@ class StyleHelperTest extends \PHPUnit_Framework_TestCase */ public function testShouldFormatNumericValueAsDateWhenApplyNumberFormatNotSetAndUsingBuiltinDateFormat() { - $styleHelper = $this->getStyleHelperMock([[], ['applyNumberFormat' => null, 'numFmtId' => 14]]); - $shouldFormatAsDate = $styleHelper->shouldFormatNumericValueAsDate(1); + $styleManager = $this->getStyleManagerMock([[], ['applyNumberFormat' => null, 'numFmtId' => 14]]); + $shouldFormatAsDate = $styleManager->shouldFormatNumericValueAsDate(1); $this->assertTrue($shouldFormatAsDate); } @@ -115,8 +119,8 @@ class StyleHelperTest extends \PHPUnit_Framework_TestCase */ public function testShouldFormatNumericValueAsDateWhenApplyNumberFormatNotSetAndUsingBuiltinNonDateFormat() { - $styleHelper = $this->getStyleHelperMock([[], ['applyNumberFormat' => null, 'numFmtId' => 9]]); - $shouldFormatAsDate = $styleHelper->shouldFormatNumericValueAsDate(1); + $styleManager = $this->getStyleManagerMock([[], ['applyNumberFormat' => null, 'numFmtId' => 9]]); + $shouldFormatAsDate = $styleManager->shouldFormatNumericValueAsDate(1); $this->assertFalse($shouldFormatAsDate); } @@ -126,8 +130,8 @@ class StyleHelperTest extends \PHPUnit_Framework_TestCase */ public function testShouldFormatNumericValueAsDateWhenCustomNumberFormatNotFound() { - $styleHelper = $this->getStyleHelperMock([[], ['applyNumberFormat' => true, 'numFmtId' => 165]], [166 => []]); - $shouldFormatAsDate = $styleHelper->shouldFormatNumericValueAsDate(1); + $styleManager = $this->getStyleManagerMock([[], ['applyNumberFormat' => true, 'numFmtId' => 165]], [166 => []]); + $shouldFormatAsDate = $styleManager->shouldFormatNumericValueAsDate(1); $this->assertFalse($shouldFormatAsDate); } @@ -176,8 +180,8 @@ class StyleHelperTest extends \PHPUnit_Framework_TestCase public function testShouldFormatNumericValueAsDateWithCustomDateFormats($numberFormat, $expectedResult) { $numFmtId = 165; - $styleHelper = $this->getStyleHelperMock([[], ['applyNumberFormat' => true, 'numFmtId' => $numFmtId]], [$numFmtId => $numberFormat]); - $shouldFormatAsDate = $styleHelper->shouldFormatNumericValueAsDate(1); + $styleManager = $this->getStyleManagerMock([[], ['applyNumberFormat' => true, 'numFmtId' => $numFmtId]], [$numFmtId => $numberFormat]); + $shouldFormatAsDate = $styleManager->shouldFormatNumericValueAsDate(1); $this->assertEquals($expectedResult, $shouldFormatAsDate); }