Completed
Push — little_bit_endian_bitset ( a9375f...7ddb0b )
by Marc
02:01
created

EnumSetTest::testSetBinaryBitsetLeShort()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 6
rs 9.4285
cc 1
eloc 4
nc 1
nop 0
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 testGetBinaryBitsetLe()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
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("\x01\x00\x00\x00\x00\x00\x00\x00\x00", $enumSet->getBinaryBitsetLe());
264
        $this->assertTrue($enumSet->contains($enum1));
265
266
        $this->assertNull($enumSet->attach($enum2));
267
        $this->assertSame("\x03\x00\x00\x00\x00\x00\x00\x00\x00", $enumSet->getBinaryBitsetLe());
268
        $this->assertTrue($enumSet->contains($enum2));
269
270
        $this->assertNull($enumSet->attach($enum3));
271
        $this->assertSame("\x03\x00\x00\x00\x00\x00\x00\x00\x01", $enumSet->getBinaryBitsetLe());
272
        $this->assertTrue($enumSet->contains($enum3));
273
274
        $this->assertNull($enumSet->attach($enum4));
275
        $this->assertSame("\x03\x00\x00\x00\x00\x00\x00\x80\x01", $enumSet->getBinaryBitsetLe());
276
        $this->assertTrue($enumSet->contains($enum4));
277
        
278
        $this->assertSame(4, $enumSet->count());
279
280
        $this->assertNull($enumSet->detach($enum2));
281
        $this->assertSame("\x01\x00\x00\x00\x00\x00\x00\x80\x01", $enumSet->getBinaryBitsetLe());
282
        $this->assertFalse($enumSet->contains($enum2));
283
        
284
        $this->assertSame(3, $enumSet->count());
285
    }
286
287 View Code Duplication
    public function testGetBinaryBitsetBe()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
288
    {
289
        $enumSet = new EnumSet('MabeEnumTest\TestAsset\Enum65');
290
        
291
        $enum1 = Enum65::ONE;
292
        $enum2 = Enum65::TWO;
293
        $enum3 = Enum65::SIXTYFIVE;
294
        $enum4 = Enum65::SIXTYFOUR;
295
296
        $this->assertNull($enumSet->attach($enum1));
297
        $this->assertSame("\x00\x00\x00\x00\x00\x00\x00\x00\x01", $enumSet->getBinaryBitsetBe());
298
        $this->assertTrue($enumSet->contains($enum1));
299
300
        $this->assertNull($enumSet->attach($enum2));
301
        $this->assertSame("\x00\x00\x00\x00\x00\x00\x00\x00\x03", $enumSet->getBinaryBitsetBe());
302
        $this->assertTrue($enumSet->contains($enum2));
303
304
        $this->assertNull($enumSet->attach($enum3));
305
        $this->assertSame("\x01\x00\x00\x00\x00\x00\x00\x00\x03", $enumSet->getBinaryBitsetBe());
306
        $this->assertTrue($enumSet->contains($enum3));
307
308
        $this->assertNull($enumSet->attach($enum4));
309
        $this->assertSame("\x01\x80\x00\x00\x00\x00\x00\x00\x03", $enumSet->getBinaryBitsetBe());
310
        $this->assertTrue($enumSet->contains($enum4));
311
        
312
        $this->assertSame(4, $enumSet->count());
313
314
        $this->assertNull($enumSet->detach($enum2));
315
        $this->assertSame("\x01\x80\x00\x00\x00\x00\x00\x00\x01", $enumSet->getBinaryBitsetBe());
316
        $this->assertFalse($enumSet->contains($enum2));
317
        
318
        $this->assertSame(3, $enumSet->count());
319
    }
320
321
    /**
322
     * @deprecated
323
     */
324 View Code Duplication
    public function testGetBitset()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
325
    {
326
        $enumSet = new EnumSet('MabeEnumTest\TestAsset\Enum65');
327
        
328
        $enum1 = Enum65::ONE;
329
        $enum2 = Enum65::TWO;
330
        $enum3 = Enum65::SIXTYFIVE;
331
        $enum4 = Enum65::SIXTYFOUR;
332
333
        $this->assertNull($enumSet->attach($enum1));
334
        $this->assertSame("\x00\x00\x00\x00\x00\x00\x00\x00\x01", $enumSet->getBitset());
0 ignored issues
show
Deprecated Code introduced by
The method MabeEnum\EnumSet::getBitset() has been deprecated with message: Please use getBinaryBitsetBe() instead

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
335
        $this->assertTrue($enumSet->contains($enum1));
336
337
        $this->assertNull($enumSet->attach($enum2));
338
        $this->assertSame("\x00\x00\x00\x00\x00\x00\x00\x00\x03", $enumSet->getBitset());
0 ignored issues
show
Deprecated Code introduced by
The method MabeEnum\EnumSet::getBitset() has been deprecated with message: Please use getBinaryBitsetBe() instead

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
339
        $this->assertTrue($enumSet->contains($enum2));
340
341
        $this->assertNull($enumSet->attach($enum3));
342
        $this->assertSame("\x01\x00\x00\x00\x00\x00\x00\x00\x03", $enumSet->getBitset());
0 ignored issues
show
Deprecated Code introduced by
The method MabeEnum\EnumSet::getBitset() has been deprecated with message: Please use getBinaryBitsetBe() instead

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
343
        $this->assertTrue($enumSet->contains($enum3));
344
345
        $this->assertNull($enumSet->attach($enum4));
346
        $this->assertSame("\x01\x80\x00\x00\x00\x00\x00\x00\x03", $enumSet->getBitset());
0 ignored issues
show
Deprecated Code introduced by
The method MabeEnum\EnumSet::getBitset() has been deprecated with message: Please use getBinaryBitsetBe() instead

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
347
        $this->assertTrue($enumSet->contains($enum4));
348
        
349
        $this->assertSame(4, $enumSet->count());
350
351
        $this->assertNull($enumSet->detach($enum2));
352
        $this->assertSame("\x01\x80\x00\x00\x00\x00\x00\x00\x01", $enumSet->getBitset());
0 ignored issues
show
Deprecated Code introduced by
The method MabeEnum\EnumSet::getBitset() has been deprecated with message: Please use getBinaryBitsetBe() instead

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
353
        $this->assertFalse($enumSet->contains($enum2));
354
        
355
        $this->assertSame(3, $enumSet->count());
356
    }
