Skip to content

Commit ffe4de0

Browse files
committed
refactor: limits enums to strings
1 parent 00fd8dd commit ffe4de0

14 files changed

+226
-46
lines changed

tests/unit/Common/AbstractEnumTest.php

Lines changed: 60 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@
1414
*/
1515
class AbstractEnumTest extends TestCase
1616
{
17+
/**
18+
* Tests that from() creates an enum instance with a valid value.
19+
*/
1720
public function testFromWithValidValue(): void
1821
{
1922
$enum = ValidTestEnum::from('first');
@@ -22,50 +25,56 @@ public function testFromWithValidValue(): void
2225
$this->assertSame('FIRST_NAME', $enum->name);
2326
}
2427

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-
}
3228

29+
/**
30+
* Tests that from() throws an exception for invalid values.
31+
*/
3332
public function testFromWithInvalidValueThrowsException(): void
3433
{
3534
$this->expectException(InvalidArgumentException::class);
3635
$this->expectExceptionMessage('invalid is not a valid backing value for enum WordPress\AiClient\Tests\unit\Common\ValidTestEnum');
3736
ValidTestEnum::from('invalid');
3837
}
3938

39+
/**
40+
* Tests that tryFrom() returns an enum instance for valid values.
41+
*/
4042
public function testTryFromWithValidValue(): void
4143
{
4244
$enum = ValidTestEnum::tryFrom('first');
4345
$this->assertInstanceOf(ValidTestEnum::class, $enum);
4446
$this->assertSame('first', $enum->value);
4547
}
4648

49+
/**
50+
* Tests that tryFrom() returns null for invalid values.
51+
*/
4752
public function testTryFromWithInvalidValueReturnsNull(): void
4853
{
4954
$enum = ValidTestEnum::tryFrom('invalid');
5055
$this->assertNull($enum);
5156
}
5257

58+
/**
59+
* Tests that cases() returns all enum instances.
60+
*/
5361
public function testCasesReturnsAllEnumInstances(): void
5462
{
5563
$cases = ValidTestEnum::cases();
56-
$this->assertCount(3, $cases);
64+
$this->assertCount(2, $cases);
5765

5866
$values = array_map(fn($case) => $case->value, $cases);
5967
$this->assertContains('first', $values);
6068
$this->assertContains('last', $values);
61-
$this->assertContains(42, $values);
6269

6370
$names = array_map(fn($case) => $case->name, $cases);
6471
$this->assertContains('FIRST_NAME', $names);
6572
$this->assertContains('LAST_NAME', $names);
66-
$this->assertContains('AGE', $names);
6773
}
6874

75+
/**
76+
* Tests that enum instances are singletons.
77+
*/
6978
public function testSingletonBehavior(): void
7079
{
7180
$enum1 = ValidTestEnum::from('first');
@@ -76,6 +85,9 @@ public function testSingletonBehavior(): void
7685
$this->assertSame($enum1, $enum3);
7786
}
7887

88+
/**
89+
* Tests static factory methods for creating enum instances.
90+
*/
7991
public function testStaticFactoryMethods(): void
8092
{
8193
$firstName = ValidTestEnum::firstName();
@@ -85,12 +97,11 @@ public function testStaticFactoryMethods(): void
8597
$lastName = ValidTestEnum::lastName();
8698
$this->assertSame('last', $lastName->value);
8799
$this->assertSame('LAST_NAME', $lastName->name);
88-
89-
$age = ValidTestEnum::age();
90-
$this->assertSame(42, $age->value);
91-
$this->assertSame('AGE', $age->name);
92100
}
93101

102+
/**
103+
* Tests that invalid static methods throw exceptions.
104+
*/
94105
public function testInvalidStaticMethodThrowsException(): void
95106
{
96107
$this->expectException(BadMethodCallException::class);
@@ -100,15 +111,20 @@ public function testInvalidStaticMethodThrowsException(): void
100111
ValidTestEnum::invalidMethod();
101112
}
102113

114+
/**
115+
* Tests the is* check methods.
116+
*/
103117
public function testIsCheckMethods(): void
104118
{
105119
$enum = ValidTestEnum::firstName();
106120

107121
$this->assertTrue($enum->isFirstName());
108122
$this->assertFalse($enum->isLastName());
109-
$this->assertFalse($enum->isAge());
110123
}
111124

