From 2545d476a8e8ff6789a6ab489c581f5d0ed54216 Mon Sep 17 00:00:00 2001 From: Petr Kotek Date: Thu, 22 Sep 2016 01:25:45 +0200 Subject: [PATCH 1/2] Tests: add test covering area we're going to change --- composer.json | 17 ++++--- phpunit.xml | 7 +++ tests/Diff/Renderer/Html/ArrayTest.php | 66 ++++++++++++++++++++++++++ tests/bootstrap.php | 3 ++ 4 files changed, 86 insertions(+), 7 deletions(-) create mode 100644 phpunit.xml create mode 100644 tests/Diff/Renderer/Html/ArrayTest.php create mode 100644 tests/bootstrap.php 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/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 @@ + Date: Thu, 22 Sep 2016 01:30:07 +0200 Subject: [PATCH 2/2] Diff_Renderer_Html_Array: avoid using 'e' modified in preg_replace - use preg_replace_callback instead --- lib/Diff/Renderer/Html/Array.php | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) 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 '';