‪TYPO3CMS  11.5
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 
33 use TYPO3\TestingFramework\Core\Functional\FunctionalTestCase;
34 
35 class ‪PropertyMapperTest extends FunctionalTestCase
36 {
37  // @todo: Switch to a simple test extension that contains a test model, instead.
38  protected ‪$coreExtensionsToLoad = ['beuser'];
39 
44  {
45  $this->expectExceptionCode(1297951378);
46 
47  $class = new class () extends ‪ArrayConverter {};
49  $this->getContainer()->set(get_class($class), $class);
50  $this->get(PropertyMapper::class);
51  }
52 
57  {
58  // This test just increases the test coverage
59  $this->get(PropertyMapper::class)
60  ->convert('string', 'string');
61  }
62 
67  {
68  $propertyMapper = $this->get(PropertyMapper::class);
69 
70  self::assertNull($propertyMapper->convert('string', 'integer'));
71  self::assertNotEmpty($propertyMapper->getMessages());
72  }
73 
78  {
79  $this->expectException(TargetNotFoundException::class);
80  $this->expectExceptionCode(1297933823);
81 
82  $propertyMapper = $this->get(PropertyMapper::class);
83  $propertyMapper->convert(9999, BackendUser::class);
84  }
85 
90  {
91  $this->expectException(\Exception::class);
92  $this->expectExceptionCode(1297759968);
93  $this->expectExceptionMessage('Exception while property mapping at property path "": No converter found which can be used to convert from "integer" to "boolean"');
94 
95  $propertyMapper = $this->get(PropertyMapper::class);
96  $propertyMapper->convert(9999, 'boolean');
97  }
98 
103  {
104  $this->expectException(\Exception::class);
105  $this->expectExceptionCode(1297759968);
106  $this->expectExceptionMessage('Exception while property mapping at property path "": The target type was no string, but of type "NULL"');
107 
108  $propertyMapper = $this->get(PropertyMapper::class);
109  $propertyMapper->convert(9999, null);
110  }
111 
116  {
117  $propertyMapper = $this->get(PropertyMapper::class);
118  self::assertSame('', $propertyMapper->convert(null, 'string'));
119  }
120 
125  {
126  $this->expectException(\Exception::class);
127  $this->expectExceptionCode(1297759968);
128  $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.');
129 
130  $propertyMapper = $this->get(PropertyMapper::class);
131  $propertyMapper->convert(1, 'NonExistingClass');
132  }
133 
138  {
139  $this->expectException(\Exception::class);
140  $this->expectExceptionCode(1297759968);
141  $this->expectExceptionMessage('There exist at least two converters which handle the conversion to an interface with priority "10"');
142 
143  $converterOne = new class () extends ‪AbstractTypeConverter {
144  protected $priority = 10;
145  protected $sourceTypes = ['integer'];
146  protected $targetType = \Countable::class;
147 
148  public function convertFrom($source, string $targetType, array $convertedChildProperties = [], ‪PropertyMappingConfigurationInterface $configuration = null): bool
149  {
150  return true;
151  }
152  };
153 
154  $converterTwo = new class () extends ‪AbstractTypeConverter {
155  protected $priority = 10;
156  protected $sourceTypes = ['integer'];
157  protected $targetType = ExtendedCountableInterface::class;
158 
159  public function convertFrom($source, string $targetType, array $convertedChildProperties = [], ‪PropertyMappingConfigurationInterface $configuration = null): bool
160  {
161  return true;
162  }
163  };
164 
165  $counter = new class () implements ‪ExtendedCountableInterface {
166  public function count(): int
167  {
168  return 1;
169  }
170  };
171 
172  ‪ExtensionUtility::registerTypeConverter(get_class($converterOne));
173  $this->getContainer()->set(get_class($converterOne), $converterOne);
174  ‪ExtensionUtility::registerTypeConverter(get_class($converterTwo));
175  $this->getContainer()->set(get_class($converterTwo), $converterTwo);
176 
177  $propertyMapper = $this->get(PropertyMapper::class);
178  $propertyMapper->convert(1, get_class($counter));
179  }
180 
185  {
186  $objectStorage = new ‪ObjectStorage();
187 
188  $result = $this->get(PropertyMapper::class)->convert(
189  $objectStorage,
190  '\TYPO3\CMS\Extbase\Persistence\ObjectStorage<\TYPO3\CMS\Beuser\Domain\Model\BackendUser>'
191  );
192 
193  self::assertSame($objectStorage, $result);
194  }
195 
200  {
201  $class = new class () extends ‪IntegerConverter {
202  public function convertFrom($source, string $targetType, array $convertedChildProperties = [], ‪PropertyMappingConfigurationInterface $configuration = null): int
203  {
204  return 1575648246;
205  }
206  };
207 
208  $propertyMappingConfiguration = new ‪PropertyMappingConfiguration();
209  $propertyMappingConfiguration->setTypeConverter($class);
210 
211  $result = $this->get(PropertyMapper::class)->convert(
212  1,
213  'integer',
214  $propertyMappingConfiguration
215  );
216 
217  self::assertSame(1575648246, $result);
218  }
219 
224  {
225  $this->expectException(\Exception::class);
226  $this->expectExceptionCode(1297759968);
227  $this->expectExceptionMessage('The source is not of type string, array, float, integer or boolean, but of type "object"');
228 
229  $generator = static function () {
230  return 'string';
231  };
232 
233  $propertyMapper = $this->get(PropertyMapper::class);
234  $propertyMapper->convert($generator, 'string');
235  }
236 
241  {
242  $this->expectException(\Exception::class);
243  $this->expectExceptionCode(1297759968);
244  $this->expectExceptionMessage('Exception while property mapping at property path "": No converter found which can be used to convert from "integer" to "TYPO3\CMS\Extbase\Tests\Functional\Property\Fixtures\Cat"');
245 
246  ‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['extbase']['typeConverters'] = [];
247 
248  $result = $this->get(PropertyMapper::class)->convert(1, Cat::class);
249  self::assertNull($result);
250  }
251 
256  {
257  $converter = new class () extends ‪AbstractTypeConverter {
258  protected $priority = 10;
259  protected $sourceTypes = ['string'];
260  protected $targetType = Cat::class;
261 
262  public function convertFrom($source, string $targetType, array $convertedChildProperties = [], ‪PropertyMappingConfigurationInterface $configuration = null): ‪Cat
263  {
264  return new ‪Cat();
265  }
266  };
267 
268  ‪ExtensionUtility::registerTypeConverter(get_class($converter));
269  $this->getContainer()->set(get_class($converter), $converter);
270 
271  $result = $this->get(PropertyMapper::class)->convert('tigger', Cat::class);
272  self::assertInstanceOf(Cat::class, $result);
273  }
274 
279  {
280  $converter = new class () extends ‪AbstractTypeConverter {
281  protected $priority = 10;
282  protected $sourceTypes = ['string'];
283  protected $targetType = Animal::class;
284 
285  public function convertFrom($source, string $targetType, array $convertedChildProperties = [], ‪PropertyMappingConfigurationInterface $configuration = null): ‪Animal
286  {
287  return new ‪Animal();
288  }
289  };
290 
291  ‪ExtensionUtility::registerTypeConverter(get_class($converter));
292  $this->getContainer()->set(get_class($converter), $converter);
293 
294  $result = $this->get(PropertyMapper::class)->convert('tigger', Cat::class);
295  self::assertInstanceOf(Animal::class, $result);
296  }
297 
302  {
303  $converter = new class () extends ‪AbstractTypeConverter {
304  protected $priority = 10;
305  protected $sourceTypes = ['integer'];
306  protected $targetType = \Countable::class;
307 
308  public function convertFrom($source, string $targetType, array $convertedChildProperties = [], ‪PropertyMappingConfigurationInterface $configuration = null): array
309  {
310  return [];
311  }
312  };
313 
314  $counter = new class () implements \Countable {
315  public function count(): int
316  {
317  return 1;
318  }
319  };
320 
321  ‪ExtensionUtility::registerTypeConverter(get_class($converter));
322  $this->getContainer()->set(get_class($converter), $converter);
323 
324  $propertyMapper = $this->get(PropertyMapper::class);
325  $result = $propertyMapper->convert(1, get_class($counter));
326 
327  self::assertSame([], $result);
328  }
329 
334  {
335  $source = [
336  'color' => 'black',
337  ];
338 
339  $propertyMappingConfiguration = new ‪PropertyMappingConfiguration();
340  $propertyMappingConfiguration->allowAllProperties();
341 
342  $propertyMapper = $this->get(PropertyMapper::class);
344  $result = $propertyMapper->convert(
345  $source,
346  Cat::class,
347  $propertyMappingConfiguration
348  );
349 
350  self::assertInstanceOf(Cat::class, $result);
351  self::assertSame('black', $result->getColor());
352  }
353 
357  public function ‪skipPropertiesConfiguration(): void
358  {
359  $source = [
360  'color' => 'black',
361  ];
362 
363  $propertyMappingConfiguration = new ‪PropertyMappingConfiguration();
364  $propertyMappingConfiguration->skipProperties('color');
365 
366  $propertyMapper = $this->get(PropertyMapper::class);
368  $result = $propertyMapper->convert(
369  $source,
370  Cat::class,
371  $propertyMappingConfiguration
372  );
373 
374  self::assertInstanceOf(Cat::class, $result);
375  self::assertNull($result->getColor());
376  }
377 
382  {
383  $this->expectException(\Exception::class);
384  $this->expectExceptionCode(1297759968);
385  $this->expectExceptionMessage('It is not allowed to map property "color". You need to use $propertyMappingConfiguration->allowProperties(\'color\') to enable mapping of this property.');
386 
387  $source = [
388  'color' => 'black',
389  ];
390 
391  $propertyMappingConfiguration = new ‪PropertyMappingConfiguration();
392  $propertyMappingConfiguration->allowAllPropertiesExcept('color');
393 
394  $propertyMapper = $this->get(PropertyMapper::class);
395  $propertyMapper->convert(
396  $source,
397  Cat::class,
398  $propertyMappingConfiguration
399  );
400  }
401 
406  {
407  $source = [
408  'color' => 'black',
409  ];
410 
411  $propertyMappingConfiguration = new ‪PropertyMappingConfiguration();
412  $propertyMappingConfiguration->allowAllPropertiesExcept('color');
413  $propertyMappingConfiguration->skipUnknownProperties();
414 
415  $propertyMapper = $this->get(PropertyMapper::class);
417  $result = $propertyMapper->convert(
418  $source,
419  Cat::class,
420  $propertyMappingConfiguration
421  );
422 
423  self::assertInstanceOf(Cat::class, $result);
424  self::assertNull($result->getColor());
425  }
426 }
‪TYPO3\CMS\Extbase\Tests\Functional\Property\PropertyMapperTest\convertReturnsNullIfDoMappingReturnsAnError
‪convertReturnsNullIfDoMappingReturnsAnError()
Definition: PropertyMapperTest.php:66
‪TYPO3\CMS\Extbase\Tests\Functional\Property
‪TYPO3\CMS\Extbase\Tests\Functional\Property\PropertyMapperTest\convertCreatesAPropertyMappingConfigurationIfNotGiven
‪convertCreatesAPropertyMappingConfigurationIfNotGiven()
Definition: PropertyMapperTest.php:56
‪TYPO3\CMS\Extbase\Tests\Functional\Property\PropertyMapperTest\determineSourceTypeThrowsInvalidSourceExceptionForNonSupportedTypes
‪determineSourceTypeThrowsInvalidSourceExceptionForNonSupportedTypes()
Definition: PropertyMapperTest.php:223
‪TYPO3\CMS\Extbase\Tests\Functional\Property\Fixtures\Cat
Definition: Cat.php:21
‪TYPO3\CMS\Extbase\Tests\Functional\Property\PropertyMapperTest\allowAllPropertiesExceptWithSkipUnknownPropertiesConfiguration
‪allowAllPropertiesExceptWithSkipUnknownPropertiesConfiguration()
Definition: PropertyMapperTest.php:405
‪TYPO3\CMS\Extbase\Property\TypeConverter\IntegerConverter
Definition: IntegerConverter.php:27
‪TYPO3\CMS\Extbase\Tests\Functional\Property\PropertyMapperTest\findTypeConverterReturnsTheConverterFromThePropertyMappingConfiguration
‪findTypeConverterReturnsTheConverterFromThePropertyMappingConfiguration()
Definition: PropertyMapperTest.php:199
‪TYPO3\CMS\Extbase\Utility\ExtensionUtility
Definition: ExtensionUtility.php:29
‪TYPO3\CMS\Extbase\Tests\Functional\Property\PropertyMapperTest\skipPropertiesConfiguration
‪skipPropertiesConfiguration()
Definition: PropertyMapperTest.php:357
‪TYPO3\CMS\Extbase\Tests\Functional\Property\Fixtures\Animal
Definition: Animal.php:21
‪TYPO3\CMS\Extbase\Tests\Functional\Property\PropertyMapperTest
Definition: PropertyMapperTest.php:36
‪TYPO3\CMS\Extbase\Tests\Functional\Property\PropertyMapperTest\convertInternallyConvertsANullSourceToAnEmptyString
‪convertInternallyConvertsANullSourceToAnEmptyString()
Definition: PropertyMapperTest.php:115
‪TYPO3\CMS\Extbase\Utility\ExtensionUtility\registerTypeConverter
‪static registerTypeConverter($typeConverterClassName)
Definition: ExtensionUtility.php:367
‪TYPO3\CMS\Extbase\Property\PropertyMappingConfigurationInterface
Definition: PropertyMappingConfigurationInterface.php:22
‪TYPO3\CMS\Extbase\Persistence\ObjectStorage
Definition: ObjectStorage.php:32
‪TYPO3\CMS\Extbase\Tests\Functional\Property\PropertyMapperTest\findFirstEligibleTypeConverterInObjectHierarchyReturnsNullIfNoTypeConvertersExistForTheSourceType
‪findFirstEligibleTypeConverterInObjectHierarchyReturnsNullIfNoTypeConvertersExistForTheSourceType()
Definition: PropertyMapperTest.php:240
‪TYPO3\CMS\Extbase\Tests\Functional\Property\PropertyMapperTest\initializeObjectThrowsDuplicateTypeConverterException
‪initializeObjectThrowsDuplicateTypeConverterException()
Definition: PropertyMapperTest.php:43
‪TYPO3\CMS\Extbase\Property\PropertyMappingConfiguration
Definition: PropertyMappingConfiguration.php:22
‪TYPO3\CMS\Extbase\Tests\Functional\Property\PropertyMapperTest\defaultPropertyMappingConfiguration
‪defaultPropertyMappingConfiguration()
Definition: PropertyMapperTest.php:333
‪TYPO3\CMS\Extbase\Tests\Functional\Property\PropertyMapperTest\convertThrowsAnExceptionIfAtLeastTwoConvertersAreRegisteredThatHandleTheConversionToTheSameInterface
‪convertThrowsAnExceptionIfAtLeastTwoConvertersAreRegisteredThatHandleTheConversionToTheSameInterface()
Definition: PropertyMapperTest.php:137
‪TYPO3\CMS\Extbase\Tests\Functional\Property\PropertyMapperTest\allowAllPropertiesExceptConfiguration
‪allowAllPropertiesExceptConfiguration()
Definition: PropertyMapperTest.php:381
‪TYPO3\CMS\Extbase\Tests\Functional\Property\PropertyMapperTest\findFirstEligibleTypeConverterInObjectHierarchyReturnsConverterForInterfaces
‪findFirstEligibleTypeConverterInObjectHierarchyReturnsConverterForInterfaces()
Definition: PropertyMapperTest.php:301
‪TYPO3\CMS\Extbase\Property\PropertyMapper
Definition: PropertyMapper.php:39
‪TYPO3\CMS\Extbase\Tests\Functional\Property\PropertyMapperTest\convertThrowsAnExceptionIfTargetTypeIsANonExistingClass
‪convertThrowsAnExceptionIfTargetTypeIsANonExistingClass()
Definition: PropertyMapperTest.php:124
‪TYPO3\CMS\Extbase\Tests\Functional\Property\PropertyMapperTest\findFirstEligibleTypeConverterInObjectHierarchyReturnsConverterForParentClass
‪findFirstEligibleTypeConverterInObjectHierarchyReturnsConverterForParentClass()
Definition: PropertyMapperTest.php:278
‪TYPO3\CMS\Extbase\Property\TypeConverter\AbstractTypeConverter
Definition: AbstractTypeConverter.php:34
‪TYPO3\CMS\Extbase\Tests\Functional\Property\PropertyMapperTest\$coreExtensionsToLoad
‪$coreExtensionsToLoad
Definition: PropertyMapperTest.php:38
‪TYPO3\CMS\Extbase\Tests\Functional\Property\PropertyMapperTest\findFirstEligibleTypeConverterInObjectHierarchyFindsConverterFromStringToObject
‪findFirstEligibleTypeConverterInObjectHierarchyFindsConverterFromStringToObject()
Definition: PropertyMapperTest.php:255
‪TYPO3\CMS\Extbase\Tests\Functional\Property\PropertyMapperTest\convertThrowsATargetNotFoundException
‪convertThrowsATargetNotFoundException()
Definition: PropertyMapperTest.php:77
‪TYPO3\CMS\Extbase\Tests\Functional\Property\PropertyMapperTest\convertThrowsAnExceptionIfNoTypeConverterCanBeFoundForTheConversionOfSimpleTypes
‪convertThrowsAnExceptionIfNoTypeConverterCanBeFoundForTheConversionOfSimpleTypes()
Definition: PropertyMapperTest.php:89
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:25
‪TYPO3\CMS\Extbase\Property\TypeConverter\ArrayConverter
Definition: ArrayConverter.php:28
‪TYPO3\CMS\Extbase\Tests\Functional\Property\PropertyMapperTest\doMappingReturnsTheSourceIfItIsAlreadyTheDesiredTypeWithoutCallingAConverter
‪doMappingReturnsTheSourceIfItIsAlreadyTheDesiredTypeWithoutCallingAConverter()
Definition: PropertyMapperTest.php:184
‪TYPO3\CMS\Extbase\Tests\Functional\Property\PropertyMapperTest\convertThrowsAnExceptionIfTargetTypeIsNotAString
‪convertThrowsAnExceptionIfTargetTypeIsNotAString()
Definition: PropertyMapperTest.php:102
‪TYPO3\CMS\Beuser\Domain\Model\BackendUser
Definition: BackendUser.php:30
‪TYPO3\CMS\Extbase\Tests\Functional\Property\Fixtures\ExtendedCountableInterface
Definition: ExtendedCountableInterface.php:20
‪TYPO3\CMS\Extbase\Property\Exception\TargetNotFoundException
Definition: TargetNotFoundException.php:25