diff --git a/composer.json b/composer.json index e08be75b..2c4b3ad7 100644 --- a/composer.json +++ b/composer.json @@ -18,10 +18,10 @@ } ], "require": { - "php": "^5.6 || ^7.0" + "php": "^5.6 || ^7.0 || ^8.0" }, "require-dev": { - "phpunit/phpunit": "^5.7.23 || ^6.4.3", + "phpunit/phpunit": "^5.7.23 || ^6.4.3 || ^7.0", "symfony/process": "^3.3" }, "autoload": { diff --git a/src/v3_0/Output/UnifiedDiffOutputBuilder.php b/src/v3_0/Output/UnifiedDiffOutputBuilder.php index 5e7276ab..fb4d227d 100644 --- a/src/v3_0/Output/UnifiedDiffOutputBuilder.php +++ b/src/v3_0/Output/UnifiedDiffOutputBuilder.php @@ -67,11 +67,11 @@ public function getDiff(array $diff) \fclose($buffer); - // If the last char is not a linebreak: add it. + // If the diff is non-empty and a linebreak: add it. // This might happen when both the `from` and `to` do not have a trailing linebreak $last = \substr($diff, -1); - return "\n" !== $last && "\r" !== $last + return 0 !== \strlen($diff) && "\n" !== $last && "\r" !== $last ? $diff . "\n" : $diff ; diff --git a/tests/v3_0/Output/StrictUnifiedDiffOutputBuilderTest.php b/tests/v3_0/Output/StrictUnifiedDiffOutputBuilderTest.php index 2131cd41..718aaac6 100644 --- a/tests/v3_0/Output/StrictUnifiedDiffOutputBuilderTest.php +++ b/tests/v3_0/Output/StrictUnifiedDiffOutputBuilderTest.php @@ -303,6 +303,35 @@ public function testEmptyDiff() ); } + /** + * @param string $from + * @param string $to + * + * @dataProvider provideSameEmptyDiff + */ + public function testSameEmptyDiff($from, $to) + { + $builder = new StrictUnifiedDiffOutputBuilder([ + 'fromFile' => 'input.txt', + 'toFile' => 'output.txt', + ]); + + $differ = new Differ($builder); + + $this->assertSame( + '', + $differ->diff($from, $to) + ); + } + + public function provideSameEmptyDiff() + { + return [ + ['', ''], + ['a', 'a'], + ]; + } + /** * @param array $options * @param string $message diff --git a/tests/v3_0/Output/UnifiedDiffOutputBuilderTest.php b/tests/v3_0/Output/UnifiedDiffOutputBuilderTest.php index b68e350a..beaae67f 100644 --- a/tests/v3_0/Output/UnifiedDiffOutputBuilderTest.php +++ b/tests/v3_0/Output/UnifiedDiffOutputBuilderTest.php @@ -87,4 +87,28 @@ public function provideDiffWithLineNumbers() { return UnifiedDiffOutputBuilderDataProvider::provideDiffWithLineNumbers(); } + + /** + * @param string $from + * @param string $to + * + * @dataProvider provideStringsThatAreTheSame + */ + public function testEmptyDiffProducesEmptyOutput($from, $to) + { + $differ = new Differ(new UnifiedDiffOutputBuilder('', false)); + $output = $differ->diff($from, $to); + $this->assertEmpty($output); + } + + public function provideStringsThatAreTheSame() + { + return [ + ['', ''], + ['a', 'a'], + ['these strings are the same', 'these strings are the same'], + ["\n", "\n"], + ["multi-line strings\nare the same", "multi-line strings\nare the same"] + ]; + } }