-
-
Notifications
You must be signed in to change notification settings - Fork 132
/
Copy pathEnumTest.php
executable file
·428 lines (368 loc) · 13.8 KB
/
EnumTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
<?php
/**
* @link http://github.com/myclabs/php-enum
* @license http://www.opensource.org/licenses/mit-license.php MIT (see the LICENSE file)
*/
namespace MyCLabs\Tests\Enum;
use MyCLabs\Enum\Enum;
/**
* @author Matthieu Napoli <[email protected]>
* @author Daniel Costa <[email protected]>
* @author Mirosław Filip <[email protected]>
*/
class EnumTest extends \PHPUnit\Framework\TestCase
{
/**
* getValue()
*/
public function testGetValue()
{
$value = new EnumFixture(EnumFixture::FOO);
$this->assertEquals(EnumFixture::FOO, $value->getValue());
$value = new EnumFixture(EnumFixture::BAR);
$this->assertEquals(EnumFixture::BAR, $value->getValue());
$value = new EnumFixture(EnumFixture::NUMBER);
$this->assertEquals(EnumFixture::NUMBER, $value->getValue());
}
/**
* getKey()
*/
public function testGetKey()
{
$value = new EnumFixture(EnumFixture::FOO);
$this->assertEquals('FOO', $value->getKey());
$this->assertNotEquals('BA', $value->getKey());
}
/** @dataProvider invalidValueProvider */
public function testCreatingEnumWithInvalidValue($value)
{
$this->expectException(\UnexpectedValueException::class);
$this->expectExceptionMessage('is not part of the enum ' . EnumFixture::class);
new EnumFixture($value);
}
/**
* @dataProvider invalidValueProvider
* @param mixed $value
*/
public function testFailToCreateEnumWithInvalidValueThroughNamedConstructor($value): void
{
$this->expectException(\UnexpectedValueException::class);
$this->expectExceptionMessage('is not part of the enum MyCLabs\Tests\Enum\EnumFixture');
EnumFixture::from($value);
}
/**
* @dataProvider invalidValueProvider
* @param mixed $value
*/
public function testRejectInvalidEnumValueWhenAssertingOnIt($value): void
{
$this->expectException(\UnexpectedValueException::class);
$this->expectExceptionMessage('is not part of the enum MyCLabs\Tests\Enum\EnumFixture');
Enum::assertValidEnumValue(EnumFixture::class, $value);
}
/**
* @dataProvider invalidValueProvider
* @param mixed $value
*/
public function testReportsFalseOnNonValidEnumValueUponChecking($value): void
{
self::assertFalse(Enum::isValidEnumValue(EnumFixture::class, $value));
}
public function testFailToCreateEnumWithEnumItselfThroughNamedConstructor(): void
{
$this->expectException(\UnexpectedValueException::class);
$this->expectExceptionMessage("Value 'foo' is not part of the enum " . EnumFixture::class);
EnumFixture::from(EnumFixture::FOO());
}
public function testCreateEnumWithValidEnumValueThroughNamedConstructor(): void
{
self::assertEquals(EnumFixture::FOO(), EnumFixture::from('foo'));
}
/**
* Contains values not existing in EnumFixture
* @return array
*/
public function invalidValueProvider()
{
return array(
"string" => array('test'),
"int" => array(1234),
);
}
/**
* __toString()
* @dataProvider toStringProvider
*/
public function testToString($expected, $enumObject)
{
$this->assertSame($expected, (string) $enumObject);
}
public function toStringProvider()
{
return array(
array(EnumFixture::FOO, new EnumFixture(EnumFixture::FOO)),
array(EnumFixture::BAR, new EnumFixture(EnumFixture::BAR)),
array((string) EnumFixture::NUMBER, new EnumFixture(EnumFixture::NUMBER)),
);
}
/**
* keys()
*/
public function testKeys()
{
$values = EnumFixture::keys();
$expectedValues = array(
"FOO",
"BAR",
"NUMBER",
"PROBLEMATIC_NUMBER",
"PROBLEMATIC_NULL",
"PROBLEMATIC_EMPTY_STRING",
"PROBLEMATIC_BOOLEAN_FALSE",
);
$this->assertSame($expectedValues, $values);
}
/**
* values()
*/
public function testValues()
{
$values = EnumFixture::values();
$expectedValues = array(
"FOO" => new EnumFixture(EnumFixture::FOO),
"BAR" => new EnumFixture(EnumFixture::BAR),
"NUMBER" => new EnumFixture(EnumFixture::NUMBER),
"PROBLEMATIC_NUMBER" => new EnumFixture(EnumFixture::PROBLEMATIC_NUMBER),
"PROBLEMATIC_NULL" => new EnumFixture(EnumFixture::PROBLEMATIC_NULL),
"PROBLEMATIC_EMPTY_STRING" => new EnumFixture(EnumFixture::PROBLEMATIC_EMPTY_STRING),
"PROBLEMATIC_BOOLEAN_FALSE" => new EnumFixture(EnumFixture::PROBLEMATIC_BOOLEAN_FALSE),
);
$this->assertEquals($expectedValues, $values);
}
/**
* toArray()
*/
public function testToArray()
{
$values = EnumFixture::toArray();
$expectedValues = array(
"FOO" => EnumFixture::FOO,
"BAR" => EnumFixture::BAR,
"NUMBER" => EnumFixture::NUMBER,
"PROBLEMATIC_NUMBER" => EnumFixture::PROBLEMATIC_NUMBER,
"PROBLEMATIC_NULL" => EnumFixture::PROBLEMATIC_NULL,
"PROBLEMATIC_EMPTY_STRING" => EnumFixture::PROBLEMATIC_EMPTY_STRING,
"PROBLEMATIC_BOOLEAN_FALSE" => EnumFixture::PROBLEMATIC_BOOLEAN_FALSE,
);
$this->assertSame($expectedValues, $values);
}
/**
* __callStatic()
*/
public function testStaticAccess()
{
$this->assertEquals(new EnumFixture(EnumFixture::FOO), EnumFixture::FOO());
$this->assertEquals(new EnumFixture(EnumFixture::BAR), EnumFixture::BAR());
$this->assertEquals(new EnumFixture(EnumFixture::NUMBER), EnumFixture::NUMBER());
$this->assertNotSame(EnumFixture::NUMBER(), EnumFixture::NUMBER());
}
public function testBadStaticAccess()
{
$this->expectException(\BadMethodCallException::class);
$this->expectExceptionMessage('No static method or enum constant \'UNKNOWN\' in class ' . EnumFixture::class);
EnumFixture::UNKNOWN();
}
/**
* isValid()
* @dataProvider isValidProvider
*/
public function testIsValid($value, $isValid)
{
self::assertSame($isValid, EnumFixture::isValid($value));
self::assertSame($isValid, Enum::isValidEnumValue(EnumFixture::class, $value));
}
public function isValidProvider()
{
return [
/**
* Valid values
*/
['foo', true],
[42, true],
[null, true],
[0, true],
['', true],
[false, true],
/**
* Invalid values
*/
['baz', false]
];
}
/**
* isValidKey()
*/
public function testIsValidKey()
{
$this->assertTrue(EnumFixture::isValidKey('FOO'));
$this->assertFalse(EnumFixture::isValidKey('BAZ'));
$this->assertTrue(EnumFixture::isValidKey('PROBLEMATIC_NULL'));
}
/**
* search()
* @see https://github.com/myclabs/php-enum/issues/13
* @dataProvider searchProvider
*/
public function testSearch($value, $expected)
{
$this->assertSame($expected, EnumFixture::search($value));
}
public function searchProvider()
{
return array(
array('foo', 'FOO'),
array(0, 'PROBLEMATIC_NUMBER'),
array(null, 'PROBLEMATIC_NULL'),
array('', 'PROBLEMATIC_EMPTY_STRING'),
array(false, 'PROBLEMATIC_BOOLEAN_FALSE'),
array('bar I do not exist', false),
array(array(), false),
);
}
/**
* equals()
*/
public function testEquals()
{
$foo = new EnumFixture(EnumFixture::FOO);
$number = new EnumFixture(EnumFixture::NUMBER);
$anotherFoo = new EnumFixture(EnumFixture::FOO);
$objectOfDifferentClass = new \stdClass();
$notAnObject = 'foo';
$this->assertTrue($foo->equals($foo));
$this->assertFalse($foo->equals($number));
$this->assertTrue($foo->equals($anotherFoo));
$this->assertFalse($foo->equals(null));
$this->assertFalse($foo->equals($objectOfDifferentClass));
$this->assertFalse($foo->equals($notAnObject));
}
/**
* equals()
*/
public function testEqualsComparesProblematicValuesProperly()
{
$false = new EnumFixture(EnumFixture::PROBLEMATIC_BOOLEAN_FALSE);
$emptyString = new EnumFixture(EnumFixture::PROBLEMATIC_EMPTY_STRING);
$null = new EnumFixture(EnumFixture::PROBLEMATIC_NULL);
$this->assertTrue($false->equals($false));
$this->assertFalse($false->equals($emptyString));
$this->assertFalse($emptyString->equals($null));
$this->assertFalse($null->equals($false));
}
/**
* equals()
*/
public function testEqualsConflictValues()
{
$this->assertFalse(EnumFixture::FOO()->equals(EnumConflict::FOO()));
}
/**
* jsonSerialize()
*/
public function testJsonSerialize()
{
$this->assertJsonEqualsJson('"foo"', json_encode(new EnumFixture(EnumFixture::FOO)));
$this->assertJsonEqualsJson('"bar"', json_encode(new EnumFixture(EnumFixture::BAR)));
$this->assertJsonEqualsJson('42', json_encode(new EnumFixture(EnumFixture::NUMBER)));
$this->assertJsonEqualsJson('0', json_encode(new EnumFixture(EnumFixture::PROBLEMATIC_NUMBER)));
$this->assertJsonEqualsJson('null', json_encode(new EnumFixture(EnumFixture::PROBLEMATIC_NULL)));
$this->assertJsonEqualsJson('""', json_encode(new EnumFixture(EnumFixture::PROBLEMATIC_EMPTY_STRING)));
$this->assertJsonEqualsJson('false', json_encode(new EnumFixture(EnumFixture::PROBLEMATIC_BOOLEAN_FALSE)));
}
public function testNullableEnum()
{
$this->assertNull(EnumFixture::PROBLEMATIC_NULL()->getValue());
$this->assertNull((new EnumFixture(EnumFixture::PROBLEMATIC_NULL))->getValue());
$this->assertNull((new EnumFixture(EnumFixture::PROBLEMATIC_NULL))->jsonSerialize());
}
public function testBooleanEnum()
{
$this->assertFalse(EnumFixture::PROBLEMATIC_BOOLEAN_FALSE()->getValue());
$this->assertFalse((new EnumFixture(EnumFixture::PROBLEMATIC_BOOLEAN_FALSE))->jsonSerialize());
}
public function testConstructWithSameEnumArgument()
{
$enum = new EnumFixture(EnumFixture::FOO);
$enveloped = new EnumFixture($enum);
$this->assertEquals($enum, $enveloped);
}
private function assertJsonEqualsJson($json1, $json2)
{
$this->assertJsonStringEqualsJsonString($json1, $json2);
}
public function testSerialize()
{
// split string for Pretty CI: "Line exceeds 120 characters"
$bin = '4f3a33303a224d79434c6162735c54657374735c456e756d5c456e756d4669787'.
'4757265223a323a7b733a383a22002a0076616c7565223b733a333a22666f6f223b73'.
'3a32323a22004d79434c6162735c456e756d5c456e756d006b6579223b733a333a22464f4f223b7d';
$this->assertEquals($bin, bin2hex(serialize(EnumFixture::FOO())));
}
public function testUnserializeVersionWithoutKey()
{
// split string for Pretty CI: "Line exceeds 120 characters"
$bin = '4f3a33303a224d79434c6162735c54657374735c456e756d5c456e756d4669787'.
'4757265223a313a7b733a383a22002a0076616c7565223b733a333a22666f6f223b7d';
/* @var $value EnumFixture */
$value = unserialize(pack('H*', $bin));
$this->assertEquals(EnumFixture::FOO, $value->getValue());
$this->assertTrue(EnumFixture::FOO()->equals($value));
$this->assertTrue(EnumFixture::FOO() == $value);
}
public function testUnserialize()
{
// split string for Pretty CI: "Line exceeds 120 characters"
$bin = '4f3a33303a224d79434c6162735c54657374735c456e756d5c456e756d4669787'.
'4757265223a323a7b733a383a22002a0076616c7565223b733a333a22666f6f223b73'.
'3a32323a22004d79434c6162735c456e756d5c456e756d006b6579223b733a333a22464f4f223b7d';
/* @var $value EnumFixture */
$value = unserialize(pack('H*', $bin));
$this->assertEquals(EnumFixture::FOO, $value->getValue());
$this->assertTrue(EnumFixture::FOO()->equals($value));
$this->assertTrue(EnumFixture::FOO() == $value);
}
/**
* @see https://github.com/myclabs/php-enum/issues/95
*/
public function testEnumValuesInheritance()
{
$this->expectException(\UnexpectedValueException::class);
$this->expectExceptionMessage("Value 'value' is not part of the enum MyCLabs\Tests\Enum\EnumFixture");
$inheritedEnumFixture = InheritedEnumFixture::VALUE();
new EnumFixture($inheritedEnumFixture);
}
/**
* @dataProvider isValidProvider
*/
public function testAssertValidValue($value, $isValid): void
{
if (!$isValid) {
$this->expectException(\UnexpectedValueException::class);
$this->expectExceptionMessage("Value '$value' is not part of the enum " . EnumFixture::class);
}
EnumFixture::assertValidValue($value);
self::assertTrue(EnumFixture::isValid($value));
}
/**
* @dataProvider isValidProvider
*/
public function testAssertValidValueWithTypedApi($value, $isValid): void
{
if (!$isValid) {
$this->expectException(\UnexpectedValueException::class);
$this->expectExceptionMessage("Value '$value' is not part of the enum " . EnumFixture::class);
}
Enum::assertValidEnumValue(EnumFixture::class, $value);
self::assertTrue(Enum::isValidEnumValue(EnumFixture::class, $value));
}
}