[FOLLOWUP][TASK] Replace @expectedException in ext:core
[Packages/TYPO3.CMS.git] / typo3 / sysext / core / Tests / Unit / Type / EnumerationTest.php
1 <?php
2 namespace TYPO3\CMS\Core\Tests\Unit\Type;
3
4 /*
5 * This file is part of the TYPO3 CMS project.
6 *
7 * It is free software; you can redistribute it and/or modify it under
8 * the terms of the GNU General Public License, either version 2
9 * of the License, or any later version.
10 *
11 * For the full copyright and license information, please read the
12 * LICENSE.txt file that was distributed with this source code.
13 *
14 * The TYPO3 project - inspiring people to share!
15 */
16
17 use TYPO3\CMS\Core\Tests\Unit\Type\Fixture\Enumeration;
18 use TYPO3\CMS\Core\Type\Exception\InvalidEnumerationDefinitionException;
19 use TYPO3\CMS\Core\Type\Exception\InvalidEnumerationValueException;
20
21 /**
22 * Test case
23 */
24 class EnumerationTest extends \TYPO3\CMS\Core\Tests\UnitTestCase
25 {
26 /**
27 * @test
28 */
29 public function constructorThrowsExceptionIfNoConstantsAreDefined()
30 {
31 $this->expectException(InvalidEnumerationValueException::class);
32 $this->expectExceptionCode(1381512753);
33
34 new Enumeration\MissingConstantsEnumeration();
35 }
36
37 /**
38 * @test
39 */
40 public function constructorThrowsExceptionIfInvalidValueIsRequested()
41 {
42 $this->expectException(InvalidEnumerationValueException::class);
43 $this->expectExceptionCode(1381512761);
44
45 new Enumeration\CompleteEnumeration('bar');
46 }
47
48 /**
49 * @test
50 */
51 public function loadValuesThrowsExceptionIfGivenValueIsNotAvailableInEnumeration()
52 {
53 $this->expectException(InvalidEnumerationValueException::class);
54 $this->expectExceptionCode(1381512807);
55
56 new Enumeration\MissingConstantsEnumeration(2);
57 }
58
59 /**
60 * @test
61 */
62 public function loadValuesThrowsExceptionIfDisallowedTypeIsDefinedAsConstant()
63 {
64 $this->expectException(InvalidEnumerationDefinitionException::class);
65 $this->expectExceptionCode(1381512797);
66
67 new Enumeration\InvalidConstantEnumeration(1);
68 }
69
70 /**
71 * @test
72 */
73 public function loadValuesThrowsExceptionIfNoDefaultConstantIsDefinedAndNoValueIsGiven()
74 {
75 $this->expectException(InvalidEnumerationValueException::class);
76 $this->expectExceptionCode(1381512753);
77
78 new Enumeration\MissingDefaultEnumeration();
79 }
80
81 /**
82 * @test
83 */
84 public function loadValuesThrowsExceptionIfValueIsDefinedMultipleTimes()
85 {
86 $this->expectException(InvalidEnumerationDefinitionException::class);
87 $this->expectExceptionCode(1381512859);
88
89 new Enumeration\DuplicateConstantValueEnumeration(1);
90 }
91
92 /**
93 * @test
94 */
95 public function loadValuesSetsStaticEnumConstants()
96 {
97 $enumeration = $this->getAccessibleMock(
98 \TYPO3\CMS\Core\Tests\Unit\Type\Fixture\Enumeration\CompleteEnumeration::class,
99 array('dummy')
100 );
101
102 $enumClassName = get_class($enumeration);
103
104 $expectedValue = array(
105 'INTEGER_VALUE' => 1,
106 'STRING_VALUE' => 'foo',
107 '__default' => 1
108 );
109
110 $result = $enumeration->_getStatic('enumConstants');
111 $this->assertArrayHasKey($enumClassName, $result);
112 $this->assertSame($expectedValue, $result[$enumClassName]);
113 }
114
115 /**
116 * @test
117 */
118 public function constructorSetsValue()
119 {
120 $enumeration = $this->getAccessibleMock(
121 \TYPO3\CMS\Core\Tests\Unit\Type\Fixture\Enumeration\CompleteEnumeration::class,
122 array('dummy'),
123 array(1)
124 );
125 $this->assertEquals(1, $enumeration->_get('value'));
126 }
127
128 /**
129 * @test
130 */
131 public function setValueSetsValue()
132 {
133 $enumeration = $this->getAccessibleMock(
134 \TYPO3\CMS\Core\Tests\Unit\Type\Fixture\Enumeration\CompleteEnumeration::class,
135 array('dummy'),
136 array(1)
137 );
138 $enumeration->_call('setValue', 'foo');
139 $this->assertEquals('foo', $enumeration->_get('value'));
140 }
141
142 /**
143 * @test
144 */
145 public function setValueToAnInvalidValueThrowsException()
146 {
147 $this->expectException(InvalidEnumerationValueException::class);
148 $this->expectExceptionCode(1381615295);
149
150 $enumeration = $this->getAccessibleMock(
151 \TYPO3\CMS\Core\Tests\Unit\Type\Fixture\Enumeration\CompleteEnumeration::class,
152 array('dummy'),
153 array(1)
154 );
155 $enumeration->_call('setValue', 2);
156 $this->assertEquals(2, $enumeration->_get('value'));
157 }
158
159 /**
160 * Array of value pairs and expected comparison result
161 */
162 public function isValidComparisonExpectations()
163 {
164 return array(
165 array(
166 1,
167 1,
168 true
169 ),
170 array(
171 1,
172 '1',
173 true
174 ),
175 array(
176 '1',
177 1,
178 true
179 ),
180 array(
181 'a1',
182 1,
183 false
184 ),
185 array(
186 1,
187 'a1',
188 false
189 ),
190 array(
191 '1a',
192 1,
193 false
194 ),
195 array(
196 1,
197 '1a',
198 false
199 ),
200 array(
201 'foo',
202 'foo',
203 true
204 ),
205 array(
206 'foo',
207 'bar',
208 false
209 ),
210 array(
211 'foo',
212 'foobar',
213 false
214 )
215 );
216 }
217
218 /**
219 * @test
220 * @dataProvider isValidComparisonExpectations
221 */
222 public function isValidDoesTypeLooseComparison($enumerationValue, $testValue, $expectation)
223 {
224 $mockName = $this->getUniqueId('CompleteEnumerationMock');
225 $enumeration = $this->getAccessibleMock(
226 \TYPO3\CMS\Core\Tests\Unit\Type\Fixture\Enumeration\CompleteEnumeration::class,
227 array('dummy'),
228 array(),
229 $mockName,
230 false
231 );
232 $enumeration->_setStatic('enumConstants', array($mockName => array('CONSTANT_NAME' => $enumerationValue)));
233 $enumeration->_set('value', $enumerationValue);
234 $this->assertSame($expectation, $enumeration->_call('isValid', $testValue));
235 }
236
237 /**
238 * @test
239 */
240 public function getConstantsReturnsArrayOfPossibleValuesWithoutDefault()
241 {
242 $this->assertEquals(array('INTEGER_VALUE' => 1, 'STRING_VALUE' => 'foo'), Enumeration\CompleteEnumeration::getConstants());
243 }
244
245 /**
246 * @test
247 */
248 public function getConstantsReturnsArrayOfPossibleValuesWithDefaultIfRequested()
249 {
250 $this->assertEquals(array('INTEGER_VALUE' => 1, 'STRING_VALUE' => 'foo', '__default' => 1), Enumeration\CompleteEnumeration::getConstants(true));
251 }
252
253 /**
254 * @test
255 */
256 public function getConstantsCanBeCalledOnInstances()
257 {
258 $enumeration = new Enumeration\CompleteEnumeration();
259 $this->assertEquals(array('INTEGER_VALUE' => 1, 'STRING_VALUE' => 'foo'), $enumeration->getConstants());
260 }
261
262 /**
263 * @test
264 */
265 public function toStringReturnsValueAsString()
266 {
267 $enumeration = new Enumeration\CompleteEnumeration();
268 $this->assertSame('1', $enumeration->__toString());
269 }
270
271 /**
272 * @test
273 */
274 public function castReturnsObjectOfEnumerationTypeIfSimpleValueIsGiven()
275 {
276 $enumeration = Enumeration\CompleteEnumeration::cast(1);
277 $this->assertInstanceOf(\TYPO3\CMS\Core\Tests\Unit\Type\Fixture\Enumeration\CompleteEnumeration::class, $enumeration);
278 }
279
280 /**
281 * @test
282 */
283 public function castReturnsObjectOfCalledEnumerationTypeIfCalledWithValueOfDifferentType()
284 {
285 $initialEnumeration = new Enumeration\MissingDefaultEnumeration(1);
286 $enumeration = Enumeration\CompleteEnumeration::cast($initialEnumeration);
287 $this->assertInstanceOf(\TYPO3\CMS\Core\Tests\Unit\Type\Fixture\Enumeration\CompleteEnumeration::class, $enumeration);
288 }
289
290 /**
291 * @test
292 */
293 public function castReturnsGivenObjectIfCalledWithValueOfSameType()
294 {
295 $initialEnumeration = new Enumeration\CompleteEnumeration(1);
296 $enumeration = Enumeration\CompleteEnumeration::cast($initialEnumeration);
297 $this->assertSame($initialEnumeration, $enumeration);
298 }
299
300 /**
301 * @test
302 */
303 public function castCastsStringToEnumerationWithCorrespondingValue()
304 {
305 $enumeration = $this->getAccessibleMock(
306 \TYPO3\CMS\Core\Tests\Unit\Type\Fixture\Enumeration\CompleteEnumeration::class,
307 array('dummy'),
308 array('1')
309 );
310 $this->assertSame(1, $enumeration->_get('value'));
311 }
312
313 /**
314 * @test
315 */
316 public function castCastsIntegerToEnumerationWithCorrespondingValue()
317 {
318 $enumeration = $this->getAccessibleMock(
319 \TYPO3\CMS\Core\Tests\Unit\Type\Fixture\Enumeration\CompleteEnumeration::class,
320 array('dummy'),
321 array(1)
322 );
323 $this->assertSame(1, $enumeration->_get('value'));
324 }
325
326 /**
327 * @test
328 */
329 public function equalsReturnsTrueIfIntegerIsGivenThatEqualsEnumerationsIntegerValue()
330 {
331 $enumeration = new Enumeration\CompleteEnumeration(1);
332 $this->assertTrue($enumeration->equals(1));
333 }
334
335 /**
336 * @test
337 */
338 public function equalsReturnsTrueIfStringIsGivenThatEqualsEnumerationsIntegerValue()
339 {
340 $enumeration = new Enumeration\CompleteEnumeration(1);
341 $this->assertTrue($enumeration->equals('1'));
342 }
343
344 /**
345 * @test
346 */
347 public function equalsReturnsTrueIfEqualEnumerationIsGiven()
348 {
349 $enumerationFoo = new Enumeration\CompleteEnumeration(1);
350 $enumerationBar = new Enumeration\CompleteEnumeration(1);
351 $this->assertTrue($enumerationFoo->equals($enumerationBar));
352 }
353
354 /**
355 * @test
356 */
357 public function equalsReturnsTrueIfDifferentEnumerationWithSameValueIsGiven()
358 {
359 $enumerationFoo = new Enumeration\CompleteEnumeration(1);
360 $enumerationBar = new Enumeration\MissingDefaultEnumeration(1);
361 $this->assertTrue($enumerationFoo->equals($enumerationBar));
362 }
363
364 /**
365 * @test
366 */
367 public function equalsReturnsFalseIfDifferentEnumerationWithDifferentValueIsGiven()
368 {
369 $enumerationFoo = new Enumeration\CompleteEnumeration('foo');
370 $enumerationBar = new Enumeration\MissingDefaultEnumeration(1);
371 $this->assertFalse($enumerationFoo->equals($enumerationBar));
372 }
373
374 /**
375 * @test
376 */
377 public function equalsReturnsFalseIfEnumerationOfSameTypeWithDifferentValueIsGiven()
378 {
379 $enumerationFoo = new Enumeration\CompleteEnumeration(1);
380 $enumerationBar = new Enumeration\CompleteEnumeration('foo');
381 $this->assertFalse($enumerationFoo->equals($enumerationBar));
382 }
383 }