Compare commits

...
5 Commits
Author SHA1 Message Date
Adrien Loison e9cd7a397e Merge pull request #199 from box/check_valid_resource_on_close
Check file handle is valid before manipulating it
2016-04-12 11:03:15 -07:00
Adrien Loison 8bb42ebc23 Check file handle is valid before manipulating it
This fixes issues when something went wrong on reader/writer init and the developer wants to close the reader/writer.
The file handle may not be defined so we need to add a check for it, before actually using it.
2016-04-12 10:36:59 -07:00
Adrien Loison 71a6f6a937 Merge pull request #196 from madflow/trim-inline-strings
Fix #195
2016-04-09 13:20:48 -07:00
madflow 616925148e Renamed xlsx file, #195 2016-04-07 08:58:54 +02:00
madflow 6f0f7c9690 Fix #195 2016-04-06 22:00:47 +02:00
11 changed files with 112 additions and 2 deletions

No files matched your search

@@ -118,8 +118,7 @@ class CellValueFormatter
// inline strings are formatted this way: // inline strings are formatted this way:
// <c r="A1" t="inlineStr"><is><t>[INLINE_STRING]</t></is></c> // <c r="A1" t="inlineStr"><is><t>[INLINE_STRING]</t></is></c>
$tNode = $node->getElementsByTagName(self::XML_NODE_INLINE_STRING_VALUE)->item(0); $tNode = $node->getElementsByTagName(self::XML_NODE_INLINE_STRING_VALUE)->item(0);
$escapedCellValue = trim($tNode->nodeValue); $cellValue = $this->escaper->unescape($tNode->nodeValue);
$cellValue = $this->escaper->unescape($escapedCellValue);
return $cellValue; return $cellValue;
} }
@@ -218,6 +218,10 @@ class Worksheet implements WorksheetInterface
*/ */
public function close() public function close()
{ {
if (!is_resource($this->sheetFilePointer)) {
return;
}
fclose($this->sheetFilePointer); fclose($this->sheetFilePointer);
} }
} }
@@ -88,6 +88,10 @@ EOD;
*/ */
public function close() public function close()
{ {
if (!is_resource($this->sharedStringsFilePointer)) {
return;
}
fwrite($this->sharedStringsFilePointer, '</sst>'); fwrite($this->sharedStringsFilePointer, '</sst>');
// Replace the default strings count with the actual number of shared strings in the file header // Replace the default strings count with the actual number of shared strings in the file header
@@ -178,6 +178,10 @@ EOD;
*/ */
public function close() public function close()
{ {
if (!is_resource($this->sheetFilePointer)) {
return;
}
fwrite($this->sheetFilePointer, '</sheetData>'); fwrite($this->sheetFilePointer, '</sheetData>');
fwrite($this->sheetFilePointer, '</worksheet>'); fwrite($this->sheetFilePointer, '</worksheet>');
fclose($this->sheetFilePointer); fclose($this->sheetFilePointer);
+17
View File
@@ -384,6 +384,23 @@ class ReaderTest extends \PHPUnit_Framework_TestCase
$this->assertEquals($expectedRows, $allRows, 'There should be 3 rows, with equal length'); $this->assertEquals($expectedRows, $allRows, 'There should be 3 rows, with equal length');
} }
/**
* https://github.com/box/spout/issues/195
* @return void
*/
public function testReaderShouldNotTrimCellValues()
{
$allRows = $this->getAllRowsForFile('sheet_with_untrimmed_strings.csv');
$expectedRows = [
['A'],
[' A '],
["\n\tA\n\t"],
];
$this->assertEquals($expectedRows, $allRows, 'Cell values should not be trimmed');
}
/** /**
* @param string $fileName * @param string $fileName
* @param string|void $fieldDelimiter * @param string|void $fieldDelimiter
+17
View File
@@ -419,6 +419,23 @@ class ReaderTest extends \PHPUnit_Framework_TestCase
$this->assertEquals($expectedRows, $allRows, 'There should be 3 rows, with equal length'); $this->assertEquals($expectedRows, $allRows, 'There should be 3 rows, with equal length');
} }
/**
* https://github.com/box/spout/issues/195
* @return void
*/
public function testReaderShouldNotTrimCellValues()
{
$allRows = $this->getAllRowsForFile('sheet_with_untrimmed_strings.ods');
$expectedRows = [
['A'],
[' A '],
["\n\tA\n\t"],
];
$this->assertEquals($expectedRows, $allRows, 'Cell values should not be trimmed');
}
/** /**
* @param string $fileName * @param string $fileName
* @return array All the read rows the given file * @return array All the read rows the given file
@@ -123,4 +123,46 @@ class CellValueFormatterTest extends \PHPUnit_Framework_TestCase
$this->assertEquals($expectedFormattedValue, $formattedValue); $this->assertEquals($expectedFormattedValue, $formattedValue);
$this->assertEquals($expectedType, gettype($formattedValue)); $this->assertEquals($expectedType, gettype($formattedValue));
} }
/**
* @return array
*/
public function dataProviderForTestFormatStringCellValue()
{
return [
['A', 'A'],
[' A ', ' A '],
["\n\tA\n\t", "\n\tA\n\t"],
[' ', ' '],
];
}
/**
* @dataProvider dataProviderForTestFormatStringCellValue
*
* @param string $value
* @param string $expectedFormattedValue
* @return void
*/
public function testFormatInlineStringCellValue($value, $expectedFormattedValue)
{
$nodeListMock = $this->getMockBuilder('DOMNodeList')->disableOriginalConstructor()->getMock();
$nodeListMock
->expects($this->atLeastOnce())
->method('item')
->with(0)
->will($this->returnValue((object)['nodeValue' => $value]));
$nodeMock = $this->getMockBuilder('DOMElement')->disableOriginalConstructor()->getMock();
$nodeMock
->expects($this->atLeastOnce())
->method('getElementsByTagName')
->with(CellValueFormatter::XML_NODE_INLINE_STRING_VALUE)
->will($this->returnValue($nodeListMock));
$formatter = new CellValueFormatter(null, null);
$formattedValue = \ReflectionHelper::callMethodOnObject($formatter, 'formatInlineStringCellValue', $nodeMock);
$this->assertEquals($expectedFormattedValue, $formattedValue);
}
} }
+18
View File
@@ -461,6 +461,24 @@ class ReaderTest extends \PHPUnit_Framework_TestCase
} }
/**
* https://github.com/box/spout/issues/195
* @return void
*/
public function testReaderShouldNotTrimCellValues()
{
$allRows = $this->getAllRowsForFile('sheet_with_untrimmed_inline_strings.xlsx');
$expectedRows = [
['A'],
[' A '],
["\n\tA\n\t"],
];
$this->assertEquals($expectedRows, $allRows, 'Cell values should not be trimmed');
}
/** /**
* @param string $fileName * @param string $fileName
* @return array All the read rows the given file * @return array All the read rows the given file
@@ -0,0 +1,5 @@
A
" A "
"
A
"
1 A
2 A
3 A
Binary file not shown.