diff --git a/src/Spout/Reader/XLSX/Helper/CellValueFormatter.php b/src/Spout/Reader/XLSX/Helper/CellValueFormatter.php index c6c40f4..e63384d 100644 --- a/src/Spout/Reader/XLSX/Helper/CellValueFormatter.php +++ b/src/Spout/Reader/XLSX/Helper/CellValueFormatter.php @@ -118,8 +118,7 @@ class CellValueFormatter // inline strings are formatted this way: // [INLINE_STRING] $tNode = $node->getElementsByTagName(self::XML_NODE_INLINE_STRING_VALUE)->item(0); - $escapedCellValue = trim($tNode->nodeValue); - $cellValue = $this->escaper->unescape($escapedCellValue); + $cellValue = $this->escaper->unescape($tNode->nodeValue); return $cellValue; } diff --git a/tests/Spout/Reader/CSV/ReaderTest.php b/tests/Spout/Reader/CSV/ReaderTest.php index 1bf0e88..f806fd2 100644 --- a/tests/Spout/Reader/CSV/ReaderTest.php +++ b/tests/Spout/Reader/CSV/ReaderTest.php @@ -384,6 +384,23 @@ class ReaderTest extends \PHPUnit_Framework_TestCase $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|void $fieldDelimiter diff --git a/tests/Spout/Reader/ODS/ReaderTest.php b/tests/Spout/Reader/ODS/ReaderTest.php index e852630..8683459 100644 --- a/tests/Spout/Reader/ODS/ReaderTest.php +++ b/tests/Spout/Reader/ODS/ReaderTest.php @@ -419,6 +419,23 @@ class ReaderTest extends \PHPUnit_Framework_TestCase $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 * @return array All the read rows the given file diff --git a/tests/Spout/Reader/XLSX/Helper/CellValueFormatterTest.php b/tests/Spout/Reader/XLSX/Helper/CellValueFormatterTest.php index 68ad980..73863ae 100644 --- a/tests/Spout/Reader/XLSX/Helper/CellValueFormatterTest.php +++ b/tests/Spout/Reader/XLSX/Helper/CellValueFormatterTest.php @@ -123,4 +123,46 @@ class CellValueFormatterTest extends \PHPUnit_Framework_TestCase $this->assertEquals($expectedFormattedValue, $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); + } } diff --git a/tests/Spout/Reader/XLSX/ReaderTest.php b/tests/Spout/Reader/XLSX/ReaderTest.php index 65a0069..b1e6fdd 100644 --- a/tests/Spout/Reader/XLSX/ReaderTest.php +++ b/tests/Spout/Reader/XLSX/ReaderTest.php @@ -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 * @return array All the read rows the given file diff --git a/tests/resources/csv/sheet_with_untrimmed_strings.csv b/tests/resources/csv/sheet_with_untrimmed_strings.csv new file mode 100644 index 0000000..4be5db5 --- /dev/null +++ b/tests/resources/csv/sheet_with_untrimmed_strings.csv @@ -0,0 +1,5 @@ +A +" A " +" + A + " diff --git a/tests/resources/ods/sheet_with_untrimmed_strings.ods b/tests/resources/ods/sheet_with_untrimmed_strings.ods new file mode 100644 index 0000000..eb55bf8 Binary files /dev/null and b/tests/resources/ods/sheet_with_untrimmed_strings.ods differ diff --git a/tests/resources/xlsx/sheet_with_untrimmed_inline_strings.xlsx b/tests/resources/xlsx/sheet_with_untrimmed_inline_strings.xlsx new file mode 100644 index 0000000..c1f6ccc Binary files /dev/null and b/tests/resources/xlsx/sheet_with_untrimmed_inline_strings.xlsx differ