{"record":{"id":"e16f188b7a8d394c","repo":"sebastianbergmann/comparator","slug":"failed-asserting-that-two-values-of-enumeration-s-are-equal","errorCode":null,"errorMessage":"Failed asserting that two values of enumeration %s are equal, %s does not match expected %s.","messagePattern":"Failed asserting that two values of enumeration (.+?) are equal, (.+?) does not match expected (.+?)\\.","errorType":"exception","errorClass":"ComparisonFailure","httpStatus":null,"severity":"error","filePath":"src/EnumerationComparator.php","lineNumber":46,"sourceCode":"    {\n        return $expected instanceof UnitEnum &&\n               $actual instanceof UnitEnum &&\n               $expected::class === $actual::class;\n    }\n\n    /**\n     * @throws ComparisonFailure\n     */\n    public function assertEquals(mixed $expected, mixed $actual, float $delta = 0.0, bool $canonicalize = false, bool $ignoreCase = false): void\n    {\n        assert($expected instanceof UnitEnum);\n        assert($actual instanceof UnitEnum);\n\n        if ($expected === $actual) {\n            return;\n        }\n\n        throw new ComparisonFailure(\n            $expected,\n            $actual,\n            '',\n            '',\n            sprintf(\n                'Failed asserting that two values of enumeration %s are equal, %s does not match expected %s.',\n                $expected::class,\n                $actual->name,\n                $expected->name,\n            ),\n            $this->contextLines(),\n        );\n    }\n}\n","sourceCodeStart":28,"sourceCodeEnd":61,"githubUrl":"https://github.com/sebastianbergmann/comparator/blob/00837a9d227b4e123b0b42a91736960f6b0b3583/src/EnumerationComparator.php#L28-L61","documentation":"sebastian/comparator's EnumerationComparator compares two PHP 8.1+ UnitEnum cases. It throws a ComparisonFailure when both values are cases of the same enum class but are not the identical case (===). This surfaces in PHPUnit as a failed assertion when two enum values differ.","triggerScenarios":"Calling ComparatorFactory->getComparatorFor(...)->assertEquals($expected, $actual) (or a PHPUnit assertion like assertEquals/assertSame on enum values) where $expected and $actual are different cases of the same enum class, e.g. Suit::Hearts vs Suit::Spades.","commonSituations":"Unit tests where a function under test returns a different enum case than expected after a refactor or a mapping/lookup table change; comparing enums whose cases were renamed or re-ordered; asserting a default enum value that changed between library versions.","solutions":["Inspect the assertion diff for the actual case name reported in the message and fix the code under test to return the expected enum case","Update the test's expected enum case if the new behavior is correct","If the enum cases should be considered equivalent, compare on a property/backing value instead of case identity, or register a custom comparator"],"exampleFix":"// before\n$this->assertEquals(Status::Active, $service->statusFor($user));\n// ComparisonFailure: Status::Suspended does not match expected Status::Active\n// after\n$this->assertEquals(Status::Active, $service->activate($user)); // fix code or update expected case","handlingStrategy":"validation","validationCode":"if (!$a instanceof Suit || !$b instanceof Suit) throw new InvalidArgumentException('Both values must be Suit enum cases');\nif ($a !== $b) { /* handle mismatch before asserting */ }","typeGuard":"function isSuit(mixed $v): bool { return $v instanceof Suit; }","tryCatchPattern":null,"preventionTips":["Use assertSame/assertEquals on enum cases only when identity is intended; otherwise compare ->name or backing values","Keep enum case names stable or grep tests when renaming cases","Let the type system help: parameter-hint the enum so wrong cases fail at call time"],"tags":["php","phpunit","enum","assertion"],"backgroundTag":"invalid-enum-value","analyzedSha":"00837a9d227b4e123b0b42a91736960f6b0b3583","analyzedAt":"2026-09-15T03:36:55.154Z","contentChangedAt":"2026-09-15T03:36:55.154Z","schemaVersion":2},"datasetVersion":"2026-09-16T04:17:20.429Z"}