‪TYPO3CMS  ‪main
PropertyMapperTest.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\Test;
29 use TYPO3\TestingFramework\Core\Functional\FunctionalTestCase;
36 
37 final class ‪PropertyMapperTest extends FunctionalTestCase
38 {
39  protected array ‪$coreExtensionsToLoad = ['extbase'];
40  protected array ‪$testExtensionsToLoad = [
41  'typo3/sysext/extbase/Tests/Functional/Fixtures/Extensions/blog_example/',
42  'typo3/sysext/extbase/Tests/Functional/Fixtures/Extensions/type_converter_test/',
43  ];
44 
45  protected function ‪setUp(): void
46  {
47  parent::setUp();
48  $request = (new ‪ServerRequest())->withAttribute('applicationType', ‪SystemEnvironmentBuilder::REQUESTTYPE_BE);
49  ‪$GLOBALS['TYPO3_REQUEST'] = $request;
50  }
51 
52  #[Test]
54  {
55  // This test just increases the test coverage
56  $this->get(PropertyMapper::class)
57  ->convert('string', 'string');
58  }
59 
60  #[Test]
62  {
63  $propertyMapper = $this->get(PropertyMapper::class);
64 
65  self::assertNull($propertyMapper->convert('string', 'integer'));
66  self::assertNotEmpty($propertyMapper->getMessages());
67  }
68 
69  #[Test]
71  {
72  $this->expectException(TargetNotFoundException::class);
73  $this->expectExceptionCode(1297933823);
74 
75  $propertyMapper = $this->get(PropertyMapper::class);
76  $propertyMapper->convert(9999, Blog::class);
77  }
78 
79  #[Test]
81  {
82  $this->expectException(\Exception::class);
83  $this->expectExceptionCode(1297759968);
84  $this->expectExceptionMessage('Exception while property mapping at property path "": No converter found which can be used to convert from "integer" to "boolean"');
85 
86  $propertyMapper = $this->get(PropertyMapper::class);
87  $propertyMapper->convert(9999, 'boolean');
88  }
89 
90  #[Test]
92  {
93  $propertyMapper = $this->get(PropertyMapper::class);
94  self::assertSame('', $propertyMapper->convert(null, 'string'));
95  }
96 
97  #[Test]
99  {
100  $this->expectException(\Exception::class);
101  $this->expectExceptionCode(1297759968);
102  $this->expectExceptionMessage('Exception while property mapping at property path "": Could not find a suitable type converter for "NonExistingClass" because no such class or interface exists.');
103 
104  $propertyMapper = $this->get(PropertyMapper::class);
105  $propertyMapper->convert(1, 'NonExistingClass');
106  }
107 
108  #[Test]
110  {
111  $this->expectException(\Exception::class);
112  $this->expectExceptionCode(1297759968);
113  $this->expectExceptionMessage('There exist at least two converters which handle the conversion to an interface with priority "10"');
114 
115  $counter = new class () implements ‪ExtendedCountableInterface {
116  public function count(): int
117  {
118  return 1;
119  }
120  };
121 
122  $propertyMapper = $this->get(PropertyMapper::class);
123  $propertyMapper->convert(1, get_class($counter));
124  }
125 
126  #[Test]
128  {
129  $objectStorage = new ‪ObjectStorage();
130 
131  $result = $this->get(PropertyMapper::class)->convert(
132  $objectStorage,
133  '\TYPO3\CMS\Extbase\Persistence\ObjectStorage<\TYPO3\CMS\Beuser\Domain\Model\BackendUser>'
134  );
135 
136  self::assertSame($objectStorage, $result);
137  }
138 
139  #[Test]
141  {
142  $class = new class () extends ‪IntegerConverter {
143  public function convertFrom($source, string $targetType, array $convertedChildProperties = [], ‪PropertyMappingConfigurationInterface $configuration = null): int
144  {
145  return 1575648246;
146  }
147  };
148 
149  $propertyMappingConfiguration = new ‪PropertyMappingConfiguration();
150  $propertyMappingConfiguration->setTypeConverter($class);
151 
152  $result = $this->get(PropertyMapper::class)->convert(
153  1,
154  'integer',
155  $propertyMappingConfiguration
156  );
157 
158  self::assertSame(1575648246, $result);
159  }
160 
161  #[Test]
163  {
164  $this->expectException(\Exception::class);
165  $this->expectExceptionCode(1297759968);
166  $this->expectExceptionMessage('The source is not of type string, array, float, integer or boolean, but of type "object"');
167 
168  $generator = static function () {
169  return 'string';
170  };
171 
172  $propertyMapper = $this->get(PropertyMapper::class);
173  $propertyMapper->convert($generator, 'string');
174  }
175 
176  #[Test]
178  {
179  $this->expectException(\Exception::class);
180  $this->expectExceptionCode(1297759968);
181  $this->expectExceptionMessage('Exception while property mapping at property path "": No converter found which can be used to convert from "boolean" to "TYPO3Tests\TypeConverterTest\Domain\Model\Cat"');
182 
183  $result = $this->get(PropertyMapper::class)->convert(false, Cat::class);
184  self::assertNull($result);
185  }
186 
187  #[Test]
189  {
190  $result = $this->get(PropertyMapper::class)->convert('tigger', Cat::class);
191  self::assertInstanceOf(Cat::class, $result);
192  }
193 
194  #[Test]
196  {
197  $result = $this->get(PropertyMapper::class)->convert('fluffy', Dog::class);
198  self::assertInstanceOf(Animal::class, $result);
199  }
200 
201  #[Test]
203  {
204  $propertyMapper = $this->get(PropertyMapper::class);
205  $result = $propertyMapper->convert(1, Countable::class);
206 
207  self::assertSame([], $result);
208  }
209 
210  #[Test]
212  {
213  $source = [
214  'color' => 'black',
215  ];
216 
217  $propertyMappingConfiguration = new ‪PropertyMappingConfiguration();
218  $propertyMappingConfiguration->allowAllProperties();
219 
220  $propertyMapper = $this->get(PropertyMapper::class);
222  $result = $propertyMapper->convert(
223  $source,
224  Cat::class,
225  $propertyMappingConfiguration
226  );
227 
228  self::assertInstanceOf(Cat::class, $result);
229  self::assertSame('black', $result->getColor());
230  }
231 
232  #[Test]
233  public function ‪skipPropertiesConfiguration(): void
234  {
235  $source = [
236  'color' => 'black',
237  ];
238 
239  $propertyMappingConfiguration = new ‪PropertyMappingConfiguration();
240  $propertyMappingConfiguration->skipProperties('color');
241 
242  $propertyMapper = $this->get(PropertyMapper::class);
244  $result = $propertyMapper->convert(
245  $source,
246  Cat::class,
247  $propertyMappingConfiguration
248  );
249 
250  self::assertInstanceOf(Cat::class, $result);
251  self::assertNull($result->getColor());
252  }
253 
254  #[Test]
256  {
257  $this->expectException(\Exception::class);
258  $this->expectExceptionCode(1297759968);
259  $this->expectExceptionMessage('It is not allowed to map property "color". You need to use $propertyMappingConfiguration->allowProperties(\'color\') to enable mapping of this property.');
260 
261  $source = [
262  'color' => 'black',
263  ];
264 
265  $propertyMappingConfiguration = new ‪PropertyMappingConfiguration();
266  $propertyMappingConfiguration->allowAllPropertiesExcept('color');
267 
268  $propertyMapper = $this->get(PropertyMapper::class);
269  $propertyMapper->convert(
270  $source,
271  Cat::class,
272  $propertyMappingConfiguration
273  );
274  }
275 
276  #[Test]
278  {
279  $source = [
280  'color' => 'black',
281  ];
282 
283  $propertyMappingConfiguration = new ‪PropertyMappingConfiguration();
284  $propertyMappingConfiguration->allowAllPropertiesExcept('color');
285  $propertyMappingConfiguration->skipUnknownProperties();
286 
287  $propertyMapper = $this->get(PropertyMapper::class);
289  $result = $propertyMapper->convert(
290  $source,
291  Cat::class,
292  $propertyMappingConfiguration
293  );
294 
295  self::assertInstanceOf(Cat::class, $result);
296  self::assertNull($result->getColor());
297  }
298 }
‪TYPO3\CMS\Extbase\Tests\Functional\Property\PropertyMapperTest\convertReturnsNullIfDoMappingReturnsAnError
‪convertReturnsNullIfDoMappingReturnsAnError()
Definition: PropertyMapperTest.php:61
‪TYPO3Tests\TypeConverterTest\Domain\Model\Animal
Definition: Animal.php:21
‪TYPO3\CMS\Extbase\Tests\Functional\Property
‪TYPO3\CMS\Extbase\Tests\Functional\Property\PropertyMapperTest\$coreExtensionsToLoad
‪array $coreExtensionsToLoad
Definition: PropertyMapperTest.php:39
‪TYPO3\CMS\Extbase\Tests\Functional\Property\PropertyMapperTest\convertCreatesAPropertyMappingConfigurationIfNotGiven
‪convertCreatesAPropertyMappingConfigurationIfNotGiven()
Definition: PropertyMapperTest.php:53
‪TYPO3\CMS\Extbase\Tests\Functional\Property\PropertyMapperTest\determineSourceTypeThrowsInvalidSourceExceptionForNonSupportedTypes
‪determineSourceTypeThrowsInvalidSourceExceptionForNonSupportedTypes()
Definition: PropertyMapperTest.php:162
‪TYPO3\CMS\Extbase\Tests\Functional\Property\PropertyMapperTest\allowAllPropertiesExceptWithSkipUnknownPropertiesConfiguration
‪allowAllPropertiesExceptWithSkipUnknownPropertiesConfiguration()
Definition: PropertyMapperTest.php:277
‪TYPO3\CMS\Extbase\Property\TypeConverter\IntegerConverter
Definition: IntegerConverter.php:27
‪TYPO3\CMS\Extbase\Tests\Functional\Property\PropertyMapperTest\findTypeConverterReturnsTheConverterFromThePropertyMappingConfiguration
‪findTypeConverterReturnsTheConverterFromThePropertyMappingConfiguration()
Definition: PropertyMapperTest.php:140
‪TYPO3\CMS\Extbase\Tests\Functional\Property\PropertyMapperTest\skipPropertiesConfiguration
‪skipPropertiesConfiguration()
Definition: PropertyMapperTest.php:233
‪TYPO3\CMS\Core\Core\SystemEnvironmentBuilder
Definition: SystemEnvironmentBuilder.php:41
‪TYPO3\CMS\Extbase\Tests\Functional\Property\PropertyMapperTest
Definition: PropertyMapperTest.php:38
‪TYPO3\CMS\Extbase\Tests\Functional\Property\PropertyMapperTest\convertInternallyConvertsANullSourceToAnEmptyString
‪convertInternallyConvertsANullSourceToAnEmptyString()
Definition: PropertyMapperTest.php:91
‪TYPO3\CMS\Core\Core\SystemEnvironmentBuilder\REQUESTTYPE_BE
‪const REQUESTTYPE_BE
Definition: SystemEnvironmentBuilder.php:45
‪TYPO3Tests\TypeConverterTest\Domain\Model\Countable
Definition: Countable.php:21
‪TYPO3\CMS\Extbase\Property\PropertyMappingConfigurationInterface
Definition: PropertyMappingConfigurationInterface.php:22
‪TYPO3\CMS\Extbase\Persistence\ObjectStorage
Definition: ObjectStorage.php:34
‪TYPO3Tests\TypeConverterTest\Domain\Model\ExtendedCountableInterface
Definition: ExtendedCountableInterface.php:20
‪TYPO3\CMS\Extbase\Tests\Functional\Property\PropertyMapperTest\findFirstEligibleTypeConverterInObjectHierarchyReturnsNullIfNoTypeConvertersExistForTheSourceType
‪findFirstEligibleTypeConverterInObjectHierarchyReturnsNullIfNoTypeConvertersExistForTheSourceType()
Definition: PropertyMapperTest.php:177
‪TYPO3\CMS\Extbase\Property\PropertyMappingConfiguration
Definition: PropertyMappingConfiguration.php:22
‪TYPO3\CMS\Extbase\Tests\Functional\Property\PropertyMapperTest\defaultPropertyMappingConfiguration
‪defaultPropertyMappingConfiguration()
Definition: PropertyMapperTest.php:211
‪TYPO3Tests\TypeConverterTest\Domain\Model\Dog
Definition: Dog.php:20
‪TYPO3\CMS\Extbase\Tests\Functional\Property\PropertyMapperTest\convertThrowsAnExceptionIfAtLeastTwoConvertersAreRegisteredThatHandleTheConversionToTheSameInterface
‪convertThrowsAnExceptionIfAtLeastTwoConvertersAreRegisteredThatHandleTheConversionToTheSameInterface()
Definition: PropertyMapperTest.php:109
‪TYPO3\CMS\Extbase\Tests\Functional\Property\PropertyMapperTest\allowAllPropertiesExceptConfiguration
‪allowAllPropertiesExceptConfiguration()
Definition: PropertyMapperTest.php:255
‪TYPO3\CMS\Core\Http\ServerRequest
Definition: ServerRequest.php:39
‪TYPO3\CMS\Extbase\Tests\Functional\Property\PropertyMapperTest\findFirstEligibleTypeConverterInObjectHierarchyReturnsConverterForInterfaces
‪findFirstEligibleTypeConverterInObjectHierarchyReturnsConverterForInterfaces()
Definition: PropertyMapperTest.php:202
‪TYPO3\CMS\Extbase\Property\PropertyMapper
Definition: PropertyMapper.php:30
‪TYPO3\CMS\Extbase\Tests\Functional\Property\PropertyMapperTest\convertThrowsAnExceptionIfTargetTypeIsANonExistingClass
‪convertThrowsAnExceptionIfTargetTypeIsANonExistingClass()
Definition: PropertyMapperTest.php:98
‪TYPO3\CMS\Extbase\Tests\Functional\Property\PropertyMapperTest\findFirstEligibleTypeConverterInObjectHierarchyReturnsConverterForParentClass
‪findFirstEligibleTypeConverterInObjectHierarchyReturnsConverterForParentClass()
Definition: PropertyMapperTest.php:195
‪TYPO3\CMS\Extbase\Tests\Functional\Property\PropertyMapperTest\findFirstEligibleTypeConverterInObjectHierarchyFindsConverterFromStringToObject
‪findFirstEligibleTypeConverterInObjectHierarchyFindsConverterFromStringToObject()
Definition: PropertyMapperTest.php:188
‪TYPO3\CMS\Extbase\Tests\Functional\Property\PropertyMapperTest\convertThrowsATargetNotFoundException
‪convertThrowsATargetNotFoundException()
Definition: PropertyMapperTest.php:70
‪TYPO3\CMS\Extbase\Tests\Functional\Property\PropertyMapperTest\convertThrowsAnExceptionIfNoTypeConverterCanBeFoundForTheConversionOfSimpleTypes
‪convertThrowsAnExceptionIfNoTypeConverterCanBeFoundForTheConversionOfSimpleTypes()
Definition: PropertyMapperTest.php:80
‪TYPO3Tests\BlogExample\Domain\Model\Blog
Definition: Blog.php:30
‪TYPO3\CMS\Extbase\Tests\Functional\Property\PropertyMapperTest\$testExtensionsToLoad
‪array $testExtensionsToLoad
Definition: PropertyMapperTest.php:40
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:25
‪TYPO3Tests\TypeConverterTest\Domain\Model\Cat
Definition: Cat.php:21
‪TYPO3\CMS\Extbase\Tests\Functional\Property\PropertyMapperTest\setUp
‪setUp()
Definition: PropertyMapperTest.php:45
‪TYPO3\CMS\Extbase\Tests\Functional\Property\PropertyMapperTest\doMappingReturnsTheSourceIfItIsAlreadyTheDesiredTypeWithoutCallingAConverter
‪doMappingReturnsTheSourceIfItIsAlreadyTheDesiredTypeWithoutCallingAConverter()
Definition: PropertyMapperTest.php:127
‪TYPO3\CMS\Extbase\Property\Exception\TargetNotFoundException
Definition: TargetNotFoundException.php:25