TYPO3 CMS  TYPO3_8-7
EnumerationTest.php
Go to the documentation of this file.
1 <?php
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 
20 
24 class EnumerationTest extends \TYPO3\TestingFramework\Core\Unit\UnitTestCase
25 {
30  {
31  $this->expectException(InvalidEnumerationValueException::class);
32  $this->expectExceptionCode(1381512753);
33 
34  new Enumeration\MissingConstantsEnumeration();
35  }
36 
41  {
42  $this->expectException(InvalidEnumerationValueException::class);
43  $this->expectExceptionCode(1381512761);
44 
45  new Enumeration\CompleteEnumeration('bar');
46  }
47 
52  {
53  $this->expectException(InvalidEnumerationValueException::class);
54  $this->expectExceptionCode(1381512807);
55 
56  new Enumeration\MissingConstantsEnumeration(2);
57  }
58 
63  {
64  $this->expectException(InvalidEnumerationDefinitionException::class);
65  $this->expectExceptionCode(1381512797);
66 
67  new Enumeration\InvalidConstantEnumeration(1);
68  }
69 
74  {
75  $this->expectException(InvalidEnumerationValueException::class);
76  $this->expectExceptionCode(1381512753);
77 
78  new Enumeration\MissingDefaultEnumeration();
79  }
80 
85  {
86  $this->expectException(InvalidEnumerationDefinitionException::class);
87  $this->expectExceptionCode(1381512859);
88 
89  new Enumeration\DuplicateConstantValueEnumeration(1);
90  }
91 
96  {
97  $enumeration = $this->getAccessibleMock(
98  \TYPO3\CMS\Core\Tests\Unit\Type\Fixture\Enumeration\CompleteEnumeration::class,
99  ['dummy']
100  );
101 
102  $enumClassName = get_class($enumeration);
103 
104  $expectedValue = [
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 
118  public function constructorSetsValue()
119  {
120  $enumeration = $this->getAccessibleMock(
121  \TYPO3\CMS\Core\Tests\Unit\Type\Fixture\Enumeration\CompleteEnumeration::class,
122  ['dummy'],
123  [1]
124  );
125  $this->assertEquals(1, $enumeration->_get('value'));
126  }
127 
131  public function setValueSetsValue()
132  {
133  $enumeration = $this->getAccessibleMock(
134  \TYPO3\CMS\Core\Tests\Unit\Type\Fixture\Enumeration\CompleteEnumeration::class,
135  ['dummy'],
136  [1]
137  );
138  $enumeration->_call('setValue', 'foo');
139  $this->assertEquals('foo', $enumeration->_get('value'));
140  }
141 
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  ['dummy'],
153  [1]
154  );
155  $enumeration->_call('setValue', 2);
156  $this->assertEquals(2, $enumeration->_get('value'));
157  }
158 
163  {
164  return [
165  [
166  1,
167  1,
168  true
169  ],
170  [
171  1,
172  '1',
173  true
174  ],
175  [
176  '1',
177  1,
178  true
179  ],
180  [
181  'a1',
182  1,
183  false
184  ],
185  [
186  1,
187  'a1',
188  false
189  ],
190  [
191  '1a',
192  1,
193  false
194  ],
195  [
196  1,
197  '1a',
198  false
199  ],
200  [
201  'foo',
202  'foo',
203  true
204  ],
205  [
206  'foo',
207  'bar',
208  false
209  ],
210  [
211  'foo',
212  'foobar',
213  false
214  ]
215  ];
216  }
217 
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  ['dummy'],
228  [],
229  $mockName,
230  false
231  );
232  $enumeration->_setStatic('enumConstants', [$mockName => ['CONSTANT_NAME' => $enumerationValue]]);
233  $enumeration->_set('value', $enumerationValue);
234  $this->assertSame($expectation, $enumeration->_call('isValid', $testValue));
235  }
236 
241  {
242  $this->assertEquals(['INTEGER_VALUE' => 1, 'STRING_VALUE' => 'foo'], Enumeration\CompleteEnumeration::getConstants());
243  }
244 
249  {
250  $this->assertEquals(['INTEGER_VALUE' => 1, 'STRING_VALUE' => 'foo', '__default' => 1], Enumeration\CompleteEnumeration::getConstants(true));
251  }
252 
257  {
258  $enumeration = new Enumeration\CompleteEnumeration();
259  $this->assertEquals(['INTEGER_VALUE' => 1, 'STRING_VALUE' => 'foo'], $enumeration->getConstants());
260  }
261 
266  {
267  $enumeration = new Enumeration\CompleteEnumeration();
268  $this->assertSame('1', $enumeration->__toString());
269  }
270 
275  {
276  $enumeration = Enumeration\CompleteEnumeration::cast(1);
277  $this->assertInstanceOf(\TYPO3\CMS\Core\Tests\Unit\Type\Fixture\Enumeration\CompleteEnumeration::class, $enumeration);
278  }
279 
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 
294  {
295  $initialEnumeration = new Enumeration\CompleteEnumeration(1);
296  $enumeration = Enumeration\CompleteEnumeration::cast($initialEnumeration);
297  $this->assertSame($initialEnumeration, $enumeration);
298  }
299 
304  {
305  $enumeration = $this->getAccessibleMock(
306  \TYPO3\CMS\Core\Tests\Unit\Type\Fixture\Enumeration\CompleteEnumeration::class,
307  ['dummy'],
308  ['1']
309  );
310  $this->assertSame(1, $enumeration->_get('value'));
311  }
312 
317  {
318  $enumeration = $this->getAccessibleMock(
319  \TYPO3\CMS\Core\Tests\Unit\Type\Fixture\Enumeration\CompleteEnumeration::class,
320  ['dummy'],
321  [1]
322  );
323  $this->assertSame(1, $enumeration->_get('value'));
324  }
325 
330  {
331  $enumeration = new Enumeration\CompleteEnumeration(1);
332  $this->assertTrue($enumeration->equals(1));
333  }
334 
339  {
340  $enumeration = new Enumeration\CompleteEnumeration(1);
341  $this->assertTrue($enumeration->equals('1'));
342  }
343 
348  {
349  $enumerationFoo = new Enumeration\CompleteEnumeration(1);
350  $enumerationBar = new Enumeration\CompleteEnumeration(1);
351  $this->assertTrue($enumerationFoo->equals($enumerationBar));
352  }
353 
358  {
359  $enumerationFoo = new Enumeration\CompleteEnumeration(1);
360  $enumerationBar = new Enumeration\MissingDefaultEnumeration(1);
361  $this->assertTrue($enumerationFoo->equals($enumerationBar));
362  }
363 
368  {
369  $enumerationFoo = new Enumeration\CompleteEnumeration('foo');
370  $enumerationBar = new Enumeration\MissingDefaultEnumeration(1);
371  $this->assertFalse($enumerationFoo->equals($enumerationBar));
372  }
373 
378  {
379  $enumerationFoo = new Enumeration\CompleteEnumeration(1);
380  $enumerationBar = new Enumeration\CompleteEnumeration('foo');
381  $this->assertFalse($enumerationFoo->equals($enumerationBar));
382  }
383 
388  {
389  $result = Enumeration\CompleteEnumeration::getName(Enumeration\CompleteEnumeration::INTEGER_VALUE);
390  $this->assertSame('INTEGER_VALUE', $result);
391  }
392 
397  {
398  $result = Enumeration\CompleteEnumeration::getName(42);
399  $this->assertSame('', $result);
400  }
401 
406  {
407  $result = Enumeration\CompleteEnumeration::getHumanReadableName(Enumeration\CompleteEnumeration::INTEGER_VALUE);
408  $this->assertSame('Integer Value', $result);
409  }
410 
415  {
416  $result = Enumeration\CompleteEnumeration::getName(42);
417  $this->assertSame('', $result);
418  }
419 }
static getConstants($include_default=false)
isValidDoesTypeLooseComparison($enumerationValue, $testValue, $expectation)