From 1ad20d060a8fcaff87cd88841c8439963cb8c552 Mon Sep 17 00:00:00 2001 From: gordinskiy Date: Sat, 23 Sep 2023 19:32:53 +0200 Subject: [PATCH] Add support for enums in error message --- src/Assert.php | 4 ++++ tests/AssertTest.php | 23 +++++++++++++++++++++++ 2 files changed, 27 insertions(+) diff --git a/src/Assert.php b/src/Assert.php index b962e3e..f02b8c9 100644 --- a/src/Assert.php +++ b/src/Assert.php @@ -2087,6 +2087,10 @@ protected static function valueToString($value) return \get_class($value).': '.self::valueToString($value->format('c')); } + if (\function_exists('enum_exists') && \enum_exists(\get_class($value))) { + return \get_class($value).'::'.$value->name; + } + return \get_class($value); } diff --git a/tests/AssertTest.php b/tests/AssertTest.php index ef9b6be..70639f1 100644 --- a/tests/AssertTest.php +++ b/tests/AssertTest.php @@ -833,6 +833,20 @@ public function testResourceOfTypeCustomMessage(): void Assert::resource(null, 'curl', 'I want a resource of type %2$s. Got: %s'); } + + public function testEnumAssertionErrorMessage(): void + { + if (version_compare(PHP_VERSION, ENUM_INTRODUCTION_VERSION, '<')) { + $this->expectNotToPerformAssertions(); + + return; + } + + $this->expectException('\InvalidArgumentException'); + $this->expectExceptionMessage('Expected null. Got: Webmozart\Assert\Tests\TestEnum::CaseName'); + + Assert::null(TestEnum::CaseName, 'Expected null. Got: %s'); + } } /** @@ -855,3 +869,12 @@ public function __toString() return $this->value; } } + +const ENUM_INTRODUCTION_VERSION = '8.1.0'; + +if (version_compare(PHP_VERSION, ENUM_INTRODUCTION_VERSION, '>=')) { + enum TestEnum + { + case CaseName; + } +}