Skip to content

Commit 097c2e4

Browse files
committed
Better error msg and more tests
1 parent df05fed commit 097c2e4

File tree

2 files changed

+51
-18
lines changed

2 files changed

+51
-18
lines changed

src/Enum.php

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -192,10 +192,13 @@ final public static function byValue($value)
192192
$constants = self::detectConstants(static::class);
193193
$name = \array_search($value, $constants, true);
194194
if ($name === false) {
195-
$message = \is_scalar($value)
196-
? 'Unknown value ' . \var_export($value, true)
197-
: 'Invalid value of type ' . (\is_object($value) ? \get_class($value) : \gettype($value));
198-
throw new InvalidArgumentException($message);
195+
throw new InvalidArgumentException(sprintf(
196+
'Unknown value %s for enumeration %s',
197+
\is_scalar($value)
198+
? \var_export($value, true)
199+
: 'of type ' . (\is_object($value) ? \get_class($value) : \gettype($value)),
200+
static::class
201+
));
199202
}
200203

201204
if (!isset(self::$instances[static::class][$name])) {

tests/MabeEnumTest/EnumTest.php

Lines changed: 44 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use MabeEnumTest\TestAsset\SerializableEnum;
1616
use PHPUnit\Framework\TestCase;
1717
use ReflectionClass;
18+
use ReflectionProperty;
1819

1920
/**
2021
* Unit tests for the class MabeEnum\Enum
@@ -27,26 +28,43 @@ class EnumTest extends TestCase
2728
{
2829
public function setUp()
2930
{
30-
$enumRefl = new ReflectionClass(Enum::class);
31-
32-
$constantsProp = $enumRefl->getProperty('constants');
33-
$namesProp = $enumRefl->getProperty('names');
34-
$instancesProp = $enumRefl->getProperty('instances');
35-
36-
$constantsProp->setAccessible(true);
37-
$namesProp->setAccessible(true);
38-
$instancesProp->setAccessible(true);
39-
40-
$constantsProp->setValue(null, []);
41-
$namesProp->setValue(null, []);
42-
$instancesProp->setValue(null, []);
31+
$this->resetStaticEnumProps();
4332
}
4433

4534
public function tearDown()
4635
{
4736
assert_options(ASSERT_ACTIVE, 1);
4837
}
4938

39+
/**
40+
* Un-initialize all known enumerations
41+
*/
42+
private function resetStaticEnumProps()
43+
{
44+
$enumRefl = new ReflectionClass(Enum::class);
45+
$enumPropsRefl = $enumRefl->getProperties(ReflectionProperty::IS_STATIC);
46+
foreach ($enumPropsRefl as $enumPropRefl) {
47+
$enumPropRefl->setAccessible(true);
48+
$enumPropRefl->setValue([]);
49+
}
50+
}
51+
52+
/**
53+
* Test that Enumeration getters works fine after Enum::byName()
54+
* as Enum::byName() does not initialize the enumeration directly
55+
*/
56+
public function testByNameEnumGettersWorks()
57+
{
58+
$this->resetStaticEnumProps();
59+
$this->assertSame(EnumBasic::ONE, EnumBasic::byName('ONE')->getValue());
60+
61+
$this->resetStaticEnumProps();
62+
$this->assertSame('ONE', EnumBasic::byName('ONE')->getName());
63+
64+
$this->resetStaticEnumProps();
65+
$this->assertSame(0, EnumBasic::byName('ONE')->getOrdinal());
66+
}
67+
5068
public function testGetNameReturnsConstantNameOfCurrentValue()
5169
{
5270
$enum = EnumBasic::get(EnumBasic::ONE);
@@ -100,21 +118,33 @@ public function testGetWithStrictValue()
100118
public function testGetWithNonStrictValueThrowsInvalidArgumentException()
101119
{
102120
$this->expectException(InvalidArgumentException::class);
121+
$this->expectExceptionMessage("Unknown value '2' for enumeration MabeEnumTest\\TestAsset\\EnumBasic");
103122
EnumBasic::get((string)EnumBasic::TWO);
104123
}
105124

106125
public function testGetWithInvalidValueThrowsInvalidArgumentException()
107126
{
108127
$this->expectException(InvalidArgumentException::class);
128+
$this->expectExceptionMessage("Unknown value 'unknown' for enumeration MabeEnumTest\\TestAsset\\EnumBasic");
109129
EnumBasic::get('unknown');
110130
}
111131

112-
public function testGetWithInvalidTypeOfValueThrowsInvalidArgumentException()
132+
public function testGetWithInvalidArrayValueThrowsInvalidArgumentException()
113133
{
114134
$this->expectException(InvalidArgumentException::class);
135+
$this->expectExceptionMessage("Unknown value of type array for enumeration MabeEnumTest\\TestAsset\\EnumBasic");
115136
EnumBasic::get(array());
116137
}
117138

139+
public function testGetWithInvalidTypeOfValueThrowsInvalidArgumentException()
140+
{
141+
$this->expectException(InvalidArgumentException::class);
142+
$this->expectExceptionMessage(
143+
"Unknown value of type " . get_class($this) . " for enumeration MabeEnumTest\\TestAsset\\EnumBasic"
144+
);
145+
EnumBasic::get($this);
146+
}
147+
118148
public function testGetByInstance()
119149
{
120150
$enum1 = EnumBasic::get(EnumBasic::ONE);

0 commit comments

Comments
 (0)