‪TYPO3CMS  11.5
ObjectConverterTest.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 
29 use TYPO3\TestingFramework\Core\Functional\FunctionalTestCase;
30 
31 class ‪ObjectConverterTest extends FunctionalTestCase
32 {
36  protected ‪$initializeDatabase = false;
37 
41  public function ‪convertToObject(): void
42  {
43  $propertyMapper = $this->get(PropertyMapper::class);
44 
45  $model = new class () extends ‪AbstractEntity {
49  protected $name;
50 
51  public function setName(string $name): void
52  {
53  $this->name = $name;
54  }
55  };
56 
58  $object = $propertyMapper->convert(['name' => 'John Doe'], get_class($model));
59 
60  self::assertInstanceOf(get_class($model), $object);
61  self::assertSame('John Doe', $object->_getProperty('name'));
62  }
63 
67  public function ‪convertToObjectViaTypeInArray(): void
68  {
69  $propertyMapper = $this->get(PropertyMapper::class);
70 
71  $propertyMapperConfiguration = new ‪PropertyMappingConfiguration();
72  $propertyMapperConfiguration->allowAllProperties();
73  $propertyMapperConfiguration->setTypeConverterOption(
74  ObjectConverter::class,
76  true
77  );
78 
80  $object = $propertyMapper->convert(
81  ['name' => 'John Doe', '__type' => Cat::class],
82  Animal::class,
83  $propertyMapperConfiguration
84  );
85 
86  self::assertInstanceOf(Cat::class, $object);
87  self::assertSame('John Doe', $object->getName());
88  }
89 
94  {
95  $class = new class () {
96  public $name;
97  };
98 
99  $propertyMapper = $this->get(PropertyMapper::class);
100  $propertyMapperConfiguration = new ‪PropertyMappingConfiguration();
101  $propertyMapperConfiguration->allowAllProperties();
102  $propertyMapperConfiguration
103  ->forProperty('name')
104  ->setTypeConverterOption(
105  ObjectConverter::class,
107  'string'
108  )
109  ;
110 
111  $result = $propertyMapper->convert(
112  ['name' => 'foo'],
113  get_class($class),
114  $propertyMapperConfiguration
115  );
116 
117  self::assertSame('foo', $result->name);
118  }
119 
124  {
125  $class = new class ('') {
126  private $name;
127  public function __construct(string $name)
128  {
129  $this->name = $name;
130  }
131  public function getName(): string
132  {
133  return $this->name;
134  }
135  };
136 
137  $propertyMapper = $this->get(PropertyMapper::class);
138  $propertyMapperConfiguration = new ‪PropertyMappingConfiguration();
139  $propertyMapperConfiguration->allowAllProperties();
140 
141  $result = $propertyMapper->convert(
142  ['name' => 'foo'],
143  get_class($class),
144  $propertyMapperConfiguration
145  );
146 
147  self::assertSame('foo', $result->getName());
148  }
149 
153  public function collectionTypesAreConsideredInMapping(): void
154  {
155  $class = new class () {
159  protected ‪ObjectStorage $collection;
160 
164  public function getCollection(): ‪ObjectStorage
165  {
166  return $this->collection;
167  }
168 
172  public function setCollection(‪ObjectStorage $collection): void
173  {
174  $this->collection = $collection;
175  }
176  };
177 
178  $propertyMapper = $this->get(PropertyMapper::class);
179  $propertyMapperConfiguration = new ‪PropertyMappingConfiguration();
180  $propertyMapperConfiguration->allowAllProperties();
181  $propertyMapperConfiguration->forProperty('collection.*')->allowAllProperties();
182 
183  $result = $propertyMapper->convert(
184  ['collection' => [['name' => 'Zebra'], ['name' => 'Lion']]],
185  get_class($class),
186  $propertyMapperConfiguration
187  );
188 
189  self::assertSame(2, $result->getCollection()->count());
190  self::assertSame('Zebra', $result->getCollection()->current()->getName());
191  $result->getCollection()->next();
192  self::assertSame('Lion', $result->getCollection()->current()->getName());
193  }
194 
199  {
200  $class = new class () {
201  private $name;
202  public function setName(string $name): void
203  {
204  $this->name = $name;
205  }
206  public function getName(): string
207  {
208  return $this->name;
209  }
210  };
211 
212  $propertyMapper = $this->get(PropertyMapper::class);
213  $propertyMapperConfiguration = new ‪PropertyMappingConfiguration();
214  $propertyMapperConfiguration->allowAllProperties();
215 
216  $result = $propertyMapper->convert(
217  ['name' => 'foo'],
218  get_class($class),
219  $propertyMapperConfiguration
220  );
221 
222  self::assertSame('foo', $result->getName());
223  }
224 
229  {
230  $class = new class () {};
231 
232  $className = get_class($class);
233  $propertyName = 'name';
234 
235  $this->expectException(Exception::class);
236  $this->expectExceptionCode(1297759968);
237  $this->expectExceptionMessage('Exception while property mapping at property path "": Type of child property "' . $propertyName . '" of class "' . $className . '" could not be derived from constructor arguments as said class does not have a constructor defined.');
238 
239  $propertyMapper = $this->get(PropertyMapper::class);
240  $propertyMapperConfiguration = new ‪PropertyMappingConfiguration();
241  $propertyMapperConfiguration->allowAllProperties();
242 
243  $propertyMapper->convert(
244  [$propertyName => 'foo'],
245  $className,
246  $propertyMapperConfiguration
247  );
248  }
249 
254  {
255  $class = new class () {
256  public function __construct() {}
257  };
258 
259  $className = get_class($class);
260  $propertyName = 'name';
261 
262  $this->expectException(Exception::class);
263  $this->expectExceptionCode(1297759968);
264  $this->expectExceptionMessage('Exception while property mapping at property path "": Type of child property "' . $propertyName . '" of class "' . $className . '" could not be derived from constructor arguments as the constructor of said class does not have a parameter with property name "' . $propertyName . '".');
265 
266  $propertyMapper = $this->get(PropertyMapper::class);
267  $propertyMapperConfiguration = new ‪PropertyMappingConfiguration();
268  $propertyMapperConfiguration->allowAllProperties();
269 
270  $propertyMapper->convert(
271  [$propertyName => 'foo'],
272  $className,
273  $propertyMapperConfiguration
274  );
275  }
276 
281  {
282  $class = new class () {
283  public function __construct($name = null) {}
284  };
285 
286  $className = get_class($class);
287  $propertyName = 'name';
288 
289  $this->expectException(Exception::class);
290  $this->expectExceptionCode(1297759968);
291  $this->expectExceptionMessage('Exception while property mapping at property path "": Type of child property "' . $propertyName . '" of class "' . $className . '" could not be derived from constructor argument "' . $propertyName . '". This usually happens if the argument misses a type hint.');
292 
293  $propertyMapper = $this->get(PropertyMapper::class);
294  $propertyMapperConfiguration = new ‪PropertyMappingConfiguration();
295  $propertyMapperConfiguration->allowAllProperties();
296 
297  $propertyMapper->convert(
298  [$propertyName => 'foo'],
299  $className,
300  $propertyMapperConfiguration
301  );
302  }
303 
308  {
309  $this->expectException(Exception::class);
310  $this->expectExceptionCode(1297759968);
311  $this->expectExceptionMessage('Exception while property mapping at property path "": Setter for property "name" had no type hint or documentation in target object of type "');
312 
313  $class = new class () {
314  public function setName($name): void {}
315  };
316 
317  $propertyMapper = $this->get(PropertyMapper::class);
318  $propertyMapperConfiguration = new ‪PropertyMappingConfiguration();
319  $propertyMapperConfiguration->allowAllProperties();
320 
321  $propertyMapper->convert(
322  ['name' => 'foo'],
323  get_class($class),
324  $propertyMapperConfiguration
325  );
326  }
327 
332  {
333  $this->expectException(Exception::class);
334  $this->expectExceptionCode(1297759968);
335  $this->expectExceptionMessage('Exception while property mapping at property path "": Property "name" having a value of type "string" could not be set in target object of type "');
336 
337  $class = new class () {
338  private $name;
339  };
340 
341  $propertyMapper = $this->get(PropertyMapper::class);
342  $propertyMapperConfiguration = new ‪PropertyMappingConfiguration();
343  $propertyMapperConfiguration->allowAllProperties();
344  $propertyMapperConfiguration
345  ->forProperty('name')
346  ->setTypeConverterOption(
347  ObjectConverter::class,
349  'string'
350  )
351  ;
352 
353  $propertyMapper->convert(
354  ['name' => 'foo'],
355  get_class($class),
356  $propertyMapperConfiguration
357  );
358  }
359 
364  {
365  $class = new class ('', '') {
366  public $name;
367  public $color;
368  public function __construct(string $name, ?string $color = 'red')
369  {
370  $this->name = $name;
371  $this->color = $color;
372  }
373  };
374 
375  $result = $this->get(PropertyMapper::class)->convert(
376  ['name' => 'foo'],
377  get_class($class)
378  );
379 
380  self::assertSame('foo', $result->name);
381  self::assertSame('red', $result->color);
382  }
383 
388  {
389  $this->expectException(Exception::class);
390  $this->expectExceptionCode(1297759968);
391  $this->expectExceptionMessage('Exception while property mapping at property path "": Missing constructor argument "color" for object of type "');
392 
393  $class = new class ('', '') {
394  public $name;
395  public $color;
396  public function __construct(string $name, string $color)
397  {
398  $this->name = $name;
399  $this->color = $color;
400  }
401  };
402 
403  $this->get(PropertyMapper::class)->convert(
404  ['name' => 'foo'],
405  get_class($class)
406  );
407  }
408 
413  {
414  $this->expectException(Exception::class);
415  $this->expectExceptionCode(1297759968);
416  $this->expectExceptionMessage('Exception while property mapping at property path "": Override of target type not allowed. To enable this, you need to set the PropertyMappingConfiguration Value "CONFIGURATION_OVERRIDE_TARGET_TYPE_ALLOWED" to TRUE.');
417 
418  $class = new class () {};
419 
420  $this->get(PropertyMapper::class)->convert(
421  ['__type' => Animal::class],
422  get_class($class)
423  );
424  }
425 
430  {
431  $this->expectException(Exception::class);
432  $this->expectExceptionCode(1297759968);
433  $this->expectExceptionMessage('Exception while property mapping at property path "": The given type "TYPO3\CMS\Extbase\Tests\Functional\Property\Fixtures\Animal" is not a subtype of "');
434 
435  $class = new class () {};
436 
437  $propertyMapperConfiguration = new ‪PropertyMappingConfiguration();
438  $propertyMapperConfiguration->allowAllProperties();
439  $propertyMapperConfiguration->setTypeConverterOption(
440  ObjectConverter::class,
442  true
443  );
444 
445  $this->get(PropertyMapper::class)->convert(
446  ['__type' => Animal::class],
447  get_class($class),
448  $propertyMapperConfiguration
449  );
450  }
451 }
‪TYPO3\CMS\Extbase\Tests\Functional\Property\TypeConverter\ObjectConverterTest\getTargetTypeForSourceThrowsInvalidPropertyMappingConfigurationExceptionIfTargetTypeOverridingIsNotAllowed
‪getTargetTypeForSourceThrowsInvalidPropertyMappingConfigurationExceptionIfTargetTypeOverridingIsNotAllowed()
Definition: ObjectConverterTest.php:410
‪TYPO3\CMS\Extbase\Property\TypeConverter\ObjectConverter\CONFIGURATION_TARGET_TYPE
‪const CONFIGURATION_TARGET_TYPE
Definition: ObjectConverter.php:41
‪TYPO3\CMS\Extbase\Property\Exception
Definition: DuplicateObjectException.php:18
‪TYPO3\CMS\Extbase\Tests\Functional\Property\TypeConverter\ObjectConverterTest\buildObjectUsesDefaultValueOfOptionalConstructorArguments
‪buildObjectUsesDefaultValueOfOptionalConstructorArguments()
Definition: ObjectConverterTest.php:361
‪TYPO3\CMS\Extbase\Tests\Functional\Property\Fixtures\Cat
Definition: Cat.php:21
‪TYPO3\CMS\Extbase\Tests\Functional\Property\TypeConverter\ObjectConverterTest\getTypeOfChildPropertyReturnsTypeDefinedByConstructorArgument
‪getTypeOfChildPropertyReturnsTypeDefinedByConstructorArgument()
Definition: ObjectConverterTest.php:121
‪TYPO3\CMS\Extbase\Tests\Functional\Property\TypeConverter\ObjectConverterTest\getTypeOfChildPropertyThrowsInvalidTargetExceptionIfPropertySetterDoesNotDefineAType
‪getTypeOfChildPropertyThrowsInvalidTargetExceptionIfPropertySetterDoesNotDefineAType()
Definition: ObjectConverterTest.php:305
‪TYPO3\CMS\Extbase\Tests\Functional\Property\TypeConverter\ObjectConverterTest\getTypeOfChildPropertyThrowsInvalidTargetExceptionIfPropertyTypeCannotBeDerivedFromExistingConstructorArgument
‪getTypeOfChildPropertyThrowsInvalidTargetExceptionIfPropertyTypeCannotBeDerivedFromExistingConstructorArgument()
Definition: ObjectConverterTest.php:278
‪TYPO3\CMS\Extbase\Tests\Functional\Property\Fixtures\Animal
Definition: Animal.php:21
‪TYPO3\CMS\Extbase\Tests\Functional\Property\TypeConverter\ObjectConverterTest\getTypeOfChildPropertyThrowsInvalidTargetExceptionIfPropertyTypeCannotBeDerivedFromNonExistingConstructorArgument
‪getTypeOfChildPropertyThrowsInvalidTargetExceptionIfPropertyTypeCannotBeDerivedFromNonExistingConstructorArgument()
Definition: ObjectConverterTest.php:251
‪TYPO3\CMS\Extbase\Tests\Functional\Property\TypeConverter\ObjectConverterTest\getTypeOfChildPropertyReturnsTypeDefinedByPropertyMappingConfiguration
‪getTypeOfChildPropertyReturnsTypeDefinedByPropertyMappingConfiguration()
Definition: ObjectConverterTest.php:91
‪TYPO3\CMS\Extbase\Tests\Functional\Property\TypeConverter\ObjectConverterTest
Definition: ObjectConverterTest.php:32
‪TYPO3\CMS\Extbase\Tests\Functional\Property\TypeConverter\ObjectConverterTest\convertFromThrowsInvalidTargetExceptionIfPropertiesCannotBeSet
‪convertFromThrowsInvalidTargetExceptionIfPropertiesCannotBeSet()
Definition: ObjectConverterTest.php:329
‪TYPO3\CMS\Extbase\DomainObject\AbstractEntity
Definition: AbstractEntity.php:22
‪TYPO3\CMS\Extbase\Property\TypeConverter\ObjectConverter\CONFIGURATION_OVERRIDE_TARGET_TYPE_ALLOWED
‪const CONFIGURATION_OVERRIDE_TARGET_TYPE_ALLOWED
Definition: ObjectConverter.php:46
‪TYPO3\CMS\Extbase\Persistence\ObjectStorage
Definition: ObjectStorage.php:32
‪TYPO3\CMS\Extbase\Tests\Functional\Property\TypeConverter
Definition: ArrayConverterTest.php:18
‪TYPO3\CMS\Extbase\Tests\Functional\Property\TypeConverter\ObjectConverterTest\getTypeOfChildPropertyReturnsTypeDefinedBySetter
‪getTypeOfChildPropertyReturnsTypeDefinedBySetter()
Definition: ObjectConverterTest.php:196
‪TYPO3\CMS\Extbase\Tests\Functional\Property\TypeConverter\ObjectConverterTest\buildObjectThrowsInvalidTargetExceptionIfMandatoryConstructorArgumentIsMissing
‪buildObjectThrowsInvalidTargetExceptionIfMandatoryConstructorArgumentIsMissing()
Definition: ObjectConverterTest.php:385
‪TYPO3\CMS\Extbase\Property\PropertyMappingConfiguration
Definition: PropertyMappingConfiguration.php:22
‪TYPO3\CMS\Extbase\DomainObject\DomainObjectInterface
Definition: DomainObjectInterface.php:29
‪TYPO3\CMS\Extbase\Property\PropertyMapper
Definition: PropertyMapper.php:39
‪TYPO3\CMS\Extbase\Property\TypeConverter\ObjectConverter
Definition: ObjectConverter.php:37
‪TYPO3\CMS\Extbase\Tests\Functional\Property\TypeConverter\ObjectConverterTest\getTypeOfChildPropertyThrowsInvalidTargetExceptionIfPropertyIsNotAccessible
‪getTypeOfChildPropertyThrowsInvalidTargetExceptionIfPropertyIsNotAccessible()
Definition: ObjectConverterTest.php:226
‪TYPO3\CMS\Extbase\Tests\Functional\Property\TypeConverter\ObjectConverterTest\$initializeDatabase
‪bool $initializeDatabase
Definition: ObjectConverterTest.php:35
‪TYPO3\CMS\Extbase\Tests\Functional\Property\TypeConverter\ObjectConverterTest\getTargetTypeForSourceThrowsInvalidDataTypeExceptionIfOverriddenTargetTypeIsNotASubtypeOfOriginalTargetType
‪getTargetTypeForSourceThrowsInvalidDataTypeExceptionIfOverriddenTargetTypeIsNotASubtypeOfOriginalTargetType()
Definition: ObjectConverterTest.php:427
‪TYPO3\CMS\Extbase\Tests\Functional\Property\TypeConverter\ObjectConverterTest\convertToObjectViaTypeInArray
‪convertToObjectViaTypeInArray()
Definition: ObjectConverterTest.php:65
‪TYPO3\CMS\Extbase\Tests\Functional\Property\TypeConverter\ObjectConverterTest\convertToObject
‪convertToObject()
Definition: ObjectConverterTest.php:40