125+
/**
126+
* Tests that invalid is* methods throw exceptions.
127+
*/
112128
public function testInvalidIsMethodThrowsException(): void
113129
{
114130
$enum = ValidTestEnum::firstName();
@@ -120,6 +136,9 @@ public function testInvalidIsMethodThrowsException(): void
120136
$enum->isInvalidMethod();
121137
}
122138

139+
/**
140+
* Tests the equals() method with various values.
141+
*/
123142
public function testEqualsWithSameValue(): void
124143
{
125144
$enum = ValidTestEnum::firstName();
@@ -130,15 +149,10 @@ public function testEqualsWithSameValue(): void
130149
$this->assertFalse($enum->equals(ValidTestEnum::lastName()));
131150
}
132151

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-
}
141152

153+
/**
154+
* Tests the is() method for identity comparison.
155+
*/
142156
public function testIsMethodForIdentityComparison(): void
143157
{
144158
$enum1 = ValidTestEnum::firstName();
@@ -149,27 +163,33 @@ public function testIsMethodForIdentityComparison(): void
149163
$this->assertFalse($enum1->is($enum3)); // Different instance
150164
}
151165

166+
/**
167+
* Tests that getValues() returns all valid enum values.
168+
*/
152169
public function testGetValuesReturnsAllValidValues(): void
153170
{
154171
$values = ValidTestEnum::getValues();
155172

156173
$this->assertSame([
157174
'FIRST_NAME' => 'first',
158175
'LAST_NAME' => 'last',
159-
'AGE' => 42,
160176
], $values);
161177
}
162178

179+
/**
180+
* Tests the isValidValue() method.
181+
*/
163182
public function testIsValidValue(): void
164183
{
165184
$this->assertTrue(ValidTestEnum::isValidValue('first'));
166185
$this->assertTrue(ValidTestEnum::isValidValue('last'));
167-
$this->assertTrue(ValidTestEnum::isValidValue(42));
168186

169187
$this->assertFalse(ValidTestEnum::isValidValue('invalid'));
170-
$this->assertFalse(ValidTestEnum::isValidValue(43));
171188
}
172189

190+
/**
191+
* Tests that enum properties are read-only.
192+
*/
173193
public function testPropertiesAreReadOnly(): void
174194
{
175195
$enum = ValidTestEnum::firstName();
@@ -181,6 +201,9 @@ public function testPropertiesAreReadOnly(): void
181201
$enum->value = 'modified';
182202
}
183203

204+
/**
205+
* Tests that accessing invalid properties throws exceptions.
206+
*/
184207
public function testInvalidPropertyAccessThrowsException(): void
185208
{
186209
$enum = ValidTestEnum::firstName();
@@ -192,15 +215,19 @@ public function testInvalidPropertyAccessThrowsException(): void
192215
$enum->invalid;
193216
}
194217

218+
/**
219+
* Tests the __toString() method.
220+
*/
195221
public function testToString(): void
196222
{
197223
$stringEnum = ValidTestEnum::firstName();
198-
$intEnum = ValidTestEnum::age();
199224

200225
$this->assertSame('first', (string) $stringEnum);
201-
$this->assertSame('42', (string) $intEnum);
202226
}
203227

228+
/**
229+
* Tests that invalid constant names throw exceptions.
230+
*/
204231
public function testInvalidConstantNameThrowsException(): void
205232
{
206233
$this->expectException(RuntimeException::class);
@@ -212,13 +239,16 @@ public function testInvalidConstantNameThrowsException(): void
212239
InvalidNameTestEnum::cases();
213240
}
214241

