webmozarts / assert

Assertions to validate method input/output with nice error messages.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Add support for enums in error message

SVillette opened this issue · 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.

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