Skip to content
This repository was archived by the owner on May 26, 2022. It is now read-only.

Commit 71a6f6a

Browse files
committed
Merge pull request #196 from madflow/trim-inline-strings
Fix #195
2 parents b69e280 + 6169251 commit 71a6f6a

File tree

8 files changed

+100
-2
lines changed

8 files changed

+100
-2
lines changed

src/Spout/Reader/XLSX/Helper/CellValueFormatter.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -118,8 +118,7 @@ protected function formatInlineStringCellValue($node)
118118
// inline strings are formatted this way:
119119
// <c r="A1" t="inlineStr"><is><t>[INLINE_STRING]</t></is></c>
120120
$tNode = $node->getElementsByTagName(self::XML_NODE_INLINE_STRING_VALUE)->item(0);
121-
$escapedCellValue = trim($tNode->nodeValue);
122-
$cellValue = $this->escaper->unescape($escapedCellValue);
121+
$cellValue = $this->escaper->unescape($tNode->nodeValue);
123122
return $cellValue;
124123
}
125124

tests/Spout/Reader/CSV/ReaderTest.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -384,6 +384,23 @@ public function testReadShouldCreateOutputEmptyCellPreserved()
384384
$this->assertEquals($expectedRows, $allRows, 'There should be 3 rows, with equal length');
385385
}
386386

387+
/**
388+
* https://github.com/box/spout/issues/195
389+
* @return void
390+
*/
391+
public function testReaderShouldNotTrimCellValues()
392+
{
393+
$allRows = $this->getAllRowsForFile('sheet_with_untrimmed_strings.csv');
394+
395+
$expectedRows = [
396+
['A'],
397+
[' A '],
398+
["\n\tA\n\t"],
399+
];
400+
401+
$this->assertEquals($expectedRows, $allRows, 'Cell values should not be trimmed');
402+
}
403+
387404
/**
388405
* @param string $fileName
389406
* @param string|void $fieldDelimiter

tests/Spout/Reader/ODS/ReaderTest.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -419,6 +419,23 @@ public function testReadShouldCreateOutputEmptyCellPreserved()
419419
$this->assertEquals($expectedRows, $allRows, 'There should be 3 rows, with equal length');
420420
}
421421

422+
/**
423+
* https://github.com/box/spout/issues/195
424+
* @return void
425+
*/
426+
public function testReaderShouldNotTrimCellValues()
427+
{
428+
$allRows = $this->getAllRowsForFile('sheet_with_untrimmed_strings.ods');
429+
430+
$expectedRows = [
431+
['A'],
432+
[' A '],
433+
["\n\tA\n\t"],
434+
];
435+
436+
$this->assertEquals($expectedRows, $allRows, 'Cell values should not be trimmed');
437+
}
438+
422439
/**
423440
* @param string $fileName
424441
* @return array All the read rows the given file

tests/Spout/Reader/XLSX/Helper/CellValueFormatterTest.php

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,4 +123,46 @@ public function testFormatNumericCellValueWithNumbers($value, $expectedFormatted
123123
$this->assertEquals($expectedFormattedValue, $formattedValue);
124124
$this->assertEquals($expectedType, gettype($formattedValue));
125125
}
126+
127+
/**
128+
* @return array
129+
*/
130+
public function dataProviderForTestFormatStringCellValue()
131+
{
132+
return [
133+
['A', 'A'],
134+
[' A ', ' A '],
135+
["\n\tA\n\t", "\n\tA\n\t"],
136+
[' ', ' '],
137+
];
138+
}
139+
140+
/**
141+
* @dataProvider dataProviderForTestFormatStringCellValue
142+
*
143+
* @param string $value
144+
* @param string $expectedFormattedValue
145+
* @return void
146+
*/
147+
public function testFormatInlineStringCellValue($value, $expectedFormattedValue)
148+
{
149+
$nodeListMock = $this->getMockBuilder('DOMNodeList')->disableOriginalConstructor()->getMock();
150+
$nodeListMock
151+
->expects($this->atLeastOnce())
152+
->method('item')
153+
->with(0)
154+
->will($this->returnValue((object)['nodeValue' => $value]));
155+
156+
$nodeMock = $this->getMockBuilder('DOMElement')->disableOriginalConstructor()->getMock();
157+
$nodeMock
158+
->expects($this->atLeastOnce())
159+
->method('getElementsByTagName')
160+
->with(CellValueFormatter::XML_NODE_INLINE_STRING_VALUE)
161+
->will($this->returnValue($nodeListMock));
162+
163+
$formatter = new CellValueFormatter(null, null);
164+
$formattedValue = \ReflectionHelper::callMethodOnObject($formatter, 'formatInlineStringCellValue', $nodeMock);
165+
166+
$this->assertEquals($expectedFormattedValue, $formattedValue);
167+
}
126168
}

tests/Spout/Reader/XLSX/ReaderTest.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -461,6 +461,24 @@ public function testReadShouldCreateOutputEmptyCellPreserved()
461461
}
462462

463463

464+
/**
465+
* https://github.com/box/spout/issues/195
466+
* @return void
467+
*/
468+
public function testReaderShouldNotTrimCellValues()
469+
{
470+
$allRows = $this->getAllRowsForFile('sheet_with_untrimmed_inline_strings.xlsx');
471+
472+
$expectedRows = [
473+
['A'],
474+
[' A '],
475+
["\n\tA\n\t"],
476+
];
477+
478+
$this->assertEquals($expectedRows, $allRows, 'Cell values should not be trimmed');
479+
}
480+
481+
464482
/**
465483
* @param string $fileName
466484
* @return array All the read rows the given file
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
A
2+
" A "
3+
"
4+
A
5+
"
2.4 KB
Binary file not shown.
3.15 KB
Binary file not shown.

0 commit comments

Comments
 (0)