Skip to content

Commit

Permalink
Fixed ReflectionEnum::getCase()/hasCase() adapters compatibility with…
Browse files Browse the repository at this point in the history
… native reflection
  • Loading branch information
kukulich committed Oct 14, 2022
1 parent fdbc0d8 commit 66c2712
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 5 deletions.
9 changes: 4 additions & 5 deletions src/Reflection/Adapter/ReflectionEnum.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
use function array_combine;
use function array_map;
use function array_values;
use function assert;
use function constant;
use function sprintf;
use function strtolower;
Expand Down Expand Up @@ -499,16 +498,16 @@ public function isEnum(): bool

public function hasCase(string $name): bool
{
assert($name !== '');
if ($name === '') {
return false;
}

return $this->betterReflectionEnum->hasCase($name);
}

public function getCase(string $name): ReflectionEnumUnitCase|ReflectionEnumBackedCase
{
assert($name !== '');

$case = $this->betterReflectionEnum->getCase($name);
$case = $name !== '' ? $this->betterReflectionEnum->getCase($name) : null;

if ($case === null) {
throw new CoreReflectionException(sprintf('Case %s::%s does not exist', $this->betterReflectionEnum->getName(), $name));
Expand Down
22 changes: 22 additions & 0 deletions test/unit/Reflection/Adapter/ReflectionEnumTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -594,6 +594,28 @@ public function testGetAttributesThrowsExceptionForInvalidFlags(): void
$reflectionEnumAdapter->getAttributes(null, 123);
}

public function testHasCaseReturnsFalseWhenCaseNameIsEmpty(): void
{
$betterReflectionEnum = $this->createMock(BetterReflectionEnum::class);
$reflectionEnumAdapter = new ReflectionEnumAdapter($betterReflectionEnum);

self::assertFalse($reflectionEnumAdapter->hasCase(''));
}

public function testGetCaseThrowsExceptionWhenCaseNameIsEmpty(): void
{
$betterReflectionEnum = $this->createMock(BetterReflectionEnum::class);
$betterReflectionEnum
->method('getName')
->willReturn('SomeEnum');

$reflectionEnumAdapter = new ReflectionEnumAdapter($betterReflectionEnum);

self::expectException(CoreReflectionException::class);
self::expectExceptionMessage('Case SomeEnum:: does not exist');
$reflectionEnumAdapter->getCase('');
}

public function testGetCaseWhenCaseDoesNotExist(): void
{
$betterReflectionEnum = $this->createMock(BetterReflectionEnum::class);
Expand Down

0 comments on commit 66c2712

Please sign in to comment.