diff --git a/README.md b/README.md index 1e4d1ff..1a2e00c 100644 --- a/README.md +++ b/README.md @@ -51,8 +51,6 @@ $action = Action::VIEW(); $action = Action::$key(); // or with a dynamic value: $action = Action::from($value); -// or -$action = new Action($value); ``` As you can see, static methods are automatically implemented to provide quick access to an enum value. @@ -67,7 +65,6 @@ function setAction(Action $action) { ## Documentation -- `__construct()` The constructor checks that the value exist in the enum - `__toString()` You can `echo $myValue`, it will display the enum value (value of the constant) - `getValue()` Returns the current value of the enum - `getKey()` Returns the key of the current value on Enum @@ -111,7 +108,7 @@ final class Action extends Enum * @return Action */ public static function VIEW() { - return new Action(self::VIEW); + return self::from(self::VIEW); } } ``` diff --git a/src/Enum.php b/src/Enum.php index 6967ab5..fec5138 100644 --- a/src/Enum.php +++ b/src/Enum.php @@ -14,6 +14,7 @@ * @author Matthieu Napoli * @author Daniel Costa * @author Mirosław Filip + * @author Alexandru Pătrănescu * * @psalm-template T * @psalm-immutable @@ -27,7 +28,7 @@ abstract class Enum implements \JsonSerializable * @var mixed * @psalm-var T */ - protected $value; + private $value; /** * Enum key, the constant name @@ -43,7 +44,7 @@ abstract class Enum implements \JsonSerializable * @var array * @psalm-var array> */ - protected static $cache = []; + private static $cache = []; /** * Cache of instances of the Enum class @@ -51,45 +52,38 @@ abstract class Enum implements \JsonSerializable * @var array * @psalm-var array> */ - protected static $instances = []; + private static $instances = []; /** * Creates a new value of some type * * @psalm-pure + * @param string $key * @param mixed $value * * @psalm-param T $value - * @throws \UnexpectedValueException if incompatible type is given. */ - public function __construct($value) + final private function __construct(string $key, $value) { - if ($value instanceof static) { - /** @psalm-var T */ - $value = $value->getValue(); - } - - /** @psalm-suppress ImplicitToStringCast assertValidValueReturningKey returns always a string but psalm has currently an issue here */ - $this->key = static::assertValidValueReturningKey($value); - - /** @psalm-var T */ + $this->key = $key; $this->value = $value; } /** - * This method exists only for the compatibility reason when deserializing a previously serialized version - * that didn't had the key property + * The single place where the instance is created, other than unserialize + * + * @psalm-pure + * @param string $key + * @param mixed $value + * @return static */ - public function __wakeup() + private static function getInstance(string $key, $value): self { - /** @psalm-suppress DocblockTypeContradiction key can be null when deserializing an enum without the key */ - if ($this->key === null) { - /** - * @psalm-suppress InaccessibleProperty key is not readonly as marked by psalm - * @psalm-suppress PossiblyFalsePropertyAssignmentValue deserializing a case that was removed - */ - $this->key = static::search($this->value); + if (!isset(self::$instances[static::class][$key])) { + return self::$instances[static::class][$key] = new static($key, $value); } + + return clone self::$instances[static::class][$key]; } /** @@ -97,11 +91,31 @@ public function __wakeup() * @return static * @psalm-return static */ - public static function from($value): self + final public static function from($value): self { - $key = static::assertValidValueReturningKey($value); + $key = self::search($value); + + if ($key === false) { + throw new \UnexpectedValueException("Value '{$value}' is not part of the enum " . static::class); + } + + return self::getInstance($key, $value); + } + + /** + * @param mixed $value + * @return static|null + * @psalm-return static|null + */ + final public static function tryFrom($value): ?self + { + $key = self::search($value); + + if ($key === false) { + return null; + } - return self::__callStatic($key, []); + return self::getInstance($key, $value); } /** @@ -109,7 +123,7 @@ public static function from($value): self * @return mixed * @psalm-return T */ - public function getValue() + final public function getValue() { return $this->value; } @@ -120,7 +134,7 @@ public function getValue() * @psalm-pure * @return string */ - public function getKey() + final public function getKey(): string { return $this->key; } @@ -130,7 +144,7 @@ public function getKey() * @psalm-suppress InvalidCast * @return string */ - public function __toString() + final public function __toString(): string { return (string)$this->value; } @@ -140,14 +154,13 @@ public function __toString() * Returns false if an argument is an object of different class or not an object. * * This method is final, for more information read https://github.com/myclabs/php-enum/issues/4 - * - * @psalm-pure - * @psalm-param mixed $variable + * @param static $variable * @return bool */ - final public function equals($variable = null): bool + final public function equals(self $variable): bool { - return $variable instanceof self + return $variable instanceof static + && $this->getKey() === $variable->getKey() && $this->getValue() === $variable->getValue() && static::class === \get_class($variable); } @@ -157,11 +170,10 @@ final public function equals($variable = null): bool * * @psalm-pure * @psalm-return list - * @return array */ - public static function keys() + final public static function keys(): array { - return \array_keys(static::toArray()); + return \array_keys(self::toArray()); } /** @@ -171,13 +183,12 @@ public static function keys() * @psalm-return array * @return static[] Constant name in key, Enum instance in value */ - public static function values() + final public static function values(): array { - $values = array(); + $values = []; - /** @psalm-var T $value */ - foreach (static::toArray() as $key => $value) { - $values[$key] = new static($value); + foreach (self::toArray() as $key => $value) { + $values[$key] = self::getInstance($key, $value); } return $values; @@ -192,18 +203,22 @@ public static function values() * @psalm-return array * @return array Constant name in key, constant value in value */ - public static function toArray() + final public static function toArray(): array { $class = static::class; - if (!isset(static::$cache[$class])) { + if (!isset(self::$cache[$class])) { /** @psalm-suppress ImpureMethodCall this reflection API usage has no side-effects here */ - $reflection = new \ReflectionClass($class); + $reflection = new \ReflectionClass($class); /** @psalm-suppress ImpureMethodCall this reflection API usage has no side-effects here */ - static::$cache[$class] = $reflection->getConstants(); + if (!$reflection->isFinal()) { + throw new \ParseError("Class " . $class . " is not declared final"); + } + /** @psalm-suppress ImpureMethodCall this reflection API usage has no side-effects here */ + self::$cache[$class] = $reflection->getConstants(); } - return static::$cache[$class]; + return self::$cache[$class]; } /** @@ -215,7 +230,7 @@ public static function toArray() * @psalm-assert-if-true T $value * @return bool */ - public static function isValid($value) + final public static function isValid($value): bool { return \in_array($value, static::toArray(), true); } @@ -227,39 +242,24 @@ public static function isValid($value) * @psalm-assert T $value * @param mixed $value */ - public static function assertValidValue($value): void - { - self::assertValidValueReturningKey($value); - } - - /** - * Asserts valid enum value - * - * @psalm-pure - * @psalm-assert T $value - * @param mixed $value - * @return string - */ - private static function assertValidValueReturningKey($value): string + final public static function assertValidValue($value): void { - if (false === ($key = static::search($value))) { + if (!self::isValid($value)) { throw new \UnexpectedValueException("Value '$value' is not part of the enum " . static::class); } - - return $key; } /** * Check if is valid enum key * - * @param $key + * @param string $key * @psalm-param string $key * @psalm-pure * @return bool */ - public static function isValidKey($key) + final public static function isValidKey(string $key): bool { - $array = static::toArray(); + $array = self::toArray(); return isset($array[$key]) || \array_key_exists($key, $array); } @@ -273,7 +273,7 @@ public static function isValidKey($key) * @psalm-pure * @return string|false */ - public static function search($value) + final public static function search($value) { return \array_search($value, static::toArray(), true); } @@ -282,7 +282,7 @@ public static function search($value) * Returns a value when called statically like so: MyEnum::SOME_VALUE() given SOME_VALUE is a class constant * * @param string $name - * @param array $arguments + * @param array $arguments * * @return static * @throws \BadMethodCallException @@ -291,16 +291,12 @@ public static function search($value) */ public static function __callStatic($name, $arguments) { - $class = static::class; - if (!isset(self::$instances[$class][$name])) { - $array = static::toArray(); - if (!isset($array[$name]) && !\array_key_exists($name, $array)) { - $message = "No static method or enum constant '$name' in class " . static::class; - throw new \BadMethodCallException($message); - } - return self::$instances[$class][$name] = new static($array[$name]); + $array = self::toArray(); + if (!isset($array[$name]) && !\array_key_exists($name, $array)) { + $message = "No static method or enum constant '$name' in class " . static::class; + throw new \BadMethodCallException($message); } - return clone self::$instances[$class][$name]; + return self::getInstance($name, $array[$name]); } /** diff --git a/tests/EnumConflict.php b/tests/EnumConflict.php index edeef37..b322dd0 100644 --- a/tests/EnumConflict.php +++ b/tests/EnumConflict.php @@ -16,7 +16,7 @@ * @author Daniel Costa * @author Mirosław Filip */ -class EnumConflict extends Enum +final class EnumConflict extends Enum { const FOO = "foo"; const BAR = "bar"; diff --git a/tests/EnumFixture.php b/tests/EnumFixture.php index d04b890..2b2fdef 100644 --- a/tests/EnumFixture.php +++ b/tests/EnumFixture.php @@ -23,7 +23,7 @@ * @author Daniel Costa * @author Mirosław Filip */ -class EnumFixture extends Enum +final class EnumFixture extends Enum { const FOO = "foo"; const BAR = "bar"; diff --git a/tests/EnumTest.php b/tests/EnumTest.php index 6fcc5b1..8031706 100755 --- a/tests/EnumTest.php +++ b/tests/EnumTest.php @@ -18,14 +18,14 @@ class EnumTest extends \PHPUnit\Framework\TestCase */ public function testGetValue() { - $value = new EnumFixture(EnumFixture::FOO); - $this->assertEquals(EnumFixture::FOO, $value->getValue()); + $value = EnumFixture::FOO(); + self::assertEquals(EnumFixture::FOO, $value->getValue()); - $value = new EnumFixture(EnumFixture::BAR); - $this->assertEquals(EnumFixture::BAR, $value->getValue()); + $value = EnumFixture::BAR(); + self::assertEquals(EnumFixture::BAR, $value->getValue()); - $value = new EnumFixture(EnumFixture::NUMBER); - $this->assertEquals(EnumFixture::NUMBER, $value->getValue()); + $value = EnumFixture::NUMBER(); + self::assertEquals(EnumFixture::NUMBER, $value->getValue()); } /** @@ -33,18 +33,9 @@ public function testGetValue() */ public function testGetKey() { - $value = new EnumFixture(EnumFixture::FOO); - $this->assertEquals('FOO', $value->getKey()); - $this->assertNotEquals('BA', $value->getKey()); - } - - /** @dataProvider invalidValueProvider */ - public function testCreatingEnumWithInvalidValue($value) - { - $this->expectException(\UnexpectedValueException::class); - $this->expectExceptionMessage('is not part of the enum ' . EnumFixture::class); - - new EnumFixture($value); + $value = EnumFixture::FOO(); + self::assertEquals('FOO', $value->getKey()); + self::assertNotEquals('BA', $value->getKey()); } /** @@ -59,14 +50,6 @@ public function testFailToCreateEnumWithInvalidValueThroughNamedConstructor($val EnumFixture::from($value); } - public function testFailToCreateEnumWithEnumItselfThroughNamedConstructor(): void - { - $this->expectException(\UnexpectedValueException::class); - $this->expectExceptionMessage("Value 'foo' is not part of the enum " . EnumFixture::class); - - EnumFixture::from(EnumFixture::FOO()); - } - /** * Contains values not existing in EnumFixture * @return array @@ -85,22 +68,22 @@ public function invalidValueProvider() */ public function testToString($expected, $enumObject) { - $this->assertSame($expected, (string) $enumObject); + self::assertSame($expected, (string) $enumObject); } public function toStringProvider() { return array( - array(EnumFixture::FOO, new EnumFixture(EnumFixture::FOO)), - array(EnumFixture::BAR, new EnumFixture(EnumFixture::BAR)), - array((string) EnumFixture::NUMBER, new EnumFixture(EnumFixture::NUMBER)), + array(EnumFixture::FOO, EnumFixture::FOO()), + array(EnumFixture::BAR, EnumFixture::BAR()), + array((string) EnumFixture::NUMBER, EnumFixture::NUMBER()), ); } /** * keys() */ - public function testKeys() + public function testStringKeys() { $values = EnumFixture::keys(); $expectedValues = array( @@ -113,32 +96,32 @@ public function testKeys() "PROBLEMATIC_BOOLEAN_FALSE", ); - $this->assertSame($expectedValues, $values); + self::assertSame($expectedValues, $values); } /** * values() */ - public function testValues() + public function testStringValues() { $values = EnumFixture::values(); $expectedValues = array( - "FOO" => new EnumFixture(EnumFixture::FOO), - "BAR" => new EnumFixture(EnumFixture::BAR), - "NUMBER" => new EnumFixture(EnumFixture::NUMBER), - "PROBLEMATIC_NUMBER" => new EnumFixture(EnumFixture::PROBLEMATIC_NUMBER), - "PROBLEMATIC_NULL" => new EnumFixture(EnumFixture::PROBLEMATIC_NULL), - "PROBLEMATIC_EMPTY_STRING" => new EnumFixture(EnumFixture::PROBLEMATIC_EMPTY_STRING), - "PROBLEMATIC_BOOLEAN_FALSE" => new EnumFixture(EnumFixture::PROBLEMATIC_BOOLEAN_FALSE), + "FOO" => EnumFixture::FOO(), + "BAR" => EnumFixture::BAR(), + "NUMBER" => EnumFixture::NUMBER(), + "PROBLEMATIC_NUMBER" => EnumFixture::PROBLEMATIC_NUMBER(), + "PROBLEMATIC_NULL" => EnumFixture::PROBLEMATIC_NULL(), + "PROBLEMATIC_EMPTY_STRING" => EnumFixture::PROBLEMATIC_EMPTY_STRING(), + "PROBLEMATIC_BOOLEAN_FALSE" => EnumFixture::PROBLEMATIC_BOOLEAN_FALSE(), ); - $this->assertEquals($expectedValues, $values); + self::assertEquals($expectedValues, $values); } /** * toArray() */ - public function testToArray() + public function testStringEnumToArray() { $values = EnumFixture::toArray(); $expectedValues = array( @@ -151,7 +134,7 @@ public function testToArray() "PROBLEMATIC_BOOLEAN_FALSE" => EnumFixture::PROBLEMATIC_BOOLEAN_FALSE, ); - $this->assertSame($expectedValues, $values); + self::assertSame($expectedValues, $values); } /** @@ -159,9 +142,9 @@ public function testToArray() */ public function testStaticAccess() { - $this->assertEquals(new EnumFixture(EnumFixture::FOO), EnumFixture::FOO()); - $this->assertEquals(new EnumFixture(EnumFixture::BAR), EnumFixture::BAR()); - $this->assertEquals(new EnumFixture(EnumFixture::NUMBER), EnumFixture::NUMBER()); + $this->assertEquals(EnumFixture::from(EnumFixture::FOO), EnumFixture::FOO()); + $this->assertEquals(EnumFixture::from(EnumFixture::BAR), EnumFixture::BAR()); + $this->assertEquals(EnumFixture::from(EnumFixture::NUMBER), EnumFixture::NUMBER()); $this->assertNotSame(EnumFixture::NUMBER(), EnumFixture::NUMBER()); } @@ -179,7 +162,7 @@ public function testBadStaticAccess() */ public function testIsValid($value, $isValid) { - $this->assertSame($isValid, EnumFixture::isValid($value)); + self::assertSame($isValid, EnumFixture::isValid($value)); } public function isValidProvider() @@ -206,9 +189,9 @@ public function isValidProvider() */ public function testIsValidKey() { - $this->assertTrue(EnumFixture::isValidKey('FOO')); - $this->assertFalse(EnumFixture::isValidKey('BAZ')); - $this->assertTrue(EnumFixture::isValidKey('PROBLEMATIC_NULL')); + self::assertTrue(EnumFixture::isValidKey('FOO')); + self::assertFalse(EnumFixture::isValidKey('BAZ')); + self::assertTrue(EnumFixture::isValidKey('PROBLEMATIC_NULL')); } /** @@ -218,7 +201,7 @@ public function testIsValidKey() */ public function testSearch($value, $expected) { - $this->assertSame($expected, EnumFixture::search($value)); + self::assertSame($expected, EnumFixture::search($value)); } public function searchProvider() @@ -239,18 +222,13 @@ public function searchProvider() */ public function testEquals() { - $foo = new EnumFixture(EnumFixture::FOO); - $number = new EnumFixture(EnumFixture::NUMBER); - $anotherFoo = new EnumFixture(EnumFixture::FOO); - $objectOfDifferentClass = new \stdClass(); - $notAnObject = 'foo'; - - $this->assertTrue($foo->equals($foo)); - $this->assertFalse($foo->equals($number)); - $this->assertTrue($foo->equals($anotherFoo)); - $this->assertFalse($foo->equals(null)); - $this->assertFalse($foo->equals($objectOfDifferentClass)); - $this->assertFalse($foo->equals($notAnObject)); + $foo = EnumFixture::from(EnumFixture::FOO); + $number = EnumFixture::from(EnumFixture::NUMBER); + $anotherFoo = EnumFixture::from(EnumFixture::FOO); + + self::assertTrue($foo->equals($foo)); + self::assertFalse($foo->equals($number)); + self::assertTrue($foo->equals($anotherFoo)); } /** @@ -258,14 +236,14 @@ public function testEquals() */ public function testEqualsComparesProblematicValuesProperly() { - $false = new EnumFixture(EnumFixture::PROBLEMATIC_BOOLEAN_FALSE); - $emptyString = new EnumFixture(EnumFixture::PROBLEMATIC_EMPTY_STRING); - $null = new EnumFixture(EnumFixture::PROBLEMATIC_NULL); - - $this->assertTrue($false->equals($false)); - $this->assertFalse($false->equals($emptyString)); - $this->assertFalse($emptyString->equals($null)); - $this->assertFalse($null->equals($false)); + $false = EnumFixture::from(EnumFixture::PROBLEMATIC_BOOLEAN_FALSE); + $emptyString = EnumFixture::from(EnumFixture::PROBLEMATIC_EMPTY_STRING); + $null = EnumFixture::from(EnumFixture::PROBLEMATIC_NULL); + + self::assertTrue($false->equals($false)); + self::assertFalse($false->equals($emptyString)); + self::assertFalse($emptyString->equals($null)); + self::assertFalse($null->equals($false)); } /** @@ -273,7 +251,7 @@ public function testEqualsComparesProblematicValuesProperly() */ public function testEqualsConflictValues() { - $this->assertFalse(EnumFixture::FOO()->equals(EnumConflict::FOO())); + self::assertFalse(EnumFixture::FOO()->equals(EnumConflict::FOO())); } /** @@ -281,96 +259,66 @@ public function testEqualsConflictValues() */ public function testJsonSerialize() { - $this->assertJsonEqualsJson('"foo"', json_encode(new EnumFixture(EnumFixture::FOO))); - $this->assertJsonEqualsJson('"bar"', json_encode(new EnumFixture(EnumFixture::BAR))); - $this->assertJsonEqualsJson('42', json_encode(new EnumFixture(EnumFixture::NUMBER))); - $this->assertJsonEqualsJson('0', json_encode(new EnumFixture(EnumFixture::PROBLEMATIC_NUMBER))); - $this->assertJsonEqualsJson('null', json_encode(new EnumFixture(EnumFixture::PROBLEMATIC_NULL))); - $this->assertJsonEqualsJson('""', json_encode(new EnumFixture(EnumFixture::PROBLEMATIC_EMPTY_STRING))); - $this->assertJsonEqualsJson('false', json_encode(new EnumFixture(EnumFixture::PROBLEMATIC_BOOLEAN_FALSE))); + self::assertJsonEqualsJson('"foo"', json_encode(EnumFixture::from(EnumFixture::FOO))); + self::assertJsonEqualsJson('"bar"', json_encode(EnumFixture::from(EnumFixture::BAR))); + self::assertJsonEqualsJson('42', json_encode(EnumFixture::from(EnumFixture::NUMBER))); + self::assertJsonEqualsJson('0', json_encode(EnumFixture::from(EnumFixture::PROBLEMATIC_NUMBER))); + self::assertJsonEqualsJson('null', json_encode(EnumFixture::from(EnumFixture::PROBLEMATIC_NULL))); + self::assertJsonEqualsJson('""', json_encode(EnumFixture::from(EnumFixture::PROBLEMATIC_EMPTY_STRING))); + self::assertJsonEqualsJson('false', json_encode(EnumFixture::from(EnumFixture::PROBLEMATIC_BOOLEAN_FALSE))); } public function testNullableEnum() { - $this->assertNull(EnumFixture::PROBLEMATIC_NULL()->getValue()); - $this->assertNull((new EnumFixture(EnumFixture::PROBLEMATIC_NULL))->getValue()); - $this->assertNull((new EnumFixture(EnumFixture::PROBLEMATIC_NULL))->jsonSerialize()); + self::assertNull(EnumFixture::PROBLEMATIC_NULL()->getValue()); + self::assertNull((EnumFixture::from(EnumFixture::PROBLEMATIC_NULL))->getValue()); + self::assertNull((EnumFixture::from(EnumFixture::PROBLEMATIC_NULL))->jsonSerialize()); } - public function testBooleanEnum() + private static function assertJsonEqualsJson(string $json1, string $json2): void { - $this->assertFalse(EnumFixture::PROBLEMATIC_BOOLEAN_FALSE()->getValue()); - $this->assertFalse((new EnumFixture(EnumFixture::PROBLEMATIC_BOOLEAN_FALSE))->jsonSerialize()); - } - - public function testConstructWithSameEnumArgument() - { - $enum = new EnumFixture(EnumFixture::FOO); - - $enveloped = new EnumFixture($enum); - - $this->assertEquals($enum, $enveloped); - } - - private function assertJsonEqualsJson($json1, $json2) - { - $this->assertJsonStringEqualsJsonString($json1, $json2); + self::assertJsonStringEqualsJsonString($json1, $json2); } public function testSerialize() { // split string for Pretty CI: "Line exceeds 120 characters" $bin = '4f3a33303a224d79434c6162735c54657374735c456e756d5c456e756d4669787'. - '4757265223a323a7b733a383a22002a0076616c7565223b733a333a22666f6f223b73'. + '4757265223a323a7b733a32343a22004d79434c6162735c456e756d5c456e756d0076616c7565223b733a333a22666f6f223b73'. '3a32323a22004d79434c6162735c456e756d5c456e756d006b6579223b733a333a22464f4f223b7d'; - $this->assertEquals($bin, bin2hex(serialize(EnumFixture::FOO()))); - } - - public function testUnserializeVersionWithoutKey() - { - // split string for Pretty CI: "Line exceeds 120 characters" - $bin = '4f3a33303a224d79434c6162735c54657374735c456e756d5c456e756d4669787'. - '4757265223a313a7b733a383a22002a0076616c7565223b733a333a22666f6f223b7d'; - - /* @var $value EnumFixture */ - $value = unserialize(pack('H*', $bin)); - - $this->assertEquals(EnumFixture::FOO, $value->getValue()); - $this->assertTrue(EnumFixture::FOO()->equals($value)); - $this->assertTrue(EnumFixture::FOO() == $value); + self::assertEquals($bin, bin2hex(serialize(EnumFixture::FOO()))); } public function testUnserialize() { // split string for Pretty CI: "Line exceeds 120 characters" $bin = '4f3a33303a224d79434c6162735c54657374735c456e756d5c456e756d4669787'. - '4757265223a323a7b733a383a22002a0076616c7565223b733a333a22666f6f223b73'. + '4757265223a323a7b733a32343a22004d79434c6162735c456e756d5c456e756d0076616c7565223b733a333a22666f6f223b73'. '3a32323a22004d79434c6162735c456e756d5c456e756d006b6579223b733a333a22464f4f223b7d'; /* @var $value EnumFixture */ $value = unserialize(pack('H*', $bin)); - $this->assertEquals(EnumFixture::FOO, $value->getValue()); - $this->assertTrue(EnumFixture::FOO()->equals($value)); - $this->assertTrue(EnumFixture::FOO() == $value); + self::assertEquals(EnumFixture::FOO, $value->getValue()); + self::assertTrue(EnumFixture::FOO()->equals($value)); + self::assertTrue(EnumFixture::FOO() == $value); } /** * @see https://github.com/myclabs/php-enum/issues/95 */ - public function testEnumValuesInheritance() + public function testEnumMustBeFinal() { - $this->expectException(\UnexpectedValueException::class); - $this->expectExceptionMessage("Value 'value' is not part of the enum MyCLabs\Tests\Enum\EnumFixture"); - $inheritedEnumFixture = InheritedEnumFixture::VALUE(); - new EnumFixture($inheritedEnumFixture); + $this->expectException(\ParseError::class); + $this->expectExceptionMessage("Class MyCLabs\Tests\Enum\NotFinalEnumFixture is not declared final"); + NotFinalEnumFixture::VALUE(); } /** * @dataProvider isValidProvider */ - public function testAssertValidValue($value, $isValid): void + public function testAssertValidValueStringEnum($value, $isValid): void { if (!$isValid) { $this->expectException(\UnexpectedValueException::class); diff --git a/tests/InheritedEnumFixture.php b/tests/NotFinalEnumFixture.php similarity index 56% rename from tests/InheritedEnumFixture.php rename to tests/NotFinalEnumFixture.php index 301f9bb..68270ec 100644 --- a/tests/InheritedEnumFixture.php +++ b/tests/NotFinalEnumFixture.php @@ -2,13 +2,15 @@ namespace MyCLabs\Tests\Enum; +use MyCLabs\Enum\Enum; + /** * Class InheritedEnumFixture. * @package MyCLabs\Tests\Enum * - * @method static InheritedEnumFixture VALUE() + * @method static NotFinalEnumFixture VALUE() */ -class InheritedEnumFixture extends EnumFixture +class NotFinalEnumFixture extends Enum { const VALUE = 'value'; }