120 lines
3.4 KiB
PHP
120 lines
3.4 KiB
PHP
<?php
|
|
|
|
namespace Box\Spout\Reader\Helper\XLSX;
|
|
|
|
use Box\Spout\TestUsingResource;
|
|
|
|
/**
|
|
* Class SharedStringsHelperTest
|
|
*
|
|
* @package Box\Spout\Reader\Helper\XLSX
|
|
*/
|
|
class SharedStringsHelperTest extends \PHPUnit_Framework_TestCase
|
|
{
|
|
use TestUsingResource;
|
|
|
|
/** @var SharedStringsHelper */
|
|
private $sharedStringsHelper;
|
|
|
|
/**
|
|
* @return void
|
|
*/
|
|
public function setUp()
|
|
{
|
|
$resourcePath = $this->getResourcePath('one_sheet_with_shared_strings.xlsx');
|
|
$this->sharedStringsHelper = new SharedStringsHelper($resourcePath);
|
|
}
|
|
|
|
/**
|
|
* @return void
|
|
*/
|
|
public function tearDown()
|
|
{
|
|
$this->sharedStringsHelper->cleanup();
|
|
}
|
|
|
|
/**
|
|
* @return void
|
|
*/
|
|
public function testExtractSharedStringsShouldCreateTempFileWithSharedStrings()
|
|
{
|
|
$this->sharedStringsHelper->extractSharedStrings();
|
|
|
|
$cachingStrategy = \ReflectionHelper::getValueOnObject($this->sharedStringsHelper, 'cachingStrategy');
|
|
$tempFolder = \ReflectionHelper::getValueOnObject($cachingStrategy, 'tempFolder');
|
|
|
|
$filesInTempFolder = $this->getFilesInFolder($tempFolder);
|
|
$this->assertEquals(1, count($filesInTempFolder), 'One temp file should have been created in the temp folder.');
|
|
|
|
$tempFileContents = file_get_contents($filesInTempFolder[0]);
|
|
$tempFileContentsPerLine = explode(PHP_EOL, $tempFileContents);
|
|
|
|
$this->assertEquals('s1--A1', $tempFileContentsPerLine[0]);
|
|
$this->assertEquals('s1--E5', $tempFileContentsPerLine[24]);
|
|
}
|
|
|
|
/**
|
|
* Returns all files that are in the given folder.
|
|
* It does not include "." and ".." and is not recursive.
|
|
*
|
|
* @param string $folderPath
|
|
* @return array
|
|
*/
|
|
private function getFilesInFolder($folderPath)
|
|
{
|
|
$files = [];
|
|
$directoryIterator = new \DirectoryIterator($folderPath);
|
|
|
|
foreach ($directoryIterator as $fileInfo) {
|
|
if ($fileInfo->isFile()) {
|
|
$files[] = $fileInfo->getPathname();
|
|
}
|
|
}
|
|
|
|
return $files;
|
|
}
|
|
|
|
/**
|
|
* @expectedException \Box\Spout\Reader\Exception\SharedStringNotFoundException
|
|
* @return void
|
|
*/
|
|
public function testGetStringAtIndexShouldThrowExceptionIfStringNotFound()
|
|
{
|
|
$this->sharedStringsHelper->extractSharedStrings();
|
|
$this->sharedStringsHelper->getStringAtIndex(PHP_INT_MAX);
|
|
}
|
|
|
|
/**
|
|
* @return void
|
|
*/
|
|
public function testGetStringAtIndexShouldReturnTheCorrectStringIfFound()
|
|
{
|
|
$this->sharedStringsHelper->extractSharedStrings();
|
|
|
|
$sharedString = $this->sharedStringsHelper->getStringAtIndex(0);
|
|
$this->assertEquals('s1--A1', $sharedString);
|
|
|
|
$sharedString = $this->sharedStringsHelper->getStringAtIndex(24);
|
|
$this->assertEquals('s1--E5', $sharedString);
|
|
}
|
|
|
|
/**
|
|
* @return void
|
|
*/
|
|
public function testGetStringAtIndexShouldWorkWithMultilineStrings()
|
|
{
|
|
$resourcePath = $this->getResourcePath('one_sheet_with_shared_multiline_strings.xlsx');
|
|
$sharedStringsHelper = new SharedStringsHelper($resourcePath);
|
|
|
|
$sharedStringsHelper->extractSharedStrings();
|
|
|
|
$sharedString = $sharedStringsHelper->getStringAtIndex(0);
|
|
$this->assertEquals("s1\nA1", $sharedString);
|
|
|
|
$sharedString = $sharedStringsHelper->getStringAtIndex(24);
|
|
$this->assertEquals("s1\nE5", $sharedString);
|
|
|
|
$sharedStringsHelper->cleanup();
|
|
}
|
|
}
|