Skip to content

Commit 53cd540

Browse files
committed
test: adds enum unit tests
1 parent 58eaec4 commit 53cd540

15 files changed

+870
-0
lines changed

phpunit.xml.dist

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.5/phpunit.xsd"
4+
bootstrap="vendor/autoload.php"
5+
cacheResultFile=".phpunit.cache/test-results"
6+
executionOrder="depends,defects"
7+
forceCoversAnnotation="true"
8+
beStrictAboutCoversAnnotation="true"
9+
beStrictAboutOutputDuringTests="true"
10+
beStrictAboutTodoAnnotatedTests="true"
11+
failOnRisky="true"
12+
failOnWarning="true"
13+
verbose="true"
14+
colors="true">
15+
<testsuites>
16+
<testsuite name="unit">
17+
<directory suffix="Test.php">tests/unit</directory>
18+
</testsuite>
19+
</testsuites>
20+
21+
<coverage cacheDirectory=".phpunit.cache/code-coverage"
22+
processUncoveredFiles="true">
23+
<include>
24+
<directory suffix=".php">src</directory>
25+
</include>
26+
<exclude>
27+
<file>src/DummyForAnalysis.php</file>
28+
</exclude>
29+
</coverage>
30+
</phpunit>
Lines changed: 233 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,233 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace WordPress\AiClient\Tests\unit\Common;
6+
7+
use BadMethodCallException;
8+
use InvalidArgumentException;
9+
use PHPUnit\Framework\TestCase;
10+
use RuntimeException;
11+
12+
/**
13+
* @covers \WordPress\AiClient\Common\AbstractEnum
14+
*/
15+
class AbstractEnumTest extends TestCase
16+
{
17+
public function testFromWithValidValue(): void
18+
{
19+
$enum = ValidTestEnum::from('first');
20+
$this->assertInstanceOf(ValidTestEnum::class, $enum);
21+
$this->assertSame('first', $enum->value);
22+
$this->assertSame('FIRST_NAME', $enum->name);
23+
}
24+
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+
33+
public function testFromWithInvalidValueThrowsException(): void
34+
{
35+
$this->expectException(InvalidArgumentException::class);
36+
$this->expectExceptionMessage('invalid is not a valid backing value for enum WordPress\AiClient\Tests\unit\Common\ValidTestEnum');
37+
ValidTestEnum::from('invalid');
38+
}
39+
40+
public function testTryFromWithValidValue(): void
41+
{
42+
$enum = ValidTestEnum::tryFrom('first');
43+
$this->assertInstanceOf(ValidTestEnum::class, $enum);
44+
$this->assertSame('first', $enum->value);
45+
}
46+
47+
public function testTryFromWithInvalidValueReturnsNull(): void
48+
{
49+
$enum = ValidTestEnum::tryFrom('invalid');
50+
$this->assertNull($enum);
51+
}
52+
53+
public function testCasesReturnsAllEnumInstances(): void
54+
{
55+
$cases = ValidTestEnum::cases();
56+
$this->assertCount(3, $cases);
57+
58+
$values = array_map(fn($case) => $case->value, $cases);
59+
$this->assertContains('first', $values);
60+
$this->assertContains('last', $values);
61+
$this->assertContains(42, $values);
62+
63+
$names = array_map(fn($case) => $case->name, $cases);
64+
$this->assertContains('FIRST_NAME', $names);
65+
$this->assertContains('LAST_NAME', $names);
66+
$this->assertContains('AGE', $names);
67+
}
68+
69+
public function testSingletonBehavior(): void
70+
{
71+
$enum1 = ValidTestEnum::from('first');
72+
$enum2 = ValidTestEnum::from('first');
73+
$enum3 = ValidTestEnum::firstName();
74+
75+
$this->assertSame($enum1, $enum2);
76+
$this->assertSame($enum1, $enum3);
77+
}
78+
79+
public function testStaticFactoryMethods(): void
80+
{
81+
$firstName = ValidTestEnum::firstName();
82+
$this->assertSame('first', $firstName->value);
83+
$this->assertSame('FIRST_NAME', $firstName->name);
84+
85+
$lastName = ValidTestEnum::lastName();
86+
$this->assertSame('last', $lastName->value);
87+
$this->assertSame('LAST_NAME', $lastName->name);
88+
89+
$age = ValidTestEnum::age();
90+
$this->assertSame(42, $age->value);
91+
$this->assertSame('AGE', $age->name);
92+
}
93+
94+
public function testInvalidStaticMethodThrowsException(): void
95+
{
96+
$this->expectException(BadMethodCallException::class);
97+
$this->expectExceptionMessage(
98+
'Method WordPress\AiClient\Tests\unit\Common\ValidTestEnum::invalidMethod does not exist'
99+
);
100+
ValidTestEnum::invalidMethod();
101+
}
102+
103+
public function testIsCheckMethods(): void
104+
{
105+
$enum = ValidTestEnum::firstName();
106+
107+
$this->assertTrue($enum->isFirstName());
108+
$this->assertFalse($enum->isLastName());
109+
$this->assertFalse($enum->isAge());
110+
}
111+
112+
public function testInvalidIsMethodThrowsException(): void
113+
{
114+
$enum = ValidTestEnum::firstName();
115+
116+
$this->expectException(BadMethodCallException::class);
117+
$this->expectExceptionMessage(
118+
'Method WordPress\AiClient\Tests\unit\Common\ValidTestEnum::isInvalidMethod does not exist'
119+
);
120+
$enum->isInvalidMethod();
121+
}
122+
123+
public function testEqualsWithSameValue(): void
124+
{
125+
$enum = ValidTestEnum::firstName();
126+
127+
$this->assertTrue($enum->equals('first'));
128+
$this->assertTrue($enum->equals(ValidTestEnum::firstName()));
129+
$this->assertFalse($enum->equals('last'));
130+
$this->assertFalse($enum->equals(ValidTestEnum::lastName()));
131+
}
132+
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+
142+
public function testIsMethodForIdentityComparison(): void
143+
{
144+
$enum1 = ValidTestEnum::firstName();
145+
$enum2 = ValidTestEnum::firstName();
146+
$enum3 = ValidTestEnum::lastName();
147+
148+
$this->assertTrue($enum1->is($enum2)); // Same instance
149+
$this->assertFalse($enum1->is($enum3)); // Different instance
150+
}
151+
152+
public function testGetValuesReturnsAllValidValues(): void
153+
{
154+
$values = ValidTestEnum::getValues();
155+
156+
$this->assertSame([
157+
'FIRST_NAME' => 'first',
158+
'LAST_NAME' => 'last',
159+
'AGE' => 42,
160+
], $values);
161+
}
162+
163+
public function testIsValidValue(): void
164+
{
165+
$this->assertTrue(ValidTestEnum::isValidValue('first'));
166+
$this->assertTrue(ValidTestEnum::isValidValue('last'));
167+
$this->assertTrue(ValidTestEnum::isValidValue(42));
168+
169+
$this->assertFalse(ValidTestEnum::isValidValue('invalid'));
170+
$this->assertFalse(ValidTestEnum::isValidValue(43));
171+
}
172+
173+
public function testFromValueDeprecatedMethod(): void
174+
{
175+
$enum = ValidTestEnum::fromValue('first');
176+
$this->assertInstanceOf(ValidTestEnum::class, $enum);
177+
$this->assertSame('first', $enum->value);
178+
}
179+
180+
public function testPropertiesAreReadOnly(): void
181+
{
182+
$enum = ValidTestEnum::firstName();
183+
184+
$this->expectException(BadMethodCallException::class);
185+
$this->expectExceptionMessage(
186+
'Cannot modify property WordPress\AiClient\Tests\unit\Common\ValidTestEnum::value - enum properties are read-only'
187+
);
188+
$enum->value = 'modified';
189+
}
190+
191+
public function testInvalidPropertyAccessThrowsException(): void
192+
{
193+
$enum = ValidTestEnum::firstName();
194+
195+
$this->expectException(BadMethodCallException::class);
196+
$this->expectExceptionMessage(
197+
'Property WordPress\AiClient\Tests\unit\Common\ValidTestEnum::invalid does not exist'
198+
);
199+
$enum->invalid;
200+
}
201+
202+
public function testToString(): void
203+
{
204+
$stringEnum = ValidTestEnum::firstName();
205+
$intEnum = ValidTestEnum::age();
206+
207+
$this->assertSame('first', (string) $stringEnum);
208+
$this->assertSame('42', (string) $intEnum);
209+
}
210+
211+
public function testInvalidConstantNameThrowsException(): void
212+
{
213+
$this->expectException(RuntimeException::class);
214+
$this->expectExceptionMessage(
215+
'Invalid enum constant name "invalid_name" in ' .
216+
'WordPress\AiClient\Tests\unit\Common\InvalidNameTestEnum. Constants must be UPPER_SNAKE_CASE.'
217+
);
218+
219+
InvalidNameTestEnum::cases();
220+
}
221+
222+
public function testInvalidConstantTypeThrowsException(): void
223+
{
224+
$this->expectException(RuntimeException::class);
225+
$this->expectExceptionMessage(
226+
'Invalid enum value type for constant ' .
227+
'WordPress\AiClient\Tests\unit\Common\InvalidTypeTestEnum::FLOAT_VALUE. ' .
228+
'Only string and int values are allowed, double given.'
229+
);
230+
231+
InvalidTypeTestEnum::cases();
232+
}
233+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace WordPress\AiClient\Tests\unit\Common;
6+
7+
use WordPress\AiClient\Common\AbstractEnum;
8+
9+
/**
10+
* Invalid test enum with lowercase constant name
11+
*/
12+
class InvalidNameTestEnum extends AbstractEnum
13+
{
14+
public const VALID_NAME = 'valid';
15+
// phpcs:ignore Generic.NamingConventions.UpperCaseConstantName.ClassConstantNotUpperCase
16+
public const invalid_name = 'invalid'; // This should cause an exception
17+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace WordPress\AiClient\Tests\unit\Common;
6+
7+
use WordPress\AiClient\Common\AbstractEnum;
8+
9+
/**
10+
* Invalid test enum with float value
11+
*/
12+
class InvalidTypeTestEnum extends AbstractEnum
13+
{
14+
public const VALID_VALUE = 'valid';
15+
public const FLOAT_VALUE = 3.14; // This should cause an exception
16+
}

tests/unit/Common/ValidTestEnum.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace WordPress\AiClient\Tests\unit\Common;
6+
7+
use WordPress\AiClient\Common\AbstractEnum;
8+
9+
/**
10+
* Valid test enum for testing AbstractEnum functionality
11+
*
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
18+
*/
19+
class ValidTestEnum extends AbstractEnum
20+
{
21+
public const FIRST_NAME = 'first';
22+
public const LAST_NAME = 'last';
23+
public const AGE = 42;
24+
}

0 commit comments

Comments
 (0)