diff --git a/phpunit.xml b/phpunit.xml index af5293b..67a61e1 100644 --- a/phpunit.xml +++ b/phpunit.xml @@ -11,7 +11,7 @@ processIsolation="false" stopOnFailure="false" syntaxCheck="false" - bootstrap="./vendor/autoload.php"> + bootstrap="./tests/bootstrap.php"> ./tests diff --git a/src/PHPUnit/Comparator.php b/src/PHPUnit/Comparator.php new file mode 100644 index 0000000..302bf80 --- /dev/null +++ b/src/PHPUnit/Comparator.php @@ -0,0 +1,54 @@ +register(new \MyCLabs\Enum\PHPUnit\Comparator()); + */ +final class Comparator extends \SebastianBergmann\Comparator\Comparator +{ + public function accepts($expected, $actual) + { + return $expected instanceof Enum && ( + $actual instanceof Enum || $actual === null + ); + } + + /** + * @param Enum $expected + * @param Enum|null $actual + * + * @return void + */ + public function assertEquals($expected, $actual, $delta = 0.0, $canonicalize = false, $ignoreCase = false) + { + if ($expected->equals($actual)) { + return; + } + + throw new ComparisonFailure( + $expected, + $actual, + $this->formatEnum($expected), + $this->formatEnum($actual), + false, + 'Failed asserting that two Enums are equal.' + ); + } + + private function formatEnum(Enum $enum = null) + { + if ($enum === null) { + return "null"; + } + + return get_class($enum)."::{$enum->getKey()}()"; + } +} diff --git a/tests/bootstrap.php b/tests/bootstrap.php new file mode 100644 index 0000000..d72b404 --- /dev/null +++ b/tests/bootstrap.php @@ -0,0 +1,3 @@ +register(new \MyCLabs\Enum\PHPUnit\Comparator());