From 47ad4f837a9a939ddecd120d6f6045e1486f5fde Mon Sep 17 00:00:00 2001 From: SpacePossum Date: Wed, 14 Oct 2020 09:17:56 +0200 Subject: [PATCH] PHP8 --- composer.json | 4 +-- src/Output/UnifiedDiffOutputBuilder.php | 4 +-- .../StrictUnifiedDiffOutputBuilderTest.php | 29 +++++++++++++++++++ tests/Output/UnifiedDiffOutputBuilderTest.php | 24 +++++++++++++++ 4 files changed, 57 insertions(+), 4 deletions(-) diff --git a/composer.json b/composer.json index af566e1b..910267f0 100644 --- a/composer.json +++ b/composer.json @@ -15,10 +15,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" }, "config": { diff --git a/src/Output/UnifiedDiffOutputBuilder.php b/src/Output/UnifiedDiffOutputBuilder.php index 69a12708..13e6636a 100644 --- a/src/Output/UnifiedDiffOutputBuilder.php +++ b/src/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/Output/StrictUnifiedDiffOutputBuilderTest.php b/tests/Output/StrictUnifiedDiffOutputBuilderTest.php index 2c770c58..17d76ff5 100644 --- a/tests/Output/StrictUnifiedDiffOutputBuilderTest.php +++ b/tests/Output/StrictUnifiedDiffOutputBuilderTest.php @@ -305,6 +305,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/Output/UnifiedDiffOutputBuilderTest.php b/tests/Output/UnifiedDiffOutputBuilderTest.php index e3001f53..d05569d8 100644 --- a/tests/Output/UnifiedDiffOutputBuilderTest.php +++ b/tests/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"] + ]; + } }