1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace ArpTest\Enum; |
6
|
|
|
|
7
|
|
|
use Arp\Enum\AbstractEnum; |
8
|
|
|
use Arp\Enum\EnumInterface; |
9
|
|
|
use PHPUnit\Framework\TestCase; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* @covers \Arp\Enum\AbstractEnum |
13
|
|
|
* |
14
|
|
|
* @author Alex Patterson <[email protected]> |
15
|
|
|
* @package ArpTest\Enum |
16
|
|
|
*/ |
17
|
|
|
class AbstractEnumTest extends TestCase |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* @var EnumInterface |
21
|
|
|
*/ |
22
|
|
|
private EnumInterface $enum; |
23
|
|
|
|
24
|
|
|
public function setUp(): void |
25
|
|
|
{ |
26
|
|
|
$this->enum = new class () extends AbstractEnum { |
27
|
|
|
public const FOO = -1; |
28
|
|
|
public const BAR = true; |
29
|
|
|
public const BAZ = 1.223; |
30
|
|
|
public const FAB = 'hello'; |
31
|
|
|
public const FOB = 1234; |
32
|
|
|
}; |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Assert the class implements EnumInterface |
37
|
|
|
*/ |
38
|
|
|
public function testImplementsEnumInterface(): void |
39
|
|
|
{ |
40
|
|
|
$this->assertInstanceOf(EnumInterface::class, $this->enum); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Assert the class implements EnumInterface |
45
|
|
|
*/ |
46
|
|
|
public function testWillThrowInvalidArgumentException(): void |
47
|
|
|
{ |
48
|
|
|
$invalidValue = 123; |
49
|
|
|
|
50
|
|
|
$this->expectException(\InvalidArgumentException::class); |
51
|
|
|
$this->expectExceptionMessage( |
52
|
|
|
sprintf('The value \'%s\' for enum class', $invalidValue) |
53
|
|
|
); |
54
|
|
|
|
55
|
|
|
new class ($invalidValue) extends AbstractEnum { |
56
|
|
|
public const TRUE = 1; |
57
|
|
|
public const FALSE = 0; |
58
|
|
|
public const MAYBE = 2; |
59
|
|
|
}; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Assert that toArray() will return a array map of the class constants |
64
|
|
|
*/ |
65
|
|
|
public function testToArray(): void |
66
|
|
|
{ |
67
|
|
|
$expected = [ |
68
|
|
|
'FOO' => -1, |
69
|
|
|
'BAR' => true, |
70
|
|
|
'BAZ' => 1.223, |
71
|
|
|
'FAB' => 'hello', |
72
|
|
|
'FOB' => 1234, |
73
|
|
|
]; |
74
|
|
|
|
75
|
|
|
$this->assertSame($expected, $this->enum::toArray()); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Assert calls to hasKey() return the expected bool |
80
|
|
|
*/ |
81
|
|
|
public function testHasKey(): void |
82
|
|
|
{ |
83
|
|
|
$this->assertTrue($this->enum::hasKey('FOO')); |
84
|
|
|
$this->assertTrue($this->enum::hasKey('BAR')); |
85
|
|
|
$this->assertTrue($this->enum::hasKey('BAZ')); |
86
|
|
|
$this->assertFalse($this->enum::hasKey('HELLO')); |
87
|
|
|
$this->assertFalse($this->enum::hasKey('TEST')); |
88
|
|
|
$this->assertFalse($this->enum::hasKey('ABC')); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Assert the result of getKeys() |
93
|
|
|
*/ |
94
|
|
|
public function testGetKeys(): void |
95
|
|
|
{ |
96
|
|
|
$expected = [ |
97
|
|
|
'FOO', |
98
|
|
|
'BAR', |
99
|
|
|
'BAZ', |
100
|
|
|
'FAB', |
101
|
|
|
'FOB', |
102
|
|
|
]; |
103
|
|
|
|
104
|
|
|
$this->assertSame($expected, $this->enum::getKeys()); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* Assert the result of testGetValues() |
109
|
|
|
*/ |
110
|
|
|
public function testGetValues(): void |
111
|
|
|
{ |
112
|
|
|
$expected = [ |
113
|
|
|
-1, |
114
|
|
|
true, |
115
|
|
|
1.223, |
116
|
|
|
'hello', |
117
|
|
|
1234, |
118
|
|
|
]; |
119
|
|
|
|
120
|
|
|
$this->assertSame($expected, $this->enum::getValues()); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* Assert that the class constant key can be returned by it's matched value, or NULL if non-existing |
125
|
|
|
* |
126
|
|
|
* @param mixed $expectedKey |
127
|
|
|
* @param mixed $value |
128
|
|
|
* |
129
|
|
|
* @dataProvider getKeyByValueData |
130
|
|
|
*/ |
131
|
|
|
public function testGetKeyByValue($expectedKey, $value): void |
132
|
|
|
{ |
133
|
|
|
$this->assertSame($expectedKey, $this->enum::getKeyByValue($value)); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* @return array<int, mixed> |
138
|
|
|
*/ |
139
|
|
|
public function getKeyByValueData(): array |
140
|
|
|
{ |
141
|
|
|
return [ |
142
|
|
|
[ |
143
|
|
|
'FOO', |
144
|
|
|
-1, |
145
|
|
|
], |
146
|
|
|
[ |
147
|
|
|
'BAR', |
148
|
|
|
true, |
149
|
|
|
], |
150
|
|
|
[ |
151
|
|
|
'FAB', |
152
|
|
|
'hello', |
153
|
|
|
], |
154
|
|
|
[ |
155
|
|
|
'BAZ', |
156
|
|
|
1.223, |
157
|
|
|
], |
158
|
|
|
[ |
159
|
|
|
null, |
160
|
|
|
'not-valid-vale', |
161
|
|
|
], |
162
|
|
|
[ |
163
|
|
|
null, |
164
|
|
|
0, |
165
|
|
|
], |
166
|
|
|
]; |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* Assert that the class constant value can be returned by it's matched key, or NULL if non-existing |
171
|
|
|
* |
172
|
|
|
* @param mixed $expectedValue |
173
|
|
|
* @param string $key |
174
|
|
|
* |
175
|
|
|
* @dataProvider getValueByKeyData |
176
|
|
|
*/ |
177
|
|
|
public function testGetValueByKey($expectedValue, string $key): void |
178
|
|
|
{ |
179
|
|
|
$this->assertSame($expectedValue, $this->enum::getValueByKey($key)); |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
/** |
183
|
|
|
* @return array<int, mixed> |
184
|
|
|
*/ |
185
|
|
|
public function getValueByKeyData(): array |
186
|
|
|
{ |
187
|
|
|
return [ |
188
|
|
|
[ |
189
|
|
|
-1, |
190
|
|
|
'FOO', |
191
|
|
|
], |
192
|
|
|
[ |
193
|
|
|
true, |
194
|
|
|
'BAR', |
195
|
|
|
], |
196
|
|
|
[ |
197
|
|
|
'hello', |
198
|
|
|
'FAB', |
199
|
|
|
], |
200
|
|
|
[ |
201
|
|
|
1.223, |
202
|
|
|
'BAZ', |
203
|
|
|
], |
204
|
|
|
[ |
205
|
|
|
null, |
206
|
|
|
'testing-not-found-value', |
207
|
|
|
], |
208
|
|
|
]; |
209
|
|
|
} |
210
|
|
|
|
211
|
|
|
/** |
212
|
|
|
* Assert the expected key is returned from getKey() when using instance based methods of the enum. |
213
|
|
|
*/ |
214
|
|
|
public function testGetKey(): void |
215
|
|
|
{ |
216
|
|
|
$value = 1.223; |
217
|
|
|
$expectedKey = 'BAZ'; |
218
|
|
|
|
219
|
|
|
$enum = new class ($value) extends AbstractEnum { |
220
|
|
|
public const FOO = -1; |
221
|
|
|
public const BAR = true; |
222
|
|
|
public const BAZ = 1.223; |
223
|
|
|
public const FAB = 'hello'; |
224
|
|
|
public const FOB = 1234; |
225
|
|
|
}; |
226
|
|
|
|
227
|
|
|
$this->assertSame($expectedKey, $enum->getKey()); |
228
|
|
|
} |
229
|
|
|
|
230
|
|
|
/** |
231
|
|
|
* Assert the expected value is returned from getValue() when using instance based methods of the enum. |
232
|
|
|
* |
233
|
|
|
* @param mixed $value |
234
|
|
|
* |
235
|
|
|
* @dataProvider getGetValueData |
236
|
|
|
*/ |
237
|
|
|
public function testGetValue($value): void |
238
|
|
|
{ |
239
|
|
|
$enum = new class ($value) extends AbstractEnum { |
240
|
|
|
public const FOO = -1; |
241
|
|
|
public const BAR = true; |
242
|
|
|
public const BAZ = 1.223; |
243
|
|
|
public const FAB = 'hello'; |
244
|
|
|
public const FOB = 1234; |
245
|
|
|
}; |
246
|
|
|
|
247
|
|
|
$this->assertSame( |
248
|
|
|
($value instanceof EnumInterface) ? $value->getValue() : $value, |
249
|
|
|
$enum->getValue() |
250
|
|
|
); |
251
|
|
|
} |
252
|
|
|
|
253
|
|
|
/** |
254
|
|
|
* @return array<mixed> |
255
|
|
|
*/ |
256
|
|
|
public function getGetValueData(): array |
257
|
|
|
{ |
258
|
|
|
$enum = new class (true) extends AbstractEnum { |
259
|
|
|
public const TEST = 'testing'; |
260
|
|
|
public const HELLO = true; |
261
|
|
|
public const KEY_NAME = 'value123'; |
262
|
|
|
public const ANOTHER_KEY = 123; |
263
|
|
|
}; |
264
|
|
|
|
265
|
|
|
return [ |
266
|
|
|
[ |
267
|
|
|
null, |
268
|
|
|
], |
269
|
|
|
[ |
270
|
|
|
1234, |
271
|
|
|
], |
272
|
|
|
[ |
273
|
|
|
'hello', |
274
|
|
|
], |
275
|
|
|
[ |
276
|
|
|
-1, |
277
|
|
|
], |
278
|
|
|
[ |
279
|
|
|
true, |
280
|
|
|
], |
281
|
|
|
[ |
282
|
|
|
$enum |
283
|
|
|
] |
284
|
|
|
]; |
285
|
|
|
} |
286
|
|
|
} |
287
|
|
|
|