‪TYPO3CMS  ‪main
EnumConverterTest.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;
26 use TYPO3\TestingFramework\Core\Functional\FunctionalTestCase;
27 
28 final class ‪EnumConverterTest extends FunctionalTestCase
29 {
30  protected bool ‪$initializeDatabase = false;
31 
32  #[DataProvider('convertEnumDataProvider')]
33  #[Test]
34  public function ‪convertEnum(string $enumClass, float|int|string $input, ?object $expected): void
35  {
36  $propertyMapper = $this->get(PropertyMapper::class);
37  $enumItem = $propertyMapper->convert($input, $enumClass);
38 
39  self::assertSame($expected, $enumItem);
40  }
41 
42  public static function ‪convertEnumDataProvider(): \Generator
43  {
44  yield 'convert by name 1' => [
45  UnbackedEnum::class,
46  'FirstCase',
47  UnbackedEnum::FirstCase,
48  ];
49  yield 'convert by name 2' => [
50  UnbackedEnum::class,
51  'SecondCase',
52  UnbackedEnum::SecondCase,
53  ];
54  yield 'convert by name 3' => [
55  UnbackedEnum::class,
56  'ThirdCase',
58  ];
59  yield 'convert by name 4' => [
60  UnbackedEnum::class,
61  'FourthCase',
62  null,
63  ];
64  yield 'convert by value string 1' => [
65  StringBackedEnum::class,
66  'first',
67  StringBackedEnum::FirstCase,
68  ];
69  yield 'convert by value string 2' => [
70  StringBackedEnum::class,
71  'second',
72  StringBackedEnum::SecondCase,
73  ];
74  yield 'convert by value string 3' => [
75  StringBackedEnum::class,
76  'third',
78  ];
79  yield 'convert by value string 4' => [
80  StringBackedEnum::class,
81  'fourth',
82  null,
83  ];
84  yield 'convert by value string, value comes first 1' => [
85  StringBackedEnum::class,
86  'A',
88  ];
89  yield 'convert by value string, value comes first 2' => [
90  StringBackedEnum::class,
91  'B',
92  StringBackedEnum::A,
93  ];
94  yield 'convert by value integer 1' => [
95  IntegerBackedEnum::class,
96  1,
97  IntegerBackedEnum::FirstCase,
98  ];
99  yield 'convert by value integer 2' => [
100  IntegerBackedEnum::class,
101  2,
102  IntegerBackedEnum::SecondCase,
103  ];
104  yield 'convert by value integer 3' => [
105  IntegerBackedEnum::class,
106  3,
108  ];
109  yield 'convert by value integer 4' => [
110  IntegerBackedEnum::class,
111  4,
112  null,
113  ];
114  yield 'convert by value float 1' => [
115  IntegerBackedEnum::class,
116  1.0,
117  IntegerBackedEnum::FirstCase,
118  ];
119  yield 'convert by value float 2' => [
120  IntegerBackedEnum::class,
121  2.0,
122  IntegerBackedEnum::SecondCase,
123  ];
124  yield 'convert by value float 3' => [
125  IntegerBackedEnum::class,
126  3.0,
128  ];
129  yield 'convert by value float 4' => [
130  IntegerBackedEnum::class,
131  4.0,
132  null,
133  ];
134  }
135 }
‪TYPO3\CMS\Extbase\Tests\Functional\Property\Fixtures\UnbackedEnum
‪UnbackedEnum
Definition: UnbackedEnum.php:21
‪TYPO3\CMS\Extbase\Tests\Functional\Property\TypeConverter\EnumConverterTest\$initializeDatabase
‪bool $initializeDatabase
Definition: EnumConverterTest.php:30
‪TYPO3\CMS\Extbase\Tests\Functional\Property\Fixtures\ThirdCase
‪@ ThirdCase
Definition: IntegerBackedEnum.php:24
‪TYPO3\CMS\Extbase\Tests\Functional\Property\TypeConverter\EnumConverterTest\convertEnum
‪convertEnum(string $enumClass, float|int|string $input, ?object $expected)
Definition: EnumConverterTest.php:34
‪TYPO3\CMS\Extbase\Tests\Functional\Property\Fixtures\StringBackedEnum
‪StringBackedEnum
Definition: StringBackedEnum.php:21
‪TYPO3\CMS\Extbase\Tests\Functional\Property\TypeConverter
Definition: ArrayConverterTest.php:18
‪TYPO3\CMS\Extbase\Tests\Functional\Property\Fixtures\IntegerBackedEnum
‪IntegerBackedEnum
Definition: IntegerBackedEnum.php:21
‪TYPO3\CMS\Extbase\Property\PropertyMapper
Definition: PropertyMapper.php:30
‪TYPO3\CMS\Extbase\Tests\Functional\Property\Fixtures\B
‪@ B
Definition: StringBackedEnum.php:26
‪TYPO3\CMS\Extbase\Tests\Functional\Property\TypeConverter\EnumConverterTest
Definition: EnumConverterTest.php:29
‪TYPO3\CMS\Extbase\Tests\Functional\Property\TypeConverter\EnumConverterTest\convertEnumDataProvider
‪static convertEnumDataProvider()
Definition: EnumConverterTest.php:42