Skip to content

Commit

Permalink
Make data provider methods static (and use attribute instead of annot…
Browse files Browse the repository at this point in the history
…ation)
  • Loading branch information
sebastianbergmann committed Dec 19, 2022
1 parent 28a322f commit fb00f83
Show file tree
Hide file tree
Showing 10 changed files with 384 additions and 440 deletions.
121 changes: 56 additions & 65 deletions tests/DifferTest.php
Expand Up @@ -9,6 +9,7 @@
*/
namespace SebastianBergmann\Diff;

use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\TestCase;
use ReflectionObject;
use SebastianBergmann\Diff\Output\UnifiedDiffOutputBuilder;
Expand All @@ -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 [
[
Expand Down Expand Up @@ -236,7 +187,7 @@ public function arrayProvider(): array
];
}

public function textProvider(): array
public static function textProvider(): array
{
return [
[
Expand Down Expand Up @@ -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 [
[
Expand Down Expand Up @@ -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));
}
}
47 changes: 23 additions & 24 deletions tests/Output/AbstractChunkOutputBuilderTest.php
Expand Up @@ -9,6 +9,7 @@
*/
namespace SebastianBergmann\Diff\Output;

use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\TestCase;
use SebastianBergmann\Diff\Differ;

Expand All @@ -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)' => [
Expand Down Expand Up @@ -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)
);
}
}
21 changes: 10 additions & 11 deletions tests/Output/DiffOnlyOutputBuilderTest.php
Expand Up @@ -9,6 +9,7 @@
*/
namespace SebastianBergmann\Diff\Output;

use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\TestCase;
use SebastianBergmann\Diff\Differ;

Expand All @@ -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 [
[
Expand Down Expand Up @@ -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));
}
}

0 comments on commit fb00f83

Please sign in to comment.