Skip to content

Commit

Permalink
BC support for some PHPUnit 6 versions.
Browse files Browse the repository at this point in the history
  • Loading branch information
SpacePossum authored and sebastianbergmann committed Jul 12, 2017
1 parent 3afb628 commit 1416854
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 2 deletions.
21 changes: 19 additions & 2 deletions src/Differ.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,26 @@ final class Differ
*/
private $outputBuilder;

public function __construct(DiffOutputBuilderInterface $outputBuilder = null)
/**
* @param DiffOutputBuilderInterface $outputBuilder
*/
public function __construct($outputBuilder = null)
{
$this->outputBuilder = $outputBuilder ?? new UnifiedDiffOutputBuilder();
if ($outputBuilder instanceof DiffOutputBuilderInterface) {
$this->outputBuilder = $outputBuilder;
} elseif (null === $outputBuilder) {
$this->outputBuilder = new UnifiedDiffOutputBuilder();
} elseif (\is_string($outputBuilder)) {
// PHPUnit 6.1.4, 6.2.0, 6.2.1, 6.2.2, and 6.2.3 support
// @ see https://github.com/sebastianbergmann/phpunit/issues/2734#issuecomment-314514056
// @ deprecated
$this->outputBuilder = new UnifiedDiffOutputBuilder($outputBuilder);
} else {
throw new \InvalidArgumentException(\sprintf(
'Expected builder to be an instance of DiffOutputBuilderInterface, <null> or a string, got %s.',
\is_object($outputBuilder) ? 'instance of "' . \get_class($outputBuilder) . '"' : \gettype($outputBuilder) . ' "' . $outputBuilder . '"'
));
}
}

/**
Expand Down
36 changes: 36 additions & 0 deletions tests/DifferTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1005,4 +1005,40 @@ public function provideDiffWithLineNumbers(): array
],
];
}

public function testConstructorNull()
{
$diff = new Differ(null);
$reflection = new \ReflectionObject($diff);
$property = $reflection->getProperty('outputBuilder');
$property->setAccessible(true);

$this->assertInstanceOf(UnifiedDiffOutputBuilder::class, $property->getValue($diff));
}

public function testConstructorString()
{
$diff = new Differ("--- Original\n+++ New\n");
$reflection = new \ReflectionObject($diff);
$property = $reflection->getProperty('outputBuilder');
$property->setAccessible(true);

$this->assertInstanceOf(UnifiedDiffOutputBuilder::class, $property->getValue($diff));
}

public function testConstructorInvalidArgInt()
{
$this->expectException(\InvalidArgumentException::class);
$this->expectExceptionMessageRegExp('/^Expected builder to be an instance of DiffOutputBuilderInterface, <null> or a string, got integer "1"\.$/');

new Differ(1);
}

public function testConstructorInvalidArgObject()
{
$this->expectException(\InvalidArgumentException::class);
$this->expectExceptionMessageRegExp('/^Expected builder to be an instance of DiffOutputBuilderInterface, <null> or a string, got instance of "SplFileInfo"\.$/');

new Differ(new \SplFileInfo(__FILE__));
}
}

0 comments on commit 1416854

Please sign in to comment.