1515use MabeEnumTest \TestAsset \SerializableEnum ;
1616use PHPUnit \Framework \TestCase ;
1717use 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