Skip to content

Commit

Permalink
add test for whitespace
Browse files Browse the repository at this point in the history
moved the functionality to a unit-testable function
  • Loading branch information
malles committed Mar 4, 2020
1 parent 304a331 commit 496705b
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
7 changes: 6 additions & 1 deletion src/PhpSpreadsheet/Reader/Xlsx.php
Original file line number Diff line number Diff line change
Expand Up @@ -1828,7 +1828,7 @@ private static function dirAdd($base, $add)

private static function toCSSArray($style)
{
$style = trim(str_replace(["\r", "\n", ' '], '', $style), ';');
$style = self::stripWhiteSpaceFromStyleString($style);

$temp = explode(';', $style);
$style = [];
Expand Down Expand Up @@ -1857,6 +1857,11 @@ private static function toCSSArray($style)
return $style;
}

public static function stripWhiteSpaceFromStyleString($string)
{
return trim(str_replace(["\r", "\n", ' '], '', $string), ';');
}

private static function boolean($value)
{
if (is_object($value)) {
Expand Down
25 changes: 25 additions & 0 deletions tests/PhpSpreadsheetTests/Reader/XlsxTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -228,4 +228,29 @@ public function testLoadSaveWithEmptyDrawings()
// Fake assert. The only thing we need is to ensure the file is loaded without exception
$this->assertNotNull($excel);
}

/**
* Test if all whitespace is removed from a style definition string.
* This is needed to parse it into properties with the correct keys.
*
* @param $string
* @dataProvider providerStripsWhiteSpaceFromStyleString
*/
public function testStripsWhiteSpaceFromStyleString($string)
{
$string = Xlsx::stripWhiteSpaceFromStyleString($string);
$this->assertEquals(preg_match('/\s/', $string), 0);
}

public function providerStripsWhiteSpaceFromStyleString()
{
return [
['position:absolute;margin-left:424.5pt;margin-top:169.5pt;width:67.5pt;
height:13.5pt;z-index:5;mso-wrap-style:tight'],
['position:absolute;margin-left:424.5pt;margin-top:169.5pt;width:67.5pt;
height:13.5pt;z-index:5;mso-wrap-style:tight'],
['position:absolute; margin-left:424.5pt; margin-top:169.5pt; width:67.5pt;
height:13.5pt;z-index:5;mso-wrap-style:tight'],
];
}
}

0 comments on commit 496705b

Please sign in to comment.