Completed
Pull Request — master (#59)
by Marc
67:33 queued 65:23
created

testCallingGetOrdinalTwoTimesWillResultTheSameValue()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

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

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
267
        $this->assertSame('public', ConstVisibilityEnum::PUB()->getValue());
268
269
        $this->assertTrue($enum->has('PRO'));
270
        $this->assertSame('protected', ConstVisibilityEnum::PRO()->getValue());
271
        
272
        $this->assertTrue($enum->has('PRI'));
273
        $this->assertSame('private', ConstVisibilityEnum::PRI()->getValue());
274
    }
275
}
276