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