diff options
Diffstat (limited to 'vendor/myclabs/php-enum/src/PHPUnit/Comparator.php')
-rw-r--r-- | vendor/myclabs/php-enum/src/PHPUnit/Comparator.php | 54 |
1 files changed, 54 insertions, 0 deletions
diff --git a/vendor/myclabs/php-enum/src/PHPUnit/Comparator.php b/vendor/myclabs/php-enum/src/PHPUnit/Comparator.php new file mode 100644 index 0000000..b52fce3 --- /dev/null +++ b/vendor/myclabs/php-enum/src/PHPUnit/Comparator.php @@ -0,0 +1,54 @@ +<?php
+
+namespace MyCLabs\Enum\PHPUnit;
+
+use MyCLabs\Enum\Enum;
+use SebastianBergmann\Comparator\ComparisonFailure;
+
+/**
+ * Use this Comparator to get nice output when using PHPUnit assertEquals() with Enums.
+ *
+ * Add this to your PHPUnit bootstrap PHP file:
+ *
+ * \SebastianBergmann\Comparator\Factory::getInstance()->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()}()";
+ }
+}
|