From 960cdf1332e6049af5934c1be2e3b025bb81c5d0 Mon Sep 17 00:00:00 2001 From: Willem Stuursma Date: Thu, 2 May 2019 13:30:04 +0200 Subject: [PATCH 1/3] Add a comparator to help with writing tests --- src/PHPUnit/Comparator.php | 52 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 src/PHPUnit/Comparator.php diff --git a/src/PHPUnit/Comparator.php b/src/PHPUnit/Comparator.php new file mode 100644 index 0000000..9608833 --- /dev/null +++ b/src/PHPUnit/Comparator.php @@ -0,0 +1,52 @@ +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 + */ + 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()}()"; + } +} \ No newline at end of file From 2d72a4c51245d615c6cfef743fd4966ced8bc0a8 Mon Sep 17 00:00:00 2001 From: Willem Stuursma Date: Thu, 2 May 2019 14:38:47 +0200 Subject: [PATCH 2/3] Add newline to EOF --- src/PHPUnit/Comparator.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/PHPUnit/Comparator.php b/src/PHPUnit/Comparator.php index 9608833..302bf80 100644 --- a/src/PHPUnit/Comparator.php +++ b/src/PHPUnit/Comparator.php @@ -24,6 +24,8 @@ public function accepts($expected, $actual) /** * @param Enum $expected * @param Enum|null $actual + * + * @return void */ public function assertEquals($expected, $actual, $delta = 0.0, $canonicalize = false, $ignoreCase = false) { @@ -49,4 +51,4 @@ private function formatEnum(Enum $enum = null) return get_class($enum)."::{$enum->getKey()}()"; } -} \ No newline at end of file +} From 3c33ff203ce42802ebc7796f91f1b5bbe587c996 Mon Sep 17 00:00:00 2001 From: Willem Stuursma Date: Thu, 2 May 2019 14:43:01 +0200 Subject: [PATCH 3/3] Use Comparator in internal tests --- phpunit.xml | 2 +- tests/bootstrap.php | 3 +++ 2 files changed, 4 insertions(+), 1 deletion(-) create mode 100644 tests/bootstrap.php 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/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());