Completed
Push — enumset_produce ( 3d5c3d...ac9aa5 )
by Marc
25:47 queued 21:25
created

testIntersectThrowsInvalidArgumentException()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 8
rs 9.4285
cc 1
eloc 5
nc 1
nop 0
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 View Code Duplication
    public function testRewindFirstOnEmptySet()
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...
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 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...
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("\x01\x00\x00\x00\x00\x00\x00\x00\x00", $enumSet->getBinaryBitsetLe());
265
        $this->assertTrue($enumSet->contains($enum1));
266
267
        $this->assertNull($enumSet->attach($enum2));
268
        $this->assertSame("\x03\x00\x00\x00\x00\x00\x00\x00\x00", $enumSet->getBinaryBitsetLe());
269
        $this->assertTrue($enumSet->contains($enum2));
270
271
        $this->assertNull($enumSet->attach($enum3));
272
        $this->assertSame("\x03\x00\x00\x00\x00\x00\x00\x00\x01", $enumSet->getBinaryBitsetLe());
273
        $this->assertTrue($enumSet->contains($enum3));
274
275
        $this->assertNull($enumSet->attach($enum4));
276
        $this->assertSame("\x03\x00\x00\x00\x00\x00\x00\x80\x01", $enumSet->getBinaryBitsetLe());
277
        $this->assertTrue($enumSet->contains($enum4));
278
        
279
        $this->assertSame(4, $enumSet->count());
280
281
        $this->assertNull($enumSet->detach($enum2));
282
        $this->assertSame("\x01\x00\x00\x00\x00\x00\x00\x80\x01", $enumSet->getBinaryBitsetLe());
283
        $this->assertFalse($enumSet->contains($enum2));
284
        
285
        $this->assertSame(3, $enumSet->count());
286
    }
287
288 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...
289
    {
290
        $enumSet = new EnumSet('MabeEnumTest\TestAsset\Enum65');
291
        
292
        $enum1 = Enum65::ONE;
293
        $enum2 = Enum65::TWO;
294
        $enum3 = Enum65::SIXTYFIVE;
295
        $enum4 = Enum65::SIXTYFOUR;
296
297
        $this->assertNull($enumSet->attach($enum1));
298
        $this->assertSame("\x00\x00\x00\x00\x00\x00\x00\x00\x01", $enumSet->getBinaryBitsetBe());
299
        $this->assertTrue($enumSet->contains($enum1));
300
301
        $this->assertNull($enumSet->attach($enum2));
302
        $this->assertSame("\x00\x00\x00\x00\x00\x00\x00\x00\x03", $enumSet->getBinaryBitsetBe());
303
        $this->assertTrue($enumSet->contains($enum2));
304
305
        $this->assertNull($enumSet->attach($enum3));
306
        $this->assertSame("\x01\x00\x00\x00\x00\x00\x00\x00\x03", $enumSet->getBinaryBitsetBe());
307
        $this->assertTrue($enumSet->contains($enum3));
308
309
        $this->assertNull($enumSet->attach($enum4));
310
        $this->assertSame("\x01\x80\x00\x00\x00\x00\x00\x00\x03", $enumSet->getBinaryBitsetBe());
311
        $this->assertTrue($enumSet->contains($enum4));
312
        
313
        $this->assertSame(4, $enumSet->count());
314
315
        $this->assertNull($enumSet->detach($enum2));
316
        $this->assertSame("\x01\x80\x00\x00\x00\x00\x00\x00\x01", $enumSet->getBinaryBitsetBe());
317
        $this->assertFalse($enumSet->contains($enum2));
318
        
319
        $this->assertSame(3, $enumSet->count());
320
    }
321
322
    /**
323
     * @deprecated
324
     */
