Merge pull request #196 from madflow/trim-inline-strings

Fix #195
This commit is contained in:
Adrien Loison 2016-04-09 13:20:48 -07:00
commit 71a6f6a937
8 changed files with 100 additions and 2 deletions

View File

@ -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;
} }

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

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

View 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);
}
} }

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

View File

@ -0,0 +1,5 @@
A
" A "
"
A
"
1 A
2 A
3 A

Binary file not shown.