[BUGFIX] Follow-Up: Fix major bug in Enumeration::equals + Tests
[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 * Copyright notice
6 *
7 * (c) 2013 Sascha Egerer <sascha.egerer@dkd.de>
8 * All rights reserved
9 *
10 * This script is part of the TYPO3 project. The TYPO3 project is
11 * free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * The GNU General Public License can be found at
17 * http://www.gnu.org/copyleft/gpl.html.
18 *
19 * This script is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
23 *
24 * This copyright notice MUST APPEAR in all copies of the script!
25 ***************************************************************/
26
27 use TYPO3\CMS\Core\Tests\Unit\Type\Fixture\Enumeration;
28 use TYPO3\CMS\Core\Type;
29
30 /**
31 * Testcase for class \TYPO3\CMS\Core\Type\Enumeration
32 */
33 class EnumerationTest extends \TYPO3\CMS\Core\Tests\UnitTestCase {
34
35 /**
36 * @test
37 * @expectedException \TYPO3\CMS\Core\Type\Exception\InvalidEnumerationValueException
38 */
39 public function constructThrowsExceptionIfNoConstantsAreDefined() {
40 new Enumeration\MissingConstantsEnumeration();
41 }
42
43 /**
44 * @test
45 * @expectedException \TYPO3\CMS\Core\Type\Exception\InvalidEnumerationValueException
46 */
47 public function loadValuesThrowsExceptionIfGivenValueIsNotAvailableInEnumeration() {
48 new Enumeration\MissingConstantsEnumeration(2);
49 }
50
51 /**
52 * @test
53 * @expectedException \TYPO3\CMS\Core\Type\Exception\InvalidEnumerationDefinitionException
54 */
55 public function loadValuesThrowsExceptionIfDisallowedTypeIsDefinedAsConstant() {
56 new Enumeration\InvalidConstantEnumeration(1);
57 }
58
59 /**
60 * @test
61 * @expectedException \TYPO3\CMS\Core\Type\Exception\InvalidEnumerationValueException
62 */
63 public function loadValuesThrowsExceptionIfNoDefaultConstantIsDefinedAndNoValueIsGiven() {
64 new Enumeration\MissingDefaultEnumeration();
65 }
66
67 /**
68 * @test
69 * @expectedException \TYPO3\CMS\Core\Type\Exception\InvalidEnumerationDefinitionException
70 */
71 public function loadValuesThrowsExceptionIfValueIsDefinedMultipleTimes() {
72 new Enumeration\DuplicateConstantValueEnumeration(1);
73 }
74
75 /**
76 * @test
77 */
78 public function loadValuesSetsStaticEnumConstants() {
79 $enumeration = $this->getAccessibleMock(
80 'TYPO3\\CMS\\Core\\Tests\\Unit\\Type\Fixture\\Enumeration\\CompleteEnumeration',
81 array('dummy')
82 );
83
84 $expectedValue = array(
85 get_class($enumeration) => array(
86 'INTEGER_VALUE' => 1,
87 'STRING_VALUE' => 'foo',
88 '__default' => 1
89 )
90 );
91
92 $this->assertEquals($expectedValue, $enumeration->_getStatic('enumConstants'));
93 }
94
95 /**
96 * @test
97 */
98 public function constructerSetsValue() {
99 $enumeration = $this->getAccessibleMock(
100 'TYPO3\\CMS\\Core\\Tests\\Unit\\Type\Fixture\\Enumeration\\CompleteEnumeration',
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',
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',
127 array('dummy'),
128 array(1)
129 );
130 $enumeration->_call('setValue', 2);
131 $this->assertEquals(2, $enumeration->_get('value'));
132 }
133
134 /**
135 * @test
136 */
137 public function isValueReturnsTrueIfValueIsValid() {
138 $enumeration = $this->getAccessibleMock(
139 'TYPO3\\CMS\\Core\\Tests\\Unit\\Type\Fixture\\Enumeration\\CompleteEnumeration',
140 array('dummy')
141 );
142 $this->assertTrue($enumeration->_call('isValid', 1));
143 }
144
145 /**
146 * @test
147 */
148 public function isValueReturnsFalseIfValueIsValid() {
149 $enumeration = $this->getAccessibleMock(
150 'TYPO3\\CMS\\Core\\Tests\\Unit\\Type\Fixture\\Enumeration\\CompleteEnumeration',
151 array('dummy')
152 );
153 $this->assertFalse($enumeration->_call('isValid', 2));
154 }
155
156 /**
157 * @test
158 */
159 public function getConstantsReturnsArrayOfPossibleValuesWithoutDefault() {
160 $enumeration = new Enumeration\CompleteEnumeration();
161
162 $this->assertEquals(array('INTEGER_VALUE' => 1, 'STRING_VALUE' => 'foo'), $enumeration->getConstants());
163 }
164
165 /**
166 * @test
167 */
168 public function getConstantsReturnsArrayOfPossibleValuesWithDefaultIfRequested() {
169 $enumeration = new Enumeration\CompleteEnumeration();
170 $this->assertEquals(array('INTEGER_VALUE' => 1, 'STRING_VALUE' => 'foo', '__default' => 1), $enumeration->getConstants(TRUE));
171 }
172
173 /**
174 * @test
175 */
176 public function toStringReturnsValueAsString() {
177 $enumeration = new Enumeration\CompleteEnumeration();
178 $this->assertSame('1', $enumeration->__toString());
179 }
180
181 /**
182 * @test
183 */
184 public function castReturnsObjectOfEnumerationTypeIfSimpleValueIsGiven() {
185 $enumeration = Enumeration\CompleteEnumeration::cast(1);
186 $this->assertInstanceOf('TYPO3\\CMS\\Core\\Tests\\Unit\\Type\Fixture\\Enumeration\\CompleteEnumeration', $enumeration);
187 }
188
189 /**
190 * @test
191 */
192 public function castReturnsObjectOfCalledEnumerationTypeIfCalledWithValueOfDifferentType() {
193 $initialEnumeration = new Enumeration\MissingDefaultEnumeration(1);
194 $enumeration = Enumeration\CompleteEnumeration::cast($initialEnumeration);
195 $this->assertInstanceOf('TYPO3\\CMS\\Core\\Tests\\Unit\\Type\Fixture\\Enumeration\\CompleteEnumeration', $enumeration);
196 }
197
198 /**
199 * @test
200 */
201 public function castReturnsGivenObjectIfCalledWithValueOfSameType() {
202 $initialEnumeration = new Enumeration\CompleteEnumeration(1);
203 $enumeration = Enumeration\CompleteEnumeration::cast($initialEnumeration);
204 $this->assertSame($initialEnumeration, $enumeration);
205 }
206
207 /**
208 * @test
209 */
210 public function castCastsStringToEnumerationWithCorrespondingValue() {
211 $enumeration = $this->getAccessibleMock(
212 'TYPO3\\CMS\\Core\\Tests\\Unit\\Type\Fixture\\Enumeration\\CompleteEnumeration',
213 array('dummy'),
214 array('1')
215 );
216 $this->assertSame(1, $enumeration->_get('value'));
217 }
218
219 /**
220 * @test
221 */
222 public function castCastsIntegerToEnumerationWithCorrespondingValue() {
223 $enumeration = $this->getAccessibleMock(
224 'TYPO3\\CMS\\Core\\Tests\\Unit\\Type\Fixture\\Enumeration\\CompleteEnumeration',
225 array('dummy'),
226 array(1)
227 );
228 $this->assertSame(1, $enumeration->_get('value'));
229 }
230
231 /**
232 * @test
233 */
234 public function equalsReturnsTrueIfIntegerIsGivenThatEqualsEnumerationsIntegerValue() {
235 $enumeration = new Enumeration\CompleteEnumeration(1);
236 $this->assertTrue($enumeration->equals(1));
237 }
238
239 /**
240 * @test
241 */
242 public function equalsReturnsTrueIfStringIsGivenThatEqualsEnumerationsIntegerValue() {
243 $enumeration = new Enumeration\CompleteEnumeration(1);
244 $this->assertTrue($enumeration->equals('1'));
245 }
246
247 /**
248 * @test
249 */
250 public function equalsReturnsTrueIfEqualEnumerationIsGiven() {
251 $enumerationFoo = new Enumeration\CompleteEnumeration(1);
252 $enumerationBar = new Enumeration\CompleteEnumeration(1);
253 $this->assertTrue($enumerationFoo->equals($enumerationBar));
254 }
255
256 /**
257 * @test
258 */
259 public function equalsReturnsTrueIfDifferentEnumerationWithSameValueIsGiven() {
260 $enumerationFoo = new Enumeration\CompleteEnumeration(1);
261 $enumerationBar = new Enumeration\MissingDefaultEnumeration(1);
262 $this->assertTrue($enumerationFoo->equals($enumerationBar));
263 }
264
265 /**
266 * @test
267 */
268 public function equalsReturnsFalseIfDifferentEnumerationWithDifferentValueIsGiven() {
269 $enumerationFoo = new Enumeration\CompleteEnumeration('foo');
270 $enumerationBar = new Enumeration\MissingDefaultEnumeration(1);
271 $this->assertFalse($enumerationFoo->equals($enumerationBar));
272 }
273
274 /**
275 * @test
276 */
277 public function equalsReturnsFalseIfEnumerationOfSameTypeWithDifferentValueIsGiven() {
278 $enumerationFoo = new Enumeration\CompleteEnumeration(1);
279 $enumerationBar = new Enumeration\CompleteEnumeration('foo');
280 $this->assertFalse($enumerationFoo->equals($enumerationBar));
281 }
282
283 }