Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
2074781852 | ||
|
|
fd5cfc5440 | ||
|
|
a538813827 | ||
|
|
3e5ef284a5 | ||
|
|
f770f38967 | ||
|
|
0edd9c434f | ||
|
|
3c9bf12a5f | ||
|
|
69c1436625 | ||
|
|
af940069bc | ||
|
|
d6155a4243 | ||
|
|
538f6109ad | ||
|
|
93cdd398dd | ||
|
|
66e5792a7e | ||
|
|
d4dbc32a53 | ||
|
|
09eac62c22 | ||
|
|
7bb5e02d2c | ||
|
|
aebdd1acd7 | ||
|
|
41a449f245 | ||
|
|
39c72a91b4 | ||
|
|
4ca1fc5851 | ||
|
|
419544a85f | ||
|
|
6e11a043c1 | ||
|
|
c24cdbb9be | ||
|
|
18f6e64910 |
No files matched your search
@@ -0,0 +1,15 @@
|
||||
root = true
|
||||
|
||||
[*]
|
||||
charset = utf-8
|
||||
end_of_line = lf
|
||||
indent_style = space
|
||||
indent_size = 4
|
||||
trim_trailing_whitespace = true
|
||||
insert_final_newline = true
|
||||
|
||||
[{.travis.yml}]
|
||||
indent_size = 2
|
||||
|
||||
[*.md]
|
||||
trim_trailing_whitespace = false
|
||||
@@ -149,7 +149,7 @@ $writer->setShouldCreateNewSheetsAutomatically(true); // default value
|
||||
$writer->setShouldCreateNewSheetsAutomatically(false); // will stop writing new data when limit is reached
|
||||
```
|
||||
|
||||
### Using custom temporary folder
|
||||
#### Using custom temporary folder
|
||||
|
||||
Processing XLSX files require temporary files to be created. By default, Spout will use the system default temporary folder (as returned by sys_get_temp_dir()). It is possible to override this by explicitly setting it on the reader or writer:
|
||||
```php
|
||||
|
||||
+1
-1
@@ -2,7 +2,7 @@
|
||||
"name": "box/spout",
|
||||
"description": "PHP Library to read and write CSV and XLSX files, in a fast and scalable way",
|
||||
"type": "library",
|
||||
"version": "1.0.1",
|
||||
"version": "1.0.4",
|
||||
"keywords": ["php","read","write","csv","xlsx","excel","spreadsheet","scale","memory","stream","ooxml"],
|
||||
"license": "Apache-2.0",
|
||||
"homepage": "https://www.github.com/box/spout",
|
||||
|
||||
@@ -27,7 +27,7 @@ class XLSX implements EscaperInterface
|
||||
public function escape($string)
|
||||
{
|
||||
$escapedString = $this->escapeControlCharacters($string);
|
||||
$escapedString = htmlspecialchars($escapedString, ENT_QUOTES, 'utf-8');
|
||||
$escapedString = htmlspecialchars($escapedString, ENT_QUOTES);
|
||||
|
||||
return $escapedString;
|
||||
}
|
||||
|
||||
@@ -52,7 +52,7 @@ class CSV extends AbstractReader
|
||||
* The file must be UTF-8 encoded.
|
||||
* @TODO add encoding detection/conversion
|
||||
*
|
||||
* @param string $filePath Path of the XLSX file to be read
|
||||
* @param string $filePath Path of the CSV file to be read
|
||||
* @return void
|
||||
* @throws \Box\Spout\Common\Exception\IOException
|
||||
*/
|
||||
|
||||
@@ -31,6 +31,9 @@ class SharedStringsHelper
|
||||
*/
|
||||
const MAX_NUM_STRINGS_PER_TEMP_FILE = 10000;
|
||||
|
||||
/** Value to use to escape the line feed character ("\n") */
|
||||
const ESCAPED_LINE_FEED_CHARACTER = '_x000A_';
|
||||
|
||||
/** @var string Path of the XLSX file being read */
|
||||
protected $filePath;
|
||||
|
||||
@@ -80,7 +83,6 @@ class SharedStringsHelper
|
||||
* Please note that SimpleXML does not provide such a functionality but since it is faster
|
||||
* and more handy to parse few XML nodes, it is used in combination with XMLReader for that purpose.
|
||||
*
|
||||
* @param string $filePath
|
||||
* @return void
|
||||
* @throws \Box\Spout\Common\Exception\IOException If sharedStrings.xml can't be read
|
||||
*/
|
||||
@@ -120,7 +122,12 @@ class SharedStringsHelper
|
||||
}
|
||||
|
||||
$unescapedTextValue = $escaper->unescape($textValue);
|
||||
$this->writeSharedStringToTempFile($unescapedTextValue, $sharedStringIndex);
|
||||
|
||||
// The shared string retrieval logic expects each cell data to be on one line only
|
||||
// Encoding the line feed character allows to preserve this assumption
|
||||
$lineFeedEncodedTextValue = $this->escapeLineFeed($unescapedTextValue);
|
||||
|
||||
$this->writeSharedStringToTempFile($lineFeedEncodedTextValue, $sharedStringIndex);
|
||||
|
||||
$sharedStringIndex++;
|
||||
|
||||
@@ -246,16 +253,39 @@ class SharedStringsHelper
|
||||
|
||||
$sharedString = null;
|
||||
if (array_key_exists($indexInFile, $this->inMemoryTempFileContents)) {
|
||||
$sharedString = $this->inMemoryTempFileContents[$indexInFile];
|
||||
$escapedSharedString = $this->inMemoryTempFileContents[$indexInFile];
|
||||
$sharedString = $this->unescapeLineFeed($escapedSharedString);
|
||||
}
|
||||
|
||||
if (!$sharedString) {
|
||||
if ($sharedString === null) {
|
||||
throw new SharedStringNotFoundException("Shared string not found for index: $sharedStringIndex");
|
||||
}
|
||||
|
||||
return rtrim($sharedString, PHP_EOL);
|
||||
}
|
||||
|
||||
/**
|
||||
* Escapes the line feed character (\n)
|
||||
*
|
||||
* @param string $unescapedString
|
||||
* @return string
|
||||
*/
|
||||
private function escapeLineFeed($unescapedString)
|
||||
{
|
||||
return str_replace("\n", self::ESCAPED_LINE_FEED_CHARACTER, $unescapedString);
|
||||
}
|
||||
|
||||
/**
|
||||
* Unescapes the line feed character (\n)
|
||||
*
|
||||
* @param string $escapedString
|
||||
* @return string
|
||||
*/
|
||||
private function unescapeLineFeed($escapedString)
|
||||
{
|
||||
return str_replace(self::ESCAPED_LINE_FEED_CHARACTER, "\n", $escapedString);
|
||||
}
|
||||
|
||||
/**
|
||||
* Deletes the created temporary folder and all its contents
|
||||
*
|
||||
|
||||
@@ -31,7 +31,7 @@ class Worksheet
|
||||
*/
|
||||
public function getDataXmlFilePath()
|
||||
{
|
||||
return ltrim($this->dataXmlFilePath, DIRECTORY_SEPARATOR);
|
||||
return ltrim($this->dataXmlFilePath, '/');
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -39,6 +39,7 @@ abstract class AbstractWriter implements WriterInterface
|
||||
|
||||
/**
|
||||
* Adds data to the currently openned writer.
|
||||
* The data must be UTF-8 encoded.
|
||||
*
|
||||
* @param array $dataRow Array containing data to be streamed.
|
||||
* Example $dataRow = ['data1', 1234, null, '', 'data5'];
|
||||
@@ -135,9 +136,11 @@ abstract class AbstractWriter implements WriterInterface
|
||||
|
||||
/**
|
||||
* Write given data to the output. New data will be appended to end of stream.
|
||||
* The data must be UTF-8 encoded.
|
||||
*
|
||||
* @param array $dataRow Array containing data to be streamed.
|
||||
* Example $dataRow = ['data1', 1234, null, '', 'data5'];
|
||||
* If empty, no data is added (i.e. not even as a blank row)
|
||||
* Example: $dataRow = ['data1', 1234, null, '', 'data5', false];
|
||||
*
|
||||
* @return \Box\Spout\Writer\AbstractWriter
|
||||
* @throws \Box\Spout\Writer\Exception\WriterNotOpenedException If this function is called before opening the writer
|
||||
@@ -146,7 +149,10 @@ abstract class AbstractWriter implements WriterInterface
|
||||
public function addRow(array $dataRow)
|
||||
{
|
||||
if ($this->isWriterOpened) {
|
||||
$this->addRowToWriter($dataRow);
|
||||
// empty $dataRow should not add an empty line
|
||||
if (!empty($dataRow)) {
|
||||
$this->addRowToWriter($dataRow);
|
||||
}
|
||||
} else {
|
||||
throw new WriterNotOpenedException('The writer needs to be opened before adding row.');
|
||||
}
|
||||
@@ -156,12 +162,14 @@ abstract class AbstractWriter implements WriterInterface
|
||||
|
||||
/**
|
||||
* Write given data to the output. New data will be appended to end of stream.
|
||||
* The data must be UTF-8 encoded.
|
||||
*
|
||||
* @param array $dataRows Array of array containing data to be streamed.
|
||||
* Example $dataRow = [
|
||||
* ['data11', 12, , '', 'data13'],
|
||||
* ['data21', 'data22', null],
|
||||
* ];
|
||||
* If a row is empty, it won't be added (i.e. not even as a blank row)
|
||||
* Example: $dataRows = [
|
||||
* ['data11', 12, , '', 'data13'],
|
||||
* ['data21', 'data22', null, false],
|
||||
* ];
|
||||
*
|
||||
* @return \Box\Spout\Writer\AbstractWriter
|
||||
* @throws \Box\Spout\Common\Exception\InvalidArgumentException If the input param is not valid
|
||||
|
||||
@@ -34,4 +34,38 @@ class CellHelper
|
||||
|
||||
return $cellIndex;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $value
|
||||
* @return bool Whether the given value is a non empty string
|
||||
*/
|
||||
public static function isNonEmptyString($value)
|
||||
{
|
||||
return (gettype($value) === 'string' && $value !== '');
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns whether the given value is numeric.
|
||||
* A numeric value is from type "integer" or "double" ("float" is not returned by gettype).
|
||||
*
|
||||
* @param $value
|
||||
* @return bool Whether the given value is numeric
|
||||
*/
|
||||
public static function isNumeric($value)
|
||||
{
|
||||
$valueType = gettype($value);
|
||||
return ($valueType === 'integer' || $valueType === 'double');
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns whether the given value is boolean.
|
||||
* "true"/"false" and 0/1 are not booleans.
|
||||
*
|
||||
* @param $value
|
||||
* @return bool Whether the given value is boolean
|
||||
*/
|
||||
public static function isBoolean($value)
|
||||
{
|
||||
return gettype($value) === 'boolean';
|
||||
}
|
||||
}
|
||||
@@ -5,7 +5,6 @@ namespace Box\Spout\Writer\Internal\XLSX;
|
||||
use Box\Spout\Writer\Exception\SheetNotFoundException;
|
||||
use Box\Spout\Writer\Helper\XLSX\FileSystemHelper;
|
||||
use Box\Spout\Writer\Helper\XLSX\SharedStringsHelper;
|
||||
use Box\Spout\Writer\Helper\XLSX\ZipHelper;
|
||||
use Box\Spout\Writer\Sheet;
|
||||
|
||||
/**
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
|
||||
namespace Box\Spout\Writer\Internal\XLSX;
|
||||
|
||||
use Box\Spout\Common\Exception\InvalidArgumentException;
|
||||
use Box\Spout\Common\Exception\IOException;
|
||||
use Box\Spout\Writer\Helper\XLSX\CellHelper;
|
||||
|
||||
@@ -42,7 +43,8 @@ EOD;
|
||||
|
||||
/**
|
||||
* @param \Box\Spout\Writer\Sheet $externalSheet The associated "external" sheet
|
||||
* @param string $tempFolder Temporary folder where the files to create the XLSX will be stored
|
||||
* @param string $worksheetFilesFolder Temporary folder where the files to create the XLSX will be stored
|
||||
* @param \Box\Spout\Writer\Helper\XLSX\SharedStringsHelper $sharedStringsHelper Helper for shared strings
|
||||
* @param bool $shouldUseInlineStrings Whether inline or shared strings should be used
|
||||
* @throws \Box\Spout\Common\Exception\IOException If the sheet data file cannot be opened for writing
|
||||
*/
|
||||
@@ -118,6 +120,7 @@ EOD;
|
||||
* Example $dataRow = ['data1', 1234, null, '', 'data5'];
|
||||
* @return void
|
||||
* @throws \Box\Spout\Common\Exception\IOException If the data cannot be written
|
||||
* @throws \Box\Spout\Common\Exception\InvalidArgumentException If a cell value's type is not supported
|
||||
*/
|
||||
public function addRow($dataRow)
|
||||
{
|
||||
@@ -131,19 +134,19 @@ EOD;
|
||||
$columnIndex = CellHelper::getCellIndexFromColumnIndex($cellNumber);
|
||||
$data .= ' <c r="' . $columnIndex . $rowIndex . '"';
|
||||
|
||||
if (empty($cellValue)) {
|
||||
if (CellHelper::isNonEmptyString($cellValue)) {
|
||||
if ($this->shouldUseInlineStrings) {
|
||||
$data .= ' t="inlineStr"><is><t>' . $this->stringsEscaper->escape($cellValue) . '</t></is></c>' . PHP_EOL;
|
||||
} else {
|
||||
$sharedStringId = $this->sharedStringsHelper->writeString($cellValue);
|
||||
$data .= ' t="s"><v>' . $sharedStringId . '</v></c>' . PHP_EOL;
|
||||
}
|
||||
} else if (CellHelper::isNumeric($cellValue) || CellHelper::isBoolean($cellValue)) {
|
||||
$data .= '><v>' . $cellValue . '</v></c>' . PHP_EOL;
|
||||
} else if (empty($cellValue)) {
|
||||
$data .= '/>' . PHP_EOL;
|
||||
} else {
|
||||
if (is_numeric($cellValue)) {
|
||||
$data .= '><v>' . $cellValue . '</v></c>' . PHP_EOL;
|
||||
} else {
|
||||
if ($this->shouldUseInlineStrings) {
|
||||
$data .= ' t="inlineStr"><is><t>' . $this->stringsEscaper->escape($cellValue) . '</t></is></c>' . PHP_EOL;
|
||||
} else {
|
||||
$sharedStringId = $this->sharedStringsHelper->writeString($cellValue);
|
||||
$data .= ' t="s"><v>' . $sharedStringId . '</v></c>' . PHP_EOL;
|
||||
}
|
||||
}
|
||||
throw new InvalidArgumentException('Trying to add a value with an unsupported type: ' . gettype($cellValue));
|
||||
}
|
||||
|
||||
$cellNumber++;
|
||||
|
||||
@@ -46,7 +46,7 @@ class SharedStringsHelperTest extends \PHPUnit_Framework_TestCase
|
||||
$this->assertEquals(1, count($filesInTempFolder), 'One temp file should have been created in the temp folder.');
|
||||
|
||||
$tempFileContents = file_get_contents($filesInTempFolder[0]);
|
||||
$tempFileContentsPerLine = explode("\n", $tempFileContents);
|
||||
$tempFileContentsPerLine = explode(PHP_EOL, $tempFileContents);
|
||||
|
||||
$this->assertEquals('s1--A1', $tempFileContentsPerLine[0]);
|
||||
$this->assertEquals('s1--E5', $tempFileContentsPerLine[24]);
|
||||
@@ -96,4 +96,23 @@ class SharedStringsHelperTest extends \PHPUnit_Framework_TestCase
|
||||
$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();
|
||||
}
|
||||
}
|
||||
@@ -140,6 +140,19 @@ class XLSXTest extends \PHPUnit_Framework_TestCase
|
||||
$this->assertEquals($expectedRows, $allRows);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
public function testReadShouldSupportEmptySharedString()
|
||||
{
|
||||
$allRows = $this->getAllRowsForFile('sheet_with_empty_shared_string.xlsx');
|
||||
|
||||
$expectedRows = [
|
||||
['s1--A1', '', 's1--C1'],
|
||||
];
|
||||
$this->assertEquals($expectedRows, $allRows);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
|
||||
@@ -76,6 +76,22 @@ class CSVTest extends \PHPUnit_Framework_TestCase
|
||||
$this->assertEquals('csv--11,,csv--13', $writtenContent, 'The null values should be replaced by empty values');
|
||||
}
|
||||
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
public function testWriteShouldSkipEmptyRows()
|
||||
{
|
||||
$allRows = [
|
||||
['csv--11', 'csv--12'],
|
||||
[],
|
||||
['csv--31', 'csv--32'],
|
||||
];
|
||||
$writtenContent = $this->writeToCsvFileAndReturnWrittenContent($allRows, 'csv_with_empty_rows.csv');
|
||||
$writtenContent = $this->trimWrittenContent($writtenContent);
|
||||
|
||||
$this->assertEquals("csv--11,csv--12\ncsv--31,csv--32", $writtenContent, 'Empty rows should be skipped');
|
||||
}
|
||||
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
@@ -135,6 +151,6 @@ class CSVTest extends \PHPUnit_Framework_TestCase
|
||||
private function trimWrittenContent($writtenContent)
|
||||
{
|
||||
// remove line feeds and UTF-8 BOM
|
||||
return trim($writtenContent, "\n" . CSV::UTF8_BOM);
|
||||
return trim($writtenContent, PHP_EOL . CSV::UTF8_BOM);
|
||||
}
|
||||
}
|
||||
@@ -34,4 +34,62 @@ class CellHelperTest extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
$this->assertEquals($expectedCellIndex, CellHelper::getCellIndexFromColumnIndex($columnIndex));
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
public function testIsNonEmptyString()
|
||||
{
|
||||
$this->assertTrue(CellHelper::isNonEmptyString("string"));
|
||||
|
||||
$this->assertFalse(CellHelper::isNonEmptyString(""));
|
||||
$this->assertFalse(CellHelper::isNonEmptyString(0));
|
||||
$this->assertFalse(CellHelper::isNonEmptyString(1));
|
||||
$this->assertFalse(CellHelper::isNonEmptyString(true));
|
||||
$this->assertFalse(CellHelper::isNonEmptyString(false));
|
||||
$this->assertFalse(CellHelper::isNonEmptyString(["string"]));
|
||||
$this->assertFalse(CellHelper::isNonEmptyString(new \stdClass()));
|
||||
$this->assertFalse(CellHelper::isNonEmptyString(null));
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
public function testIsNumeric()
|
||||
{
|
||||
$this->assertTrue(CellHelper::isNumeric(0));
|
||||
$this->assertTrue(CellHelper::isNumeric(10));
|
||||
$this->assertTrue(CellHelper::isNumeric(10.1));
|
||||
$this->assertTrue(CellHelper::isNumeric(10.10000000000000000000001));
|
||||
$this->assertTrue(CellHelper::isNumeric(0x539));
|
||||
$this->assertTrue(CellHelper::isNumeric(02471));
|
||||
$this->assertTrue(CellHelper::isNumeric(0b10100111001));
|
||||
$this->assertTrue(CellHelper::isNumeric(1337e0));
|
||||
|
||||
$this->assertFalse(CellHelper::isNumeric("0"));
|
||||
$this->assertFalse(CellHelper::isNumeric("42"));
|
||||
$this->assertFalse(CellHelper::isNumeric(true));
|
||||
$this->assertFalse(CellHelper::isNumeric([2]));
|
||||
$this->assertFalse(CellHelper::isNumeric(new \stdClass()));
|
||||
$this->assertFalse(CellHelper::isNumeric(null));
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
public function testIsBoolean()
|
||||
{
|
||||
$this->assertTrue(CellHelper::isBoolean(true));
|
||||
$this->assertTrue(CellHelper::isBoolean(false));
|
||||
|
||||
$this->assertFalse(CellHelper::isBoolean(0));
|
||||
$this->assertFalse(CellHelper::isBoolean(1));
|
||||
$this->assertFalse(CellHelper::isBoolean("0"));
|
||||
$this->assertFalse(CellHelper::isBoolean("1"));
|
||||
$this->assertFalse(CellHelper::isBoolean("true"));
|
||||
$this->assertFalse(CellHelper::isBoolean("false"));
|
||||
$this->assertFalse(CellHelper::isBoolean([true]));
|
||||
$this->assertFalse(CellHelper::isBoolean(new \stdClass()));
|
||||
$this->assertFalse(CellHelper::isBoolean(null));
|
||||
}
|
||||
}
|
||||
@@ -49,6 +49,19 @@ class XLSXTest extends \PHPUnit_Framework_TestCase
|
||||
$writer->close();
|
||||
}
|
||||
|
||||
/**
|
||||
* @expectedException \Box\Spout\Common\Exception\InvalidArgumentException
|
||||
*/
|
||||
public function testAddRowShouldThrowExceptionIfUnsupportedDataTypePassedIn()
|
||||
{
|
||||
$fileName = 'test_add_row_should_throw_exception_if_unsupported_data_type_passed_in.xlsx';
|
||||
$dataRows = [
|
||||
[new \stdClass()],
|
||||
];
|
||||
|
||||
$this->writeToXLSXFile($dataRows, $fileName);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
@@ -96,7 +109,7 @@ class XLSXTest extends \PHPUnit_Framework_TestCase
|
||||
*/
|
||||
public function testAddRowShouldWriteGivenDataToSheetUsingInlineStrings()
|
||||
{
|
||||
$fileName = 'test_add_row_should_write_given_data_to_sheet.xlsx';
|
||||
$fileName = 'test_add_row_should_write_given_data_to_sheet_using_inline_strings.xlsx';
|
||||
$dataRows = [
|
||||
['xlsx--11', 'xlsx--12'],
|
||||
['xlsx--21', 'xlsx--22', 'xlsx--23'],
|
||||
@@ -106,7 +119,7 @@ class XLSXTest extends \PHPUnit_Framework_TestCase
|
||||
|
||||
foreach ($dataRows as $dataRow) {
|
||||
foreach ($dataRow as $cellValue) {
|
||||
$this->assertInlineStringWasWrittenToSheet($fileName, 1, $cellValue);
|
||||
$this->assertInlineDataWasWrittenToSheet($fileName, 1, $cellValue);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -116,7 +129,7 @@ class XLSXTest extends \PHPUnit_Framework_TestCase
|
||||
*/
|
||||
public function testAddRowShouldWriteGivenDataToTwoSheetUsingInlineStrings()
|
||||
{
|
||||
$fileName = 'test_add_row_should_write_given_data_to_sheet.xlsx';
|
||||
$fileName = 'test_add_row_should_write_given_data_to_two_sheets_using_inline_strings.xlsx';
|
||||
$dataRows = [
|
||||
['xlsx--11', 'xlsx--12'],
|
||||
['xlsx--21', 'xlsx--22', 'xlsx--23'],
|
||||
@@ -128,7 +141,7 @@ class XLSXTest extends \PHPUnit_Framework_TestCase
|
||||
for ($i = 1; $i <= $numSheets; $i++) {
|
||||
foreach ($dataRows as $dataRow) {
|
||||
foreach ($dataRow as $cellValue) {
|
||||
$this->assertInlineStringWasWrittenToSheet($fileName, $numSheets, $cellValue);
|
||||
$this->assertInlineDataWasWrittenToSheet($fileName, $numSheets, $cellValue);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -139,7 +152,7 @@ class XLSXTest extends \PHPUnit_Framework_TestCase
|
||||
*/
|
||||
public function testAddRowShouldWriteGivenDataToSheetUsingSharedStrings()
|
||||
{
|
||||
$fileName = 'test_add_row_should_write_given_data_to_sheet.xlsx';
|
||||
$fileName = 'test_add_row_should_write_given_data_to_sheet_using_shared_strings.xlsx';
|
||||
$dataRows = [
|
||||
['xlsx--11', 'xlsx--12'],
|
||||
['xlsx--21', 'xlsx--22', 'xlsx--23'],
|
||||
@@ -159,7 +172,7 @@ class XLSXTest extends \PHPUnit_Framework_TestCase
|
||||
*/
|
||||
public function testAddRowShouldWriteGivenDataToTwoSheetUsingSharedStrings()
|
||||
{
|
||||
$fileName = 'test_add_row_should_write_given_data_to_two_sheet_using_shared_strings.xlsx';
|
||||
$fileName = 'test_add_row_should_write_given_data_to_two_sheets_using_shared_strings.xlsx';
|
||||
$dataRows = [
|
||||
['xlsx--11', 'xlsx--12'],
|
||||
['xlsx--21', 'xlsx--22', 'xlsx--23'],
|
||||
@@ -177,6 +190,24 @@ class XLSXTest extends \PHPUnit_Framework_TestCase
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
public function testAddRowShouldSupportMultipleTypesOfData()
|
||||
{
|
||||
$fileName = 'test_add_row_should_support_multiple_types_of_data.xlsx';
|
||||
$dataRows = [
|
||||
['xlsx--11', true, '', 0, 10.2, null],
|
||||
];
|
||||
|
||||
$this->writeToXLSXFile($dataRows, $fileName, $shouldUseInlineStrings = false);
|
||||
|
||||
$this->assertSharedStringWasWritten($fileName, 'xlsx--11');
|
||||
$this->assertInlineDataWasWrittenToSheet($fileName, 1, 1); // true is converted to 1
|
||||
$this->assertInlineDataWasWrittenToSheet($fileName, 1, 0);
|
||||
$this->assertInlineDataWasWrittenToSheet($fileName, 1, 10.2);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
@@ -219,17 +250,17 @@ class XLSXTest extends \PHPUnit_Framework_TestCase
|
||||
|
||||
foreach ($dataRowsSheet1 as $dataRow) {
|
||||
foreach ($dataRow as $cellValue) {
|
||||
$this->assertInlineStringWasWrittenToSheet($fileName, 1, $cellValue, 'Data should have been written in Sheet 1');
|
||||
$this->assertInlineDataWasWrittenToSheet($fileName, 1, $cellValue, 'Data should have been written in Sheet 1');
|
||||
}
|
||||
}
|
||||
foreach ($dataRowsSheet2 as $dataRow) {
|
||||
foreach ($dataRow as $cellValue) {
|
||||
$this->assertInlineStringWasWrittenToSheet($fileName, 2, $cellValue, 'Data should have been written in Sheet 2');
|
||||
$this->assertInlineDataWasWrittenToSheet($fileName, 2, $cellValue, 'Data should have been written in Sheet 2');
|
||||
}
|
||||
}
|
||||
foreach ($dataRowsSheet1Again as $dataRow) {
|
||||
foreach ($dataRow as $cellValue) {
|
||||
$this->assertInlineStringWasWrittenToSheet($fileName, 1, $cellValue, 'Data should have been written in Sheet 1');
|
||||
$this->assertInlineDataWasWrittenToSheet($fileName, 1, $cellValue, 'Data should have been written in Sheet 1');
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -252,8 +283,8 @@ class XLSXTest extends \PHPUnit_Framework_TestCase
|
||||
$writer = $this->writeToXLSXFile($dataRows, $fileName, true, $shouldCreateSheetsAutomatically = true);
|
||||
$this->assertEquals(2, count($writer->getSheets()), '2 sheets should have been created.');
|
||||
|
||||
$this->assertInlineStringWasNotWrittenToSheet($fileName, 1, 'xlsx--sheet2--11');
|
||||
$this->assertInlineStringWasWrittenToSheet($fileName, 2, 'xlsx--sheet2--11');
|
||||
$this->assertInlineDataWasNotWrittenToSheet($fileName, 1, 'xlsx--sheet2--11');
|
||||
$this->assertInlineDataWasWrittenToSheet($fileName, 2, 'xlsx--sheet2--11');
|
||||
|
||||
\ReflectionHelper::reset();
|
||||
}
|
||||
@@ -276,7 +307,7 @@ class XLSXTest extends \PHPUnit_Framework_TestCase
|
||||
$writer = $this->writeToXLSXFile($dataRows, $fileName, true, $shouldCreateSheetsAutomatically = false);
|
||||
$this->assertEquals(1, count($writer->getSheets()), 'Only 1 sheet should have been created.');
|
||||
|
||||
$this->assertInlineStringWasNotWrittenToSheet($fileName, 1, 'xlsx--sheet1--31');
|
||||
$this->assertInlineDataWasNotWrittenToSheet($fileName, 1, 'xlsx--sheet1--31');
|
||||
|
||||
\ReflectionHelper::reset();
|
||||
}
|
||||
@@ -293,8 +324,8 @@ class XLSXTest extends \PHPUnit_Framework_TestCase
|
||||
|
||||
$this->writeToXLSXFile($dataRows, $fileName);
|
||||
|
||||
$this->assertInlineStringWasWrittenToSheet($fileName, 1, 'I'm in "great" mood', 'Quotes should be escaped');
|
||||
$this->assertInlineStringWasWrittenToSheet($fileName, 1, 'This <must> be escaped & tested', '<, > and & should be escaped');
|
||||
$this->assertInlineDataWasWrittenToSheet($fileName, 1, 'I'm in "great" mood', 'Quotes should be escaped');
|
||||
$this->assertInlineDataWasWrittenToSheet($fileName, 1, 'This <must> be escaped & tested', '<, > and & should be escaped');
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -309,7 +340,7 @@ class XLSXTest extends \PHPUnit_Framework_TestCase
|
||||
|
||||
$this->writeToXLSXFile($dataRows, $fileName);
|
||||
|
||||
$this->assertInlineStringWasWrittenToSheet($fileName, 1, 'control's _x0015_ "character"');
|
||||
$this->assertInlineDataWasWrittenToSheet($fileName, 1, 'control's _x0015_ "character"');
|
||||
}
|
||||
|
||||
|
||||
@@ -371,33 +402,33 @@ class XLSXTest extends \PHPUnit_Framework_TestCase
|
||||
/**
|
||||
* @param string $fileName
|
||||
* @param int $sheetNumber
|
||||
* @param string $inlineString
|
||||
* @param mixed $inlineData
|
||||
* @param string $message
|
||||
* @return void
|
||||
*/
|
||||
private function assertInlineStringWasWrittenToSheet($fileName, $sheetNumber, $inlineString, $message = '')
|
||||
private function assertInlineDataWasWrittenToSheet($fileName, $sheetNumber, $inlineData, $message = '')
|
||||
{
|
||||
$resourcePath = $this->getGeneratedResourcePath($fileName);
|
||||
$pathToSheetFile = $resourcePath . '#xl/worksheets/sheet' . $sheetNumber . '.xml';
|
||||
$xmlContents = file_get_contents('zip://' . $pathToSheetFile);
|
||||
|
||||
$this->assertContains($inlineString, $xmlContents, $message);
|
||||
$this->assertContains((string)$inlineData, $xmlContents, $message);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $fileName
|
||||
* @param int $sheetNumber
|
||||
* @param string $inlineString
|
||||
* @param mixed $inlineData
|
||||
* @param string $message
|
||||
* @return void
|
||||
*/
|
||||
private function assertInlineStringWasNotWrittenToSheet($fileName, $sheetNumber, $inlineString, $message = '')
|
||||
private function assertInlineDataWasNotWrittenToSheet($fileName, $sheetNumber, $inlineData, $message = '')
|
||||
{
|
||||
$resourcePath = $this->getGeneratedResourcePath($fileName);
|
||||
$pathToSheetFile = $resourcePath . '#xl/worksheets/sheet' . $sheetNumber . '.xml';
|
||||
$xmlContents = file_get_contents('zip://' . $pathToSheetFile);
|
||||
|
||||
$this->assertNotContains($inlineString, $xmlContents, $message);
|
||||
$this->assertNotContains((string)$inlineData, $xmlContents, $message);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Binary file not shown.
Binary file not shown.
Reference in new issue
Block a user