‪TYPO3CMS  10.4
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 {
41  {
42  static::expectExceptionCode(1297951378);
43 
44  $class = new class() extends ‪ArrayConverter {
45  };
47  $this->getContainer()->get(PropertyMapper::class);
48  }
49 
54  {
55  // This test just increases the test coverage
56  $this->getContainer()->get(PropertyMapper::class)
57  ->convert('string', 'string');
58  }
59 
64  {
65  $propertyMapper = $this->getContainer()->get(PropertyMapper::class);
66 
67  self::assertNull($propertyMapper->convert('string', 'integer'));
68  self::assertNotEmpty($propertyMapper->getMessages());
69  }
70 
75  {
76  static::expectException(TargetNotFoundException::class);
77  static::expectExceptionCode(1297933823);
78 
79  $propertyMapper = $this->getContainer()->get(PropertyMapper::class);
80  $propertyMapper->convert(9999, BackendUser::class);
81  }
82 
87  {
88  static::expectException(\Exception::class);
89  static::expectExceptionCode(1297759968);
90  static::expectExceptionMessage('Exception while property mapping at property path "": No converter found which can be used to convert from "integer" to "boolean"');
91 
92  $propertyMapper = $this->getContainer()->get(PropertyMapper::class);
93  $propertyMapper->convert(9999, 'boolean');
94  }
95 
100  {
101  static::expectException(\Exception::class);
102  static::expectExceptionCode(1297759968);
103  static::expectExceptionMessage('Exception while property mapping at property path "": The target type was no string, but of type "NULL"');
104 
105  $propertyMapper = $this->getContainer()->get(PropertyMapper::class);
106  $propertyMapper->convert(9999, null);
107  }
108 
113  {
114  $propertyMapper = $this->getContainer()->get(PropertyMapper::class);
115  self::assertSame('', $propertyMapper->convert(null, 'string'));
116  }
117 
122  {
123  static::expectException(\Exception::class);
124  static::expectExceptionCode(1297759968);
125  static::expectExceptionMessage('Exception while property mapping at property path "": Could not find a suitable type converter for "NonExistingClass" because no such class or interface exists.');
126 
127  $propertyMapper = $this->getContainer()->get(PropertyMapper::class);
128  $propertyMapper->convert(1, 'NonExistingClass');
129  }
130 
135  {
136  static::expectException(\Exception::class);
137  static::expectExceptionCode(1297759968);
138  static::expectExceptionMessage('There exist at least two converters which handle the conversion to an interface with priority "10"');
139 
140  $converterOne = new class() extends ‪AbstractTypeConverter {
141  protected $priority = 10;
142  protected $sourceTypes = ['integer'];
143  protected $targetType = \Countable::class;
144 
145  public function convertFrom($source, string $targetType, array $convertedChildProperties = [], ‪PropertyMappingConfigurationInterface $configuration = null)
146  {
147  return true;
148  }
149  };
150 
151  $converterTwo = new class() extends ‪AbstractTypeConverter {
152  protected $priority = 10;
153  protected $sourceTypes = ['integer'];
154  protected $targetType = ExtendedCountableInterface::class;
155 
156  public function convertFrom($source, string $targetType, array $convertedChildProperties = [], ‪PropertyMappingConfigurationInterface $configuration = null)
157  {
158  return true;
159  }
160  };
161 
162  $counter = new class() implements ‪ExtendedCountableInterface {
163  public function count()
164  {
165  return 1;
166  }
167  };
168 
169  ‪ExtensionUtility::registerTypeConverter(get_class($converterOne));
170  ‪ExtensionUtility::registerTypeConverter(get_class($converterTwo));
171 
172  $propertyMapper = $this->getContainer()->get(PropertyMapper::class);
173  $propertyMapper->convert(1, get_class($counter));
174  }
175 
180  {
181  $objectStorage = new ‪ObjectStorage();
182 
183  $result = $this->getContainer()->get(PropertyMapper::class)->convert(
184  $objectStorage,
185  '\TYPO3\CMS\Extbase\Persistence\ObjectStorage<\TYPO3\CMS\Extbase\Domain\Model\BackendUser>'
186  );
187 
188  self::assertSame($objectStorage, $result);
189  }
190 
195  {
196  $class = new class() extends ‪IntegerConverter {
197  public function convertFrom($source, string $targetType, array $convertedChildProperties = [], ‪PropertyMappingConfigurationInterface $configuration = null)
198  {
199  return 1575648246;
200  }
201  };
202 
203  $propertyMappingConfiguration = new ‪PropertyMappingConfiguration();
204  $propertyMappingConfiguration->setTypeConverter($class);
205 
206  $result = $this->getContainer()->get(PropertyMapper::class)->convert(
207  1,
208  'integer',
209  $propertyMappingConfiguration
210  );
211 
212  self::assertSame(1575648246, $result);
213  }
214 
219  {
220  static::expectException(\Exception::class);
221  static::expectExceptionCode(1297759968);
222  static::expectExceptionMessage('The source is not of type string, array, float, integer or boolean, but of type "object"');
223 
224  $generator = function () {
225  return 'string';
226  };
227 
228  $propertyMapper = $this->getContainer()->get(PropertyMapper::class);
229  $propertyMapper->convert($generator, 'string');
230  }
231 
236  {
237  static::expectException(\Exception::class);
238  static::expectExceptionCode(1297759968);
239  static::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"');
240 
241  ‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['extbase']['typeConverters'] = [];
242 
243  $result = $this->getContainer()->get(PropertyMapper::class)->convert(1, Cat::class);
244  self::assertNull($result);
245  }
246 
251  {
252  $converter = new class() extends ‪AbstractTypeConverter {
253  protected $priority = 10;
254  protected $sourceTypes = ['string'];
255  protected $targetType = Cat::class;
256 
257  public function convertFrom($source, string $targetType, array $convertedChildProperties = [], ‪PropertyMappingConfigurationInterface $configuration = null)
258  {
259  return new ‪Cat();
260  }
261  };
262 
263  ‪ExtensionUtility::registerTypeConverter(get_class($converter));
264 
265  $result = $this->getContainer()->get(PropertyMapper::class)->convert('tigger', Cat::class);
266  self::assertInstanceOf(Cat::class, $result);
267  }
268 
273  {
274  $converter = new class() extends ‪AbstractTypeConverter {
275  protected $priority = 10;
276  protected $sourceTypes = ['string'];
277  protected $targetType = Animal::class;
278 
279  public function convertFrom($source, string $targetType, array $convertedChildProperties = [], ‪PropertyMappingConfigurationInterface $configuration = null)
280  {
281  return new ‪Animal();
282  }
283  };
284 
285  ‪ExtensionUtility::registerTypeConverter(get_class($converter));
286 
287  $result = $this->getContainer()->get(PropertyMapper::class)->convert('tigger', Cat::class);
288  self::assertInstanceOf(Animal::class, $result);
289  }
290 
295  {
296  $converter = new class() extends ‪AbstractTypeConverter {
297  protected $priority = 10;
298  protected $sourceTypes = ['integer'];
299  protected $targetType = \Countable::class;
300 
301  public function convertFrom($source, string $targetType, array $convertedChildProperties = [], ‪PropertyMappingConfigurationInterface $configuration = null)
302  {
303  return [];
304  }
305  };
306 
307  $counter = new class() implements \Countable {
308  public function count()
309  {
310  return 1;
311  }
312  };
313 
314  ‪ExtensionUtility::registerTypeConverter(get_class($converter));
315 
316  $propertyMapper = $this->getContainer()->get(PropertyMapper::class);
317  $result = $propertyMapper->convert(1, get_class($counter));
318 
319  self::assertSame([], $result);
320  }
321 
326  {
327  $source = [
328  'color' => 'black',
329  ];
330 
331  $propertyMappingConfiguration = new ‪PropertyMappingConfiguration();
332  $propertyMappingConfiguration->allowAllProperties();
333 
334  $propertyMapper = $this->getContainer()->get(PropertyMapper::class);
336  $result = $propertyMapper->convert(
337  $source,
338  Cat::class,
339  $propertyMappingConfiguration
340  );
341 
342  self::assertInstanceOf(Cat::class, $result);
343  self::assertSame('black', $result->getColor());
344  }
345 
350  {
351  $source = [
352  'color' => 'black',
353  ];
354 
355  $propertyMappingConfiguration = new ‪PropertyMappingConfiguration();
356  $propertyMappingConfiguration->skipProperties('color');
357 
358  $propertyMapper = $this->getContainer()->get(PropertyMapper::class);
360  $result = $propertyMapper->convert(
361  $source,
362  Cat::class,
363  $propertyMappingConfiguration
364  );
365 
366  self::assertInstanceOf(Cat::class, $result);
367  self::assertNull($result->getColor());
368  }
369 
374  {
375  static::expectException(\Exception::class);
376  static::expectExceptionCode(1297759968);
377  static::expectExceptionMessage('It is not allowed to map property "color". You need to use $propertyMappingConfiguration->allowProperties(\'color\') to enable mapping of this property.');
378 
379  $source = [
380  'color' => 'black',
381  ];
382 
383  $propertyMappingConfiguration = new ‪PropertyMappingConfiguration();
384  $propertyMappingConfiguration->allowAllPropertiesExcept('color');
385 
386  $propertyMapper = $this->getContainer()->get(PropertyMapper::class);
387  $propertyMapper->convert(
388  $source,
389  Cat::class,
390  $propertyMappingConfiguration
391  );
392  }
393 
398  {
399  $source = [
400  'color' => 'black',
401  ];
402 
403  $propertyMappingConfiguration = new ‪PropertyMappingConfiguration();
404  $propertyMappingConfiguration->allowAllPropertiesExcept('color');
405  $propertyMappingConfiguration->skipUnknownProperties();
406 
407  $propertyMapper = $this->getContainer()->get(PropertyMapper::class);
409  $result = $propertyMapper->convert(
410  $source,
411  Cat::class,
412  $propertyMappingConfiguration
413  );
414 
415  self::assertInstanceOf(Cat::class, $result);
416  self::assertNull($result->getColor());
417  }
418 }
‪TYPO3\CMS\Extbase\Tests\Functional\Property\PropertyMapperTest\convertReturnsNullIfDoMappingReturnsAnError
‪convertReturnsNullIfDoMappingReturnsAnError()
Definition: PropertyMapperTest.php:63
‪TYPO3\CMS\Extbase\Tests\Functional\Property
‪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:218
‪TYPO3\CMS\Extbase\Tests\Functional\Property\Fixtures\Cat
Definition: Cat.php:21
‪TYPO3\CMS\Extbase\Tests\Functional\Property\PropertyMapperTest\allowAllPropertiesExceptWithSkipUnknownPropertiesConfiguration
‪allowAllPropertiesExceptWithSkipUnknownPropertiesConfiguration()
Definition: PropertyMapperTest.php:397
‪TYPO3\CMS\Extbase\Property\TypeConverter\IntegerConverter
Definition: IntegerConverter.php:27
‪TYPO3\CMS\Extbase\Tests\Functional\Property\PropertyMapperTest\findTypeConverterReturnsTheConverterFromThePropertyMappingConfiguration
‪findTypeConverterReturnsTheConverterFromThePropertyMappingConfiguration()
Definition: PropertyMapperTest.php:194
‪TYPO3\CMS\Extbase\Utility\ExtensionUtility
Definition: ExtensionUtility.php:30
‪TYPO3\CMS\Extbase\Tests\Functional\Property\PropertyMapperTest\skipPropertiesConfiguration
‪skipPropertiesConfiguration()
Definition: PropertyMapperTest.php:349
‪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:112
‪TYPO3\CMS\Extbase\Utility\ExtensionUtility\registerTypeConverter
‪static registerTypeConverter($typeConverterClassName)
Definition: ExtensionUtility.php:399
‪TYPO3\CMS\Extbase\Property\PropertyMappingConfigurationInterface
Definition: PropertyMappingConfigurationInterface.php:22
‪TYPO3\CMS\Extbase\Persistence\ObjectStorage
Definition: ObjectStorage.php:28
‪TYPO3\CMS\Extbase\Tests\Functional\Property\PropertyMapperTest\findFirstEligibleTypeConverterInObjectHierarchyReturnsNullIfNoTypeConvertersExistForTheSourceType
‪findFirstEligibleTypeConverterInObjectHierarchyReturnsNullIfNoTypeConvertersExistForTheSourceType()
Definition: PropertyMapperTest.php:235
‪TYPO3\CMS\Extbase\Tests\Functional\Property\PropertyMapperTest\initializeObjectThrowsDuplicateTypeConverterException
‪initializeObjectThrowsDuplicateTypeConverterException()
Definition: PropertyMapperTest.php:40
‪TYPO3\CMS\Extbase\Property\PropertyMappingConfiguration
Definition: PropertyMappingConfiguration.php:22
‪TYPO3\CMS\Extbase\Tests\Functional\Property\PropertyMapperTest\defaultPropertyMappingConfiguration
‪defaultPropertyMappingConfiguration()
Definition: PropertyMapperTest.php:325
‪TYPO3\CMS\Extbase\Tests\Functional\Property\PropertyMapperTest\convertThrowsAnExceptionIfAtLeastTwoConvertersAreRegisteredThatHandleTheConversionToTheSameInterface
‪convertThrowsAnExceptionIfAtLeastTwoConvertersAreRegisteredThatHandleTheConversionToTheSameInterface()
Definition: PropertyMapperTest.php:134
‪TYPO3\CMS\Extbase\Tests\Functional\Property\PropertyMapperTest\allowAllPropertiesExceptConfiguration
‪allowAllPropertiesExceptConfiguration()
Definition: PropertyMapperTest.php:373
‪TYPO3\CMS\Extbase\Tests\Functional\Property\PropertyMapperTest\findFirstEligibleTypeConverterInObjectHierarchyReturnsConverterForInterfaces
‪findFirstEligibleTypeConverterInObjectHierarchyReturnsConverterForInterfaces()
Definition: PropertyMapperTest.php:294
‪TYPO3\CMS\Extbase\Property\PropertyMapper
Definition: PropertyMapper.php:37
‪TYPO3\CMS\Extbase\Tests\Functional\Property\PropertyMapperTest\convertThrowsAnExceptionIfTargetTypeIsANonExistingClass
‪convertThrowsAnExceptionIfTargetTypeIsANonExistingClass()
Definition: PropertyMapperTest.php:121
‪TYPO3\CMS\Extbase\Tests\Functional\Property\PropertyMapperTest\findFirstEligibleTypeConverterInObjectHierarchyReturnsConverterForParentClass
‪findFirstEligibleTypeConverterInObjectHierarchyReturnsConverterForParentClass()
Definition: PropertyMapperTest.php:272
‪TYPO3\CMS\Extbase\Property\TypeConverter\AbstractTypeConverter
Definition: AbstractTypeConverter.php:34
‪TYPO3\CMS\Extbase\Tests\Functional\Property\PropertyMapperTest\findFirstEligibleTypeConverterInObjectHierarchyFindsConverterFromStringToObject
‪findFirstEligibleTypeConverterInObjectHierarchyFindsConverterFromStringToObject()
Definition: PropertyMapperTest.php:250
‪TYPO3\CMS\Extbase\Tests\Functional\Property\PropertyMapperTest\convertThrowsATargetNotFoundException
‪convertThrowsATargetNotFoundException()
Definition: PropertyMapperTest.php:74
‪TYPO3\CMS\Extbase\Tests\Functional\Property\PropertyMapperTest\convertThrowsAnExceptionIfNoTypeConverterCanBeFoundForTheConversionOfSimpleTypes
‪convertThrowsAnExceptionIfNoTypeConverterCanBeFoundForTheConversionOfSimpleTypes()
Definition: PropertyMapperTest.php:86
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:5
‪TYPO3\CMS\Extbase\Property\TypeConverter\ArrayConverter
Definition: ArrayConverter.php:28
‪TYPO3\CMS\Extbase\Tests\Functional\Property\PropertyMapperTest\doMappingReturnsTheSourceIfItIsAlreadyTheDesiredTypeWithoutCallingAConverter
‪doMappingReturnsTheSourceIfItIsAlreadyTheDesiredTypeWithoutCallingAConverter()
Definition: PropertyMapperTest.php:179
‪TYPO3\CMS\Extbase\Tests\Functional\Property\PropertyMapperTest\convertThrowsAnExceptionIfTargetTypeIsNotAString
‪convertThrowsAnExceptionIfTargetTypeIsNotAString()
Definition: PropertyMapperTest.php:99
‪TYPO3\CMS\Extbase\Domain\Model\BackendUser
Definition: BackendUser.php:25
‪TYPO3\CMS\Extbase\Tests\Functional\Property\Fixtures\ExtendedCountableInterface
Definition: ExtendedCountableInterface.php:21
‪TYPO3\CMS\Extbase\Property\Exception\TargetNotFoundException
Definition: TargetNotFoundException.php:26