Skip to content

Commit

Permalink
Add support for enums in error message
Browse files Browse the repository at this point in the history
  • Loading branch information
gordinskiy committed Oct 19, 2024
1 parent c4c6fb8 commit 1ad20d0
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/Assert.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
23 changes: 23 additions & 0 deletions tests/AssertTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}
}

/**
Expand All @@ -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;
}
}

0 comments on commit 1ad20d0

Please sign in to comment.