Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
1 | <?php |
||
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 | public function testGetAllValues() |
||
130 | { |
||
131 | $constants = EnumBasic::getConstants(); |
||
132 | foreach ($constants as $name => $value) { |
||
133 | $enum = EnumBasic::get($value); |
||
134 | $this->assertSame($value, $enum->getValue()); |
||
135 | $this->assertSame($name, $enum->getName()); |
||
136 | } |
||
137 | } |
||
138 | |||
139 | public function testIsBasic() |
||
140 | { |
||
141 | $enum = EnumBasic::ONE(); |
||
142 | |||
143 | // by value |
||
144 | $this->assertTrue($enum->is(EnumBasic::ONE)); // same |
||
145 | $this->assertFalse($enum->is('1')); // wrong value by strict comparison |
||
146 | |||
147 | // by instance |
||
148 | $this->assertTrue($enum->is(EnumBasic::ONE())); // same |
||
149 | $this->assertFalse($enum->is(EnumBasic::TWO())); // different enumerators |
||
150 | $this->assertFalse($enum->is(EnumInheritance::ONE())); // different enumeration type |
||
151 | } |
||
152 | |||
153 | public function testCallingGetOrdinalTwoTimesWillResultTheSameValue() |
||
154 | { |
||
155 | $enum = EnumBasic::get(EnumBasic::TWO); |
||
156 | $this->assertSame(1, $enum->getOrdinal()); |
||
157 | $this->assertSame(1, $enum->getOrdinal()); |
||
158 | } |
||
159 | |||
160 | public function testInstantiateUsingOrdinalNumber() |
||
161 | { |
||
162 | $enum = EnumInheritance::getByOrdinal(16); |
||
163 | $this->assertSame(16, $enum->getOrdinal()); |
||
164 | $this->assertSame('INHERITANCE', $enum->getName()); |
||
165 | } |
||
166 | |||
167 | public function testInstantiateUsingInvalidOrdinalNumberThrowsInvalidArgumentException() |
||
168 | { |
||
169 | $this->setExpectedException('InvalidArgumentException'); |
||
170 | EnumInheritance::getByOrdinal(17); |
||
171 | } |
||
172 | |||
173 | public function testInstantiateByName() |
||
174 | { |
||
175 | $enum = EnumInheritance::getByName('ONE'); |
||
176 | $this->assertInstanceOf('MabeEnumTest\TestAsset\EnumInheritance', $enum); |
||
177 | $this->assertSame(EnumInheritance::ONE, $enum->getValue()); |
||
178 | } |
||
179 | |||
180 | public function testInstantiateByUnknownNameThrowsInvalidArgumentException() |
||
181 | { |
||
182 | $this->setExpectedException('InvalidArgumentException'); |
||
183 | EnumInheritance::getByName('UNKNOWN'); |
||
184 | } |
||
185 | |||
186 | public function testInstantiateUsingMagicMethod() |
||
187 | { |
||
188 | $enum = EnumInheritance::ONE(); |
||
189 | $this->assertInstanceOf('MabeEnumTest\TestAsset\EnumInheritance', $enum); |
||
190 | $this->assertSame(EnumInheritance::ONE, $enum->getValue()); |
||
191 | } |
||
192 | |||
193 | public function testAmbiguousConstantsThrowsLogicException() |
||
194 | { |
||
195 | $this->setExpectedException('LogicException'); |
||
196 | EnumAmbiguous::get('unknown'); |
||
197 | } |
||
198 | |||
199 | public function testExtendedAmbiguousCanstantsThrowsLogicException() |
||
200 | { |
||
201 | $this->setExpectedException('LogicException'); |
||
202 | EnumExtendedAmbiguous::get('unknown'); |
||
203 | } |
||
204 | |||
205 | public function testSingleton() |
||
206 | { |
||
207 | $enum1 = EnumBasic::get(EnumBasic::ONE); |
||
208 | $enum2 = EnumBasic::ONE(); |
||
209 | $this->assertSame($enum1, $enum2); |
||
210 | } |
||
211 | |||
212 | public function testClear() |
||
213 | { |
||
214 | $enum1 = EnumBasic::ONE(); |
||
215 | EnumBasic::clear(); |
||
216 | $enum2 = EnumBasic::ONE(); |
||
217 | $enum3 = EnumBasic::ONE(); |
||
218 | |||
219 | $this->assertNotSame($enum1, $enum2); |
||
220 | $this->assertSame($enum2, $enum3); |
||
221 | } |
||
222 | |||
223 | public function testCloneNotCallableAndThrowsLogicException() |
||
224 | { |
||
225 | $enum = EnumBasic::ONE(); |
||
226 | |||
227 | $reflectionClass = new ReflectionClass($enum); |
||
228 | $reflectionMethod = $reflectionClass->getMethod('__clone'); |
||
229 | $this->assertTrue($reflectionMethod->isPrivate(), 'The method __clone must be private'); |
||
230 | $this->assertTrue($reflectionMethod->isFinal(), 'The method __clone must be final'); |
||
231 | |||
232 | $reflectionMethod->setAccessible(true); |
||
233 | $this->setExpectedException('LogicException'); |
||
234 | $reflectionMethod->invoke($enum); |
||
235 | } |
||
236 | |||
237 | public function testNotSerializable() |
||
238 | { |
||
239 | $enum = EnumBasic::ONE(); |
||
240 | |||
241 | $this->setExpectedException('LogicException'); |
||
242 | serialize($enum); |
||
243 | } |
||
244 | |||
245 | public function testNotUnserializable() |
||
246 | { |
||
247 | $this->setExpectedException('LogicException'); |
||
248 | unserialize("O:32:\"MabeEnumTest\TestAsset\EnumBasic\":0:{}"); |
||
249 | } |
||
250 | |||
251 | public function testHas() |
||
252 | { |
||
253 | $enum = EnumBasic::ONE(); |
||
254 | |||
255 | $this->assertFalse($enum->has('invalid')); |
||
256 | $this->assertFalse($enum->has(EnumInheritance::ONE())); |
||
257 | $this->assertTrue($enum->has(EnumBasic::ONE())); |
||
258 | $this->assertTrue($enum->has(EnumBasic::ONE)); |
||
259 | } |
||
289 |