325 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...
326
    {
327
        $enumSet = new EnumSet('MabeEnumTest\TestAsset\Enum65');
328
        
329
        $enum1 = Enum65::ONE;
330
        $enum2 = Enum65::TWO;
331
        $enum3 = Enum65::SIXTYFIVE;
332
        $enum4 = Enum65::SIXTYFOUR;
333
334
        $this->assertNull($enumSet->attach($enum1));
335
        $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...
336
        $this->assertTrue($enumSet->contains($enum1));
337
338
        $this->assertNull($enumSet->attach($enum2));
339
        $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...
340
        $this->assertTrue($enumSet->contains($enum2));
341
342
        $this->assertNull($enumSet->attach($enum3));
343
        $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...
344
        $this->assertTrue($enumSet->contains($enum3));
345
346
        $this->assertNull($enumSet->attach($enum4));
347
        $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...
348
        $this->assertTrue($enumSet->contains($enum4));
349
        
350
        $this->assertSame(4, $enumSet->count());
351
352
        $this->assertNull($enumSet->detach($enum2));
353
        $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...
354
        $this->assertFalse($enumSet->contains($enum2));
355
        
356
        $this->assertSame(3, $enumSet->count());
357
    }
358
359 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...
360
    {
361
        $enumSet = new EnumSet('MabeEnumTest\TestAsset\Enum65');
362
        $enumSet->setBinaryBitsetLe("\x01\x00\x00\x00\x00\x00\x00\x80\x01");
363
364
        $this->assertTrue($enumSet->contains(Enum65::ONE));
365
        $this->assertFalse($enumSet->contains(Enum65::TWO));
366
        $this->assertTrue($enumSet->contains(Enum65::SIXTYFIVE));
367
        $this->assertTrue($enumSet->contains(Enum65::SIXTYFOUR));
368
        $this->assertTrue($enumSet->count() == 3);
369
    }
370
371
    public function testSetBinaryBitsetLeTruncateHighBits()
372
    {
373
        $enumSet = new EnumSet('MabeEnumTest\TestAsset\Enum65');
374
        foreach (Enum65::getEnumerators() as $enumerator) {
375
            $enumSet->attach($enumerator);
376
        }
377
378
        $bitset    = $enumSet->getBinaryBitsetLe();
379
        $newBitset = substr($bitset, 0, -1) . "\xff\xff";
380
        $enumSet->setBinaryBitsetLe($newBitset);
381
382
        $this->assertSame(bin2hex($bitset), bin2hex($enumSet->getBinaryBitsetLe()));
383
    }
