‪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 
20 use PHPUnit\Framework\Attributes\DataProvider;
21 use PHPUnit\Framework\Attributes\Test;
29 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
30 
31 final class ‪EnumerationTest extends UnitTestCase
32 {
33  #[Test]
35  {
36  $this->expectException(InvalidEnumerationValueException::class);
37  $this->expectExceptionCode(1381512753);
38 
40  }
41 
42  #[Test]
44  {
45  $this->expectException(InvalidEnumerationValueException::class);
46  $this->expectExceptionCode(1381512761);
47 
48  new ‪CompleteEnumeration('bar');
49  }
50 
51  #[Test]
53  {
54  $this->expectException(InvalidEnumerationValueException::class);
55  $this->expectExceptionCode(1381512807);
56 
58  }
59 
60  #[Test]
62  {
63  $this->expectException(InvalidEnumerationDefinitionException::class);
64  $this->expectExceptionCode(1381512797);
65 
67  }
68 
69  #[Test]
71  {
72  $this->expectException(InvalidEnumerationValueException::class);
73  $this->expectExceptionCode(1381512753);
74 
76  }
77 
78  #[Test]
80  {
81  $this->expectException(InvalidEnumerationDefinitionException::class);
82  $this->expectExceptionCode(1381512859);
83 
85  }
86 
90  public static function ‪looseEnumerationValues(): array
91  {
92  return [
93  [
94  1,
96  ],
97  [
98  '1',
100  ],
101  [
102  2,
104  ],
105  [
106  '2',
108  ],
109  [
110  'foo',
112  ],
113  ];
114  }
115 
116  #[DataProvider('looseEnumerationValues')]
117  #[Test]
118  public function ‪doesTypeLooseComparison(string|int $testValue, string|int $expectedValue): void
119  {
120  $value = new ‪CompleteEnumeration($testValue);
121 
122  self::assertEquals((string)$expectedValue, (string)$value);
123  }
124 
125  #[Test]
127  {
128  $expected = [
129  'INTEGER_VALUE' => 1,
130  'STRING_INTEGER_VALUE' => '2',
131  'STRING_VALUE' => 'foo',
132  ];
133 
134  self::assertEquals($expected, ‪CompleteEnumeration::getConstants());
135  }
136 
137  #[Test]
139  {
140  $expected = [
141  'INTEGER_VALUE' => 1,
142  'STRING_INTEGER_VALUE' => '2',
143  'STRING_VALUE' => 'foo',
144  '__default' => 1,
145  ];
146 
147  self::assertEquals($expected, ‪CompleteEnumeration::getConstants(true));
148  }
149 
150  #[Test]
152  {
153  $enumeration = new ‪CompleteEnumeration();
154  $expected = [
155  'INTEGER_VALUE' => 1,
156  'STRING_INTEGER_VALUE' => '2',
157  'STRING_VALUE' => 'foo',
158  ];
159 
160  self::assertEquals($expected, $enumeration::getConstants());
161  }
162 
163  #[Test]
164  public function ‪toStringReturnsValueAsString(): void
165  {
166  $enumeration = new ‪CompleteEnumeration();
167  self::assertSame('1', $enumeration->__toString());
168  }
169 
170  #[Test]
172  {
173  $enumeration = ‪CompleteEnumeration::cast(1);
174  self::assertInstanceOf(CompleteEnumeration::class, $enumeration);
175  }
176 
177  #[Test]
179  {
180  $initialEnumeration = new ‪MissingDefaultEnumeration(1);
181  $enumeration = ‪CompleteEnumeration::cast($initialEnumeration);
182  self::assertInstanceOf(CompleteEnumeration::class, $enumeration);
183  }
184 
185  #[Test]
187  {
188  $initialEnumeration = new ‪CompleteEnumeration(1);
189  $enumeration = ‪CompleteEnumeration::cast($initialEnumeration);
190  self::assertSame($initialEnumeration, $enumeration);
191  }
192 
193  #[Test]
195  {
197 
198  self::assertSame(‪CompleteEnumeration::STRING_VALUE, (string)$value);
199  }
200 
201  #[Test]
203  {
205 
206  self::assertSame((int)(string)‪CompleteEnumeration::INTEGER_VALUE, (int)(string)$value);
207  }
208 
209  #[Test]
211  {
212  $enumeration = new ‪CompleteEnumeration(1);
213  self::assertTrue($enumeration->equals(1));
214  }
215 
216  #[Test]
218  {
219  $enumeration = new ‪CompleteEnumeration(1);
220  self::assertTrue($enumeration->equals('1'));
221  }
222 
223  #[Test]
225  {
226  $enumerationFoo = new ‪CompleteEnumeration(1);
227  $enumerationBar = new ‪CompleteEnumeration(1);
228  self::assertTrue($enumerationFoo->equals($enumerationBar));
229  }
230 
231  #[Test]
233  {
234  $enumerationFoo = new ‪CompleteEnumeration(1);
235  $enumerationBar = new ‪MissingDefaultEnumeration(1);
236  self::assertTrue($enumerationFoo->equals($enumerationBar));
237  }
238 
239  #[Test]
241  {
242  $enumerationFoo = new ‪CompleteEnumeration('foo');
243  $enumerationBar = new ‪MissingDefaultEnumeration(1);
244  self::assertFalse($enumerationFoo->equals($enumerationBar));
245  }
246 
247  #[Test]
249  {
250  $enumerationFoo = new ‪CompleteEnumeration(1);
251  $enumerationBar = new ‪CompleteEnumeration('foo');
252  self::assertFalse($enumerationFoo->equals($enumerationBar));
253  }
254 
255  #[Test]
257  {
259  self::assertSame('INTEGER_VALUE', $result);
260  }
261 
262  #[Test]
264  {
265  $result = ‪CompleteEnumeration::getName(42);
266  self::assertSame('', $result);
267  }
268 
269  #[Test]
271  {
273  self::assertSame('Integer Value', $result);
274  }
275 
276  #[Test]
278  {
279  $result = ‪CompleteEnumeration::getName(42);
280  self::assertSame('', $result);
281  }
282 }
‪TYPO3\CMS\Core\Tests\UnitDeprecated\Type\EnumerationTest\getConstantsReturnsArrayOfPossibleValuesWithoutDefault
‪getConstantsReturnsArrayOfPossibleValuesWithoutDefault()
Definition: EnumerationTest.php:126
‪TYPO3\CMS\Core\Tests\UnitDeprecated\Type\EnumerationTest\equalsReturnsTrueIfIntegerIsGivenThatEqualsEnumerationsIntegerValue
‪equalsReturnsTrueIfIntegerIsGivenThatEqualsEnumerationsIntegerValue()
Definition: EnumerationTest.php:210
‪TYPO3\CMS\Core\Tests\UnitDeprecated\Type\Fixture\Enumeration\MissingConstantsEnumeration
Definition: MissingConstantsEnumeration.php:25
‪TYPO3\CMS\Core\Tests\UnitDeprecated\Type\EnumerationTest\getNameProvidesNameForAvailableConstant
‪getNameProvidesNameForAvailableConstant()
Definition: EnumerationTest.php:256
‪TYPO3\CMS\Core\Tests\UnitDeprecated\Type\EnumerationTest\castReturnsGivenObjectIfCalledWithValueOfSameType
‪castReturnsGivenObjectIfCalledWithValueOfSameType()
Definition: EnumerationTest.php:186
‪TYPO3\CMS\Core\Tests\UnitDeprecated\Type\EnumerationTest\equalsReturnsTrueIfStringIsGivenThatEqualsEnumerationsIntegerValue
‪equalsReturnsTrueIfStringIsGivenThatEqualsEnumerationsIntegerValue()
Definition: EnumerationTest.php:217
‪TYPO3\CMS\Core\Tests\UnitDeprecated\Type\Fixture\Enumeration\CompleteEnumeration\STRING_VALUE
‪const STRING_VALUE
Definition: CompleteEnumeration.php:30
‪TYPO3\CMS\Core\Tests\UnitDeprecated\Type\EnumerationTest\getConstantsReturnsArrayOfPossibleValuesWithDefaultIfRequested
‪getConstantsReturnsArrayOfPossibleValuesWithDefaultIfRequested()
Definition: EnumerationTest.php:138
‪TYPO3\CMS\Core\Tests\UnitDeprecated\Type\EnumerationTest\doesTypeLooseComparison
‪doesTypeLooseComparison(string|int $testValue, string|int $expectedValue)
Definition: EnumerationTest.php:118
‪TYPO3\CMS\Core\Tests\UnitDeprecated\Type\EnumerationTest\loadValuesThrowsExceptionIfGivenValueIsNotAvailableInEnumeration
‪loadValuesThrowsExceptionIfGivenValueIsNotAvailableInEnumeration()
Definition: EnumerationTest.php:52
‪TYPO3\CMS\Core\Tests\UnitDeprecated\Type\EnumerationTest
Definition: EnumerationTest.php:32
‪TYPO3\CMS\Core\Tests\UnitDeprecated\Type\EnumerationTest\equalsReturnsFalseIfEnumerationOfSameTypeWithDifferentValueIsGiven
‪equalsReturnsFalseIfEnumerationOfSameTypeWithDifferentValueIsGiven()
Definition: EnumerationTest.php:248
‪TYPO3\CMS\Core\Tests\UnitDeprecated\Type\EnumerationTest\constructorThrowsExceptionIfNoConstantsAreDefined
‪constructorThrowsExceptionIfNoConstantsAreDefined()
Definition: EnumerationTest.php:34
‪TYPO3\CMS\Core\Tests\UnitDeprecated\Type\EnumerationTest\loadValuesThrowsExceptionIfValueIsDefinedMultipleTimes
‪loadValuesThrowsExceptionIfValueIsDefinedMultipleTimes()
Definition: EnumerationTest.php:79
‪TYPO3\CMS\Core\Tests\UnitDeprecated\Type\EnumerationTest\toStringReturnsValueAsString
‪toStringReturnsValueAsString()
Definition: EnumerationTest.php:164
‪TYPO3\CMS\Core\Tests\UnitDeprecated\Type\EnumerationTest\loadValuesThrowsExceptionIfNoDefaultConstantIsDefinedAndNoValueIsGiven
‪loadValuesThrowsExceptionIfNoDefaultConstantIsDefinedAndNoValueIsGiven()
Definition: EnumerationTest.php:70
‪TYPO3\CMS\Core\Type\Enumeration\cast
‪static static cast($value)
Definition: Enumeration.php:190
‪TYPO3\CMS\Core\Tests\UnitDeprecated\Type\Fixture\Enumeration\MissingDefaultEnumeration
Definition: MissingDefaultEnumeration.php:26
‪TYPO3\CMS\Core\Tests\UnitDeprecated\Type\EnumerationTest\castReturnsObjectOfEnumerationTypeIfSimpleValueIsGiven
‪castReturnsObjectOfEnumerationTypeIfSimpleValueIsGiven()
Definition: EnumerationTest.php:171
‪TYPO3\CMS\Core\Type\Enumeration\getHumanReadableName
‪static string getHumanReadableName($value)
Definition: Enumeration.php:242
‪TYPO3\CMS\Core\Tests\UnitDeprecated\Type\EnumerationTest\getNameReturnsEmptyStringForNotAvailableConstant
‪getNameReturnsEmptyStringForNotAvailableConstant()
Definition: EnumerationTest.php:263
‪TYPO3\CMS\Core\Tests\UnitDeprecated\Type\EnumerationTest\looseEnumerationValues
‪static looseEnumerationValues()
Definition: EnumerationTest.php:90
‪TYPO3\CMS\Core\Tests\UnitDeprecated\Type\EnumerationTest\castCastsStringToEnumerationWithCorrespondingValue
‪castCastsStringToEnumerationWithCorrespondingValue()
Definition: EnumerationTest.php:194
‪TYPO3\CMS\Core\Tests\UnitDeprecated\Type\Fixture\Enumeration\CompleteEnumeration\INTEGER_VALUE
‪const INTEGER_VALUE
Definition: CompleteEnumeration.php:28
‪TYPO3\CMS\Core\Tests\UnitDeprecated\Type\EnumerationTest\constructorThrowsExceptionIfInvalidValueIsRequested
‪constructorThrowsExceptionIfInvalidValueIsRequested()
Definition: EnumerationTest.php:43
‪TYPO3\CMS\Core\Type\Enumeration\getConstants
‪static array getConstants($include_default=false)
Definition: Enumeration.php:174
‪TYPO3\CMS\Core\Tests\UnitDeprecated\Type
Definition: EnumerationTest.php:18
‪TYPO3\CMS\Core\Tests\UnitDeprecated\Type\EnumerationTest\getHumanReadableNameProvidesNameForAvailableConstant
‪getHumanReadableNameProvidesNameForAvailableConstant()
Definition: EnumerationTest.php:270
‪TYPO3\CMS\Core\Tests\UnitDeprecated\Type\Fixture\Enumeration\DuplicateConstantValueEnumeration
Definition: DuplicateConstantValueEnumeration.php:26
‪TYPO3\CMS\Core\Tests\UnitDeprecated\Type\EnumerationTest\getHumanReadableNameReturnsEmptyStringForNotAvailableConstant
‪getHumanReadableNameReturnsEmptyStringForNotAvailableConstant()
Definition: EnumerationTest.php:277
‪TYPO3\CMS\Core\Type\Exception\InvalidEnumerationValueException
Definition: InvalidEnumerationValueException.php:23
‪TYPO3\CMS\Core\Tests\UnitDeprecated\Type\EnumerationTest\equalsReturnsTrueIfEqualEnumerationIsGiven
‪equalsReturnsTrueIfEqualEnumerationIsGiven()
Definition: EnumerationTest.php:224
‪TYPO3\CMS\Core\Tests\UnitDeprecated\Type\Fixture\Enumeration\InvalidConstantEnumeration
Definition: InvalidConstantEnumeration.php:26
‪TYPO3\CMS\Core\Type\Enumeration\getName
‪static string getName($value)
Definition: Enumeration.php:226
‪TYPO3\CMS\Core\Type\Exception\InvalidEnumerationDefinitionException
Definition: InvalidEnumerationDefinitionException.php:23
‪TYPO3\CMS\Core\Tests\UnitDeprecated\Type\EnumerationTest\loadValuesThrowsExceptionIfDisallowedTypeIsDefinedAsConstant
‪loadValuesThrowsExceptionIfDisallowedTypeIsDefinedAsConstant()
Definition: EnumerationTest.php:61
‪TYPO3\CMS\Core\Tests\UnitDeprecated\Type\Fixture\Enumeration\CompleteEnumeration\STRING_INTEGER_VALUE
‪const STRING_INTEGER_VALUE
Definition: CompleteEnumeration.php:29
‪TYPO3\CMS\Core\Tests\UnitDeprecated\Type\EnumerationTest\getConstantsCanBeCalledOnInstances
‪getConstantsCanBeCalledOnInstances()
Definition: EnumerationTest.php:151
‪TYPO3\CMS\Core\Tests\UnitDeprecated\Type\EnumerationTest\equalsReturnsFalseIfDifferentEnumerationWithDifferentValueIsGiven
‪equalsReturnsFalseIfDifferentEnumerationWithDifferentValueIsGiven()
Definition: EnumerationTest.php:240
‪TYPO3\CMS\Core\Tests\UnitDeprecated\Type\EnumerationTest\equalsReturnsTrueIfDifferentEnumerationWithSameValueIsGiven
‪equalsReturnsTrueIfDifferentEnumerationWithSameValueIsGiven()
Definition: EnumerationTest.php:232
‪TYPO3\CMS\Core\Tests\UnitDeprecated\Type\EnumerationTest\castReturnsObjectOfCalledEnumerationTypeIfCalledWithValueOfDifferentType
‪castReturnsObjectOfCalledEnumerationTypeIfCalledWithValueOfDifferentType()
Definition: EnumerationTest.php:178
‪TYPO3\CMS\Core\Tests\UnitDeprecated\Type\EnumerationTest\castCastsIntegerToEnumerationWithCorrespondingValue
‪castCastsIntegerToEnumerationWithCorrespondingValue()
Definition: EnumerationTest.php:202
‪TYPO3\CMS\Core\Tests\UnitDeprecated\Type\Fixture\Enumeration\CompleteEnumeration
Definition: CompleteEnumeration.php:26