14
14
*/
15
15
class AbstractEnumTest extends TestCase
16
16
{
17
+ /**
18
+ * Tests that from() creates an enum instance with a valid value.
19
+ */
17
20
public function testFromWithValidValue (): void
18
21
{
19
22
$ enum = ValidTestEnum::from ('first ' );
@@ -22,50 +25,56 @@ public function testFromWithValidValue(): void
22
25
$ this ->assertSame ('FIRST_NAME ' , $ enum ->name );
23
26
}
24
27
25
- public function testFromWithValidIntValue (): void
26
- {
27
- $ enum = ValidTestEnum::from (42 );
28
- $ this ->assertInstanceOf (ValidTestEnum::class, $ enum );
29
- $ this ->assertSame (42 , $ enum ->value );
30
- $ this ->assertSame ('AGE ' , $ enum ->name );
31
- }
32
28
29
+ /**
30
+ * Tests that from() throws an exception for invalid values.
31
+ */
33
32
public function testFromWithInvalidValueThrowsException (): void
34
33
{
35
34
$ this ->expectException (InvalidArgumentException::class);
36
35
$ this ->expectExceptionMessage ('invalid is not a valid backing value for enum WordPress\AiClient\Tests\unit\Common\ValidTestEnum ' );
37
36
ValidTestEnum::from ('invalid ' );
38
37
}
39
38
39
+ /**
40
+ * Tests that tryFrom() returns an enum instance for valid values.
41
+ */
40
42
public function testTryFromWithValidValue (): void
41
43
{
42
44
$ enum = ValidTestEnum::tryFrom ('first ' );
43
45
$ this ->assertInstanceOf (ValidTestEnum::class, $ enum );
44
46
$ this ->assertSame ('first ' , $ enum ->value );
45
47
}
46
48
49
+ /**
50
+ * Tests that tryFrom() returns null for invalid values.
51
+ */
47
52
public function testTryFromWithInvalidValueReturnsNull (): void
48
53
{
49
54
$ enum = ValidTestEnum::tryFrom ('invalid ' );
50
55
$ this ->assertNull ($ enum );
51
56
}
52
57
58
+ /**
59
+ * Tests that cases() returns all enum instances.
60
+ */
53
61
public function testCasesReturnsAllEnumInstances (): void
54
62
{
55
63
$ cases = ValidTestEnum::cases ();
56
- $ this ->assertCount (3 , $ cases );
64
+ $ this ->assertCount (2 , $ cases );
57
65
58
66
$ values = array_map (fn ($ case ) => $ case ->value , $ cases );
59
67
$ this ->assertContains ('first ' , $ values );
60
68
$ this ->assertContains ('last ' , $ values );
61
- $ this ->assertContains (42 , $ values );
62
69
63
70
$ names = array_map (fn ($ case ) => $ case ->name , $ cases );
64
71
$ this ->assertContains ('FIRST_NAME ' , $ names );
65
72
$ this ->assertContains ('LAST_NAME ' , $ names );
66
- $ this ->assertContains ('AGE ' , $ names );
67
73
}
68
74
75
+ /**
76
+ * Tests that enum instances are singletons.
77
+ */
69
78
public function testSingletonBehavior (): void
70
79
{
71
80
$ enum1 = ValidTestEnum::from ('first ' );
@@ -76,6 +85,9 @@ public function testSingletonBehavior(): void
76
85
$ this ->assertSame ($ enum1 , $ enum3 );
77
86
}
78
87
88
+ /**
89
+ * Tests static factory methods for creating enum instances.
90
+ */
79
91
public function testStaticFactoryMethods (): void
80
92
{
81
93
$ firstName = ValidTestEnum::firstName ();
@@ -85,12 +97,11 @@ public function testStaticFactoryMethods(): void
85
97
$ lastName = ValidTestEnum::lastName ();
86
98
$ this ->assertSame ('last ' , $ lastName ->value );
87
99
$ this ->assertSame ('LAST_NAME ' , $ lastName ->name );
88
-
89
- $ age = ValidTestEnum::age ();
90
- $ this ->assertSame (42 , $ age ->value );
91
- $ this ->assertSame ('AGE ' , $ age ->name );
92
100
}
93
101
102
+ /**
103
+ * Tests that invalid static methods throw exceptions.
104
+ */
94
105
public function testInvalidStaticMethodThrowsException (): void
95
106
{
96
107
$ this ->expectException (BadMethodCallException::class);
@@ -100,15 +111,20 @@ public function testInvalidStaticMethodThrowsException(): void
100
111
ValidTestEnum::invalidMethod ();
101
112
}
102
113
114
+ /**
115
+ * Tests the is* check methods.
116
+ */
103
117
public function testIsCheckMethods (): void
104
118
{
105
119
$ enum = ValidTestEnum::firstName ();
106
120
107
121
$ this ->assertTrue ($ enum ->isFirstName ());
108
122
$ this ->assertFalse ($ enum ->isLastName ());
109
- $ this ->assertFalse ($ enum ->isAge ());
110
123
}
111
124
125
+ /**
126
+ * Tests that invalid is* methods throw exceptions.
127
+ */
112
128
public function testInvalidIsMethodThrowsException (): void
113
129
{
114
130
$ enum = ValidTestEnum::firstName ();
@@ -120,6 +136,9 @@ public function testInvalidIsMethodThrowsException(): void
120
136
$ enum ->isInvalidMethod ();
121
137
}
122
138
139
+ /**
140
+ * Tests the equals() method with various values.
141
+ */
123
142
public function testEqualsWithSameValue (): void
124
143
{
125
144
$ enum = ValidTestEnum::firstName ();
@@ -130,15 +149,10 @@ public function testEqualsWithSameValue(): void
130
149
$ this ->assertFalse ($ enum ->equals (ValidTestEnum::lastName ()));
131
150
}
132
151
133
- public function testEqualsWithIntValue (): void
134
- {
135
- $ enum = ValidTestEnum::age ();
136
-
137
- $ this ->assertTrue ($ enum ->equals (42 ));
138
- $ this ->assertFalse ($ enum ->equals ('42 ' )); // Strict comparison
139
- $ this ->assertFalse ($ enum ->equals (43 ));
140
- }
141
152
153
+ /**
154
+ * Tests the is() method for identity comparison.
155
+ */
142
156
public function testIsMethodForIdentityComparison (): void
143
157
{
144
158
$ enum1 = ValidTestEnum::firstName ();
@@ -149,27 +163,33 @@ public function testIsMethodForIdentityComparison(): void
149
163
$ this ->assertFalse ($ enum1 ->is ($ enum3 )); // Different instance
150
164
}
151
165
166
+ /**
167
+ * Tests that getValues() returns all valid enum values.
168
+ */
152
169
public function testGetValuesReturnsAllValidValues (): void
153
170
{
154
171
$ values = ValidTestEnum::getValues ();
155
172
156
173
$ this ->assertSame ([
157
174
'FIRST_NAME ' => 'first ' ,
158
175
'LAST_NAME ' => 'last ' ,
159
- 'AGE ' => 42 ,
160
176
], $ values );
161
177
}
162
178
179
+ /**
180
+ * Tests the isValidValue() method.
181
+ */
163
182
public function testIsValidValue (): void
164
183
{
165
184
$ this ->assertTrue (ValidTestEnum::isValidValue ('first ' ));
166
185
$ this ->assertTrue (ValidTestEnum::isValidValue ('last ' ));
167
- $ this ->assertTrue (ValidTestEnum::isValidValue (42 ));
168
186
169
187
$ this ->assertFalse (ValidTestEnum::isValidValue ('invalid ' ));
170
- $ this ->assertFalse (ValidTestEnum::isValidValue (43 ));
171
188
}
172
189
190
+ /**
191
+ * Tests that enum properties are read-only.
192
+ */
173
193
public function testPropertiesAreReadOnly (): void
174
194
{
175
195
$ enum = ValidTestEnum::firstName ();
@@ -181,6 +201,9 @@ public function testPropertiesAreReadOnly(): void
181
201
$ enum ->value = 'modified ' ;
182
202
}
183
203
204
+ /**
205
+ * Tests that accessing invalid properties throws exceptions.
206
+ */
184
207
public function testInvalidPropertyAccessThrowsException (): void
185
208
{
186
209
$ enum = ValidTestEnum::firstName ();
@@ -192,15 +215,19 @@ public function testInvalidPropertyAccessThrowsException(): void
192
215
$ enum ->invalid ;
193
216
}
194
217
218
+ /**
219
+ * Tests the __toString() method.
220
+ */
195
221
public function testToString (): void
196
222
{
197
223
$ stringEnum = ValidTestEnum::firstName ();
198
- $ intEnum = ValidTestEnum::age ();
199
224
200
225
$ this ->assertSame ('first ' , (string ) $ stringEnum );
201
- $ this ->assertSame ('42 ' , (string ) $ intEnum );
202
226
}
203
227
228
+ /**
229
+ * Tests that invalid constant names throw exceptions.
230
+ */
204
231
public function testInvalidConstantNameThrowsException (): void
205
232
{
206
233
$ this ->expectException (RuntimeException::class);
@@ -212,13 +239,16 @@ public function testInvalidConstantNameThrowsException(): void
212
239
InvalidNameTestEnum::cases ();
213
240
}
214
241
242
+ /**
243
+ * Tests that invalid constant types throw exceptions.
244
+ */
215
245
public function testInvalidConstantTypeThrowsException (): void
216
246
{
217
247
$ this ->expectException (RuntimeException::class);
218
248
$ this ->expectExceptionMessage (
219
249
'Invalid enum value type for constant ' .
220
- 'WordPress\AiClient\Tests\unit\Common\InvalidTypeTestEnum::FLOAT_VALUE . ' .
221
- 'Only string and int values are allowed, double given. '
250
+ 'WordPress\AiClient\Tests\unit\Common\InvalidTypeTestEnum::INT_VALUE . ' .
251
+ 'Only string values are allowed, integer given. '
222
252
);
223
253
224
254
InvalidTypeTestEnum::cases ();
0 commit comments