Skip to content

Commit

Permalink
minor #175 Throw bad method call on attempt to call ReadableEnumtrait…
Browse files Browse the repository at this point in the history
…::readableForValue on non backed enum (ogizanagi)

This PR was merged into the 2.x-dev branch.

Discussion
----------

Throw bad method call on attempt to call ReadableEnumtrait::readableForValue on non backed enum

Commits
-------

b7b8ead Throw bad method call on attempt to call ReadableEnumtrait::readableForValue on non backed enum
  • Loading branch information
ogizanagi committed Jan 26, 2022
2 parents ec16823 + b7b8ead commit c447d57
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 4 deletions.
1 change: 1 addition & 0 deletions .php-cs-fixer.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
'tests/Fixtures/Enum/Suit.php',
'tests/Fixtures/Enum/SuitWithAttributesMissingAttribute.php',
'tests/Fixtures/Enum/SuitWithAttributes.php',
'tests/Fixtures/Enum/UnitSuit.php',
'tests/Fixtures/Integration/Symfony/src/Enum/Suit.php',
])
;
Expand Down
7 changes: 3 additions & 4 deletions src/ReadableEnumInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,10 @@ interface ReadableEnumInterface extends \UnitEnum
public static function readables(): iterable;

/**
* TODO: only for backed enums
* Gets the human representation for a given value (only for backed enum).
*
* Gets the human representation for a given value.
*
* @throws \ValueError When $value is not acceptable for this enum
* @throws \ValueError When $value is not acceptable for this enum
* @throws \BadMethodCallException When attempting to call this method on a non-backed enum.
*
* @return string The human representation for a given value
*/
Expand Down
8 changes: 8 additions & 0 deletions src/ReadableEnumTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,14 @@ trait ReadableEnumTrait
*/
public static function readableForValue(string|int $value): string
{
if (!is_a(static::class, \BackedEnum::class, true)) {
throw new \BadMethodCallException(sprintf(
'Cannot call method "%s" on non-backed enum "%s".',
__METHOD__,
static::class,
));
}

/** @var ReadableEnumInterface $case */
$case = static::from($value);

Expand Down
34 changes: 34 additions & 0 deletions tests/Fixtures/Enum/UnitSuit.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

declare(strict_types=1);

/*
* This file is part of the "elao/enum" package.
*
* Copyright (C) Elao
*
* @author Elao <[email protected]>
*/

namespace Elao\Enum\Tests\Fixtures\Enum;

use Elao\Enum\ReadableEnumInterface;
use Elao\Enum\ReadableEnumTrait;

enum UnitSuit implements ReadableEnumInterface
{
use ReadableEnumTrait;

case Hearts;
case Diamonds;
case Clubs;
case Spades;

public static function readables(): iterable
{
yield self::Hearts => 'suit.hearts';
yield self::Diamonds => 'suit.diamonds';
yield self::Clubs => 'suit.clubs';
yield self::Spades => 'suit.spades';
}
}
9 changes: 9 additions & 0 deletions tests/Unit/ReadableEnumTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

use Elao\Enum\Exception\NameException;
use Elao\Enum\Tests\Fixtures\Enum\Suit;
use Elao\Enum\Tests\Fixtures\Enum\UnitSuit;
use PHPUnit\Framework\TestCase;

class ReadableEnumTest extends TestCase
Expand All @@ -35,6 +36,14 @@ public function testReadableForValue(): void
self::assertSame('suit.clubs', Suit::readableForValue(Suit::Clubs->value));
}

public function testReadableForValueThrowOnUnitEnum(): void
{
$this->expectException(\BadMethodCallException::class);
$this->expectExceptionMessage('Cannot call method "Elao\Enum\ReadableEnumTrait::readableForValue" on non-backed enum "Elao\Enum\Tests\Fixtures\Enum\UnitSuit');

self::assertSame('suit.clubs', UnitSuit::readableForValue('C'));
}

public function testReadableForValueExceptionOnInvalidName(): void
{
$this->expectException(\ValueError::class);
Expand Down

0 comments on commit c447d57

Please sign in to comment.