384
385 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...
386
    {
387
        $enumSet = new EnumSet('MabeEnumTest\TestAsset\Enum65');
388
        $enumSet->setBinaryBitsetBe("\x01\x80\x00\x00\x00\x00\x00\x00\x01");
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
    /**
398
     * @deprecated
399
     */
400 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...
401
    {
402
        $enumSet = new EnumSet('MabeEnumTest\TestAsset\Enum65');
403
        $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...
404
405
        $this->assertTrue($enumSet->contains(Enum65::ONE));
406
        $this->assertFalse($enumSet->contains(Enum65::TWO));
407
        $this->assertTrue($enumSet->contains(Enum65::SIXTYFIVE));
408
        $this->assertTrue($enumSet->contains(Enum65::SIXTYFOUR));
409
        $this->assertTrue($enumSet->count() == 3);
410
    }
411
412
    public function testSetBinaryBitsetLeShort()
413
    {
414
        $enumSet = new EnumSet('MabeEnumTest\TestAsset\Enum65');
415
        $enumSet->setBinaryBitsetLe("\x0A");
416
        $this->assertSame("\x0A\x00\x00\x00\x00\x00\x00\x00\x00", $enumSet->getBinaryBitsetLe());
417
    }
418
419
    public function testSetBinaryBitsetLeLong()
420
    {
421
        $enumSet = new EnumSet('MabeEnumTest\TestAsset\EnumBasic');
422
        $enumSet->setBinaryBitsetLe("\x0A\xFF\xFF\xFF\xFF\xFF");
423
        $this->assertSame("\x0A\xFF", $enumSet->getBinaryBitsetLe());
424
    }
425
426
    public function testSetBinaryBitsetLeArgumentExceptionIfNotString()
427
    {
428
        $this->setExpectedException('InvalidArgumentException');
429
        
430
        $enum = new EnumSet('MabeEnumTest\TestAsset\Enum65');
431
        $enum->setBinaryBitsetLe(0);
432
    }
433
434
    public function testSetBinaryBitsetBeArgumentExceptionIfNotString()
435
    {
436
        $this->setExpectedException('InvalidArgumentException');
437
        
438
        $enum = new EnumSet('MabeEnumTest\TestAsset\Enum65');
439
        $enum->setBinaryBitsetBe(0);
440
    }
441
442
    public function testCountingEmptyEnumEmptySet()
443
    {
444
        $set = new EnumSet('MabeEnumTest\TestAsset\EmptyEnum');
445
        $this->assertSame(0, $set->count());
446
    }
447
448 View Code Duplication
    public function testIsEqual()
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...
449
    {
450
        $set1 = new EnumSet('MabeEnumTest\TestAsset\EnumBasic');
451
        $set2 = new EnumSet('MabeEnumTest\TestAsset\EnumBasic');
452
        $this->assertTrue($set1->isEqual($set2));
453
454
        foreach (EnumBasic::getEnumerators() as $enumerator) {
455
            $set1->attach($enumerator);
456
            $this->assertFalse($set1->isEqual($set2));
457
458
            $set2->attach($enumerator);
459
            $this->assertTrue($set1->isEqual($set2));
460
        }
461
    }
462
463 View Code Duplication
    public function testIsEqualWrongInstance()
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...
464
    {
465
        $set1 = new EnumSet('MabeEnumTest\TestAsset\EnumBasic');
466
        $set2 = new EnumSet('MabeEnumTest\TestAsset\EnumInheritance');
467
        $this->assertFalse($set1->isEqual($set2));
468
469
        foreach (EnumBasic::getEnumerators() as $enumerator) {
470
            $set1->attach($enumerator);
471
            $this->assertFalse($set1->isEqual($set2));
472
473
            $set2->attach($enumerator->getValue());
474
            $this->assertFalse($set1->isEqual($set2));
475
        }
476
    }
477
478
    /**
479
     * if $A->isEqual($B) is true then $A->isSubsetOf($B) is also true
480
     */
481
    public function testIsSubsetEqual()
482
    {
483
        $set1 = new EnumSet('MabeEnumTest\TestAsset\Enum32');
484
        $set2 = new EnumSet('MabeEnumTest\TestAsset\Enum32');
485
        $this->assertTrue($set1->isSubset($set2));
486
487
        foreach (Enum32::getEnumerators() as $enumerator) {
488
            $set1->attach($enumerator);
489
            $set2->attach($enumerator);
490
            $this->assertTrue($set1->isSubset($set2));
491
        }
492
    }
493
494 View Code Duplication
    public function testIsSubsetFull()
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...
495
    {
496
        $set1 = new EnumSet('MabeEnumTest\TestAsset\Enum32');
497
        $set2 = new EnumSet('MabeEnumTest\TestAsset\Enum32');
498
499
        foreach (Enum32::getEnumerators() as $enumerator) {
500
            $set2->attach($enumerator);
501
            $this->assertTrue($set1->isSubset($set2));
502
        }
503
    }
504
505 View Code Duplication
    public function testIsSubsetFalse()
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...
506
    {
507
        $set1 = new EnumSet('MabeEnumTest\TestAsset\Enum32');
508
        $set2 = new EnumSet('MabeEnumTest\TestAsset\Enum32');
509
510
        foreach (Enum32::getEnumerators() as $enumerator) {
511
            $set1->attach($enumerator);
512
            $this->assertFalse($set1->isSubset($set2));
513
        }
514
    }
515
516 View Code Duplication
    public function testIsSubsetWrongInstance()
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...
517
    {
518
        $set1 = new EnumSet('MabeEnumTest\TestAsset\EnumBasic');
519
        $set2 = new EnumSet('MabeEnumTest\TestAsset\EnumInheritance');
520
        $this->assertFalse($set1->isSubset($set2));
521
522
        foreach (EnumBasic::getEnumerators() as $enumerator) {
523
            $set1->attach($enumerator);
524
            $this->assertFalse($set1->isSubset($set2));
525
526
            $set2->attach($enumerator->getValue());
527
            $this->assertFalse($set1->isSubset($set2));
528
        }
529
    }
530
531
    /**
532
     * if $A->isEqual($B) is true then $A->isSuperset($B) is also true
533
     */
534
    public function testIsSsetEqual()
535
    {
536
        $set1 = new EnumSet('MabeEnumTest\TestAsset\Enum32');
537
        $set2 = new EnumSet('MabeEnumTest\TestAsset\Enum32');
538
        $this->assertTrue($set1->isSuperset($set2));
539
540
        foreach (Enum32::getEnumerators() as $enumerator) {
541
            $set1->attach($enumerator);
542
            $set2->attach($enumerator);
543
            $this->assertTrue($set1->isSuperset($set2));
544
        }
545
    }
546
547 View Code Duplication
    public function testIsSupersetFull()
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...
548
    {
549
        $set1 = new EnumSet('MabeEnumTest\TestAsset\Enum32');
550
        $set2 = new EnumSet('MabeEnumTest\TestAsset\Enum32');
551
552
        foreach (Enum32::getEnumerators() as $enumerator) {
553
            $set1->attach($enumerator);
554
            $this->assertTrue($set1->isSuperset($set2));
555
        }
556
    }
557
558 View Code Duplication
    public function testIsSupersetFalse()
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...
559
    {
560
        $set1 = new EnumSet('MabeEnumTest\TestAsset\Enum32');
561
        $set2 = new EnumSet('MabeEnumTest\TestAsset\Enum32');
562
563
        foreach (Enum32::getEnumerators() as $enumerator) {
564
            $set2->attach($enumerator);
565
            $this->assertFalse($set1->isSuperset($set2));
566
        }
567
    }
568
569 View Code Duplication
    public function testIsSupersetWrongInstance()
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...
570
    {
571
        $set1 = new EnumSet('MabeEnumTest\TestAsset\EnumBasic');
572
        $set2 = new EnumSet('MabeEnumTest\TestAsset\EnumInheritance');
573
        $this->assertFalse($set1->isSuperset($set2));
574
575
        foreach (EnumBasic::getEnumerators() as $enumerator) {
576
            $set1->attach($enumerator);
577
            $this->assertFalse($set1->isSuperset($set2));
578
579
            $set2->attach($enumerator->getValue());
580
            $this->assertFalse($set1->isSuperset($set2));
581
        }
582
    }
583
584
    public function testGetOrdinals()
585
    {
586
        $set = new EnumSet('MabeEnumTest\TestAsset\EnumBasic');
587
        $this->assertSame(array(), $set->getOrdinals());
588
589
        foreach (EnumBasic::getConstants() as $value) {
590
            $set->attach($value);
591
        }
592
593
        $this->assertSame(range(0, count(EnumBasic::getConstants()) - 1), $set->getOrdinals());
594
    }
595
596 View Code Duplication
    public function testGetOrdinalsDoesNotEffectIteratorPosition()
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...
597
    {
598
        $set = new EnumSet('MabeEnumTest\TestAsset\EnumBasic');
599
        $set->attach(EnumBasic::ONE);
600
        $set->attach(EnumBasic::TWO);
601
        $set->next();
602
603
        $set->getOrdinals();
604
        $this->assertSame(EnumBasic::TWO, $set->current()->getValue());
605
    }
606
607 View Code Duplication
    public function testGetEnumerators()
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...
608
    {
609
        $set = new EnumSet('MabeEnumTest\TestAsset\EnumBasic');
610
        $this->assertSame(array(), $set->getEnumerators());
611
612
        foreach (EnumBasic::getConstants() as $value) {
613
            $set->attach($value);
614
        }
615
616
        $this->assertSame(EnumBasic::getEnumerators(), $set->getEnumerators());
617
    }
618
619 View Code Duplication
    public function testGetEnumeratorsDoesNotEffectIteratorPosition()
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...
620
    {
621
        $set = new EnumSet('MabeEnumTest\TestAsset\EnumBasic');
622
        $set->attach(EnumBasic::ONE);
623
        $set->attach(EnumBasic::TWO);
624
        $set->next();
625
626
        $set->getEnumerators();
627
        $this->assertSame(EnumBasic::TWO, $set->current()->getValue());
628
    }
629
630 View Code Duplication
    public function testGetValues()
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...
631
    {
632
        $set = new EnumSet('MabeEnumTest\TestAsset\EnumBasic');
633
        $this->assertSame(array(), $set->getValues());
634
635
        foreach (EnumBasic::getConstants() as $value) {
636
            $set->attach($value);
637
        }
638
639
        $this->assertSame(array_values(EnumBasic::getConstants()), $set->getValues());
640
    }
641
642 View Code Duplication
    public function testGetValuesDoesNotEffectIteratorPosition()
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...
643
    {
644
        $set = new EnumSet('MabeEnumTest\TestAsset\EnumBasic');
645
        $set->attach(EnumBasic::ONE);
646
        $set->attach(EnumBasic::TWO);
647
        $set->next();
648
649
        $set->getValues();
650
        $this->assertSame(EnumBasic::TWO, $set->current()->getValue());
651
    }
652
653 View Code Duplication
    public function testGetNames()
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...
654
    {
655
        $set = new EnumSet('MabeEnumTest\TestAsset\EnumBasic');
656
        $this->assertSame(array(), $set->getNames());
657
658
        foreach (EnumBasic::getConstants() as $value) {
659
            $set->attach($value);
660
        }
661
662
        $this->assertSame(array_keys(EnumBasic::getConstants()), $set->getNames());
663
    }
664
665 View Code Duplication
    public function testGetNamesDoesNotEffectIteratorPosition()
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...
666
    {
667
        $set = new EnumSet('MabeEnumTest\TestAsset\EnumBasic');
668
        $set->attach(EnumBasic::ONE);
669
        $set->attach(EnumBasic::TWO);
670
        $set->next();
671
672
        $set->getNames();
673
        $this->assertSame(EnumBasic::TWO, $set->current()->getValue());
674
    }
675
676
    public function testUnion()
677
    {
678
        $set1 = new EnumSet('MabeEnumTest\TestAsset\EnumBasic');
679
        $set1->attach(EnumBasic::ONE);
680
        $set1->attach(EnumBasic::TWO);
681
682
        $set2 = new EnumSet('MabeEnumTest\TestAsset\EnumBasic');
683
        $set2->attach(EnumBasic::TWO);
684
        $set2->attach(EnumBasic::THREE);
685
686
        $set3 = new EnumSet('MabeEnumTest\TestAsset\EnumBasic');
687
        $set3->attach(EnumBasic::THREE);
688
        $set3->attach(EnumBasic::FOUR);
689
690
        $rs = $set1->union($set2, $set3);
691
        $this->assertSame(array(
692
            EnumBasic::ONE,
693
            EnumBasic::TWO,
694
            EnumBasic::THREE,
695
            EnumBasic::FOUR,
696
        ), $rs->getValues());
697
    }
698
699
    public function testUnionThrowsInvalidArgumentException()
700
    {
701
        $set1 = new EnumSet('MabeEnumTest\TestAsset\EnumBasic');
702
        $set2 = new EnumSet('MabeEnumTest\TestAsset\Enum32');
703
704
        $this->setExpectedException('InvalidArgumentException');
705
        $set1->union($set2);
706
    }
707
708 View Code Duplication
    public function testIntersect()
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...
709
    {
710
        $set1 = new EnumSet('MabeEnumTest\TestAsset\EnumBasic');
711
        $set1->attach(EnumBasic::ONE);
712
        $set1->attach(EnumBasic::TWO);
713
        $set1->attach(EnumBasic::THREE);
714
715
        $set2 = new EnumSet('MabeEnumTest\TestAsset\EnumBasic');
716
        $set2->attach(EnumBasic::TWO);
717
        $set2->attach(EnumBasic::THREE);
718
        $set2->attach(EnumBasic::FOUR);
719
720
        $set3 = new EnumSet('MabeEnumTest\TestAsset\EnumBasic');
721
        $set3->attach(EnumBasic::THREE);
722
        $set3->attach(EnumBasic::FOUR);
723
        $set3->attach(EnumBasic::FIVE);
724
725
        $rs = $set1->intersect($set2, $set3);
726
        $this->assertSame(array(
727
            EnumBasic::THREE,
728
        ), $rs->getValues());
729
    }
730
731
    public function testIntersectThrowsInvalidArgumentException()
732
    {
733
        $set1 = new EnumSet('MabeEnumTest\TestAsset\EnumBasic');
734
        $set2 = new EnumSet('MabeEnumTest\TestAsset\Enum32');
735
736
        $this->setExpectedException('InvalidArgumentException');
737
        $set1->intersect($set2);
738
    }
739
740 View Code Duplication
    public function testDiff()
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...
741
    {
742
        $set1 = new EnumSet('MabeEnumTest\TestAsset\EnumBasic');
743
        $set1->attach(EnumBasic::ONE);
744
        $set1->attach(EnumBasic::TWO);
745
        $set1->attach(EnumBasic::THREE);
746
747
        $set2 = new EnumSet('MabeEnumTest\TestAsset\EnumBasic');
748
        $set2->attach(EnumBasic::TWO);
749
        $set2->attach(EnumBasic::THREE);
750
        $set2->attach(EnumBasic::FOUR);
751
752
        $set3 = new EnumSet('MabeEnumTest\TestAsset\EnumBasic');
753
        $set3->attach(EnumBasic::THREE);
754
        $set3->attach(EnumBasic::FOUR);
755
        $set3->attach(EnumBasic::FIVE);
756
757
        $rs = $set1->diff($set2, $set3);
758
        $this->assertSame(array(
759
            EnumBasic::ONE,
760
        ), $rs->getValues());
761
    }
762
763 View Code Duplication
    public function testDiffThrowsInvalidArgumentException()
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...
764
    {
765
        $set1 = new EnumSet('MabeEnumTest\TestAsset\EnumBasic');
766
        $set2 = new EnumSet('MabeEnumTest\TestAsset\Enum32');
767
768
        $this->setExpectedException('InvalidArgumentException');
769
        $set1->diff($set2);
770
    }
771
772
    public function testSymDiff()
773
    {
774
        $set1 = new EnumSet('MabeEnumTest\TestAsset\EnumBasic');
775
        $set1->attach(EnumBasic::ONE);
776
        $set1->attach(EnumBasic::TWO);
777
        $set1->attach(EnumBasic::THREE);
778
779
        $set2 = new EnumSet('MabeEnumTest\TestAsset\EnumBasic');
780
        $set2->attach(EnumBasic::TWO);
781
        $set2->attach(EnumBasic::THREE);
782
        $set2->attach(EnumBasic::FOUR);
783
784
        $set3 = new EnumSet('MabeEnumTest\TestAsset\EnumBasic');
785
        $set3->attach(EnumBasic::THREE);
786
        $set3->attach(EnumBasic::FOUR);
787
        $set3->attach(EnumBasic::FIVE);
788
789
        $rs = $set1->symDiff($set2, $set3);
790
        $this->assertSame(array(
791
            EnumBasic::ONE,
792
            EnumBasic::FOUR,
793
            EnumBasic::FIVE,
794
        ), $rs->getValues());
795
    }
796
797 View Code Duplication
    public function testSymDiffThrowsInvalidArgumentException()
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...
798
    {
799
        $set1 = new EnumSet('MabeEnumTest\TestAsset\EnumBasic');
800
        $set2 = new EnumSet('MabeEnumTest\TestAsset\Enum32');
801
802
        $this->setExpectedException('InvalidArgumentException');
803
        $set1->symDiff($set2);
804
    }
805
}
806