1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace MabeEnumTest; |
4
|
|
|
|
5
|
|
|
use MabeEnum\EnumSet; |
6
|
|
|
use MabeEnumTest\TestAsset\EnumBasic; |
7
|
|
|
use MabeEnumTest\TestAsset\EnumInheritance; |
8
|
|
|
use MabeEnumTest\TestAsset\Enum32; |
9
|
|
|
use MabeEnumTest\TestAsset\Enum64; |
10
|
|
|
use MabeEnumTest\TestAsset\Enum65; |
11
|
|
|
use PHPUnit_Framework_TestCase as TestCase; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Unit tests for the class MabeEnum\EnumSet |
15
|
|
|
* |
16
|
|
|
* @link http://github.com/marc-mabe/php-enum for the canonical source repository |
17
|
|
|
* @copyright Copyright (c) 2015 Marc Bennewitz |
18
|
|
|
* @license http://github.com/marc-mabe/php-enum/blob/master/LICENSE.txt New BSD License |
19
|
|
|
*/ |
20
|
|
|
class EnumSetTest extends TestCase |
21
|
|
|
{ |
22
|
|
|
public function testBasic() |
23
|
|
|
{ |
24
|
|
|
$enumSet = new EnumSet('MabeEnumTest\TestAsset\EnumBasic'); |
25
|
|
|
$this->assertSame('MabeEnumTest\TestAsset\EnumBasic', $enumSet->getEnumeration()); |
26
|
|
|
|
27
|
|
|
$enum1 = EnumBasic::ONE(); |
28
|
|
|
$enum2 = EnumBasic::TWO(); |
29
|
|
|
|
30
|
|
|
$this->assertFalse($enumSet->contains($enum1)); |
31
|
|
|
$this->assertNull($enumSet->attach($enum1)); |
32
|
|
|
$this->assertTrue($enumSet->contains($enum1)); |
33
|
|
|
|
34
|
|
|
$this->assertFalse($enumSet->contains($enum2)); |
35
|
|
|
$this->assertNull($enumSet->attach($enum2)); |
36
|
|
|
$this->assertTrue($enumSet->contains($enum2)); |
37
|
|
|
|
38
|
|
|
$this->assertNull($enumSet->detach($enum1)); |
39
|
|
|
$this->assertFalse($enumSet->contains($enum1)); |
40
|
|
|
|
41
|
|
|
$this->assertNull($enumSet->detach($enum2)); |
42
|
|
|
$this->assertFalse($enumSet->contains($enum2)); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
public function testDeprecatedGetEnumClass() |
46
|
|
|
{ |
47
|
|
|
$enumSet = new EnumSet('MabeEnumTest\TestAsset\EnumBasic'); |
48
|
|
|
$this->assertSame('MabeEnumTest\TestAsset\EnumBasic', $enumSet->getEnumClass()); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
public function testBasicWithConstantValuesAsEnums() |
52
|
|
|
{ |
53
|
|
|
$enumSet = new EnumSet('MabeEnumTest\TestAsset\EnumBasic'); |
54
|
|
|
|
55
|
|
|
$enum1 = EnumBasic::ONE; |
56
|
|
|
$enum2 = EnumBasic::TWO; |
57
|
|
|
|
58
|
|
|
$this->assertFalse($enumSet->contains($enum1)); |
59
|
|
|
$this->assertNull($enumSet->attach($enum1)); |
60
|
|
|
$this->assertTrue($enumSet->contains($enum1)); |
61
|
|
|
|
62
|
|
|
$this->assertFalse($enumSet->contains($enum2)); |
63
|
|
|
$this->assertNull($enumSet->attach($enum2)); |
64
|
|
|
$this->assertTrue($enumSet->contains($enum2)); |
65
|
|
|
|
66
|
|
|
$this->assertNull($enumSet->detach($enum1)); |
67
|
|
|
$this->assertFalse($enumSet->contains($enum1)); |
68
|
|
|
|
69
|
|
|
$this->assertNull($enumSet->detach($enum2)); |
70
|
|
|
$this->assertFalse($enumSet->contains($enum2)); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
public function testUnique() |
74
|
|
|
{ |
75
|
|
|
$enumSet = new EnumSet('MabeEnumTest\TestAsset\EnumBasic'); |
76
|
|
|
|
77
|
|
|
$enumSet->attach(EnumBasic::ONE()); |
78
|
|
|
$enumSet->attach(EnumBasic::ONE); |
79
|
|
|
|
80
|
|
|
$enumSet->attach(EnumBasic::TWO()); |
81
|
|
|
$enumSet->attach(EnumBasic::TWO); |
82
|
|
|
|
83
|
|
|
$this->assertSame(2, $enumSet->count()); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
public function testIterateOrdered() |
87
|
|
|
{ |
88
|
|
|
$enumSet = new EnumSet('MabeEnumTest\TestAsset\EnumBasic'); |
89
|
|
|
|
90
|
|
|
// an empty enum set needs to be invalid, starting by 0 |
91
|
|
|
$this->assertSame(0, $enumSet->count()); |
92
|
|
|
$this->assertFalse($enumSet->valid()); |
93
|
|
|
$this->assertNull($enumSet->current()); |
94
|
|
|
|
95
|
|
|
// attach |
96
|
|
|
$enum1 = EnumBasic::ONE(); |
97
|
|
|
$enum2 = EnumBasic::TWO(); |
98
|
|
|
$enumSet->attach($enum1); |
99
|
|
|
$enumSet->attach($enum2); |
100
|
|
|
|
101
|
|
|
// a not empty enum set should be valid, starting by 0 (if not iterated) |
102
|
|
|
$enumSet->rewind(); |
103
|
|
|
$this->assertSame(2, $enumSet->count()); |
104
|
|
|
$this->assertTrue($enumSet->valid()); |
105
|
|
|
$this->assertSame($enum1->getOrdinal(), $enumSet->key()); |
106
|
|
|
$this->assertSame($enum1, $enumSet->current()); |
107
|
|
|
|
108
|
|
|
// go to the next element (last) |
109
|
|
|
$this->assertNull($enumSet->next()); |
110
|
|
|
$this->assertTrue($enumSet->valid()); |
111
|
|
|
$this->assertSame($enum2->getOrdinal(), $enumSet->key()); |
112
|
|
|
$this->assertSame($enum2, $enumSet->current()); |
113
|
|
|
|
114
|
|
|
// go to the next element (out of range) |
115
|
|
|
$this->assertNull($enumSet->next()); |
116
|
|
|
$this->assertFalse($enumSet->valid()); |
117
|
|
|
$this->assertNull($enumSet->current()); |
118
|
|
|
|
119
|
|
|
// rewind will set the iterator position back to 0 |
120
|
|
|
$enumSet->rewind(); |
121
|
|
|
$this->assertTrue($enumSet->valid()); |
122
|
|
|
$this->assertSame(0, $enumSet->key()); |
123
|
|
|
$this->assertSame($enum1, $enumSet->current()); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
public function testIterateAndDetach() |
127
|
|
|
{ |
128
|
|
|
$enumSet = new EnumSet('MabeEnumTest\TestAsset\EnumInheritance'); |
129
|
|
|
|
130
|
|
|
$enum1 = EnumInheritance::ONE(); |
131
|
|
|
$enum2 = EnumInheritance::TWO(); |
132
|
|
|
$enum3 = EnumInheritance::INHERITANCE(); |
133
|
|
|
|
134
|
|
|
// attach |
135
|
|
|
$enumSet->attach($enum1); |
136
|
|
|
$enumSet->attach($enum2); |
137
|
|
|
$enumSet->attach($enum3); |
138
|
|
|
|
139
|
|
|
// go to the next entry |
140
|
|
|
$enumSet->next(); |
141
|
|
|
$this->assertSame($enum2, $enumSet->current()); |
142
|
|
|
|
143
|
|
|
// detach current entry |
144
|
|
|
$enumSet->detach($enumSet->current()); |
145
|
|
|
$this->assertFalse($enumSet->valid()); |
146
|
|
|
$this->assertNull($enumSet->current()); |
147
|
|
|
$this->assertSame($enum2->getOrdinal(), $enumSet->key()); |
148
|
|
|
|
149
|
|
|
// go to the next entry should be the last entry |
150
|
|
|
$enumSet->next(); |
151
|
|
|
$this->assertSame($enum3, $enumSet->current()); |
152
|
|
|
|
153
|
|
|
// detech the last entry |
154
|
|
|
$enumSet->detach($enumSet->current()); |
155
|
|
|
$this->assertFalse($enumSet->valid()); |
156
|
|
|
$this->assertNull($enumSet->current()); |
157
|
|
|
$this->assertSame($enum3->getOrdinal(), $enumSet->key()); |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
public function testConstructThrowsInvalidArgumentExceptionIfEnumClassDoesNotExtendBaseEnum() |
161
|
|
|
{ |
162
|
|
|
$this->setExpectedException('InvalidArgumentException'); |
163
|
|
|
new EnumSet('stdClass'); |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
public function testInitEnumThrowsInvalidArgumentExceptionOnInvalidEnum() |
167
|
|
|
{ |
168
|
|
|
$enumSet = new EnumSet('MabeEnumTest\TestAsset\EnumBasic'); |
169
|
|
|
$this->setExpectedException('InvalidArgumentException'); |
170
|
|
|
$this->assertFalse($enumSet->contains(EnumInheritance::INHERITANCE())); |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
public function testIterateOutOfRangeIfLastOrdinalEnumIsSet() |
174
|
|
|
{ |
175
|
|
|
$enumSet = new EnumSet('MabeEnumTest\TestAsset\EnumBasic'); |
176
|
|
|
$enum = EnumBasic::getByOrdinal(count(EnumBasic::getConstants()) - 1); |
177
|
|
|
|
178
|
|
|
$enumSet->attach($enum); |
179
|
|
|
$enumSet->rewind(); |
180
|
|
|
$this->assertSame($enum, $enumSet->current()); |
181
|
|
|
|
182
|
|
|
// go to the next entry results in out of range |
183
|
|
|
$enumSet->next(); |
184
|
|
|
$this->assertFalse($enumSet->valid()); |
185
|
|
|
$this->assertSame($enum->getOrdinal() + 1, $enumSet->key()); |
186
|
|
|
|
187
|
|
|
// go more over doesn't change iterator position |
188
|
|
|
$enumSet->next(); |
189
|
|
|
$this->assertFalse($enumSet->valid()); |
190
|
|
|
$this->assertSame($enum->getOrdinal() + 1, $enumSet->key()); |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
public function testRewindFirstOnEmptySet() |
194
|
|
|
{ |
195
|
|
|
$enumSet = new EnumSet('MabeEnumTest\TestAsset\EnumBasic'); |
196
|
|
|
|
197
|
|
|
$enumSet->attach(EnumBasic::TWO); |
198
|
|
|
$enumSet->rewind(); |
199
|
|
|
$this->assertGreaterThan(0, $enumSet->key()); |
200
|
|
|
|
201
|
|
|
$enumSet->detach(EnumBasic::TWO); |
202
|
|
|
$enumSet->rewind(); |
203
|
|
|
$this->assertSame(0, $enumSet->key()); |
204
|
|
|
} |
205
|
|
|
|
206
|
|
View Code Duplication |
public function test32EnumerationsSet() |
207
|
|
|
{ |
208
|
|
|
$enumSet = new EnumSet('MabeEnumTest\TestAsset\Enum32'); |
209
|
|
|
foreach (Enum32::getConstants() as $name => $value) { |
210
|
|
|
$this->assertFalse($enumSet->contains($value)); |
211
|
|
|
$enumSet->attach($value); |
212
|
|
|
$this->assertTrue($enumSet->contains($value)); |
213
|
|
|
} |
214
|
|
|
|
215
|
|
|
$this->assertSame(32, $enumSet->count()); |
216
|
|
|
|
217
|
|
|
$expectedOrdinal = 0; |
218
|
|
|
foreach ($enumSet as $ordinal => $enum) { |
219
|
|
|
$this->assertSame($expectedOrdinal, $ordinal); |
220
|
|
|
$this->assertSame($expectedOrdinal, $enum->getOrdinal()); |
221
|
|
|
$expectedOrdinal++; |
222
|
|
|
} |
223
|
|
|
} |
224
|
|
|
|
225
|
|
View Code Duplication |
public function test64EnumerationsSet() |
226
|
|
|
{ |
227
|
|
|
$enumSet = new EnumSet('MabeEnumTest\TestAsset\Enum64'); |
228
|
|
|
foreach (Enum64::getConstants() as $name => $value) { |
229
|
|
|
$this->assertFalse($enumSet->contains($value)); |
230
|
|
|
$enumSet->attach($value); |
231
|
|
|
$this->assertTrue($enumSet->contains($value)); |
232
|
|
|
} |
233
|
|
|
|
234
|
|
|
$this->assertSame(64, $enumSet->count()); |
235
|
|
|
|
236
|
|
|
$expectedOrdinal = 0; |
237
|
|
|
foreach ($enumSet as $ordinal => $enum) { |
238
|
|
|
$this->assertSame($expectedOrdinal, $ordinal); |
239
|
|
|
$this->assertSame($expectedOrdinal, $enum->getOrdinal()); |
240
|
|
|
$expectedOrdinal++; |
241
|
|
|
} |
242
|
|
|
} |
243
|
|
|
|
244
|
|
|
public function test65EnumerationsSet() |
245
|
|
|
{ |
246
|
|
|
$enum = new EnumSet('MabeEnumTest\TestAsset\Enum65'); |
247
|
|
|
|
248
|
|
|
$this->assertNull($enum->attach(Enum65::getByOrdinal(64))); |
249
|
|
|
$enum->next(); |
250
|
|
|
$this->assertTrue($enum->valid()); |
251
|
|
|
} |
252
|
|
|
|
253
|
|
View Code Duplication |
public function testGetBinaryBitsetBe() |
|
|
|
|
254
|
|
|
{ |
255
|
|
|
$enumSet = new EnumSet('MabeEnumTest\TestAsset\Enum65'); |
256
|
|
|
|
257
|
|
|
$enum1 = Enum65::ONE; |
258
|
|
|
$enum2 = Enum65::TWO; |
259
|
|
|
$enum3 = Enum65::SIXTYFIVE; |
260
|
|
|
$enum4 = Enum65::SIXTYFOUR; |
261
|
|
|
|
262
|
|
|
$this->assertNull($enumSet->attach($enum1)); |
263
|
|
|
$this->assertSame("\x00\x00\x00\x00\x00\x00\x00\x00\x01", $enumSet->getBinaryBitsetBe()); |
264
|
|
|
$this->assertTrue($enumSet->contains($enum1)); |
265
|
|
|
|
266
|
|
|
$this->assertNull($enumSet->attach($enum2)); |
267
|
|
|
$this->assertSame('000000000000000003', \bin2hex($enumSet->getBinaryBitsetBe())); |
268
|
|
|
$this->assertTrue($enumSet->contains($enum2)); |
269
|
|
|
|
270
|
|
|
$this->assertNull($enumSet->attach($enum3)); |
271
|
|
|
$this->assertSame("\x01\x00\x00\x00\x00\x00\x00\x00\x03", $enumSet->getBinaryBitsetBe()); |
272
|
|
|
$this->assertTrue($enumSet->contains($enum3)); |
273
|
|
|
|
274
|
|
|
$this->assertNull($enumSet->attach($enum4)); |
275
|
|
|
$this->assertSame("\x01\x80\x00\x00\x00\x00\x00\x00\x03", $enumSet->getBinaryBitsetBe()); |
276
|
|
|
$this->assertTrue($enumSet->contains($enum4)); |
277
|
|
|
|
278
|
|
|
$this->assertSame(4, $enumSet->count()); |
279
|
|
|
|
280
|
|
|
$this->assertNull($enumSet->detach($enum2)); |
281
|
|
|
$this->assertSame("\x01\x80\x00\x00\x00\x00\x00\x00\x01", $enumSet->getBinaryBitsetBe()); |
282
|
|
|
$this->assertFalse($enumSet->contains($enum2)); |
283
|
|
|
|
284
|
|
|
$this->assertSame(3, $enumSet->count()); |
285
|
|
|
} |
286
|
|
|
|
287
|
|
|
/** |
288
|
|
|
* @deprecated |
289
|
|
|
*/ |
290
|
|
View Code Duplication |
public function testGetBitset() |
|
|
|
|
291
|
|
|
{ |
292
|
|
|
$enumSet = new EnumSet('MabeEnumTest\TestAsset\Enum65'); |
293
|
|
|
|
294
|
|
|
$enum1 = Enum65::ONE; |
295
|
|
|
$enum2 = Enum65::TWO; |
296
|
|
|
$enum3 = Enum65::SIXTYFIVE; |
297
|
|
|
$enum4 = Enum65::SIXTYFOUR; |
298
|
|
|
|
299
|
|
|
$this->assertNull($enumSet->attach($enum1)); |
300
|
|
|
$this->assertSame("\x00\x00\x00\x00\x00\x00\x00\x00\x01", $enumSet->getBitset()); |
|
|
|
|
301
|
|
|
$this->assertTrue($enumSet->contains($enum1)); |
302
|
|
|
|
303
|
|
|
$this->assertNull($enumSet->attach($enum2)); |
304
|
|
|
$this->assertSame('000000000000000003', \bin2hex($enumSet->getBitset())); |
|
|
|
|
305
|
|
|
$this->assertTrue($enumSet->contains($enum2)); |
306
|
|
|
|
307
|
|
|
$this->assertNull($enumSet->attach($enum3)); |
308
|
|
|
$this->assertSame("\x01\x00\x00\x00\x00\x00\x00\x00\x03", $enumSet->getBitset()); |
|
|
|
|
309
|
|
|
$this->assertTrue($enumSet->contains($enum3)); |
310
|
|
|
|
311
|
|
|
$this->assertNull($enumSet->attach($enum4)); |
312
|
|
|
$this->assertSame("\x01\x80\x00\x00\x00\x00\x00\x00\x03", $enumSet->getBitset()); |
|
|
|
|
313
|
|
|
$this->assertTrue($enumSet->contains($enum4)); |
314
|
|
|
|
315
|
|
|
$this->assertSame(4, $enumSet->count()); |
316
|
|
|
|
317
|
|
|
$this->assertNull($enumSet->detach($enum2)); |
318
|
|
|
$this->assertSame("\x01\x80\x00\x00\x00\x00\x00\x00\x01", $enumSet->getBitset()); |
|
|
|
|
319
|
|
|
$this->assertFalse($enumSet->contains($enum2)); |
320
|
|
|
|
321
|
|
|
$this->assertSame(3, $enumSet->count()); |
322
|
|
|
} |
323
|
|
|
|
324
|
|
View Code Duplication |
public function testSetBinaryBitsetBe() |
|
|
|
|
325
|
|
|
{ |
326
|
|
|
$enumSet = new EnumSet('MabeEnumTest\TestAsset\Enum65'); |
327
|
|
|
$enumSet->setBinaryBitsetBe("\x01\x80\x00\x00\x00\x00\x00\x00\x01"); |
328
|
|
|
|
329
|
|
|
$this->assertTrue($enumSet->contains(Enum65::ONE)); |
330
|
|
|
$this->assertFalse($enumSet->contains(Enum65::TWO)); |
331
|
|
|
$this->assertTrue($enumSet->contains(Enum65::SIXTYFIVE)); |
332
|
|
|
$this->assertTrue($enumSet->contains(Enum65::SIXTYFOUR)); |
333
|
|
|
$this->assertTrue($enumSet->count() == 3); |
334
|
|
|
} |
335
|
|
|
|
336
|
|
|
/** |
337
|
|
|
* @deprecated |
338
|
|
|
*/ |
339
|
|
View Code Duplication |
public function testSetBitset() |
|
|
|
|
340
|
|
|
{ |
341
|
|
|
$enumSet = new EnumSet('MabeEnumTest\TestAsset\Enum65'); |
342
|
|
|
$enumSet->setBitset("\x01\x80\x00\x00\x00\x00\x00\x00\x01"); |
|
|
|
|
343
|
|
|
|
344
|
|
|
$this->assertTrue($enumSet->contains(Enum65::ONE)); |
345
|
|
|
$this->assertFalse($enumSet->contains(Enum65::TWO)); |
346
|
|
|
$this->assertTrue($enumSet->contains(Enum65::SIXTYFIVE)); |
347
|
|
|
$this->assertTrue($enumSet->contains(Enum65::SIXTYFOUR)); |
348
|
|
|
$this->assertTrue($enumSet->count() == 3); |
349
|
|
|
} |
350
|
|
|
|
351
|
|
|
public function testSetBinaryBitsetBeShort() |
352
|
|
|
{ |
353
|
|
|
$enumSet = new EnumSet('MabeEnumTest\TestAsset\Enum65'); |
354
|
|
|
$enumSet->setBinaryBitsetBe("\x0A"); |
355
|
|
|
$this->assertSame("\x00\x00\x00\x00\x00\x00\x00\x00\x0A", $enumSet->getBinaryBitsetBe()); |
356
|
|
|
} |
357
|
|
|
|
358
|
|
|
public function testSetBinaryBitsetBeLong() |
359
|
|
|
{ |
360
|
|
|
$enumSet = new EnumSet('MabeEnumTest\TestAsset\EnumBasic'); |
361
|
|
|
$enumSet->setBinaryBitsetBe("\xFF\xFF\xFF\xFF\xFF\x0A"); |
362
|
|
|
$this->assertSame("\xFF\x0A", $enumSet->getBinaryBitsetBe()); |
363
|
|
|
} |
364
|
|
|
|
365
|
|
|
public function testSetBinaryBitsetBeArgumentExceptionIfNotString() |
366
|
|
|
{ |
367
|
|
|
$this->setExpectedException('InvalidArgumentException'); |
368
|
|
|
|
369
|
|
|
$enum = new EnumSet('MabeEnumTest\TestAsset\Enum65'); |
370
|
|
|
$enum->setBinaryBitsetBe(0); |
371
|
|
|
} |
372
|
|
|
} |
373
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.