‪TYPO3CMS  10.4
EnumerationTest.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
4 
5 /*
6  * This file is part of the TYPO3 CMS project.
7  *
8  * It is free software; you can redistribute it and/or modify it under
9  * the terms of the GNU General Public License, either version 2
10  * of the License, or any later version.
11  *
12  * For the full copyright and license information, please read the
13  * LICENSE.txt file that was distributed with this source code.
14  *
15  * The TYPO3 project - inspiring people to share!
16  */
17 
19 
27 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
28 
32 class ‪EnumerationTest extends UnitTestCase
33 {
38  {
39  $this->expectException(InvalidEnumerationValueException::class);
40  $this->expectExceptionCode(1381512753);
41 
43  }
44 
49  {
50  $this->expectException(InvalidEnumerationValueException::class);
51  $this->expectExceptionCode(1381512761);
52 
53  new ‪CompleteEnumeration('bar');
54  }
55 
60  {
61  $this->expectException(InvalidEnumerationValueException::class);
62  $this->expectExceptionCode(1381512807);
63 
65  }
66 
71  {
72  $this->expectException(InvalidEnumerationDefinitionException::class);
73  $this->expectExceptionCode(1381512797);
74 
76  }
77 
82  {
83  $this->expectException(InvalidEnumerationValueException::class);
84  $this->expectExceptionCode(1381512753);
85 
87  }
88 
93  {
94  $this->expectException(InvalidEnumerationDefinitionException::class);
95  $this->expectExceptionCode(1381512859);
96 
98  }
99 
104  public function ‪looseEnumerationValues(): array
105  {
106  return [
107  [
108  1,
110  ],
111  [
112  '1',
114  ],
115  [
116  2,
118  ],
119  [
120  '2',
122  ],
123  [
124  'foo',
126  ],
127  ];
128  }
129 
136  public function ‪doesTypeLooseComparison($testValue, $expectedValue): void
137  {
138  $value = new ‪CompleteEnumeration($testValue);
139 
140  self::assertEquals((string)$expectedValue, (string)$value);
141  }
142 
147  {
148  $expected = [
149  'INTEGER_VALUE' => 1,
150  'STRING_INTEGER_VALUE' => '2',
151  'STRING_VALUE' => 'foo',
152  ];
153 
154  self::assertEquals($expected, ‪CompleteEnumeration::getConstants());
155  }
156 
161  {
162  $expected = [
163  'INTEGER_VALUE' => 1,
164  'STRING_INTEGER_VALUE' => '2',
165  'STRING_VALUE' => 'foo',
166  '__default' => 1,
167  ];
168 
169  self::assertEquals($expected, ‪CompleteEnumeration::getConstants(true));
170  }
171 
176  {
177  $enumeration = new ‪CompleteEnumeration();
178  $expected = [
179  'INTEGER_VALUE' => 1,
180  'STRING_INTEGER_VALUE' => '2',
181  'STRING_VALUE' => 'foo',
182  ];
183 
184  self::assertEquals($expected, $enumeration::getConstants());
185  }
186 
190  public function ‪toStringReturnsValueAsString(): void
191  {
192  $enumeration = new ‪CompleteEnumeration();
193  self::assertSame('1', $enumeration->__toString());
194  }
195 
200  {
201  $enumeration = ‪CompleteEnumeration::cast(1);
202  self::assertInstanceOf(CompleteEnumeration::class, $enumeration);
203  }
204 
209  {
210  $initialEnumeration = new ‪MissingDefaultEnumeration(1);
211  $enumeration = ‪CompleteEnumeration::cast($initialEnumeration);
212  self::assertInstanceOf(CompleteEnumeration::class, $enumeration);
213  }
214 
219  {
220  $initialEnumeration = new ‪CompleteEnumeration(1);
221  $enumeration = ‪CompleteEnumeration::cast($initialEnumeration);
222  self::assertSame($initialEnumeration, $enumeration);
223  }
224 
229  {
231 
232  self::assertSame(‪CompleteEnumeration::STRING_VALUE, (string)$value);
233  }
234 
239  {
241 
242  self::assertSame((int)(string)‪CompleteEnumeration::INTEGER_VALUE, (int)(string)$value);
243  }
244 
249  {
250  $enumeration = new ‪CompleteEnumeration(1);
251  self::assertTrue($enumeration->equals(1));
252  }
253 
258  {
259  $enumeration = new ‪CompleteEnumeration(1);
260  self::assertTrue($enumeration->equals('1'));
261  }
262 
267  {
268  $enumerationFoo = new ‪CompleteEnumeration(1);
269  $enumerationBar = new ‪CompleteEnumeration(1);
270  self::assertTrue($enumerationFoo->equals($enumerationBar));
271  }
272 
277  {
278  $enumerationFoo = new ‪CompleteEnumeration(1);
279  $enumerationBar = new ‪MissingDefaultEnumeration(1);
280  self::assertTrue($enumerationFoo->equals($enumerationBar));
281  }
282 
287  {
288  $enumerationFoo = new ‪CompleteEnumeration('foo');
289  $enumerationBar = new ‪MissingDefaultEnumeration(1);
290  self::assertFalse($enumerationFoo->equals($enumerationBar));
291  }
292 
297  {
298  $enumerationFoo = new ‪CompleteEnumeration(1);
299  $enumerationBar = new ‪CompleteEnumeration('foo');
300  self::assertFalse($enumerationFoo->equals($enumerationBar));
301  }
302 
307  {
309  self::assertSame('INTEGER_VALUE', $result);
310  }
311 
316  {
317  $result = ‪CompleteEnumeration::getName(42);
318  self::assertSame('', $result);
319  }
320 
325  {
327  self::assertSame('Integer Value', $result);
328  }
329 
334  {
335  $result = ‪CompleteEnumeration::getName(42);
336  self::assertSame('', $result);
337  }
338 }
‪TYPO3\CMS\Core\Tests\Unit\Type\EnumerationTest\getNameReturnsEmptyStringForNotAvailableConstant
‪getNameReturnsEmptyStringForNotAvailableConstant()
Definition: EnumerationTest.php:315
‪TYPO3\CMS\Core\Tests\Unit\Type\EnumerationTest\loadValuesThrowsExceptionIfGivenValueIsNotAvailableInEnumeration
‪loadValuesThrowsExceptionIfGivenValueIsNotAvailableInEnumeration()
Definition: EnumerationTest.php:59
‪TYPO3\CMS\Core\Tests\Unit\Type\EnumerationTest\getConstantsReturnsArrayOfPossibleValuesWithDefaultIfRequested
‪getConstantsReturnsArrayOfPossibleValuesWithDefaultIfRequested()
Definition: EnumerationTest.php:160
‪TYPO3\CMS\Core\Tests\Unit\Type\Fixture\Enumeration\CompleteEnumeration\STRING_INTEGER_VALUE
‪const STRING_INTEGER_VALUE
Definition: CompleteEnumeration.php:27
‪TYPO3\CMS\Core\Tests\Unit\Type\EnumerationTest\loadValuesThrowsExceptionIfValueIsDefinedMultipleTimes
‪loadValuesThrowsExceptionIfValueIsDefinedMultipleTimes()
Definition: EnumerationTest.php:92
‪TYPO3\CMS\Core\Tests\Unit\Type\EnumerationTest\castCastsStringToEnumerationWithCorrespondingValue
‪castCastsStringToEnumerationWithCorrespondingValue()
Definition: EnumerationTest.php:228
‪TYPO3\CMS\Core\Tests\Unit\Type\EnumerationTest\castReturnsGivenObjectIfCalledWithValueOfSameType
‪castReturnsGivenObjectIfCalledWithValueOfSameType()
Definition: EnumerationTest.php:218
‪TYPO3\CMS\Core\Tests\Unit\Type\EnumerationTest\getNameProvidesNameForAvailableConstant
‪getNameProvidesNameForAvailableConstant()
Definition: EnumerationTest.php:306
‪TYPO3\CMS\Core\Tests\Unit\Type\Fixture\Enumeration\InvalidConstantEnumeration
Definition: InvalidConstantEnumeration.php:24
‪TYPO3\CMS\Core\Tests\Unit\Type\EnumerationTest\doesTypeLooseComparison
‪doesTypeLooseComparison($testValue, $expectedValue)
Definition: EnumerationTest.php:136
‪TYPO3\CMS\Core\Tests\Unit\Type\EnumerationTest\getHumanReadableNameProvidesNameForAvailableConstant
‪getHumanReadableNameProvidesNameForAvailableConstant()
Definition: EnumerationTest.php:324
‪TYPO3\CMS\Core\Tests\Unit\Type\Fixture\Enumeration\CompleteEnumeration\INTEGER_VALUE
‪const INTEGER_VALUE
Definition: CompleteEnumeration.php:26
‪TYPO3\CMS\Core\Tests\Unit\Type\EnumerationTest\looseEnumerationValues
‪array looseEnumerationValues()
Definition: EnumerationTest.php:104
‪TYPO3\CMS\Core\Tests\Unit\Type\EnumerationTest\equalsReturnsFalseIfEnumerationOfSameTypeWithDifferentValueIsGiven
‪equalsReturnsFalseIfEnumerationOfSameTypeWithDifferentValueIsGiven()
Definition: EnumerationTest.php:296
‪TYPO3\CMS\Core\Tests\Unit\Type\EnumerationTest\loadValuesThrowsExceptionIfDisallowedTypeIsDefinedAsConstant
‪loadValuesThrowsExceptionIfDisallowedTypeIsDefinedAsConstant()
Definition: EnumerationTest.php:70
‪TYPO3\CMS\Core\Tests\Unit\Type\EnumerationTest\castReturnsObjectOfCalledEnumerationTypeIfCalledWithValueOfDifferentType
‪castReturnsObjectOfCalledEnumerationTypeIfCalledWithValueOfDifferentType()
Definition: EnumerationTest.php:208
‪TYPO3\CMS\Core\Tests\Unit\Type\EnumerationTest\toStringReturnsValueAsString
‪toStringReturnsValueAsString()
Definition: EnumerationTest.php:190
‪TYPO3\CMS\Core\Tests\Unit\Type\Fixture\Enumeration\MissingConstantsEnumeration
Definition: MissingConstantsEnumeration.php:24
‪TYPO3\CMS\Core\Type\Enumeration\cast
‪static static cast($value)
Definition: Enumeration.php:186
‪TYPO3\CMS\Core\Tests\Unit\Type\EnumerationTest\getHumanReadableNameReturnsEmptyStringForNotAvailableConstant
‪getHumanReadableNameReturnsEmptyStringForNotAvailableConstant()
Definition: EnumerationTest.php:333
‪TYPO3\CMS\Core\Tests\Unit\Type\Fixture\Enumeration\MissingDefaultEnumeration
Definition: MissingDefaultEnumeration.php:24
‪TYPO3\CMS\Core\Tests\Unit\Type
Definition: BitSetTest.php:18
‪TYPO3\CMS\Core\Tests\Unit\Type\EnumerationTest\equalsReturnsTrueIfIntegerIsGivenThatEqualsEnumerationsIntegerValue
‪equalsReturnsTrueIfIntegerIsGivenThatEqualsEnumerationsIntegerValue()
Definition: EnumerationTest.php:248
‪TYPO3\CMS\Core\Tests\Unit\Type\EnumerationTest\castReturnsObjectOfEnumerationTypeIfSimpleValueIsGiven
‪castReturnsObjectOfEnumerationTypeIfSimpleValueIsGiven()
Definition: EnumerationTest.php:199
‪TYPO3\CMS\Core\Tests\Unit\Type\Fixture\Enumeration\CompleteEnumeration\STRING_VALUE
‪const STRING_VALUE
Definition: CompleteEnumeration.php:28
‪TYPO3\CMS\Core\Tests\Unit\Type\EnumerationTest\getConstantsCanBeCalledOnInstances
‪getConstantsCanBeCalledOnInstances()
Definition: EnumerationTest.php:175
‪TYPO3\CMS\Core\Tests\Unit\Type\EnumerationTest\loadValuesThrowsExceptionIfNoDefaultConstantIsDefinedAndNoValueIsGiven
‪loadValuesThrowsExceptionIfNoDefaultConstantIsDefinedAndNoValueIsGiven()
Definition: EnumerationTest.php:81
‪TYPO3\CMS\Core\Tests\Unit\Type\EnumerationTest\getConstantsReturnsArrayOfPossibleValuesWithoutDefault
‪getConstantsReturnsArrayOfPossibleValuesWithoutDefault()
Definition: EnumerationTest.php:146
‪TYPO3\CMS\Core\Tests\Unit\Type\EnumerationTest\equalsReturnsTrueIfEqualEnumerationIsGiven
‪equalsReturnsTrueIfEqualEnumerationIsGiven()
Definition: EnumerationTest.php:266
‪TYPO3\CMS\Core\Type\Enumeration\getHumanReadableName
‪static string getHumanReadableName($value)
Definition: Enumeration.php:236
‪TYPO3\CMS\Core\Tests\Unit\Type\Fixture\Enumeration\DuplicateConstantValueEnumeration
Definition: DuplicateConstantValueEnumeration.php:24
‪TYPO3\CMS\Core\Type\Enumeration\getConstants
‪static array getConstants($include_default=false)
Definition: Enumeration.php:170
‪TYPO3\CMS\Core\Tests\Unit\Type\EnumerationTest\constructorThrowsExceptionIfInvalidValueIsRequested
‪constructorThrowsExceptionIfInvalidValueIsRequested()
Definition: EnumerationTest.php:48
‪TYPO3\CMS\Core\Type\Exception\InvalidEnumerationValueException
Definition: InvalidEnumerationValueException.php:24
‪TYPO3\CMS\Core\Tests\Unit\Type\EnumerationTest\equalsReturnsTrueIfStringIsGivenThatEqualsEnumerationsIntegerValue
‪equalsReturnsTrueIfStringIsGivenThatEqualsEnumerationsIntegerValue()
Definition: EnumerationTest.php:257
‪TYPO3\CMS\Core\Type\Enumeration\getName
‪static string getName($value)
Definition: Enumeration.php:220
‪TYPO3\CMS\Core\Type\Exception\InvalidEnumerationDefinitionException
Definition: InvalidEnumerationDefinitionException.php:24
‪TYPO3\CMS\Core\Tests\Unit\Type\EnumerationTest\equalsReturnsTrueIfDifferentEnumerationWithSameValueIsGiven
‪equalsReturnsTrueIfDifferentEnumerationWithSameValueIsGiven()
Definition: EnumerationTest.php:276
‪TYPO3\CMS\Core\Tests\Unit\Type\Fixture\Enumeration\CompleteEnumeration
Definition: CompleteEnumeration.php:24
‪TYPO3\CMS\Core\Tests\Unit\Type\EnumerationTest\castCastsIntegerToEnumerationWithCorrespondingValue
‪castCastsIntegerToEnumerationWithCorrespondingValue()
Definition: EnumerationTest.php:238
‪TYPO3\CMS\Core\Tests\Unit\Type\EnumerationTest\constructorThrowsExceptionIfNoConstantsAreDefined
‪constructorThrowsExceptionIfNoConstantsAreDefined()
Definition: EnumerationTest.php:37
‪TYPO3\CMS\Core\Tests\Unit\Type\EnumerationTest\equalsReturnsFalseIfDifferentEnumerationWithDifferentValueIsGiven
‪equalsReturnsFalseIfDifferentEnumerationWithDifferentValueIsGiven()
Definition: EnumerationTest.php:286
‪TYPO3\CMS\Core\Tests\Unit\Type\EnumerationTest
Definition: EnumerationTest.php:33