‪TYPO3CMS  9.5
EnumerationTest.php
Go to the documentation of this file.
1 <?php
2 declare(strict_types = 1);
3 
5 
6 /*
7  * This file is part of the TYPO3 CMS project.
8  *
9  * It is free software; you can redistribute it and/or modify it under
10  * the terms of the GNU General Public License, either version 2
11  * of the License, or any later version.
12  *
13  * For the full copyright and license information, please read the
14  * LICENSE.txt file that was distributed with this source code.
15  *
16  * The TYPO3 project - inspiring people to share!
17  */
18 
24 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
25 
29 class ‪EnumerationTest extends UnitTestCase
30 {
35  {
36  $this->expectException(InvalidEnumerationValueException::class);
37  $this->expectExceptionCode(1381512753);
38 
39  new Enumeration\MissingConstantsEnumeration();
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 
61  new Enumeration\MissingConstantsEnumeration(2);
62  }
63 
68  {
69  $this->expectException(InvalidEnumerationDefinitionException::class);
70  $this->expectExceptionCode(1381512797);
71 
72  new Enumeration\InvalidConstantEnumeration(1);
73  }
74 
79  {
80  $this->expectException(InvalidEnumerationValueException::class);
81  $this->expectExceptionCode(1381512753);
82 
83  new Enumeration\MissingDefaultEnumeration();
84  }
85 
90  {
91  $this->expectException(InvalidEnumerationDefinitionException::class);
92  $this->expectExceptionCode(1381512859);
93 
94  new Enumeration\DuplicateConstantValueEnumeration(1);
95  }
96 
101  public function ‪looseEnumerationValues(): array
102  {
103  return [
104  [
105  1,
106  Enumeration\CompleteEnumeration::INTEGER_VALUE,
107  ],
108  [
109  '1',
110  Enumeration\CompleteEnumeration::INTEGER_VALUE,
111  ],
112  [
113  2,
114  Enumeration\CompleteEnumeration::STRING_INTEGER_VALUE,
115  ],
116  [
117  '2',
118  Enumeration\CompleteEnumeration::STRING_INTEGER_VALUE,
119  ],
120  [
121  'foo',
122  Enumeration\CompleteEnumeration::STRING_VALUE,
123  ],
124  ];
125  }
126 
133  public function ‪doesTypeLooseComparison($testValue, $expectedValue): void
134  {
135  $value = new Enumeration\CompleteEnumeration($testValue);
136 
137  $this->assertEquals((string)$expectedValue, (string)$value);
138  }
139 
144  {
145  $expected = [
146  'INTEGER_VALUE' => 1,
147  'STRING_INTEGER_VALUE' => '2',
148  'STRING_VALUE' => 'foo',
149  ];
150 
151  $this->assertEquals($expected, ‪Enumeration\‪CompleteEnumeration::getConstants());
152  }
153 
158  {
159  $expected = [
160  'INTEGER_VALUE' => 1,
161  'STRING_INTEGER_VALUE' => '2',
162  'STRING_VALUE' => 'foo',
163  '__default' => 1,
164  ];
165 
166  $this->assertEquals($expected, ‪Enumeration\‪CompleteEnumeration::getConstants(true));
167  }
168 
173  {
174  $enumeration = new Enumeration\CompleteEnumeration();
175  $expected = [
176  'INTEGER_VALUE' => 1,
177  'STRING_INTEGER_VALUE' => '2',
178  'STRING_VALUE' => 'foo',
179  ];
180 
181  $this->assertEquals($expected, $enumeration::getConstants());
182  }
183 
187  public function ‪toStringReturnsValueAsString(): void
188  {
189  $enumeration = new ‪CompleteEnumeration();
190  $this->assertSame('1', $enumeration->__toString());
191  }
192 
197  {
198  $enumeration = ‪CompleteEnumeration::cast(1);
199  $this->assertInstanceOf(CompleteEnumeration::class, $enumeration);
200  }
201 
206  {
207  $initialEnumeration = new Enumeration\MissingDefaultEnumeration(1);
208  $enumeration = ‪CompleteEnumeration::cast($initialEnumeration);
209  $this->assertInstanceOf(CompleteEnumeration::class, $enumeration);
210  }
211 
216  {
217  $initialEnumeration = new ‪CompleteEnumeration(1);
218  $enumeration = ‪CompleteEnumeration::cast($initialEnumeration);
219  $this->assertSame($initialEnumeration, $enumeration);
220  }
221 
226  {
228 
229  $this->assertSame(‪CompleteEnumeration::STRING_VALUE, (string)$value);
230  }
231 
236  {
238 
239  $this->assertSame((int)(string)‪CompleteEnumeration::INTEGER_VALUE, (int)(string)$value);
240  }
241 
246  {
247  $enumeration = new ‪CompleteEnumeration(1);
248  $this->assertTrue($enumeration->equals(1));
249  }
250 
255  {
256  $enumeration = new ‪CompleteEnumeration(1);
257  $this->assertTrue($enumeration->equals('1'));
258  }
259 
264  {
265  $enumerationFoo = new ‪CompleteEnumeration(1);
266  $enumerationBar = new ‪CompleteEnumeration(1);
267  $this->assertTrue($enumerationFoo->equals($enumerationBar));
268  }
269 
274  {
275  $enumerationFoo = new ‪CompleteEnumeration(1);
276  $enumerationBar = new ‪MissingDefaultEnumeration(1);
277  $this->assertTrue($enumerationFoo->equals($enumerationBar));
278  }
279 
284  {
285  $enumerationFoo = new ‪CompleteEnumeration('foo');
286  $enumerationBar = new ‪MissingDefaultEnumeration(1);
287  $this->assertFalse($enumerationFoo->equals($enumerationBar));
288  }
289 
294  {
295  $enumerationFoo = new ‪CompleteEnumeration(1);
296  $enumerationBar = new ‪CompleteEnumeration('foo');
297  $this->assertFalse($enumerationFoo->equals($enumerationBar));
298  }
299 
304  {
306  $this->assertSame('INTEGER_VALUE', $result);
307  }
308 
313  {
314  $result = ‪CompleteEnumeration::getName(42);
315  $this->assertSame('', $result);
316  }
317 
322  {
324  $this->assertSame('Integer Value', $result);
325  }
326 
331  {
332  $result = ‪CompleteEnumeration::getName(42);
333  $this->assertSame('', $result);
334  }
335 }
‪TYPO3\CMS\Core\Tests\Unit\Type\EnumerationTest\getNameReturnsEmptyStringForNotAvailableConstant
‪getNameReturnsEmptyStringForNotAvailableConstant()
Definition: EnumerationTest.php:312
‪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:157
‪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:225
‪TYPO3\CMS\Core\Tests\Unit\Type\Fixture\Enumeration
Definition: CompleteEnumeration.php:2
‪TYPO3\CMS\Core\Tests\Unit\Type\EnumerationTest\castReturnsGivenObjectIfCalledWithValueOfSameType
‪castReturnsGivenObjectIfCalledWithValueOfSameType()
Definition: EnumerationTest.php:215
‪TYPO3\CMS\Core\Tests\Unit\Type\EnumerationTest\getNameProvidesNameForAvailableConstant
‪getNameProvidesNameForAvailableConstant()
Definition: EnumerationTest.php:303
‪TYPO3\CMS\Core\Tests\Unit\Type\EnumerationTest\doesTypeLooseComparison
‪doesTypeLooseComparison($testValue, $expectedValue)
Definition: EnumerationTest.php:133
‪TYPO3\CMS\Core\Tests\Unit\Type\EnumerationTest\getHumanReadableNameProvidesNameForAvailableConstant
‪getHumanReadableNameProvidesNameForAvailableConstant()
Definition: EnumerationTest.php:321
‪TYPO3\CMS\Core\Tests\Unit\Type\Fixture\Enumeration\CompleteEnumeration\INTEGER_VALUE
‪const INTEGER_VALUE
Definition: CompleteEnumeration.php:25
‪TYPO3\CMS\Core\Tests\Unit\Type\EnumerationTest\looseEnumerationValues
‪array looseEnumerationValues()
Definition: EnumerationTest.php:101
‪TYPO3\CMS\Core\Tests\Unit\Type\EnumerationTest\equalsReturnsFalseIfEnumerationOfSameTypeWithDifferentValueIsGiven
‪equalsReturnsFalseIfEnumerationOfSameTypeWithDifferentValueIsGiven()
Definition: EnumerationTest.php:293
‪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:205
‪TYPO3\CMS\Core\Tests\Unit\Type\EnumerationTest\toStringReturnsValueAsString
‪toStringReturnsValueAsString()
Definition: EnumerationTest.php:187
‪TYPO3\CMS\Core\Type\Enumeration\cast
‪static static cast($value)
Definition: Enumeration.php:182
‪TYPO3\CMS\Core\Tests\Unit\Type\EnumerationTest\getHumanReadableNameReturnsEmptyStringForNotAvailableConstant
‪getHumanReadableNameReturnsEmptyStringForNotAvailableConstant()
Definition: EnumerationTest.php:330
‪TYPO3\CMS\Core\Tests\Unit\Type\Fixture\Enumeration\MissingDefaultEnumeration
Definition: MissingDefaultEnumeration.php:23
‪TYPO3\CMS\Core\Tests\Unit\Type
Definition: EnumerationTest.php:4
‪TYPO3\CMS\Core\Tests\Unit\Type\EnumerationTest\equalsReturnsTrueIfIntegerIsGivenThatEqualsEnumerationsIntegerValue
‪equalsReturnsTrueIfIntegerIsGivenThatEqualsEnumerationsIntegerValue()
Definition: EnumerationTest.php:245
‪TYPO3\CMS\Core\Tests\Unit\Type\EnumerationTest\castReturnsObjectOfEnumerationTypeIfSimpleValueIsGiven
‪castReturnsObjectOfEnumerationTypeIfSimpleValueIsGiven()
Definition: EnumerationTest.php:196
‪TYPO3\CMS\Core\Tests\Unit\Type\Fixture\Enumeration\CompleteEnumeration\STRING_VALUE
‪const STRING_VALUE
Definition: CompleteEnumeration.php:27
‪TYPO3\CMS\Core\Tests\Unit\Type\EnumerationTest\getConstantsCanBeCalledOnInstances
‪getConstantsCanBeCalledOnInstances()
Definition: EnumerationTest.php:172
‪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:143
‪TYPO3\CMS\Core\Tests\Unit\Type\EnumerationTest\equalsReturnsTrueIfEqualEnumerationIsGiven
‪equalsReturnsTrueIfEqualEnumerationIsGiven()
Definition: EnumerationTest.php:263
‪TYPO3\CMS\Core\Type\Enumeration\getHumanReadableName
‪static string getHumanReadableName($value)
Definition: Enumeration.php:232
‪TYPO3\CMS\Core\Type\Enumeration\getConstants
‪static array getConstants($include_default=false)
Definition: Enumeration.php:166
‪TYPO3\CMS\Core\Tests\Unit\Type\EnumerationTest\constructorThrowsExceptionIfInvalidValueIsRequested
‪constructorThrowsExceptionIfInvalidValueIsRequested()
Definition: EnumerationTest.php:45
‪TYPO3\CMS\Core\Type\Enumeration
Definition: Enumeration.php:25
‪TYPO3\CMS\Core\Type\Exception\InvalidEnumerationValueException
Definition: InvalidEnumerationValueException.php:21
‪TYPO3\CMS\Core\Tests\Unit\Type\EnumerationTest\equalsReturnsTrueIfStringIsGivenThatEqualsEnumerationsIntegerValue
‪equalsReturnsTrueIfStringIsGivenThatEqualsEnumerationsIntegerValue()
Definition: EnumerationTest.php:254
‪TYPO3\CMS\Core\Type\Enumeration\getName
‪static string getName($value)
Definition: Enumeration.php:216
‪TYPO3\CMS\Core\Type\Exception\InvalidEnumerationDefinitionException
Definition: InvalidEnumerationDefinitionException.php:21
‪TYPO3\CMS\Core\Tests\Unit\Type\EnumerationTest\equalsReturnsTrueIfDifferentEnumerationWithSameValueIsGiven
‪equalsReturnsTrueIfDifferentEnumerationWithSameValueIsGiven()
Definition: EnumerationTest.php:273
‪TYPO3\CMS\Core\Tests\Unit\Type\Fixture\Enumeration\CompleteEnumeration
Definition: CompleteEnumeration.php:23
‪TYPO3\CMS\Core\Tests\Unit\Type\EnumerationTest\castCastsIntegerToEnumerationWithCorrespondingValue
‪castCastsIntegerToEnumerationWithCorrespondingValue()
Definition: EnumerationTest.php:235
‪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:283
‪TYPO3\CMS\Core\Tests\Unit\Type\EnumerationTest
Definition: EnumerationTest.php:30