242+
/**
243+
* Tests that invalid constant types throw exceptions.
244+
*/
215245
public function testInvalidConstantTypeThrowsException(): void
216246
{
217247
$this->expectException(RuntimeException::class);
218248
$this->expectExceptionMessage(
219249
'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.'
222252
);
223253

224254
InvalidTypeTestEnum::cases();

tests/unit/Common/InvalidNameTestEnum.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
use WordPress\AiClient\Common\AbstractEnum;
88

99
/**
10-
* Invalid test enum with lowercase constant name
10+
* Invalid test enum with lowercase constant name.
1111
*/
1212
class InvalidNameTestEnum extends AbstractEnum
1313
{

tests/unit/Common/InvalidTypeTestEnum.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@
77
use WordPress\AiClient\Common\AbstractEnum;
88

99
/**
10-
* Invalid test enum with float value
10+
* Invalid test enum with float value.
1111
*/
1212
class InvalidTypeTestEnum extends AbstractEnum
1313
{
1414
public const VALID_VALUE = 'valid';
15-
public const FLOAT_VALUE = 3.14; // This should cause an exception
15+
public const INT_VALUE = 42; // This should cause an exception
1616
}

tests/unit/Common/ValidTestEnum.php

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,15 @@
77
use WordPress\AiClient\Common\AbstractEnum;
88

99
/**
10-
* Valid test enum for testing AbstractEnum functionality
10+
* Valid test enum for testing AbstractEnum functionality.
1111
*
12-
* @method static self firstName() Create an instance for FIRST_NAME
13-
* @method static self lastName() Create an instance for LAST_NAME
14-
* @method static self age() Create an instance for AGE
15-
* @method bool isFirstName() Check if the value is FIRST_NAME
16-
* @method bool isLastName() Check if the value is LAST_NAME
17-
* @method bool isAge() Check if the value is AGE
12+
* @method static self firstName() Creates an instance for FIRST_NAME.
13+
* @method static self lastName() Creates an instance for LAST_NAME.
14+
* @method bool isFirstName() Checks if the value is FIRST_NAME.
15+
* @method bool isLastName() Checks if the value is LAST_NAME.
1816
*/
1917
class ValidTestEnum extends AbstractEnum
2018
{
2119
public const FIRST_NAME = 'first';
2220
public const LAST_NAME = 'last';
23-
public const AGE = 42;
2421
}

tests/unit/EnumTestTrait.php

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,24 +7,27 @@
77
use WordPress\AiClient\Common\AbstractEnum;
88

99
/**
10-
* Trait for testing enum classes
10+
* Trait for testing enum classes.
1111
*/
1212
trait EnumTestTrait
1313
{
1414
/**
15-
* Get the enum class name to test
15+
* Gets the enum class name to test.
1616
*
17-
* @return class-string<AbstractEnum>
17+
* @return class-string<AbstractEnum> The enum class name.
1818
*/
1919
abstract protected function getEnumClass(): string;
2020

2121
/**
22-
* Get expected enum values and their constant names
22+
* Gets expected enum values and their constant names.
2323
*
24-
* @return array<string, string|int> Array of CONSTANT_NAME => value
24+
* @return array<string, string> Array of CONSTANT_NAME => value.
2525
*/
2626
abstract protected function getExpectedValues(): array;
2727

28+
/**
29+
* Tests that the enum has expected values.
30+
*/
2831
public function testEnumHasExpectedValues(): void
2932
{
3033
$enumClass = $this->getEnumClass();
@@ -35,6 +38,9 @@ public function testEnumHasExpectedValues(): void
3538
$this->assertEquals($expectedValues, $actualValues);
3639
}
3740

41+
/**
42+
* Tests that enum cases return correct instances.
43+
*/
3844
public function testEnumCasesReturnCorrectInstances(): void
3945
{
4046
$enumClass = $this->getEnumClass();
@@ -52,6 +58,9 @@ public function testEnumCasesReturnCorrectInstances(): void
5258
}
5359
}
5460

61+
/**
62+
* Tests that the from() method works correctly.
63+
*/
5564
public function testEnumFromMethodWorks(): void
5665
{
5766
$enumClass = $this->getEnumClass();
@@ -65,6 +74,9 @@ public function testEnumFromMethodWorks(): void
6574
}
6675
}
6776

77+
/**
78+
* Tests that the tryFrom() method works correctly.
79+
*/
6880
public function testEnumTryFromMethodWorks(): void
6981
{
7082
$enumClass = $this->getEnumClass();
@@ -80,6 +92,9 @@ public function testEnumTryFromMethodWorks(): void
8092
$this->assertNull($invalidEnum);
8193
}
8294

95+
/**
96+
* Tests enum singleton behavior.
97+
*/
8398
public function testEnumSingletonBehavior(): void
8499
{
85100
$enumClass = $this->getEnumClass();
@@ -97,6 +112,9 @@ public function testEnumSingletonBehavior(): void
97112
$this->assertSame($enum1, $enum2);
98113
}
99114

115+
/**
116+
* Tests that enum properties are read-only.
117+
*/
100118
public function testEnumPropertiesAreReadOnly(): void
101119
{
102120
$enumClass = $this->getEnumClass();

0 commit comments

Comments
 (0)