Debian Codegraph @

test/EnumMapTest.php

raw
1 <?php
2 declare(strict_types = 1);
3
4 namespace DASPRiD\EnumTest;
5
6 use DASPRiD\Enum\EnumMap;
7 use DASPRiD\Enum\Exception\ExpectationException;
8 use DASPRiD\Enum\Exception\IllegalArgumentException;
9 use PHPUnit\Framework\TestCase;
10 use stdClass;
11
12 final class EnumMapTest extends TestCase
13 {
14 public function testConstructionWithInvalidEnumType() : void
15 {
16 $this->expectException(IllegalArgumentException::class);
17 new EnumMap(stdClass::class, 'string', false);
18 }
19
20 public function testUnexpectedKeyType() : void
21 {
22 $this->expectException(ExpectationException::class);
23 $map = new EnumMap(WeekDay::class, 'string', false);
24 $map->expect(Planet::class, 'string', false);
25 }
26
27 public function testUnexpectedValueType() : void
28 {
29 $this->expectException(ExpectationException::class);
30 $map = new EnumMap(WeekDay::class, 'string', false);
31 $map->expect(WeekDay::class, 'int', false);
32 }
33
34 public function testUnexpectedNullableValueType() : void
35 {
36 $this->expectException(ExpectationException::class);
37 $map = new EnumMap(WeekDay::class, 'string', true);
38 $map->expect(WeekDay::class, 'string', false);
39 }
40
41 public function testExpectedTypes() : void
42 {
43 $map = new EnumMap(WeekDay::class, 'string', true);
44 $map->expect(WeekDay::class, 'string', true);
45 $this->addToAssertionCount(1);
46 }
47
48 public function testSize() : void
49 {
50 $map = new EnumMap(WeekDay::class, 'string', true);
51 $this->assertSame(0, $map->size());
52 $map->put(WeekDay::MONDAY(), 'foo');
53 $this->assertSame(1, $map->size());
54 }
55
56 public function testContainsValue() : void
57 {
58 $map = new EnumMap(WeekDay::class, 'string', true);
59 $this->assertFalse($map->containsValue('foo'));
60 $map->put(WeekDay::TUESDAY(), 'foo');
61 $this->assertTrue($map->containsValue('foo'));
62 $this->assertFalse($map->containsValue(null));
63 $map->put(WeekDay::WEDNESDAY(), null);
64 $this->assertTrue($map->containsValue(null));
65 }
66
67 public function testContainsKey() : void
68 {
69 $map = new EnumMap(WeekDay::class, 'string', true);
70 $this->assertFalse($map->containsKey(WeekDay::TUESDAY()));
71 $map->put(WeekDay::TUESDAY(), 'foo');
72 $this->assertTrue($map->containsKey(WeekDay::TUESDAY()));
73 $map->put(WeekDay::WEDNESDAY(), null);
74 $this->assertTrue($map->containsKey(WeekDay::WEDNESDAY()));
75 }
76
77 public function testPutAndGet() : void
78 {
79 $map = new EnumMap(WeekDay::class, 'string', true);
80 $map->put(WeekDay::TUESDAY(), 'foo');
81 $map->put(WeekDay::FRIDAY(), null);
82 $this->assertSame('foo', $map->get(WeekDay::TUESDAY()));
83 $this->assertSame(null, $map->get(WeekDay::WEDNESDAY()));
84 $this->assertSame(null, $map->get(WeekDay::FRIDAY()));
85 }
86
87 public function testPutInvalidKey() : void
88 {
89 $this->expectException(IllegalArgumentException::class);
90 $map = new EnumMap(WeekDay::class, 'string', true);
91 $map->put(Planet::MARS(), 'foo');
92 }
93
94 public static function invalidValues() : array
95 {
96 return [
97 ['bool', null, false],
98 ['bool', 0],
99 ['boolean', 0],
100 ['int', 2.4],
101 ['integer', 5.3],
102 ['float', 3],
103 ['double', 7],
104 ['string', 1],
105 ['object', 1],
106 ['array', 1],
107 [stdClass::class, 1],
108 ];
109 }
110
111 /**
112 * @dataProvider invalidValues
113 * @param mixed $value
114 */
115 #[\PHPUnit\Framework\Attributes\DataProvider('invalidValues')]
116 public function testPutInvalidValue(string $valueType, $value, bool $allowNull = true) : void
117 {
118 $this->expectException(IllegalArgumentException::class);
119 $map = new EnumMap(WeekDay::class, $valueType, $allowNull);
120 $map->put(WeekDay::TUESDAY(), $value);
121 }
122
123 public static function validValues() : array
124 {
125 return [
126 ['bool', null],
127 ['mixed', 'foo'],
128 ['mixed', 1],
129 ['mixed', new stdClass()],
130 ['bool', true],
131 ['boolean', false],
132 ['int', 1],
133 ['integer', 4],
134 ['float', 2.5],
135 ['double', 6.4],
136 ['string', 'foo'],
137 ['object', new stdClass()],
138 ['array', ['foo']],
139 [stdClass::class, new stdClass()],
140 ];
141 }
142
143 /**
144 * @dataProvider validValues
145 * @param mixed $value
146 */
147 #[\PHPUnit\Framework\Attributes\DataProvider('validValues')]
148 public function testPutValidValue(string $valueType, $value, bool $allowNull = true) : void
149 {
150 $map = new EnumMap(WeekDay::class, $valueType, $allowNull);
151 $map->put(WeekDay::TUESDAY(), $value);
152 $this->addToAssertionCount(1);
153 }
154
155 public function testRemove() : void
156 {
157 $map = new EnumMap(WeekDay::class, 'string', true);
158 $map->put(WeekDay::TUESDAY(), 'foo');
159 $map->remove(WeekDay::TUESDAY());
160 $map->remove(WeekDay::WEDNESDAY());
161 $this->assertSame(null, $map->get(WeekDay::TUESDAY()));
162 $this->assertSame(0, $map->size());
163 }
164
165 public function testClear() : void
166 {
167 $map = new EnumMap(WeekDay::class, 'string', true);
168 $map->put(WeekDay::TUESDAY(), 'foo');
169 $map->clear();
170 $this->assertSame(null, $map->get(WeekDay::TUESDAY()));
171 $this->assertSame(0, $map->size());
172 }
173
174 public function testEqualsWithSameInstance() : void
175 {
176 $map = new EnumMap(WeekDay::class, 'string', true);
177 $this->assertTrue($map->equals($map));
178 }
179
180 public function testEqualsWithDifferentSize() : void
181 {
182 $mapA = new EnumMap(WeekDay::class, 'string', true);
183 $mapB = new EnumMap(WeekDay::class, 'string', true);
184 $mapB->put(WeekDay::MONDAY(), 'foo');
185
186 $this->assertFalse($mapA->equals($mapB));
187 }
188
189 public function testEqualsWithDifferentValues() : void
190 {
191 $mapA = new EnumMap(WeekDay::class, 'string', true);
192 $mapA->put(WeekDay::MONDAY(), 'foo');
193 $mapB = new EnumMap(WeekDay::class, 'string', true);
194 $mapB->put(WeekDay::MONDAY(), 'bar');
195
196 $this->assertFalse($mapA->equals($mapB));
197 }
198
199 public function testEqualsWithDifferentConstants() : void
200 {
201 $mapA = new EnumMap(WeekDay::class, 'string', true);
202 $mapA->put(WeekDay::MONDAY(), 'foo');
203 $mapB = new EnumMap(WeekDay::class, 'string', true);
204 $mapB->put(WeekDay::TUESDAY(), 'foo');
205
206 $this->assertFalse($mapA->equals($mapB));
207 }
208
209 public function testValues() : void
210 {
211 $map = new EnumMap(WeekDay::class, 'string', true);
212 $this->assertSame([], $map->values());
213
214 $map->put(WeekDay::FRIDAY(), 'foo');
215 $map->put(WeekDay::TUESDAY(), 'bar');
216 $map->put(WeekDay::SUNDAY(), null);
217
218 $this->assertSame(['bar', 'foo', null], $map->values());
219 }
220
221 public function testSerializeAndUnserialize() : void
222 {
223 $mapA = new EnumMap(WeekDay::class, 'string', true);
224 $mapA->put(WeekDay::MONDAY(), 'foo');
225 $mapB = unserialize(serialize($mapA));
226
227 $this->assertTrue($mapA->equals($mapB));
228 }
229
230 public function testIterator() : void
231 {
232 $map = new EnumMap(WeekDay::class, 'string', true);
233 $map->put(WeekDay::FRIDAY(), 'foo');
234 $map->put(WeekDay::TUESDAY(), 'bar');
235 $map->put(WeekDay::SUNDAY(), null);
236
237 $result = [];
238
239 foreach ($map as $key => $value) {
240 $result[$key->ordinal()] = $value;
241 }
242
243 $this->assertSame([1 => 'bar', 4 => 'foo', 6 => null], $result);
244 }
245 }