Skip to content

Commit

Permalink
Merge pull request #15 from simplesamlphp/feature/enum-exceptions
Browse files Browse the repository at this point in the history
Add support for enums in error message
  • Loading branch information
tvdijen authored Nov 19, 2024
2 parents 430c152 + 9364e88 commit f6872f0
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 0 deletions.
7 changes: 7 additions & 0 deletions src/Assert.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}

Expand Down
4 changes: 4 additions & 0 deletions tests/Assert/AssertTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -143,13 +144,16 @@ public static function provideValue(): array

$resource = opendir(sys_get_temp_dir());

$enum = TestEnum::PHPUnit;

return [
'null' => [null, 'null'],
'true' => [true, 'true'],
'false' => [false, 'false'],
'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"'],
Expand Down
14 changes: 14 additions & 0 deletions tests/Utils/TestEnum.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

declare(strict_types=1);

namespace SimpleSAML\Test\Utils;

// Declare a simple enumeration
enum TestEnum
{
case PHPCS;
case PHPStan;
case PHPUnit;
case Psalm;
}

0 comments on commit f6872f0

Please sign in to comment.