Completed
Pull Request — master (#63)
by Marc
04:24 queued 02:18
created

ArgumentExceptionIfNotString()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 7
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\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 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...
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->getBinaryBitsetBe());
265
        $this->assertTrue($enumSet->contains($enum1));
266
267
        $this->assertNull($enumSet->attach($enum2));
268
        $this->assertSame('000000000000000003', \bin2hex($enumSet->getBinaryBitsetBe()));
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->getBinaryBitsetBe());
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->getBinaryBitsetBe());
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->getBinaryBitsetBe());
283
        $this->assertFalse($enumSet->contains($enum2));
284
        
285
        $this->assertSame(3, $enumSet->count());
286
    }
287
288
    /**
289
     * @deprecated
290
     */
291 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...
292
    {
293
        $enumSet = new EnumSet('MabeEnumTest\TestAsset\Enum65');
294
        
295
        $enum1 = Enum65::ONE;
296
        $enum2 = Enum65::TWO;
297
        $enum3 = Enum65::SIXTYFIVE;
298
        $enum4 = Enum65::SIXTYFOUR;
299
300
        $this->assertNull($enumSet->attach($enum1));
301
        $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...
302
        $this->assertTrue($enumSet->contains($enum1));
303
304
        $this->assertNull($enumSet->attach($enum2));
305
        $this->assertSame('000000000000000003', \bin2hex($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...
306
        $this->assertTrue($enumSet->contains($enum2));
307
308
        $this->assertNull($enumSet->attach($enum3));
309
        $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...
310
        $this->assertTrue($enumSet->contains($enum3));
311
312
        $this->assertNull($enumSet->attach($enum4));
313
        $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...
314
        $this->assertTrue($enumSet->contains($enum4));
315
        
316
        $this->assertSame(4, $enumSet->count());
317
318
        $this->assertNull($enumSet->detach($enum2));
319
        $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...
320
        $this->assertFalse($enumSet->contains($enum2));
321
        
322
        $this->assertSame(3, $enumSet->count());
323
    }
324
325 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...
326
    {
327
        $enumSet = new EnumSet('MabeEnumTest\TestAsset\Enum65');
328
        $enumSet->setBinaryBitsetBe("\x01\x80\x00\x00\x00\x00\x00\x00\x01");
329
330
        $this->assertTrue($enumSet->contains(Enum65::ONE));
331
        $this->assertFalse($enumSet->contains(Enum65::TWO));
332
        $this->assertTrue($enumSet->contains(Enum65::SIXTYFIVE));
333
        $this->assertTrue($enumSet->contains(Enum65::SIXTYFOUR));
334
        $this->assertTrue($enumSet->count() == 3);
335
    }
336
337
    /**
338
     * @deprecated
339
     */
340 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...
341
    {
342
        $enumSet = new EnumSet('MabeEnumTest\TestAsset\Enum65');
343
        $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...
344
345
        $this->assertTrue($enumSet->contains(Enum65::ONE));
346
        $this->assertFalse($enumSet->contains(Enum65::TWO));
347
        $this->assertTrue($enumSet->contains(Enum65::SIXTYFIVE));
348
        $this->assertTrue($enumSet->contains(Enum65::SIXTYFOUR));
349
        $this->assertTrue($enumSet->count() == 3);
350
    }
351
352
    public function testSetBinaryBitsetBeShort()
353
    {
354
        $enumSet = new EnumSet('MabeEnumTest\TestAsset\Enum65');
355
        $enumSet->setBinaryBitsetBe("\x0A");
356
        $this->assertSame("\x00\x00\x00\x00\x00\x00\x00\x00\x0A", $enumSet->getBinaryBitsetBe());
357
    }
358
359
    public function testSetBinaryBitsetBeLong()
360
    {
361
        $enumSet = new EnumSet('MabeEnumTest\TestAsset\EnumBasic');
362
        $enumSet->setBinaryBitsetBe("\xFF\xFF\xFF\xFF\xFF\x0A");
363
        $this->assertSame("\xFF\x0A", $enumSet->getBinaryBitsetBe());
364
    }
365
366
    public function testSetBinaryBitsetBeArgumentExceptionIfNotString()
367
    {
368
        $this->setExpectedException('InvalidArgumentException');
369
        
370
        $enum = new EnumSet('MabeEnumTest\TestAsset\Enum65');
371
        $enum->setBinaryBitsetBe(0);
372
    }
373
374
    public function testCountingEmptyEnumEmptySet()
375
    {
376
        $set = new EnumSet('MabeEnumTest\TestAsset\EmptyEnum');
377
        $this->assertSame(0, $set->count());
378
    }
379
}
380