From 9364e88137ad8dda09c2009532e9b22c8ac55d75 Mon Sep 17 00:00:00 2001 From: Tim van Dijen Date: Tue, 19 Nov 2024 18:21:36 +0100 Subject: [PATCH] Add support for enums in error message --- src/Assert.php | 7 +++++++ tests/Assert/AssertTest.php | 4 ++++ tests/Utils/TestEnum.php | 14 ++++++++++++++ 3 files changed, 25 insertions(+) create mode 100644 tests/Utils/TestEnum.php diff --git a/src/Assert.php b/src/Assert.php index 59de7b1..2ce4587 100644 --- a/src/Assert.php +++ b/src/Assert.php @@ -15,6 +15,9 @@ use function array_unshift; use function call_user_func_array; use function end; +use function enum_exists; +use function function_exists; +use function get_class; use function is_object; use function is_resource; use function is_string; @@ -465,6 +468,10 @@ protected static function valueToString(mixed $value): string return $value::class . ': ' . self::valueToString($value->format('c')); } + if (function_exists('enum_exists') && enum_exists(get_class($value))) { + return get_class($value) . '::' . $value->name; + } + return $value::class; } diff --git a/tests/Assert/AssertTest.php b/tests/Assert/AssertTest.php index 0aa21fb..322d894 100644 --- a/tests/Assert/AssertTest.php +++ b/tests/Assert/AssertTest.php @@ -15,6 +15,7 @@ use SimpleSAML\Assert\Assert; use SimpleSAML\Assert\AssertionFailedException; use SimpleSAML\Test\Utils\TestClass; +use SimpleSAML\Test\Utils\TestEnum; use stdClass; use function opendir; @@ -143,6 +144,8 @@ public static function provideValue(): array $resource = opendir(sys_get_temp_dir()); + $enum = TestEnum::PHPUnit; + return [ 'null' => [null, 'null'], 'true' => [true, 'true'], @@ -150,6 +153,7 @@ public static function provideValue(): array 'array' => [[], 'array'], 'Stringable' => [$stringable, 'SimpleSAML\Test\Utils\TestClass: "phpunit"'], 'DateTime' => [$dateTime, 'DateTimeImmutable: "2000-01-01T00:00:00+00:00"'], + 'Enum' => [$enum, 'SimpleSAML\Test\Utils\TestEnum::PHPUnit'], 'object' => [$otherObject, 'stdClass'], 'resource' => [$resource, 'resource'], 'string' => ['string', '"string"'], diff --git a/tests/Utils/TestEnum.php b/tests/Utils/TestEnum.php new file mode 100644 index 0000000..5c059a2 --- /dev/null +++ b/tests/Utils/TestEnum.php @@ -0,0 +1,14 @@ +