Skip to content

Commit

Permalink
re-add the public static method assertValidValue
Browse files Browse the repository at this point in the history
  • Loading branch information
drealecs committed Feb 15, 2021
1 parent 4f2c572 commit b072dc2
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 2 deletions.
15 changes: 13 additions & 2 deletions src/Enum.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public function __construct($value)
$value = $value->getValue();
}

$this->key = static::assertValidValue($value);
$this->key = static::assertValidValueReturningKey($value);

/** @psalm-var T */
$this->value = $value;
Expand Down Expand Up @@ -213,7 +213,18 @@ public static function isValid($value)
* @psalm-pure
* @psalm-assert T $value
*/
private static function assertValidValue($value): string
public static function assertValidValue($value): void
{
self::assertValidValueReturningKey($value);
}

/**
* Asserts valid enum value
*
* @psalm-pure
* @psalm-assert T $value
*/
private static function assertValidValueReturningKey($value): string
{
if (false === ($key = static::search($value))) {
throw new \UnexpectedValueException("Value '$value' is not part of the enum " . static::class);
Expand Down
15 changes: 15 additions & 0 deletions tests/EnumTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -369,4 +369,19 @@ public function testEnumValuesInheritance()
$inheritedEnumFixture = InheritedEnumFixture::VALUE();
new EnumFixture($inheritedEnumFixture);
}

/**
* @dataProvider isValidProvider
*/
public function testAssertValidValue($value, $isValid): void
{
if (!$isValid) {
$this->expectException(\UnexpectedValueException::class);
$this->expectExceptionMessage("Value '$value' is not part of the enum " . EnumFixture::class);
}

EnumFixture::assertValidValue($value);

self::assertTrue(EnumFixture::isValid($value));
}
}

0 comments on commit b072dc2

Please sign in to comment.