357
358 View Code Duplication
    public function testSetBinaryBitsetLe()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
359
    {
360
        $enumSet = new EnumSet('MabeEnumTest\TestAsset\Enum65');
361
        $enumSet->setBinaryBitsetLe("\x01\x00\x00\x00\x00\x00\x00\x80\x01");
362
363
        $this->assertTrue($enumSet->contains(Enum65::ONE));
364
        $this->assertFalse($enumSet->contains(Enum65::TWO));
365
        $this->assertTrue($enumSet->contains(Enum65::SIXTYFIVE));
366
        $this->assertTrue($enumSet->contains(Enum65::SIXTYFOUR));
367
        $this->assertTrue($enumSet->count() == 3);
368
    }
369
370 View Code Duplication
    public function testSetBinaryBitsetBe()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
371
    {
372
        $enumSet = new EnumSet('MabeEnumTest\TestAsset\Enum65');
373
        $enumSet->setBinaryBitsetBe("\x01\x80\x00\x00\x00\x00\x00\x00\x01");
374
375
        $this->assertTrue($enumSet->contains(Enum65::ONE));
376
        $this->assertFalse($enumSet->contains(Enum65::TWO));
377
        $this->assertTrue($enumSet->contains(Enum65::SIXTYFIVE));
378
        $this->assertTrue($enumSet->contains(Enum65::SIXTYFOUR));
379
        $this->assertTrue($enumSet->count() == 3);
380
    }
381
382
    /**
383
     * @deprecated
384
     */
385 View Code Duplication
    public function testSetBitset()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
386
    {
387
        $enumSet = new EnumSet('MabeEnumTest\TestAsset\Enum65');
388
        $enumSet->setBitset("\x01\x80\x00\x00\x00\x00\x00\x00\x01");
0 ignored issues
show
Deprecated Code introduced by
The method MabeEnum\EnumSet::setBitset() has been deprecated with message: Please use setBinaryBitsetBe() instead

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
389
390
        $this->assertTrue($enumSet->contains(Enum65::ONE));
391
        $this->assertFalse($enumSet->contains(Enum65::TWO));
392
        $this->assertTrue($enumSet->contains(Enum65::SIXTYFIVE));
393
        $this->assertTrue($enumSet->contains(Enum65::SIXTYFOUR));
394
        $this->assertTrue($enumSet->count() == 3);
395
    }
396
397
    public function testSetBinaryBitsetLeShort()
398
    {
399
        $enumSet = new EnumSet('MabeEnumTest\TestAsset\Enum65');
400
        $enumSet->setBinaryBitsetLe("\x0A");
401
        $this->assertSame("\x0A\x00\x00\x00\x00\x00\x00\x00\x00", $enumSet->getBinaryBitsetLe());
402
    }
403
404
    public function testSetBinaryBitsetLeLong()
405
    {
406
        $enumSet = new EnumSet('MabeEnumTest\TestAsset\EnumBasic');
407
        $enumSet->setBinaryBitsetLe("\x0A\xFF\xFF\xFF\xFF\xFF");
408
        $this->assertSame("\x0A\xFF", $enumSet->getBinaryBitsetLe());
409
    }
410
411
    public function testSetBinaryBitsetLeArgumentExceptionIfNotString()
412
    {
413
        $this->setExpectedException('InvalidArgumentException');
414
        
415
        $enum = new EnumSet('MabeEnumTest\TestAsset\Enum65');
416
        $enum->setBinaryBitsetLe(0);
417
    }
418
419
    public function testSetBinaryBitsetBeArgumentExceptionIfNotString()
420
    {
421
        $this->setExpectedException('InvalidArgumentException');
422
        
423
        $enum = new EnumSet('MabeEnumTest\TestAsset\Enum65');
424
        $enum->setBinaryBitsetBe(0);
425
    }
426
}
427