Debian Codegraph @

test/AbstractEnumTest.php

raw
1 <?php
2 declare(strict_types = 1);
3
4 namespace DASPRiD\EnumTest;
5
6 use DASPRiD\Enum\AbstractEnum;
7 use DASPRiD\Enum\Exception\CloneNotSupportedException;
8 use DASPRiD\Enum\Exception\IllegalArgumentException;
9 use DASPRiD\Enum\Exception\MismatchException;
10 use DASPRiD\Enum\Exception\SerializeNotSupportedException;
11 use DASPRiD\Enum\Exception\UnserializeNotSupportedException;
12 use PHPUnit\Framework\TestCase;
13 use ReflectionClass;
14
15 final class AbstractEnumTest extends TestCase
16 {
17 public function setUp(): void
18 {
19 $reflectionClass = new ReflectionClass(AbstractEnum::class);
20
21 $constantsProperty = $reflectionClass->getProperty('constants');
22 $constantsProperty->setAccessible(true);
23 $constantsProperty->setValue([]);
24
25 $valuesProperty = $reflectionClass->getProperty('values');
26 $valuesProperty->setAccessible(true);
27 $valuesProperty->setValue([]);
28
29 $allValuesLoadedProperty = $reflectionClass->getProperty('allValuesLoaded');
30 $allValuesLoadedProperty->setAccessible(true);
31 $allValuesLoadedProperty->setValue([]);
32 }
33
34 public function testToString() : void
35 {
36 $weekday = WeekDay::FRIDAY();
37 self::assertSame('FRIDAY', (string) $weekday);
38 }
39
40 public function testName() : void
41 {
42 $this->assertSame('WEDNESDAY', WeekDay::WEDNESDAY()->name());
43 }
44
45 public function testOrdinal() : void
46 {
47 $this->assertSame(2, WeekDay::WEDNESDAY()->ordinal());
48 }
49
50 public function testSameInstanceIsReturned() : void
51 {
52 self::assertSame(WeekDay::FRIDAY(), WeekDay::FRIDAY());
53 }
54
55 public static function testValueOf() : void
56 {
57 self::assertSame(WeekDay::FRIDAY(), WeekDay::valueOf('FRIDAY'));
58 }
59
60 public function testValueOfInvalidConstant() : void
61 {
62 $this->expectException(IllegalArgumentException::class);
63 WeekDay::valueOf('CATURDAY');
64 }
65
66 public function testExceptionOnCloneAttempt() : void
67 {
68 $this->expectException(CloneNotSupportedException::class);
69 clone WeekDay::FRIDAY();
70 }
71
72 public function testExceptionOnSerializeAttempt() : void
73 {
74 $this->expectException(SerializeNotSupportedException::class);
75 serialize(WeekDay::FRIDAY());
76 }
77
78 public function testExceptionOnUnserializeAttempt() : void
79 {
80 $this->expectException(UnserializeNotSupportedException::class);
81 unserialize('O:24:"DASPRiD\\EnumTest\\WeekDay":0:{}');
82 }
83
84 public function testReturnValueOfValuesIsSortedByOrdinal() : void
85 {
86 // Initialize some week days out of order
87 WeekDay::SATURDAY();
88 WeekDay::TUESDAY();
89
90 $ordinals = array_values(array_map(function (WeekDay $weekDay) : int {
91 return $weekDay->ordinal();
92 }, WeekDay::values()));
93
94 self::assertSame([0, 1, 2, 3, 4, 5, 6], $ordinals);
95
96 $cachedOrdinals = array_values(array_map(function (WeekDay $weekDay) : int {
97 return $weekDay->ordinal();
98 }, WeekDay::values()));
99 $this->assertSame($ordinals, $cachedOrdinals);
100 }
101
102 public function testCompareTo() : void
103 {
104 $this->assertSame(-4, WeekDay::WEDNESDAY()->compareTo(WeekDay::SUNDAY()));
105 $this->assertSame(4, WeekDay::SUNDAY()->compareTo(WeekDay::WEDNESDAY()));
106 $this->assertSame(0, WeekDay::WEDNESDAY()->compareTo(WeekDay::WEDNESDAY()));
107 }
108
109 public function testCompareToWrongEnum() : void
110 {
111 $this->expectException(MismatchException::class);
112 WeekDay::MONDAY()->compareTo(Planet::EARTH());
113 }
114
115 public function testParameterizedEnum() : void
116 {
117 $planet = Planet::EARTH();
118 $this->assertSame(5.976e+24, $planet->mass());
119 $this->assertSame(6.37814e6, $planet->radius());
120 }
121 }