Completed
Push — enum_get_values_names_ordinals ( 2bfc88 )
by Marc
02:00
created

testInstantiateUsingInvalidOrdinalNumberThrowsInvalidArgumentException()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 2 Features 0
Metric Value
c 2
b 2
f 0
dl 0
loc 5
rs 9.4285
cc 1
eloc 3
nc 1
nop 0
1
<?php
2
3
namespace MabeEnumTest;
4
5
use MabeEnumTest\TestAsset\EnumBasic;
6
use MabeEnumTest\TestAsset\EnumInheritance;
7
use MabeEnumTest\TestAsset\EnumAmbiguous;
8
use MabeEnumTest\TestAsset\EnumExtendedAmbiguous;
9
use MabeEnumTest\TestAsset\ConstVisibilityEnum;
10
use MabeEnumTest\TestAsset\ConstVisibilityEnumExtended;
11
use PHPUnit_Framework_TestCase as TestCase;
12
use ReflectionClass;
13
14
/**
15
 * Unit tests for the class MabeEnum\Enum
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 EnumTest extends TestCase
22
{
23
    public function setUp()
24
    {
25
        EnumBasic::clear();
26
        EnumInheritance::clear();
27
    }
28
29
    public function testGetNameReturnsConstantNameOfCurrentValue()
30
    {
31
        $enum = EnumBasic::get(EnumBasic::ONE);
32
        $this->assertSame('ONE', $enum->getName());
33
    }
34
35
    public function testToStringMagicMethodReturnsName()
36
    {
37
        $enum = EnumBasic::get(EnumBasic::ONE);
38
        $this->assertSame('ONE', $enum->__toString());
39
    }
40
41
    public function testEnumInheritance()
42
    {
43
        $this->assertSame(array(
44
            'ONE'           => 1,
45
            'TWO'           => 2,
46
            'THREE'         => 3,
47
            'FOUR'          => 4,
48
            'FIVE'          => 5,
49
            'SIX'           => 6,
50
            'SEVEN'         => 7,
51
            'EIGHT'         => 8,
52
            'NINE'          => 9,
53
            'ZERO'          => 0,
54
            'FLOAT'         => 0.123,
55
            'STR'           => 'str',
56
            'STR_EMPTY'     => '',
57
            'NIL'           => null,
58
            'BOOLEAN_TRUE'  => true,
59
            'BOOLEAN_FALSE' => false,
60
            'INHERITANCE'   => 'Inheritance',
61
        ), EnumInheritance::getConstants());
62
63
        $enum = EnumInheritance::get(EnumInheritance::ONE);
64
        $this->assertSame(EnumInheritance::ONE, $enum->getValue());
65
        $this->assertSame(0, $enum->getOrdinal());
66
67
        $enum = EnumInheritance::get(EnumInheritance::INHERITANCE);
68
        $this->assertSame(EnumInheritance::INHERITANCE, $enum->getValue());
69
        $this->assertSame(16, $enum->getOrdinal());
70
    }
71
72
    public function testGetWithStrictValue()
73
    {
74
        $enum = EnumBasic::get(EnumBasic::ONE);
75
        $this->assertSame(1, $enum->getValue());
76
        $this->assertSame(0, $enum->getOrdinal());
77
    }
78
79
    public function testGetWithNonStrictValueThrowsInvalidArgumentException()
80
    {
81
        $this->setExpectedException('InvalidArgumentException');
82
        EnumBasic::get((string)EnumBasic::TWO);
83
    }
84
85
    public function testGetWithInvalidValueThrowsInvalidArgumentException()
86
    {
87
        $this->setExpectedException('InvalidArgumentException');
88
        EnumBasic::get('unknown');
89
    }
90
91
    public function testGetWithInvalidTypeOfValueThrowsInvalidArgumentException()
92
    {
93
        $this->setExpectedException('InvalidArgumentException');
94
        EnumBasic::get(array());
95
    }
96
97
    public function testGetByInstance()
98
    {
99
        $enum1 = EnumBasic::get(EnumBasic::ONE);
100
        $enum2 = EnumBasic::get($enum1);
101
        $this->assertSame($enum1, $enum2);
102
    }
103
104
    public function testGetByExtendedInstanceOfKnownValue()
105
    {
106
        $enum = EnumInheritance::get(EnumInheritance::ONE);
107
108
        $this->setExpectedException('InvalidArgumentException');
109
        EnumBasic::get($enum);
110
    }
111
112
    public function testGetEnumerators()
113
    {
114
        $constants   = EnumInheritance::getConstants();
115
        $enumerators = EnumInheritance::getEnumerators();
116
        $count       = count($enumerators);
117
118
        $this->assertSame(count($constants), $count);
119
        for ($i = 0; $i < $count; ++$i) {
120
            $this->assertArrayHasKey($i, $enumerators);
121
            $this->assertInstanceOf('MabeEnumTest\TestAsset\EnumInheritance', $enumerators[$i]);
122
123
            $enumerator = $enumerators[$i];
124
            $this->assertArrayHasKey($enumerator->getName(), $constants);
125
            $this->assertSame($constants[$enumerator->getName()], $enumerator->getValue());
126
        }
127
    }
128
129 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...
130
    {
131
        $expectedValues = array_values(EnumInheritance::getConstants());
132
        $values         = EnumInheritance::getValues();
133
        $count          = count($values);
134
135
        $this->assertSame(count($expectedValues), $count);
136
        for ($i = 0; $i < $count; ++$i) {
137
            $this->assertArrayHasKey($i, $values);
138
            $this->assertSame($expectedValues[$i], $values[$i]);
139
        }
140
    }
141
142 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...
143
    {
144
        $expectedNames = array_keys(EnumInheritance::getConstants());
145
        $names         = EnumInheritance::getNames();
146
        $count         = count($names);
147
148
        $this->assertSame(count($expectedNames), $count);
149
        for ($i = 0; $i < $count; ++$i) {
150
            $this->assertArrayHasKey($i, $names);
151
            $this->assertSame($expectedNames[$i], $names[$i]);
152
        }
153
    }
154
155 View Code Duplication
    public function testGetOrdinals()
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...
156
    {
157
        $constants = EnumInheritance::getConstants();
158
        $ordinals  = EnumInheritance::getOrdinals();
159
        $count     = count($ordinals);
160
161
        $this->assertSame(count($constants), $count);
162
        for ($i = 0; $i < $count; ++$i) {
163
            $this->assertArrayHasKey($i, $ordinals);
164
            $this->assertSame($i, $ordinals[$i]);
165
        }
166
    }
167
168
    public function testGetAllValues()
169
    {
170
        $constants = EnumBasic::getConstants();
171
        foreach ($constants as $name => $value) {
172
            $enum = EnumBasic::get($value);
173
            $this->assertSame($value, $enum->getValue());
174
            $this->assertSame($name, $enum->getName());
175
        }
176
    }
177
178 View Code Duplication
    public function testIsBasic()
179
    {
180
        $enum = EnumBasic::ONE();
181
182
        // by value
183
        $this->assertTrue($enum->is(EnumBasic::ONE));   // same
184
        $this->assertFalse($enum->is('1'));             // wrong value by strict comparison
185
186
        // by instance
187
        $this->assertTrue($enum->is(EnumBasic::ONE()));        // same
188
        $this->assertFalse($enum->is(EnumBasic::TWO()));       // different enumerators
189
        $this->assertFalse($enum->is(EnumInheritance::ONE())); // different enumeration type
190
    }
191
192
    public function testCallingGetOrdinalTwoTimesWillResultTheSameValue()
193
    {
194
        $enum = EnumBasic::get(EnumBasic::TWO);
195
        $this->assertSame(1, $enum->getOrdinal());
196
        $this->assertSame(1, $enum->getOrdinal());
197
    }
198
199
    public function testInstantiateUsingOrdinalNumber()
200
    {
201
        $enum = EnumInheritance::getByOrdinal(16);
202
        $this->assertSame(16, $enum->getOrdinal());
203
        $this->assertSame('INHERITANCE', $enum->getName());
204
    }
205
206
    public function testInstantiateUsingInvalidOrdinalNumberThrowsInvalidArgumentException()
207
    {
208
        $this->setExpectedException('InvalidArgumentException');
209
        EnumInheritance::getByOrdinal(17);
210
    }
211
212
    public function testInstantiateByName()
213
    {
214
        $enum = EnumInheritance::getByName('ONE');
215
        $this->assertInstanceOf('MabeEnumTest\TestAsset\EnumInheritance', $enum);
216
        $this->assertSame(EnumInheritance::ONE, $enum->getValue());
217
    }
218
219
    public function testInstantiateByUnknownNameThrowsInvalidArgumentException()
220
    {
221
        $this->setExpectedException('InvalidArgumentException');
222
        EnumInheritance::getByName('UNKNOWN');
223
    }
224
225
    public function testInstantiateUsingMagicMethod()
226
    {
227
        $enum = EnumInheritance::ONE();
228
        $this->assertInstanceOf('MabeEnumTest\TestAsset\EnumInheritance', $enum);
229
        $this->assertSame(EnumInheritance::ONE, $enum->getValue());
230
    }
231
232
    public function testAmbiguousConstantsThrowsLogicException()
233
    {
234
        $this->setExpectedException('LogicException');
235
        EnumAmbiguous::get('unknown');
236
    }
237
238
    public function testExtendedAmbiguousCanstantsThrowsLogicException()
239
    {
240
        $this->setExpectedException('LogicException');
241
        EnumExtendedAmbiguous::get('unknown');
242
    }
243
244
    public function testSingleton()
245
    {
246
        $enum1 = EnumBasic::get(EnumBasic::ONE);
247
        $enum2 = EnumBasic::ONE();
248
        $this->assertSame($enum1, $enum2);
249
    }
250
251
    public function testClear()
252
    {
253
        $enum1 = EnumBasic::ONE();
254
        EnumBasic::clear();
255
        $enum2 = EnumBasic::ONE();
256
        $enum3 = EnumBasic::ONE();
257
        
258
        $this->assertNotSame($enum1, $enum2);
259
        $this->assertSame($enum2, $enum3);
260
    }
261
262 View Code Duplication
    public function testCloneNotCallableAndThrowsLogicException()
263
    {
264
        $enum = EnumBasic::ONE();
265
266
        $reflectionClass  = new ReflectionClass($enum);
267
        $reflectionMethod = $reflectionClass->getMethod('__clone');
268
        $this->assertTrue($reflectionMethod->isPrivate(), 'The method __clone must be private');
269
        $this->assertTrue($reflectionMethod->isFinal(), 'The method __clone must be final');
270
271
        $reflectionMethod->setAccessible(true);
272
        $this->setExpectedException('LogicException');
273
        $reflectionMethod->invoke($enum);
274
    }
275
276
    public function testNotSerializable()
277
    {
278
        $enum = EnumBasic::ONE();
279
280
        $this->setExpectedException('LogicException');
281
        serialize($enum);
282
    }
283
284
    public function testNotUnserializable()
285
    {
286
        $this->setExpectedException('LogicException');
287
        unserialize("O:32:\"MabeEnumTest\TestAsset\EnumBasic\":0:{}");
288
    }
289
290 View Code Duplication
    public function testHas()
291
    {
292
        $enum = EnumBasic::ONE();
293
294
        $this->assertFalse($enum->has('invalid'));
295
        $this->assertFalse($enum->has(EnumInheritance::ONE()));
296
        $this->assertTrue($enum->has(EnumBasic::ONE()));
297
        $this->assertTrue($enum->has(EnumBasic::ONE));
298
    }
299
    
300
    public function testConstVisibility()
301
    {
302
        if (PHP_VERSION_ID < 70100) {
303
            $this->markTestSkipped('This test is for PHP-7.1 and upper only');
304
        }
305
306
        $constants = ConstVisibilityEnum::getConstants();
307
        $this->assertSame(array(
308
            'IPUB' => ConstVisibilityEnum::IPUB,
309
            'PUB'  => ConstVisibilityEnum::PUB,
310
        ), $constants);
311
    }
312
    
313
    public function testConstVisibilityExtended()
314
    {
315
        if (PHP_VERSION_ID < 70100) {
316
            $this->markTestSkipped('This test is for PHP-7.1 and upper only');
317
        }
318
319
        $constants = ConstVisibilityEnumExtended::getConstants();
320
        $this->assertSame(array(
321
            'IPUB'  => ConstVisibilityEnumExtended::IPUB,
322
            'PUB'   => ConstVisibilityEnumExtended::PUB,
323
            'IPUB2' => ConstVisibilityEnumExtended::IPUB2,
324
            'PUB2'  => ConstVisibilityEnumExtended::PUB2,
325
        ), $constants);
326
    }
327
}
328