Skip to content

Commit 1416854

Browse files
SpacePossumsebastianbergmann
authored andcommitted
BC support for some PHPUnit 6 versions.
1 parent 3afb628 commit 1416854

File tree

2 files changed

+55
-2
lines changed

2 files changed

+55
-2
lines changed

src/Differ.php

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,26 @@ final class Differ
2323
*/
2424
private $outputBuilder;
2525

26-
public function __construct(DiffOutputBuilderInterface $outputBuilder = null)
26+
/**
27+
* @param DiffOutputBuilderInterface $outputBuilder
28+
*/
29+
public function __construct($outputBuilder = null)
2730
{
28-
$this->outputBuilder = $outputBuilder ?? new UnifiedDiffOutputBuilder();
31+
if ($outputBuilder instanceof DiffOutputBuilderInterface) {
32+
$this->outputBuilder = $outputBuilder;
33+
} elseif (null === $outputBuilder) {
34+
$this->outputBuilder = new UnifiedDiffOutputBuilder();
35+
} elseif (\is_string($outputBuilder)) {
36+
// PHPUnit 6.1.4, 6.2.0, 6.2.1, 6.2.2, and 6.2.3 support
37+
// @ see https://github.com/sebastianbergmann/phpunit/issues/2734#issuecomment-314514056
38+
// @ deprecated
39+
$this->outputBuilder = new UnifiedDiffOutputBuilder($outputBuilder);
40+
} else {
41+
throw new \InvalidArgumentException(\sprintf(
42+
'Expected builder to be an instance of DiffOutputBuilderInterface, <null> or a string, got %s.',
43+
\is_object($outputBuilder) ? 'instance of "' . \get_class($outputBuilder) . '"' : \gettype($outputBuilder) . ' "' . $outputBuilder . '"'
44+
));
45+
}
2946
}
3047

3148
/**

tests/DifferTest.php

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1005,4 +1005,40 @@ public function provideDiffWithLineNumbers(): array
10051005
],
10061006
];
10071007
}
1008+
1009+
public function testConstructorNull()
1010+
{
1011+
$diff = new Differ(null);
1012+
$reflection = new \ReflectionObject($diff);
1013+
$property = $reflection->getProperty('outputBuilder');
1014+
$property->setAccessible(true);
1015+
1016+
$this->assertInstanceOf(UnifiedDiffOutputBuilder::class, $property->getValue($diff));
1017+
}
1018+
1019+
public function testConstructorString()
1020+
{
1021+
$diff = new Differ("--- Original\n+++ New\n");
1022+
$reflection = new \ReflectionObject($diff);
1023+
$property = $reflection->getProperty('outputBuilder');
1024+
$property->setAccessible(true);
1025+
1026+
$this->assertInstanceOf(UnifiedDiffOutputBuilder::class, $property->getValue($diff));
1027+
}
1028+
1029+
public function testConstructorInvalidArgInt()
1030+
{
1031+
$this->expectException(\InvalidArgumentException::class);
1032+
$this->expectExceptionMessageRegExp('/^Expected builder to be an instance of DiffOutputBuilderInterface, <null> or a string, got integer "1"\.$/');
1033+
1034+
new Differ(1);
1035+
}
1036+
1037+
public function testConstructorInvalidArgObject()
1038+
{
1039+
$this->expectException(\InvalidArgumentException::class);
1040+
$this->expectExceptionMessageRegExp('/^Expected builder to be an instance of DiffOutputBuilderInterface, <null> or a string, got instance of "SplFileInfo"\.$/');
1041+
1042+
new Differ(new \SplFileInfo(__FILE__));
1043+
}
10081044
}

0 commit comments

Comments
 (0)