‪TYPO3CMS  ‪main
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 
29 final class ‪EnumerationTest extends UnitTestCase
30 {
35  {
36  $this->expectException(InvalidEnumerationValueException::class);
37  $this->expectExceptionCode(1381512753);
38 
40  }
41 
46  {
47  $this->expectException(InvalidEnumerationValueException::class);
48  $this->expectExceptionCode(1381512761);
49 
50  new ‪CompleteEnumeration('bar');
51  }
52 
57  {
58  $this->expectException(InvalidEnumerationValueException::class);
59  $this->expectExceptionCode(1381512807);
60 
62  }
63 
68  {
69  $this->expectException(InvalidEnumerationDefinitionException::class);
70  $this->expectExceptionCode(1381512797);
71 
73  }
74 
79  {
80  $this->expectException(InvalidEnumerationValueException::class);
81  $this->expectExceptionCode(1381512753);
82 
84  }
85 
90  {
91  $this->expectException(InvalidEnumerationDefinitionException::class);
92  $this->expectExceptionCode(1381512859);
93 
95  }
96 
100  public static function ‪looseEnumerationValues(): array
101  {
102  return [
103  [
104  1,
106  ],
107  [
108  '1',
110  ],
111  [
112  2,
114  ],
115  [
116  '2',
118  ],
119  [
120  'foo',
122  ],
123  ];
124  }
125 
130  public function ‪doesTypeLooseComparison(string|int $testValue, string|int $expectedValue): void
131  {
132  $value = new ‪CompleteEnumeration($testValue);
133 
134  self::assertEquals((string)$expectedValue, (string)$value);
135  }
136 
141  {
142  $expected = [
143  'INTEGER_VALUE' => 1,
144  'STRING_INTEGER_VALUE' => '2',
145  'STRING_VALUE' => 'foo',
146  ];
147 
148  self::assertEquals($expected, ‪CompleteEnumeration::getConstants());
149  }
150 
155  {
156  $expected = [
157  'INTEGER_VALUE' => 1,
158  'STRING_INTEGER_VALUE' => '2',
159  'STRING_VALUE' => 'foo',
160  '__default' => 1,
161  ];
162 
163  self::assertEquals($expected, ‪CompleteEnumeration::getConstants(true));
164  }
165 
170  {
171  $enumeration = new ‪CompleteEnumeration();
172  $expected = [
173  'INTEGER_VALUE' => 1,
174  'STRING_INTEGER_VALUE' => '2',
175  'STRING_VALUE' => 'foo',
176  ];
177 
178  self::assertEquals($expected, $enumeration::getConstants());
179  }
180 
184  public function ‪toStringReturnsValueAsString(): void
185  {
186  $enumeration = new ‪CompleteEnumeration();
187  self::assertSame('1', $enumeration->__toString());
188  }
189 
194  {
195  $enumeration = ‪CompleteEnumeration::cast(1);
196  self::assertInstanceOf(CompleteEnumeration::class, $enumeration);
197  }
198 
203  {
204  $initialEnumeration = new ‪MissingDefaultEnumeration(1);
205  $enumeration = ‪CompleteEnumeration::cast($initialEnumeration);
206  self::assertInstanceOf(CompleteEnumeration::class, $enumeration);
207  }
208 
213  {
214  $initialEnumeration = new ‪CompleteEnumeration(1);
215  $enumeration = ‪CompleteEnumeration::cast($initialEnumeration);
216  self::assertSame($initialEnumeration, $enumeration);
217  }
218 
223  {
225 
226  self::assertSame(‪CompleteEnumeration::STRING_VALUE, (string)$value);
227  }
228 
233  {
235 
236  self::assertSame((int)(string)‪CompleteEnumeration::INTEGER_VALUE, (int)(string)$value);
237  }
238 
243  {
244  $enumeration = new ‪CompleteEnumeration(1);
245  self::assertTrue($enumeration->equals(1));
246  }
247 
252  {
253  $enumeration = new ‪CompleteEnumeration(1);
254  self::assertTrue($enumeration->equals('1'));
255  }
256 
261  {
262  $enumerationFoo = new ‪CompleteEnumeration(1);
263  $enumerationBar = new ‪CompleteEnumeration(1);
264  self::assertTrue($enumerationFoo->equals($enumerationBar));
265  }
266 
271  {
272  $enumerationFoo = new ‪CompleteEnumeration(1);
273  $enumerationBar = new ‪MissingDefaultEnumeration(1);
274  self::assertTrue($enumerationFoo->equals($enumerationBar));
275  }
276 
281  {
282  $enumerationFoo = new ‪CompleteEnumeration('foo');
283  $enumerationBar = new ‪MissingDefaultEnumeration(1);
284  self::assertFalse($enumerationFoo->equals($enumerationBar));
285  }
286 
291  {
292  $enumerationFoo = new ‪CompleteEnumeration(1);
293  $enumerationBar = new ‪CompleteEnumeration('foo');
294  self::assertFalse($enumerationFoo->equals($enumerationBar));
295  }
296 
301  {
303  self::assertSame('INTEGER_VALUE', $result);
304  }
305 
310  {
311  $result = ‪CompleteEnumeration::getName(42);
312  self::assertSame('', $result);
313  }
314 
319  {
321  self::assertSame('Integer Value', $result);
322  }
323 
328  {
329  $result = ‪CompleteEnumeration::getName(42);
330  self::assertSame('', $result);
331  }
332 }
‪TYPO3\CMS\Core\Tests\Unit\Type\EnumerationTest\getNameReturnsEmptyStringForNotAvailableConstant
‪getNameReturnsEmptyStringForNotAvailableConstant()
Definition: EnumerationTest.php:309
‪TYPO3\CMS\Core\Tests\Unit\Type\EnumerationTest\loadValuesThrowsExceptionIfGivenValueIsNotAvailableInEnumeration
‪loadValuesThrowsExceptionIfGivenValueIsNotAvailableInEnumeration()
Definition: EnumerationTest.php:56
‪TYPO3\CMS\Core\Tests\Unit\Type\EnumerationTest\getConstantsReturnsArrayOfPossibleValuesWithDefaultIfRequested
‪getConstantsReturnsArrayOfPossibleValuesWithDefaultIfRequested()
Definition: EnumerationTest.php:154
‪TYPO3\CMS\Core\Tests\Unit\Type\Fixture\Enumeration\CompleteEnumeration\STRING_INTEGER_VALUE
‪const STRING_INTEGER_VALUE
Definition: CompleteEnumeration.php:29
‪TYPO3\CMS\Core\Tests\Unit\Type\EnumerationTest\loadValuesThrowsExceptionIfValueIsDefinedMultipleTimes
‪loadValuesThrowsExceptionIfValueIsDefinedMultipleTimes()
Definition: EnumerationTest.php:89
‪TYPO3\CMS\Core\Tests\Unit\Type\EnumerationTest\castCastsStringToEnumerationWithCorrespondingValue
‪castCastsStringToEnumerationWithCorrespondingValue()
Definition: EnumerationTest.php:222
‪TYPO3\CMS\Core\Tests\Unit\Type\EnumerationTest\castReturnsGivenObjectIfCalledWithValueOfSameType
‪castReturnsGivenObjectIfCalledWithValueOfSameType()
Definition: EnumerationTest.php:212
‪TYPO3\CMS\Core\Tests\Unit\Type\EnumerationTest\getNameProvidesNameForAvailableConstant
‪getNameProvidesNameForAvailableConstant()
Definition: EnumerationTest.php:300
‪TYPO3\CMS\Core\Tests\Unit\Type\Fixture\Enumeration\InvalidConstantEnumeration
Definition: InvalidConstantEnumeration.php:26
‪TYPO3\CMS\Core\Tests\Unit\Type\EnumerationTest\getHumanReadableNameProvidesNameForAvailableConstant
‪getHumanReadableNameProvidesNameForAvailableConstant()
Definition: EnumerationTest.php:318
‪TYPO3\CMS\Core\Tests\Unit\Type\Fixture\Enumeration\CompleteEnumeration\INTEGER_VALUE
‪const INTEGER_VALUE
Definition: CompleteEnumeration.php:28
‪TYPO3\CMS\Core\Tests\Unit\Type\EnumerationTest\equalsReturnsFalseIfEnumerationOfSameTypeWithDifferentValueIsGiven
‪equalsReturnsFalseIfEnumerationOfSameTypeWithDifferentValueIsGiven()
Definition: EnumerationTest.php:290
‪TYPO3\CMS\Core\Tests\Unit\Type\EnumerationTest\loadValuesThrowsExceptionIfDisallowedTypeIsDefinedAsConstant
‪loadValuesThrowsExceptionIfDisallowedTypeIsDefinedAsConstant()
Definition: EnumerationTest.php:67
‪TYPO3\CMS\Core\Tests\Unit\Type\EnumerationTest\castReturnsObjectOfCalledEnumerationTypeIfCalledWithValueOfDifferentType
‪castReturnsObjectOfCalledEnumerationTypeIfCalledWithValueOfDifferentType()
Definition: EnumerationTest.php:202
‪TYPO3\CMS\Core\Tests\Unit\Type\EnumerationTest\toStringReturnsValueAsString
‪toStringReturnsValueAsString()
Definition: EnumerationTest.php:184
‪TYPO3\CMS\Core\Tests\Unit\Type\EnumerationTest\looseEnumerationValues
‪static looseEnumerationValues()
Definition: EnumerationTest.php:100
‪TYPO3\CMS\Core\Tests\Unit\Type\Fixture\Enumeration\MissingConstantsEnumeration
Definition: MissingConstantsEnumeration.php:25
‪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:327
‪TYPO3\CMS\Core\Tests\Unit\Type\EnumerationTest\doesTypeLooseComparison
‪doesTypeLooseComparison(string|int $testValue, string|int $expectedValue)
Definition: EnumerationTest.php:130
‪TYPO3\CMS\Core\Tests\Unit\Type\Fixture\Enumeration\MissingDefaultEnumeration
Definition: MissingDefaultEnumeration.php:26
‪TYPO3\CMS\Core\Tests\Unit\Type
Definition: BitSetTest.php:18
‪TYPO3\CMS\Core\Tests\Unit\Type\EnumerationTest\equalsReturnsTrueIfIntegerIsGivenThatEqualsEnumerationsIntegerValue
‪equalsReturnsTrueIfIntegerIsGivenThatEqualsEnumerationsIntegerValue()
Definition: EnumerationTest.php:242
‪TYPO3\CMS\Core\Tests\Unit\Type\EnumerationTest\castReturnsObjectOfEnumerationTypeIfSimpleValueIsGiven
‪castReturnsObjectOfEnumerationTypeIfSimpleValueIsGiven()
Definition: EnumerationTest.php:193
‪TYPO3\CMS\Core\Tests\Unit\Type\Fixture\Enumeration\CompleteEnumeration\STRING_VALUE
‪const STRING_VALUE
Definition: CompleteEnumeration.php:30
‪TYPO3\CMS\Core\Tests\Unit\Type\EnumerationTest\getConstantsCanBeCalledOnInstances
‪getConstantsCanBeCalledOnInstances()
Definition: EnumerationTest.php:169
‪TYPO3\CMS\Core\Tests\Unit\Type\EnumerationTest\loadValuesThrowsExceptionIfNoDefaultConstantIsDefinedAndNoValueIsGiven
‪loadValuesThrowsExceptionIfNoDefaultConstantIsDefinedAndNoValueIsGiven()
Definition: EnumerationTest.php:78
‪TYPO3\CMS\Core\Tests\Unit\Type\EnumerationTest\getConstantsReturnsArrayOfPossibleValuesWithoutDefault
‪getConstantsReturnsArrayOfPossibleValuesWithoutDefault()
Definition: EnumerationTest.php:140
‪TYPO3\CMS\Core\Tests\Unit\Type\EnumerationTest\equalsReturnsTrueIfEqualEnumerationIsGiven
‪equalsReturnsTrueIfEqualEnumerationIsGiven()
Definition: EnumerationTest.php:260
‪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:26
‪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:45
‪TYPO3\CMS\Core\Type\Exception\InvalidEnumerationValueException
Definition: InvalidEnumerationValueException.php:23
‪TYPO3\CMS\Core\Tests\Unit\Type\EnumerationTest\equalsReturnsTrueIfStringIsGivenThatEqualsEnumerationsIntegerValue
‪equalsReturnsTrueIfStringIsGivenThatEqualsEnumerationsIntegerValue()
Definition: EnumerationTest.php:251
‪TYPO3\CMS\Core\Type\Enumeration\getName
‪static string getName($value)
Definition: Enumeration.php:220
‪TYPO3\CMS\Core\Type\Exception\InvalidEnumerationDefinitionException
Definition: InvalidEnumerationDefinitionException.php:23
‪TYPO3\CMS\Core\Tests\Unit\Type\EnumerationTest\equalsReturnsTrueIfDifferentEnumerationWithSameValueIsGiven
‪equalsReturnsTrueIfDifferentEnumerationWithSameValueIsGiven()
Definition: EnumerationTest.php:270
‪TYPO3\CMS\Core\Tests\Unit\Type\Fixture\Enumeration\CompleteEnumeration
Definition: CompleteEnumeration.php:26
‪TYPO3\CMS\Core\Tests\Unit\Type\EnumerationTest\castCastsIntegerToEnumerationWithCorrespondingValue
‪castCastsIntegerToEnumerationWithCorrespondingValue()
Definition: EnumerationTest.php:232
‪TYPO3\CMS\Core\Tests\Unit\Type\EnumerationTest\constructorThrowsExceptionIfNoConstantsAreDefined
‪constructorThrowsExceptionIfNoConstantsAreDefined()
Definition: EnumerationTest.php:34
‪TYPO3\CMS\Core\Tests\Unit\Type\EnumerationTest\equalsReturnsFalseIfDifferentEnumerationWithDifferentValueIsGiven
‪equalsReturnsFalseIfDifferentEnumerationWithDifferentValueIsGiven()
Definition: EnumerationTest.php:280
‪TYPO3\CMS\Core\Tests\Unit\Type\EnumerationTest
Definition: EnumerationTest.php:30