diff --git a/composer.json b/composer.json index a6b043bb..12d70ab6 100644 --- a/composer.json +++ b/composer.json @@ -1,6 +1,6 @@ { "name": "chrisboulton/php-diff", - "type": "library", + "type": "library", "description": "A comprehensive library for generating differences between two hashable objects (strings or arrays).", "authors": [ { @@ -8,9 +8,12 @@ "email": "@chrisboulton" } ], - "autoload": { - "psr-0": { - "Diff": "lib/" - } - } -} \ No newline at end of file + "autoload": { + "psr-0": { + "Diff": "lib/" + } + }, + "require-dev": { + "phpunit/phpunit": "~5.5" + } +} diff --git a/lib/Diff/Renderer/Html/Array.php b/lib/Diff/Renderer/Html/Array.php index b012fb6b..521601c6 100644 --- a/lib/Diff/Renderer/Html/Array.php +++ b/lib/Diff/Renderer/Html/Array.php @@ -177,7 +177,7 @@ protected function formatLines($lines) $lines = array_map(array($this, 'ExpandTabs'), $lines); $lines = array_map(array($this, 'HtmlSafe'), $lines); foreach($lines as &$line) { - $line = preg_replace('# ( +)|^ #e', "\$this->fixSpaces('\\1')", $line); + $line = preg_replace_callback('# ( +)|^ #', array($this, 'fixSpaces'), $line); } return $lines; } @@ -185,11 +185,12 @@ protected function formatLines($lines) /** * Replace a string containing spaces with a HTML representation using  . * - * @param string $spaces The string of spaces. + * @param string[] $matches Array with preg matches. * @return string The HTML representation of the string. */ - function fixSpaces($spaces='') + private function fixSpaces(array $matches) { + $spaces = $matches[1]; $count = strlen($spaces); if($count == 0) { return ''; diff --git a/phpunit.xml b/phpunit.xml new file mode 100644 index 00000000..b9749851 --- /dev/null +++ b/phpunit.xml @@ -0,0 +1,7 @@ + + + + tests + + + diff --git a/tests/Diff/Renderer/Html/ArrayTest.php b/tests/Diff/Renderer/Html/ArrayTest.php new file mode 100644 index 00000000..e0e303d0 --- /dev/null +++ b/tests/Diff/Renderer/Html/ArrayTest.php @@ -0,0 +1,66 @@ +diff = new \Diff( + array('a'), + array() + ); + + $result = $htmlRenderer->render(); + + static::assertEquals(array( + array( + array( + 'tag' => 'delete', + 'base' => array( + 'offset' => 0, + 'lines' => array( + 'a' + ) + ), + 'changed' => array( + 'offset' => 0, + 'lines' => array() + ) + ) + ) + ), $result); + } + + public function testRenderFixesSpaces() + { + $htmlRenderer = new \Diff_Renderer_Html_Array(); + $htmlRenderer->diff = new \Diff( + array(' a'), + array('a') + ); + + $result = $htmlRenderer->render(); + + static::assertEquals(array( + array( + array( + 'tag' => 'replace', + 'base' => array( + 'offset' => 0, + 'lines' => array( + '   a', + ) + ), + 'changed' => array( + 'offset' => 0, + 'lines' => array( + 'a' + ) + ) + ) + ) + ), $result); + } +} diff --git a/tests/bootstrap.php b/tests/bootstrap.php new file mode 100644 index 00000000..6c8c4f51 --- /dev/null +++ b/tests/bootstrap.php @@ -0,0 +1,3 @@ +