From fb00f83b2139af20c1a6f9d7702efbe93985ea78 Mon Sep 17 00:00:00 2001 From: Sebastian Bergmann Date: Mon, 19 Dec 2022 08:12:46 +0100 Subject: [PATCH] Make data provider methods static (and use attribute instead of annotation) --- tests/DifferTest.php | 121 ++++---- .../Output/AbstractChunkOutputBuilderTest.php | 47 ++- tests/Output/DiffOnlyOutputBuilderTest.php | 21 +- ...nifiedDiffOutputBuilderIntegrationTest.php | 119 +++----- ...nifiedDiffOutputBuilderIntegrationTest.php | 31 +- .../StrictUnifiedDiffOutputBuilderTest.php | 281 +++++++++--------- tests/Output/UnifiedDiffOutputBuilderTest.php | 68 ++--- tests/ParserTest.php | 24 +- .../UnifiedDiffAssertTraitIntegrationTest.php | 95 +++--- tests/Utils/UnifiedDiffAssertTraitTest.php | 17 +- 10 files changed, 384 insertions(+), 440 deletions(-) diff --git a/tests/DifferTest.php b/tests/DifferTest.php index 5148eaa..d9d5471 100644 --- a/tests/DifferTest.php +++ b/tests/DifferTest.php @@ -9,6 +9,7 @@ */ namespace SebastianBergmann\Diff; +use PHPUnit\Framework\Attributes\DataProvider; use PHPUnit\Framework\TestCase; use ReflectionObject; use SebastianBergmann\Diff\Output\UnifiedDiffOutputBuilder; @@ -25,57 +26,7 @@ final class DifferTest extends TestCase { private Differ $differ; - protected function setUp(): void - { - $this->differ = new Differ(new UnifiedDiffOutputBuilder); - } - - /** - * @dataProvider arrayProvider - */ - public function testArrayRepresentationOfDiffCanBeRenderedUsingTimeEfficientLcsImplementation(array $expected, array|string $from, array|string $to): void - { - $this->assertSame($expected, $this->differ->diffToArray($from, $to, new TimeEfficientLongestCommonSubsequenceCalculator)); - } - - /** - * @dataProvider textProvider - */ - public function testTextRepresentationOfDiffCanBeRenderedUsingTimeEfficientLcsImplementation(string $expected, string $from, string $to): void - { - $this->assertSame($expected, $this->differ->diff($from, $to, new TimeEfficientLongestCommonSubsequenceCalculator)); - } - - /** - * @dataProvider arrayProvider - */ - public function testArrayRepresentationOfDiffCanBeRenderedUsingMemoryEfficientLcsImplementation(array $expected, array|string $from, array|string $to): void - { - $this->assertSame($expected, $this->differ->diffToArray($from, $to, new MemoryEfficientLongestCommonSubsequenceCalculator)); - } - - /** - * @dataProvider textProvider - */ - public function testTextRepresentationOfDiffCanBeRenderedUsingMemoryEfficientLcsImplementation(string $expected, string $from, string $to): void - { - $this->assertSame($expected, $this->differ->diff($from, $to, new MemoryEfficientLongestCommonSubsequenceCalculator)); - } - - public function testArrayDiffs(): void - { - $this->assertSame( - '--- Original -+++ New -@@ @@ --one -+two -', - $this->differ->diff(['one'], ['two']) - ); - } - - public function arrayProvider(): array + public static function arrayProvider(): array { return [ [ @@ -236,7 +187,7 @@ public function arrayProvider(): array ]; } - public function textProvider(): array + public static function textProvider(): array { return [ [ @@ -302,19 +253,7 @@ public function textProvider(): array ]; } - /** - * @dataProvider provideSplitStringByLinesCases - */ - public function testSplitStringByLines(array $expected, string $input): void - { - $reflection = new ReflectionObject($this->differ); - $method = $reflection->getMethod('splitStringByLines'); - $method->setAccessible(true); - - $this->assertSame($expected, $method->invoke($this->differ, $input)); - } - - public function provideSplitStringByLinesCases(): array + public static function provideSplitStringByLinesCases(): array { return [ [ @@ -379,4 +318,56 @@ public function provideSplitStringByLinesCases(): array ], ]; } + + protected function setUp(): void + { + $this->differ = new Differ(new UnifiedDiffOutputBuilder); + } + + #[DataProvider('arrayProvider')] + public function testArrayRepresentationOfDiffCanBeRenderedUsingTimeEfficientLcsImplementation(array $expected, array|string $from, array|string $to): void + { + $this->assertSame($expected, $this->differ->diffToArray($from, $to, new TimeEfficientLongestCommonSubsequenceCalculator)); + } + + #[DataProvider('textProvider')] + public function testTextRepresentationOfDiffCanBeRenderedUsingTimeEfficientLcsImplementation(string $expected, string $from, string $to): void + { + $this->assertSame($expected, $this->differ->diff($from, $to, new TimeEfficientLongestCommonSubsequenceCalculator)); + } + + #[DataProvider('arrayProvider')] + public function testArrayRepresentationOfDiffCanBeRenderedUsingMemoryEfficientLcsImplementation(array $expected, array|string $from, array|string $to): void + { + $this->assertSame($expected, $this->differ->diffToArray($from, $to, new MemoryEfficientLongestCommonSubsequenceCalculator)); + } + + #[DataProvider('textProvider')] + public function testTextRepresentationOfDiffCanBeRenderedUsingMemoryEfficientLcsImplementation(string $expected, string $from, string $to): void + { + $this->assertSame($expected, $this->differ->diff($from, $to, new MemoryEfficientLongestCommonSubsequenceCalculator)); + } + + public function testArrayDiffs(): void + { + $this->assertSame( + '--- Original ++++ New +@@ @@ +-one ++two +', + $this->differ->diff(['one'], ['two']) + ); + } + + #[DataProvider('provideSplitStringByLinesCases')] + public function testSplitStringByLines(array $expected, string $input): void + { + $reflection = new ReflectionObject($this->differ); + $method = $reflection->getMethod('splitStringByLines'); + $method->setAccessible(true); + + $this->assertSame($expected, $method->invoke($this->differ, $input)); + } } diff --git a/tests/Output/AbstractChunkOutputBuilderTest.php b/tests/Output/AbstractChunkOutputBuilderTest.php index 918229d..e8bb549 100644 --- a/tests/Output/AbstractChunkOutputBuilderTest.php +++ b/tests/Output/AbstractChunkOutputBuilderTest.php @@ -9,6 +9,7 @@ */ namespace SebastianBergmann\Diff\Output; +use PHPUnit\Framework\Attributes\DataProvider; use PHPUnit\Framework\TestCase; use SebastianBergmann\Diff\Differ; @@ -21,30 +22,7 @@ */ final class AbstractChunkOutputBuilderTest extends TestCase { - /** - * @dataProvider provideGetCommonChunks - */ - public function testGetCommonChunks(array $expected, string $from, string $to, int $lineThreshold = 5): void - { - $output = new class extends AbstractChunkOutputBuilder { - public function getDiff(array $diff): string - { - return ''; - } - - public function getChunks(array $diff, $lineThreshold) - { - return $this->getCommonChunks($diff, $lineThreshold); - } - }; - - $this->assertSame( - $expected, - $output->getChunks((new Differ(new UnifiedDiffOutputBuilder))->diffToArray($from, $to), $lineThreshold) - ); - } - - public function provideGetCommonChunks(): array + public static function provideGetCommonChunks(): array { return [ 'same (with default threshold)' => [ @@ -143,4 +121,25 @@ public function provideGetCommonChunks(): array ], ]; } + + #[DataProvider('provideGetCommonChunks')] + public function testGetCommonChunks(array $expected, string $from, string $to, int $lineThreshold = 5): void + { + $output = new class extends AbstractChunkOutputBuilder { + public function getDiff(array $diff): string + { + return ''; + } + + public function getChunks(array $diff, $lineThreshold) + { + return $this->getCommonChunks($diff, $lineThreshold); + } + }; + + $this->assertSame( + $expected, + $output->getChunks((new Differ(new UnifiedDiffOutputBuilder))->diffToArray($from, $to), $lineThreshold) + ); + } } diff --git a/tests/Output/DiffOnlyOutputBuilderTest.php b/tests/Output/DiffOnlyOutputBuilderTest.php index 22e079a..3751757 100644 --- a/tests/Output/DiffOnlyOutputBuilderTest.php +++ b/tests/Output/DiffOnlyOutputBuilderTest.php @@ -9,6 +9,7 @@ */ namespace SebastianBergmann\Diff\Output; +use PHPUnit\Framework\Attributes\DataProvider; use PHPUnit\Framework\TestCase; use SebastianBergmann\Diff\Differ; @@ -20,17 +21,7 @@ */ final class DiffOnlyOutputBuilderTest extends TestCase { - /** - * @dataProvider textForNoNonDiffLinesProvider - */ - public function testDiffDoNotShowNonDiffLines(string $expected, string $from, string $to, string $header = ''): void - { - $differ = new Differ(new DiffOnlyOutputBuilder($header)); - - $this->assertSame($expected, $differ->diff($from, $to)); - } - - public function textForNoNonDiffLinesProvider(): array + public static function textForNoNonDiffLinesProvider(): array { return [ [ @@ -67,4 +58,12 @@ public function textForNoNonDiffLinesProvider(): array ], ]; } + + #[DataProvider('textForNoNonDiffLinesProvider')] + public function testDiffDoNotShowNonDiffLines(string $expected, string $from, string $to, string $header = ''): void + { + $differ = new Differ(new DiffOnlyOutputBuilder($header)); + + $this->assertSame($expected, $differ->diff($from, $to)); + } } diff --git a/tests/Output/Integration/StrictUnifiedDiffOutputBuilderIntegrationTest.php b/tests/Output/Integration/StrictUnifiedDiffOutputBuilderIntegrationTest.php index 074f771..1a2188a 100644 --- a/tests/Output/Integration/StrictUnifiedDiffOutputBuilderIntegrationTest.php +++ b/tests/Output/Integration/StrictUnifiedDiffOutputBuilderIntegrationTest.php @@ -19,6 +19,7 @@ use function realpath; use function sprintf; use function unlink; +use PHPUnit\Framework\Attributes\DataProvider; use PHPUnit\Framework\TestCase; use RecursiveDirectoryIterator; use RecursiveIteratorIterator; @@ -50,6 +51,43 @@ final class StrictUnifiedDiffOutputBuilderIntegrationTest extends TestCase private string $filePatch; + public static function provideOutputBuildingCases(): array + { + return StrictUnifiedDiffOutputBuilderDataProvider::provideOutputBuildingCases(); + } + + public static function provideSample(): array + { + return StrictUnifiedDiffOutputBuilderDataProvider::provideSample(); + } + + public static function provideBasicDiffGeneration(): array + { + return StrictUnifiedDiffOutputBuilderDataProvider::provideBasicDiffGeneration(); + } + + public static function provideFilePairs(): array + { + $cases = []; + $fromFile = __FILE__; + $vendorDir = realpath(__DIR__ . '/../../../vendor'); + + $fileIterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($vendorDir, RecursiveDirectoryIterator::SKIP_DOTS)); + + /** @var SplFileInfo $file */ + foreach ($fileIterator as $file) { + if ('php' !== $file->getExtension()) { + continue; + } + + $toFile = $file->getPathname(); + $cases[sprintf("Diff file:\n\"%s\"\nvs.\n\"%s\"\n", realpath($fromFile), realpath($toFile))] = [$fromFile, $toFile]; + $fromFile = $toFile; + } + + return $cases; + } + protected function setUp(): void { $this->dir = realpath(__DIR__ . '/../../fixtures/out') . '/'; @@ -69,16 +107,7 @@ protected function tearDown(): void $this->cleanUpTempFiles(); } - /** - * Integration test. - * - * - get a file pair - * - create a `diff` between the files - * - test applying the diff using `git apply` - * - test applying the diff using `patch` - * - * @dataProvider provideFilePairs - */ + #[DataProvider('provideFilePairs')] public function testIntegrationUsingPHPFileInVendorGitApply(string $fileFrom, string $fileTo): void { $from = FileUtils::getFileContent($fileFrom); @@ -96,16 +125,7 @@ public function testIntegrationUsingPHPFileInVendorGitApply(string $fileFrom, st $this->doIntegrationTestGitApply($diff, $from, $to); } - /** - * Integration test. - * - * - get a file pair - * - create a `diff` between the files - * - test applying the diff using `git apply` - * - test applying the diff using `patch` - * - * @dataProvider provideFilePairs - */ + #[DataProvider('provideFilePairs')] public function testIntegrationUsingPHPFileInVendorPatch(string $fileFrom, string $fileTo): void { $from = FileUtils::getFileContent($fileFrom); @@ -123,68 +143,23 @@ public function testIntegrationUsingPHPFileInVendorPatch(string $fileFrom, strin $this->doIntegrationTestPatch($diff, $from, $to); } - /** - * @dataProvider provideBasicDiffGeneration - * @dataProvider provideOutputBuildingCases - * @dataProvider provideSample - */ + #[DataProvider('provideBasicDiffGeneration')] + #[DataProvider('provideOutputBuildingCases')] + #[DataProvider('provideSample')] public function testIntegrationOfUnitTestCasesGitApply(string $expected, string $from, string $to): void { $this->doIntegrationTestGitApply($expected, $from, $to); } - /** - * @dataProvider provideBasicDiffGeneration - * @dataProvider provideOutputBuildingCases - * @dataProvider provideSample - */ + #[DataProvider('provideBasicDiffGeneration')] + #[DataProvider('provideOutputBuildingCases')] + #[DataProvider('provideSample')] public function testIntegrationOfUnitTestCasesPatch(string $expected, string $from, string $to): void { $this->doIntegrationTestPatch($expected, $from, $to); } - public function provideOutputBuildingCases(): array - { - return StrictUnifiedDiffOutputBuilderDataProvider::provideOutputBuildingCases(); - } - - public function provideSample(): array - { - return StrictUnifiedDiffOutputBuilderDataProvider::provideSample(); - } - - public function provideBasicDiffGeneration(): array - { - return StrictUnifiedDiffOutputBuilderDataProvider::provideBasicDiffGeneration(); - } - - public function provideFilePairs(): array - { - $cases = []; - $fromFile = __FILE__; - $vendorDir = realpath(__DIR__ . '/../../../vendor'); - - $fileIterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($vendorDir, RecursiveDirectoryIterator::SKIP_DOTS)); - - /** @var SplFileInfo $file */ - foreach ($fileIterator as $file) { - if ('php' !== $file->getExtension()) { - continue; - } - - $toFile = $file->getPathname(); - $cases[sprintf("Diff file:\n\"%s\"\nvs.\n\"%s\"\n", realpath($fromFile), realpath($toFile))] = [$fromFile, $toFile]; - $fromFile = $toFile; - } - - return $cases; - } - - /** - * Compare diff create by builder and against one create by `diff` command. - * - * @dataProvider provideBasicDiffGeneration - */ + #[DataProvider('provideBasicDiffGeneration')] public function testIntegrationDiffOutputBuilderVersusDiffCommand(string $diff, string $from, string $to): void { $this->assertNotSame('', $diff); diff --git a/tests/Output/Integration/UnifiedDiffOutputBuilderIntegrationTest.php b/tests/Output/Integration/UnifiedDiffOutputBuilderIntegrationTest.php index 262adea..f252005 100644 --- a/tests/Output/Integration/UnifiedDiffOutputBuilderIntegrationTest.php +++ b/tests/Output/Integration/UnifiedDiffOutputBuilderIntegrationTest.php @@ -22,6 +22,7 @@ use function sprintf; use function strpos; use function unlink; +use PHPUnit\Framework\Attributes\DataProvider; use PHPUnit\Framework\TestCase; use SebastianBergmann\Diff\Utils\UnifiedDiffAssertTrait; use Symfony\Component\Process\Process; @@ -44,6 +45,17 @@ final class UnifiedDiffOutputBuilderIntegrationTest extends TestCase private string $filePatch; + public static function provideDiffWithLineNumbers(): array + { + return array_filter( + UnifiedDiffOutputBuilderDataProvider::provideDiffWithLineNumbers(), + static function ($key) { + return !is_string($key) || false === strpos($key, 'non_patch_compat'); + }, + ARRAY_FILTER_USE_KEY + ); + } + protected function setUp(): void { $this->dir = realpath(__DIR__ . '/../../fixtures/out/') . '/'; @@ -58,33 +70,18 @@ protected function tearDown(): void $this->cleanUpTempFiles(); } - /** - * @dataProvider provideDiffWithLineNumbers - */ + #[DataProvider('provideDiffWithLineNumbers')] public function testDiffWithLineNumbersPath($expected, $from, $to): void { $this->doIntegrationTestPatch($expected, $from, $to); } - /** - * @dataProvider provideDiffWithLineNumbers - */ + #[DataProvider('provideDiffWithLineNumbers')] public function testDiffWithLineNumbersGitApply($expected, $from, $to): void { $this->doIntegrationTestGitApply($expected, $from, $to); } - public function provideDiffWithLineNumbers() - { - return array_filter( - UnifiedDiffOutputBuilderDataProvider::provideDiffWithLineNumbers(), - static function ($key) { - return !is_string($key) || false === strpos($key, 'non_patch_compat'); - }, - ARRAY_FILTER_USE_KEY - ); - } - private function doIntegrationTestPatch(string $diff, string $from, string $to): void { $this->assertNotSame('', $diff); diff --git a/tests/Output/StrictUnifiedDiffOutputBuilderTest.php b/tests/Output/StrictUnifiedDiffOutputBuilderTest.php index 1969538..508621e 100644 --- a/tests/Output/StrictUnifiedDiffOutputBuilderTest.php +++ b/tests/Output/StrictUnifiedDiffOutputBuilderTest.php @@ -14,6 +14,7 @@ use function sprintf; use function substr; use function time; +use PHPUnit\Framework\Attributes\DataProvider; use PHPUnit\Framework\TestCase; use SebastianBergmann\Diff\ConfigurationException; use SebastianBergmann\Diff\Differ; @@ -31,40 +32,10 @@ final class StrictUnifiedDiffOutputBuilderTest extends TestCase { use UnifiedDiffAssertTrait; - /** - * @dataProvider provideOutputBuildingCases - */ - public function testOutputBuilding(string $expected, string $from, string $to, array $options): void - { - $diff = $this->getDiffer($options)->diff($from, $to); - - $this->assertValidDiffFormat($diff); - $this->assertSame($expected, $diff); - } - - /** - * @dataProvider provideSample - */ - public function testSample(string $expected, string $from, string $to, array $options): void - { - $diff = $this->getDiffer($options)->diff($from, $to); - - $this->assertValidDiffFormat($diff); - $this->assertSame($expected, $diff); - } - - /** - * @inheritDoc - */ - public function assertValidDiffFormat(string $diff): void - { - $this->assertValidUnifiedDiffFormat($diff); - } - /** * @inheritDoc */ - public function provideOutputBuildingCases(): array + public static function provideOutputBuildingCases(): array { return StrictUnifiedDiffOutputBuilderDataProvider::provideOutputBuildingCases(); } @@ -72,45 +43,17 @@ public function provideOutputBuildingCases(): array /** * @inheritDoc */ - public function provideSample(): array + public static function provideSample(): array { return StrictUnifiedDiffOutputBuilderDataProvider::provideSample(); } - /** - * @dataProvider provideBasicDiffGeneration - */ - public function testBasicDiffGeneration(string $expected, string $from, string $to): void - { - $diff = $this->getDiffer([ - 'fromFile' => 'input.txt', - 'toFile' => 'output.txt', - ])->diff($from, $to); - - $this->assertValidDiffFormat($diff); - $this->assertSame($expected, $diff); - } - - public function provideBasicDiffGeneration(): array + public static function provideBasicDiffGeneration(): array { return StrictUnifiedDiffOutputBuilderDataProvider::provideBasicDiffGeneration(); } - /** - * @dataProvider provideConfiguredDiffGeneration - */ - public function testConfiguredDiffGeneration(string $expected, string $from, string $to, array $config = []): void - { - $diff = $this->getDiffer(array_merge([ - 'fromFile' => 'input.txt', - 'toFile' => 'output.txt', - ], $config))->diff($from, $to); - - $this->assertValidDiffFormat($diff); - $this->assertSame($expected, $diff); - } - - public function provideConfiguredDiffGeneration(): array + public static function provideConfiguredDiffGeneration(): array { return [ [ @@ -252,57 +195,7 @@ public function provideConfiguredDiffGeneration(): array ]; } - public function testReUseBuilder(): void - { - $differ = $this->getDiffer([ - 'fromFile' => 'input.txt', - 'toFile' => 'output.txt', - ]); - - $diff = $differ->diff("A\nB\n", "A\nX\n"); - $this->assertSame( - '--- input.txt -+++ output.txt -@@ -1,2 +1,2 @@ - A --B -+X -', - $diff - ); - - $diff = $differ->diff("A\n", "A\n"); - $this->assertSame( - '', - $diff - ); - } - - public function testEmptyDiff(): void - { - $builder = new StrictUnifiedDiffOutputBuilder([ - 'fromFile' => 'input.txt', - 'toFile' => 'output.txt', - ]); - - $this->assertSame( - '', - $builder->getDiff([]) - ); - } - - /** - * @dataProvider provideInvalidConfiguration - */ - public function testInvalidConfiguration(array $options, string $message): void - { - $this->expectException(ConfigurationException::class); - $this->expectExceptionMessageMatches(sprintf('#^%s$#', preg_quote($message, '#'))); - - new StrictUnifiedDiffOutputBuilder($options); - } - - public function provideInvalidConfiguration(): array + public static function provideInvalidConfiguration(): array { $time = time(); @@ -353,23 +246,7 @@ public function provideInvalidConfiguration(): array ]; } - /** - * @dataProvider provideCommonLineThresholdCases - */ - public function testCommonLineThreshold(string $expected, string $from, string $to, int $threshold): void - { - $diff = $this->getDiffer([ - 'fromFile' => 'input.txt', - 'toFile' => 'output.txt', - 'commonLineThreshold' => $threshold, - 'contextLines' => 0, - ])->diff($from, $to); - - $this->assertValidDiffFormat($diff); - $this->assertSame($expected, $diff); - } - - public function provideCommonLineThresholdCases(): array + public static function provideCommonLineThresholdCases(): array { return [ [ @@ -406,23 +283,7 @@ public function provideCommonLineThresholdCases(): array ]; } - /** - * @dataProvider provideContextLineConfigurationCases - */ - public function testContextLineConfiguration(string $expected, string $from, string $to, int $contextLines, int $commonLineThreshold = 6): void - { - $diff = $this->getDiffer([ - 'fromFile' => 'input.txt', - 'toFile' => 'output.txt', - 'contextLines' => $contextLines, - 'commonLineThreshold' => $commonLineThreshold, - ])->diff($from, $to); - - $this->assertValidDiffFormat($diff); - $this->assertSame($expected, $diff); - } - - public function provideContextLineConfigurationCases(): array + public static function provideContextLineConfigurationCases(): array { $from = "A\nB\nC\nD\nE\nF\nX\nG\nH\nI\nJ\nK\nL\nM\n"; $to = "A\nB\nC\nD\nE\nF\nY\nG\nH\nI\nJ\nK\nL\nM\n"; @@ -644,6 +505,132 @@ public function provideContextLineConfigurationCases(): array ]; } + #[DataProvider('provideOutputBuildingCases')] + public function testOutputBuilding(string $expected, string $from, string $to, array $options): void + { + $diff = $this->getDiffer($options)->diff($from, $to); + + $this->assertValidDiffFormat($diff); + $this->assertSame($expected, $diff); + } + + #[DataProvider('provideSample')] + public function testSample(string $expected, string $from, string $to, array $options): void + { + $diff = $this->getDiffer($options)->diff($from, $to); + + $this->assertValidDiffFormat($diff); + $this->assertSame($expected, $diff); + } + + /** + * @inheritDoc + */ + public function assertValidDiffFormat(string $diff): void + { + $this->assertValidUnifiedDiffFormat($diff); + } + + #[DataProvider('provideBasicDiffGeneration')] + public function testBasicDiffGeneration(string $expected, string $from, string $to): void + { + $diff = $this->getDiffer([ + 'fromFile' => 'input.txt', + 'toFile' => 'output.txt', + ])->diff($from, $to); + + $this->assertValidDiffFormat($diff); + $this->assertSame($expected, $diff); + } + + #[DataProvider('provideConfiguredDiffGeneration')] + public function testConfiguredDiffGeneration(string $expected, string $from, string $to, array $config = []): void + { + $diff = $this->getDiffer(array_merge([ + 'fromFile' => 'input.txt', + 'toFile' => 'output.txt', + ], $config))->diff($from, $to); + + $this->assertValidDiffFormat($diff); + $this->assertSame($expected, $diff); + } + + public function testReUseBuilder(): void + { + $differ = $this->getDiffer([ + 'fromFile' => 'input.txt', + 'toFile' => 'output.txt', + ]); + + $diff = $differ->diff("A\nB\n", "A\nX\n"); + $this->assertSame( + '--- input.txt ++++ output.txt +@@ -1,2 +1,2 @@ + A +-B ++X +', + $diff + ); + + $diff = $differ->diff("A\n", "A\n"); + $this->assertSame( + '', + $diff + ); + } + + public function testEmptyDiff(): void + { + $builder = new StrictUnifiedDiffOutputBuilder([ + 'fromFile' => 'input.txt', + 'toFile' => 'output.txt', + ]); + + $this->assertSame( + '', + $builder->getDiff([]) + ); + } + + #[DataProvider('provideInvalidConfiguration')] + public function testInvalidConfiguration(array $options, string $message): void + { + $this->expectException(ConfigurationException::class); + $this->expectExceptionMessageMatches(sprintf('#^%s$#', preg_quote($message, '#'))); + + new StrictUnifiedDiffOutputBuilder($options); + } + + #[DataProvider('provideCommonLineThresholdCases')] + public function testCommonLineThreshold(string $expected, string $from, string $to, int $threshold): void + { + $diff = $this->getDiffer([ + 'fromFile' => 'input.txt', + 'toFile' => 'output.txt', + 'commonLineThreshold' => $threshold, + 'contextLines' => 0, + ])->diff($from, $to); + + $this->assertValidDiffFormat($diff); + $this->assertSame($expected, $diff); + } + + #[DataProvider('provideContextLineConfigurationCases')] + public function testContextLineConfiguration(string $expected, string $from, string $to, int $contextLines, int $commonLineThreshold = 6): void + { + $diff = $this->getDiffer([ + 'fromFile' => 'input.txt', + 'toFile' => 'output.txt', + 'contextLines' => $contextLines, + 'commonLineThreshold' => $commonLineThreshold, + ])->diff($from, $to); + + $this->assertValidDiffFormat($diff); + $this->assertSame($expected, $diff); + } + /** * Returns a new instance of a Differ with a new instance of the class (DiffOutputBuilderInterface) under test. */ diff --git a/tests/Output/UnifiedDiffOutputBuilderTest.php b/tests/Output/UnifiedDiffOutputBuilderTest.php index 4b28681..59f727b 100644 --- a/tests/Output/UnifiedDiffOutputBuilderTest.php +++ b/tests/Output/UnifiedDiffOutputBuilderTest.php @@ -9,6 +9,7 @@ */ namespace SebastianBergmann\Diff\Output; +use PHPUnit\Framework\Attributes\DataProvider; use PHPUnit\Framework\TestCase; use SebastianBergmann\Diff\Differ; @@ -21,20 +22,7 @@ */ final class UnifiedDiffOutputBuilderTest extends TestCase { - /** - * @dataProvider headerProvider - */ - public function testCustomHeaderCanBeUsed(string $expected, string $from, string $to, string $header): void - { - $differ = new Differ(new UnifiedDiffOutputBuilder($header)); - - $this->assertSame( - $expected, - $differ->diff($from, $to) - ); - } - - public function headerProvider(): array + public static function headerProvider(): array { return [ [ @@ -64,23 +52,42 @@ public function headerProvider(): array ]; } - /** - * @dataProvider provideDiffWithLineNumbers - */ - public function testDiffWithLineNumbers(string $expected, string $from, string $to): void + public static function provideDiffWithLineNumbers(): array { - $differ = new Differ(new UnifiedDiffOutputBuilder("--- Original\n+++ New\n", true)); - $this->assertSame($expected, $differ->diff($from, $to)); + return UnifiedDiffOutputBuilderDataProvider::provideDiffWithLineNumbers(); } - public function provideDiffWithLineNumbers(): array + public static function provideStringsThatAreTheSame(): array { - return UnifiedDiffOutputBuilderDataProvider::provideDiffWithLineNumbers(); + 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"], + ]; } - /** - * @dataProvider provideStringsThatAreTheSame - */ + #[DataProvider('headerProvider')] + public function testCustomHeaderCanBeUsed(string $expected, string $from, string $to, string $header): void + { + $differ = new Differ(new UnifiedDiffOutputBuilder($header)); + + $this->assertSame( + $expected, + $differ->diff($from, $to) + ); + } + + #[DataProvider('provideDiffWithLineNumbers')] + public function testDiffWithLineNumbers(string $expected, string $from, string $to): void + { + $differ = new Differ(new UnifiedDiffOutputBuilder("--- Original\n+++ New\n", true)); + + $this->assertSame($expected, $differ->diff($from, $to)); + } + + #[DataProvider('provideStringsThatAreTheSame')] public function testEmptyDiffProducesEmptyOutput(string $from, string $to): void { $differ = new Differ(new UnifiedDiffOutputBuilder('', false)); @@ -89,15 +96,4 @@ public function testEmptyDiffProducesEmptyOutput(string $from, string $to): void $this->assertEmpty($output); } - - public function provideStringsThatAreTheSame(): array - { - 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"], - ]; - } } diff --git a/tests/ParserTest.php b/tests/ParserTest.php index 03b7ce4..3c7480e 100644 --- a/tests/ParserTest.php +++ b/tests/ParserTest.php @@ -10,6 +10,7 @@ namespace SebastianBergmann\Diff; use function unserialize; +use PHPUnit\Framework\Attributes\DataProvider; use PHPUnit\Framework\TestCase; use SebastianBergmann\Diff\Utils\FileUtils; @@ -24,6 +25,16 @@ final class ParserTest extends TestCase { private Parser $parser; + public static function diffProvider(): array + { + return [ + [ + "--- old.txt 2014-11-04 08:51:02.661868729 +0300\n+++ new.txt 2014-11-04 08:51:02.665868730 +0300\n@@ -1,3 +1,4 @@\n+2222111\n 1111111\n 1111111\n 1111111\n@@ -5,10 +6,8 @@\n 1111111\n 1111111\n 1111111\n +1121211\n 1111111\n -1111111\n -1111111\n -2222222\n 2222222\n 2222222\n 2222222\n@@ -17,5 +16,6 @@\n 2222222\n 2222222\n 2222222\n +2122212\n 2222222\n 2222222\n", + unserialize(FileUtils::getFileContent(__DIR__ . '/fixtures/serialized_diff.bin')), + ], + ]; + } + protected function setUp(): void { $this->parser = new Parser; @@ -185,23 +196,12 @@ public function testParseDiffForMulitpleFiles(): void /** * @psalm-param list $expected - * - * @dataProvider diffProvider */ + #[DataProvider('diffProvider')] public function testParser(string $diff, array $expected): void { $result = $this->parser->parse($diff); $this->assertEquals($expected, $result); } - - public function diffProvider(): array - { - return [ - [ - "--- old.txt 2014-11-04 08:51:02.661868729 +0300\n+++ new.txt 2014-11-04 08:51:02.665868730 +0300\n@@ -1,3 +1,4 @@\n+2222111\n 1111111\n 1111111\n 1111111\n@@ -5,10 +6,8 @@\n 1111111\n 1111111\n 1111111\n +1121211\n 1111111\n -1111111\n -1111111\n -2222222\n 2222222\n 2222222\n 2222222\n@@ -17,5 +16,6 @@\n 2222222\n 2222222\n 2222222\n +2122212\n 2222222\n 2222222\n", - unserialize(FileUtils::getFileContent(__DIR__ . '/fixtures/serialized_diff.bin')), - ], - ]; - } } diff --git a/tests/Utils/UnifiedDiffAssertTraitIntegrationTest.php b/tests/Utils/UnifiedDiffAssertTraitIntegrationTest.php index 709bd71..1c22eb6 100644 --- a/tests/Utils/UnifiedDiffAssertTraitIntegrationTest.php +++ b/tests/Utils/UnifiedDiffAssertTraitIntegrationTest.php @@ -15,6 +15,8 @@ use function strlen; use function substr; use function unlink; +use PHPUnit\Framework\Assert; +use PHPUnit\Framework\Attributes\DataProvider; use PHPUnit\Framework\TestCase; use RecursiveDirectoryIterator; use RecursiveIteratorIterator; @@ -32,6 +34,51 @@ final class UnifiedDiffAssertTraitIntegrationTest extends TestCase private string $filePatch; + /** + * @return array> + */ + public static function provideFilePairsCases(): array + { + $cases = []; + + // created cases based on dedicated fixtures + $dir = realpath(__DIR__ . '/../fixtures/UnifiedDiffAssertTraitIntegrationTest'); + $dirLength = strlen($dir); + + for ($i = 1; ; $i++) { + $fromFile = sprintf('%s/%d_a.txt', $dir, $i); + $toFile = sprintf('%s/%d_b.txt', $dir, $i); + + if (!file_exists($fromFile)) { + break; + } + + Assert::assertFileExists($toFile); + + $cases[sprintf("Diff file:\n\"%s\"\nvs.\n\"%s\"\n", substr(realpath($fromFile), $dirLength), substr(realpath($toFile), $dirLength))] = [$fromFile, $toFile]; + } + + // create cases based on PHP files within the vendor directory for integration testing + $dir = realpath(__DIR__ . '/../../vendor'); + $dirLength = strlen($dir); + + $fileIterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($dir, RecursiveDirectoryIterator::SKIP_DOTS)); + $fromFile = __FILE__; + + /** @var SplFileInfo $file */ + foreach ($fileIterator as $file) { + if ('php' !== $file->getExtension()) { + continue; + } + + $toFile = $file->getPathname(); + $cases[sprintf("Diff file:\n\"%s\"\nvs.\n\"%s\"\n", substr(realpath($fromFile), $dirLength), substr(realpath($toFile), $dirLength))] = [$fromFile, $toFile]; + $fromFile = $toFile; + } + + return $cases; + } + protected function setUp(): void { $this->filePatch = __DIR__ . '/../fixtures/out/patch.txt'; @@ -44,9 +91,7 @@ protected function tearDown(): void $this->cleanUpTempFiles(); } - /** - * @dataProvider provideFilePairsCases - */ + #[DataProvider('provideFilePairsCases')] public function testValidPatches(string $fileFrom, string $fileTo): void { $p = Process::fromShellCommandline('diff -u $from $to > $patch'); @@ -83,50 +128,6 @@ public function testValidPatches(string $fileFrom, string $fileTo): void $this->assertValidUnifiedDiffFormat(FileUtils::getFileContent($this->filePatch)); } - /** - * @return array> - */ - public function provideFilePairsCases(): array - { - $cases = []; - - // created cases based on dedicated fixtures - $dir = realpath(__DIR__ . '/../fixtures/UnifiedDiffAssertTraitIntegrationTest'); - $dirLength = strlen($dir); - - for ($i = 1; ; $i++) { - $fromFile = sprintf('%s/%d_a.txt', $dir, $i); - $toFile = sprintf('%s/%d_b.txt', $dir, $i); - - if (!file_exists($fromFile)) { - break; - } - - $this->assertFileExists($toFile); - $cases[sprintf("Diff file:\n\"%s\"\nvs.\n\"%s\"\n", substr(realpath($fromFile), $dirLength), substr(realpath($toFile), $dirLength))] = [$fromFile, $toFile]; - } - - // create cases based on PHP files within the vendor directory for integration testing - $dir = realpath(__DIR__ . '/../../vendor'); - $dirLength = strlen($dir); - - $fileIterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($dir, RecursiveDirectoryIterator::SKIP_DOTS)); - $fromFile = __FILE__; - - /** @var SplFileInfo $file */ - foreach ($fileIterator as $file) { - if ('php' !== $file->getExtension()) { - continue; - } - - $toFile = $file->getPathname(); - $cases[sprintf("Diff file:\n\"%s\"\nvs.\n\"%s\"\n", substr(realpath($fromFile), $dirLength), substr(realpath($toFile), $dirLength))] = [$fromFile, $toFile]; - $fromFile = $toFile; - } - - return $cases; - } - private function cleanUpTempFiles(): void { @unlink($this->filePatch); diff --git a/tests/Utils/UnifiedDiffAssertTraitTest.php b/tests/Utils/UnifiedDiffAssertTraitTest.php index b4f284a..2b3a65e 100644 --- a/tests/Utils/UnifiedDiffAssertTraitTest.php +++ b/tests/Utils/UnifiedDiffAssertTraitTest.php @@ -11,6 +11,7 @@ use function preg_quote; use function sprintf; +use PHPUnit\Framework\Attributes\DataProvider; use PHPUnit\Framework\TestCase; use UnexpectedValueException; @@ -21,15 +22,7 @@ final class UnifiedDiffAssertTraitTest extends TestCase { use UnifiedDiffAssertTrait; - /** - * @dataProvider provideValidCases - */ - public function testValidCases(string $diff): void - { - $this->assertValidUnifiedDiffFormat($diff); - } - - public function provideValidCases(): array + public static function provideValidCases(): array { return [ [ @@ -57,6 +50,12 @@ public function provideValidCases(): array ]; } + #[DataProvider('provideValidCases')] + public function testValidCases(string $diff): void + { + $this->assertValidUnifiedDiffFormat($diff); + } + public function testNoLinebreakEnd(): void { $this->expectException(UnexpectedValueException::class);