assert icon indicating copy to clipboard operation
assert copied to clipboard

Add support for enums in error message

Open SVillette opened this issue 2 years ago • 1 comments

Context

In many projects, I use an assertion about an object property value. The error message in not explicit about why the assertion failed. I think it would be a nice feature to handle enum name in the error message.

$foo = new Foo(BarEnum::Bar);
Assert::same($foo->getA(), BarEnum::Baz); 
// Uncaught Webmozart\Assert\InvalidArgumentException: Expected a value identical to App\BarEnum. Got: App\BarEnum

Solution

Maybe the error message could look like this: Expected a value identical to App\BarEnum::Baz. Got: App\BarEnum::Bar. I think the enum name is the most simple solution to implement as all enum type have a name.

If you agree with the proposed solution, I can opened a PR for this feature request.

SVillette avatar Jul 07 '23 08:07 SVillette

I've encountered similar issue:

Assert::oneOf(BarEnum::A, [BarEnum::B, BarEnum::C]);
// Expected one of: BarEnum, BarEnum. Got: BarEnum

Would be much better:

// Expected one of: BarEnum::B, BarEnum::C. Got: BarEnum::A

downace avatar Sep 18 '23 03:09 downace

Is there any plans to release new version with these fixes?

gordinskiy avatar Dec 07 '24 00:12 gordinskiy