db393adc6b5fd7a3733a6793f67f23965d638f09
[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;
19
20 /**
21 * Test case
22 */
23 class EnumerationTest extends \TYPO3\CMS\Core\Tests\UnitTestCase {
24
25 /**
26 * @test
27 * @expectedException \TYPO3\CMS\Core\Type\Exception\InvalidEnumerationValueException
28 */
29 public function constructorThrowsExceptionIfNoConstantsAreDefined() {
30 new Enumeration\MissingConstantsEnumeration();
31 }
32
33 /**
34 * @test
35 * @expectedException \TYPO3\CMS\Core\Type\Exception\InvalidEnumerationValueException
36 */
37 public function constructorThrowsExceptionIfInvalidValueIsRequested() {
38 new Enumeration\CompleteEnumeration('bar');
39 }
40
41 /**
42 * @test
43 * @expectedException \TYPO3\CMS\Core\Type\Exception\InvalidEnumerationValueException
44 */
45 public function loadValuesThrowsExceptionIfGivenValueIsNotAvailableInEnumeration() {
46 new Enumeration\MissingConstantsEnumeration(2);
47 }
48
49 /**
50 * @test
51 * @expectedException \TYPO3\CMS\Core\Type\Exception\InvalidEnumerationDefinitionException
52 */
53 public function loadValuesThrowsExceptionIfDisallowedTypeIsDefinedAsConstant() {
54 new Enumeration\InvalidConstantEnumeration(1);
55 }
56
57 /**
58 * @test
59 * @expectedException \TYPO3\CMS\Core\Type\Exception\InvalidEnumerationValueException
60 */
61 public function loadValuesThrowsExceptionIfNoDefaultConstantIsDefinedAndNoValueIsGiven() {
62 new Enumeration\MissingDefaultEnumeration();
63 }
64
65 /**
66 * @test
67 * @expectedException \TYPO3\CMS\Core\Type\Exception\InvalidEnumerationDefinitionException
68 */
69 public function loadValuesThrowsExceptionIfValueIsDefinedMultipleTimes() {
70 new Enumeration\DuplicateConstantValueEnumeration(1);
71 }
72
73 /**
74 * @test
75 */
76 public function loadValuesSetsStaticEnumConstants() {
77 $enumeration = $this->getAccessibleMock(
78 \TYPO3\CMS\Core\Tests\Unit\Type\Fixture\Enumeration\CompleteEnumeration::class,
79 array('dummy')
80 );
81
82 $enumClassName = get_class($enumeration);
83
84 $expectedValue = array(
85 'INTEGER_VALUE' => 1,
86 'STRING_VALUE' => 'foo',
87 '__default' => 1
88 );
89
90 $result = $enumeration->_getStatic('enumConstants');
91 $this->assertArrayHasKey($enumClassName, $result);
92 $this->assertSame($expectedValue, $result[$enumClassName]);
93 }
94
95 /**
96 * @test
97 */
98 public function constructorSetsValue() {
99 $enumeration = $this->getAccessibleMock(
100 \TYPO3\CMS\Core\Tests\Unit\Type\Fixture\Enumeration\CompleteEnumeration::class,
101 array('dummy'),
102 array(1)
103 );
104 $this->assertEquals(1, $enumeration->_get('value'));
105 }
106
107 /**
108 * @test
109 */
110 public function setValueSetsValue() {
111 $enumeration = $this->getAccessibleMock(
112 \TYPO3\CMS\Core\Tests\Unit\Type\Fixture\Enumeration\CompleteEnumeration::class,
113 array('dummy'),
114 array(1)
115 );
116 $enumeration->_call('setValue', 'foo');
117 $this->assertEquals('foo', $enumeration->_get('value'));
118 }
119
120 /**
121 * @test
122 * @expectedException \TYPO3\CMS\Core\Type\Exception\InvalidEnumerationValueException
123 */
124 public function setValueToAnInvalidValueThrowsException() {
125 $enumeration = $this->getAccessibleMock(
126 \TYPO3\CMS\Core\Tests\Unit\Type\Fixture\Enumeration\CompleteEnumeration::class,
127 array('dummy'),
128 array(1)
129 );
130 $enumeration->_call('setValue', 2);
131 $this->assertEquals(2, $enumeration->_get('value'));
132 }
133
134 /**
135 * Array of value pairs and expected comparison result
136 */
137 public function isValidComparisonExpectations() {
138 return array(
139 array(
140 1,
141 1,
142 TRUE
143 ),
144 array(
145 1,
146 '1',
147 TRUE
148 ),
149 array(
150 '1',
151 1,
152 TRUE
153 ),
154 array(
155 'a1',
156 1,
157 FALSE
158 ),
159 array(
160 1,
161 'a1',
162 FALSE
163 ),
164 array(
165 '1a',
166 1,
167 FALSE
168 ),
169 array(
170 1,
171 '1a',
172 FALSE
173 ),
174 array(
175 'foo',
176 'foo',
177 TRUE
178 ),
179 array(
180 'foo',
181 'bar',
182 FALSE
183 ),
184 array(
185 'foo',
186 'foobar',
187 FALSE
188 )
189 );
190 }
191
192 /**
193 * @test
194 * @dataProvider isValidComparisonExpectations
195 */
196 public function isValidDoesTypeLooseComparison($enumerationValue, $testValue, $expectation) {
197 $mockName = $this->getUniqueId('CompleteEnumerationMock');
198 $enumeration = $this->getAccessibleMock(
199 \TYPO3\CMS\Core\Tests\Unit\Type\Fixture\Enumeration\CompleteEnumeration::class,
200 array('dummy'),
201 array(),
202 $mockName,
203 FALSE
204 );
205 $enumeration->_setStatic('enumConstants', array($mockName => array('CONSTANT_NAME' => $enumerationValue)));
206 $enumeration->_set('value', $enumerationValue);
207 $this->assertSame($expectation, $enumeration->_call('isValid', $testValue));
208 }
209
210 /**
211 * @test
212 */
213 public function getConstantsReturnsArrayOfPossibleValuesWithoutDefault() {
214 $enumeration = new Enumeration\CompleteEnumeration();
215
216 $this->assertEquals(array('INTEGER_VALUE' => 1, 'STRING_VALUE' => 'foo'), $enumeration->getConstants());
217 }
218
219 /**
220 * @test
221 */
222 public function getConstantsReturnsArrayOfPossibleValuesWithDefaultIfRequested() {
223 $enumeration = new Enumeration\CompleteEnumeration();
224 $this->assertEquals(array('INTEGER_VALUE' => 1, 'STRING_VALUE' => 'foo', '__default' => 1), $enumeration->getConstants(TRUE));
225 }
226
227 /**
228 * @test
229 */
230 public function toStringReturnsValueAsString() {
231 $enumeration = new Enumeration\CompleteEnumeration();
232 $this->assertSame('1', $enumeration->__toString());
233 }
234
235 /**
236 * @test
237 */
238 public function castReturnsObjectOfEnumerationTypeIfSimpleValueIsGiven() {
239 $enumeration = Enumeration\CompleteEnumeration::cast(1);
240 $this->assertInstanceOf(\TYPO3\CMS\Core\Tests\Unit\Type\Fixture\Enumeration\CompleteEnumeration::class, $enumeration);
241 }
242
243 /**
244 * @test
245 */
246 public function castReturnsObjectOfCalledEnumerationTypeIfCalledWithValueOfDifferentType() {
247 $initialEnumeration = new Enumeration\MissingDefaultEnumeration(1);
248 $enumeration = Enumeration\CompleteEnumeration::cast($initialEnumeration);
249 $this->assertInstanceOf(\TYPO3\CMS\Core\Tests\Unit\Type\Fixture\Enumeration\CompleteEnumeration::class, $enumeration);
250 }
251
252 /**
253 * @test
254 */
255 public function castReturnsGivenObjectIfCalledWithValueOfSameType() {
256 $initialEnumeration = new Enumeration\CompleteEnumeration(1);
257 $enumeration = Enumeration\CompleteEnumeration::cast($initialEnumeration);
258 $this->assertSame($initialEnumeration, $enumeration);
259 }
260
261 /**
262 * @test
263 */
264 public function castCastsStringToEnumerationWithCorrespondingValue() {
265 $enumeration = $this->getAccessibleMock(
266 \TYPO3\CMS\Core\Tests\Unit\Type\Fixture\Enumeration\CompleteEnumeration::class,
267 array('dummy'),
268 array('1')
269 );
270 $this->assertSame(1, $enumeration->_get('value'));
271 }
272
273 /**
274 * @test
275 */
276 public function castCastsIntegerToEnumerationWithCorrespondingValue() {
277 $enumeration = $this->getAccessibleMock(
278 \TYPO3\CMS\Core\Tests\Unit\Type\Fixture\Enumeration\CompleteEnumeration::class,
279 array('dummy'),
280 array(1)
281 );
282 $this->assertSame(1, $enumeration->_get('value'));
283 }
284
285 /**
286 * @test
287 */
288 public function equalsReturnsTrueIfIntegerIsGivenThatEqualsEnumerationsIntegerValue() {
289 $enumeration = new Enumeration\CompleteEnumeration(1);
290 $this->assertTrue($enumeration->equals(1));
291 }
292
293 /**
294 * @test
295 */
296 public function equalsReturnsTrueIfStringIsGivenThatEqualsEnumerationsIntegerValue() {
297 $enumeration = new Enumeration\CompleteEnumeration(1);
298 $this->assertTrue($enumeration->equals('1'));
299 }
300
301 /**
302 * @test
303 */
304 public function equalsReturnsTrueIfEqualEnumerationIsGiven() {
305 $enumerationFoo = new Enumeration\CompleteEnumeration(1);
306 $enumerationBar = new Enumeration\CompleteEnumeration(1);
307 $this->assertTrue($enumerationFoo->equals($enumerationBar));
308 }
309
310 /**
311 * @test
312 */
313 public function equalsReturnsTrueIfDifferentEnumerationWithSameValueIsGiven() {
314 $enumerationFoo = new Enumeration\CompleteEnumeration(1);
315 $enumerationBar = new Enumeration\MissingDefaultEnumeration(1);
316 $this->assertTrue($enumerationFoo->equals($enumerationBar));
317 }
318
319 /**
320 * @test
321 */
322 public function equalsReturnsFalseIfDifferentEnumerationWithDifferentValueIsGiven() {
323 $enumerationFoo = new Enumeration\CompleteEnumeration('foo');
324 $enumerationBar = new Enumeration\MissingDefaultEnumeration(1);
325 $this->assertFalse($enumerationFoo->equals($enumerationBar));
326 }
327
328 /**
329 * @test
330 */
331 public function equalsReturnsFalseIfEnumerationOfSameTypeWithDifferentValueIsGiven() {
332 $enumerationFoo = new Enumeration\CompleteEnumeration(1);
333 $enumerationBar = new Enumeration\CompleteEnumeration('foo');
334 $this->assertFalse($enumerationFoo->equals($enumerationBar));
335 }
336
337 }