‪TYPO3CMS  10.4
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 
27 use TYPO3\TestingFramework\Core\Functional\FunctionalTestCase;
28 
29 class ‪ObjectConverterTest extends FunctionalTestCase
30 {
34  public function ‪convertToObject()
35  {
36  $propertyMapper = $this->getContainer()->get(PropertyMapper::class);
37 
38  $model = new class() extends ‪AbstractEntity {
42  protected $name;
43 
44  public function setName(string $name)
45  {
46  $this->name = $name;
47  }
48  };
49 
51  $object = $propertyMapper->convert(['name' => 'John Doe'], get_class($model));
52 
53  self::assertInstanceOf(get_class($model), $object);
54  self::assertSame('John Doe', $object->_getProperty('name'));
55  }
56 
60  public function ‪convertToObjectViaTypeInArray()
61  {
62  $propertyMapper = $this->getContainer()->get(PropertyMapper::class);
63 
64  $propertyMapperConfiguration = new ‪PropertyMappingConfiguration();
65  $propertyMapperConfiguration->allowAllProperties();
66  $propertyMapperConfiguration->setTypeConverterOption(
67  ObjectConverter::class,
69  true
70  );
71 
73  $object = $propertyMapper->convert(
74  ['name' => 'John Doe', '__type' => Cat::class],
75  Animal::class,
76  $propertyMapperConfiguration
77  );
78 
79  self::assertInstanceOf(Cat::class, $object);
80  self::assertSame('John Doe', $object->getName());
81  }
82 
87  {
88  $class = new class() {
89  public $name;
90  };
91 
92  $propertyMapper = $this->getContainer()->get(PropertyMapper::class);
93  $propertyMapperConfiguration = new PropertyMappingConfiguration();
94  $propertyMapperConfiguration->allowAllProperties();
95  $propertyMapperConfiguration
96  ->forProperty('name')
97  ->setTypeConverterOption(
98  ObjectConverter::class,
100  'string'
101  )
102  ;
103 
104  $result = $propertyMapper->convert(
105  ['name' => 'foo'],
106  get_class($class),
107  $propertyMapperConfiguration
108  );
109 
110  self::assertSame('foo', $result->name);
111  }
112 
117  {
118  $class = new class('') {
119  private $name;
120  public function __construct(string $name)
121  {
122  $this->name = $name;
123  }
124  public function getName(): string
125  {
126  return $this->name;
127  }
128  };
129 
130  $propertyMapper = $this->getContainer()->get(PropertyMapper::class);
131  $propertyMapperConfiguration = new PropertyMappingConfiguration();
132  $propertyMapperConfiguration->allowAllProperties();
133 
134  $result = $propertyMapper->convert(
135  ['name' => 'foo'],
136  get_class($class),
137  $propertyMapperConfiguration
138  );
139 
140  self::assertSame('foo', $result->getName());
141  }
142 
147  {
148  $class = new class() {
149  private $name;
150  public function setName(string $name)
151  {
152  $this->name = $name;
153  }
154  public function getName(): string
155  {
156  return $this->name;
157  }
158  };
159 
160  $propertyMapper = $this->getContainer()->get(PropertyMapper::class);
161  $propertyMapperConfiguration = new PropertyMappingConfiguration();
162  $propertyMapperConfiguration->allowAllProperties();
163 
164  $result = $propertyMapper->convert(
165  ['name' => 'foo'],
166  get_class($class),
167  $propertyMapperConfiguration
168  );
169 
170  self::assertSame('foo', $result->getName());
171  }
172 
177  {
178  $class = new class() {
179  };
180 
181  $className = get_class($class);
182  $propertyName = 'name';
183 
184  static::expectException(Exception::class);
185  static::expectExceptionCode(1297759968);
186  static::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.');
187 
188  $propertyMapper = $this->getContainer()->get(PropertyMapper::class);
189  $propertyMapperConfiguration = new PropertyMappingConfiguration();
190  $propertyMapperConfiguration->allowAllProperties();
191 
192  $propertyMapper->convert(
193  [$propertyName => 'foo'],
194  $className,
195  $propertyMapperConfiguration
196  );
197  }
198 
203  {
204  $class = new class() {
205  public function __construct()
206  {
207  }
208  };
209 
210  $className = get_class($class);
211  $propertyName = 'name';
212 
213  static::expectException(Exception::class);
214  static::expectExceptionCode(1297759968);
215  static::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 . '".');
216 
217  $propertyMapper = $this->getContainer()->get(PropertyMapper::class);
218  $propertyMapperConfiguration = new PropertyMappingConfiguration();
219  $propertyMapperConfiguration->allowAllProperties();
220 
221  $propertyMapper->convert(
222  [$propertyName => 'foo'],
223  $className,
224  $propertyMapperConfiguration
225  );
226  }
227 
232  {
233  $class = new class() {
234  public function __construct($name = null)
235  {
236  }
237  };
238 
239  $className = get_class($class);
240  $propertyName = 'name';
241 
242  static::expectException(Exception::class);
243  static::expectExceptionCode(1297759968);
244  static::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.');
245 
246  $propertyMapper = $this->getContainer()->get(PropertyMapper::class);
247  $propertyMapperConfiguration = new PropertyMappingConfiguration();
248  $propertyMapperConfiguration->allowAllProperties();
249 
250  $propertyMapper->convert(
251  [$propertyName => 'foo'],
252  $className,
253  $propertyMapperConfiguration
254  );
255  }
256 
261  {
262  static::expectException(Exception::class);
263  static::expectExceptionCode(1297759968);
264  static::expectExceptionMessage('Exception while property mapping at property path "": Setter for property "name" had no type hint or documentation in target object of type "');
265 
266  $class = new class() {
267  public function setName($name)
268  {
269  }
270  };
271 
272  $propertyMapper = $this->getContainer()->get(PropertyMapper::class);
273  $propertyMapperConfiguration = new PropertyMappingConfiguration();
274  $propertyMapperConfiguration->allowAllProperties();
275 
276  $propertyMapper->convert(
277  ['name' => 'foo'],
278  get_class($class),
279  $propertyMapperConfiguration
280  );
281  }
282 
287  {
288  static::expectException(Exception::class);
289  static::expectExceptionCode(1297759968);
290  static::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 "');
291 
292  $class = new class() {
293  private $name;
294  };
295 
296  $propertyMapper = $this->getContainer()->get(PropertyMapper::class);
297  $propertyMapperConfiguration = new PropertyMappingConfiguration();
298  $propertyMapperConfiguration->allowAllProperties();
299  $propertyMapperConfiguration
300  ->forProperty('name')
301  ->setTypeConverterOption(
302  ObjectConverter::class,
304  'string'
305  )
306  ;
307 
308  $propertyMapper->convert(
309  ['name' => 'foo'],
310  get_class($class),
311  $propertyMapperConfiguration
312  );
313  }
314 
319  {
320  $class = new class('', '') {
321  public $name;
322  public $color;
323  public function __construct(string $name, ?string $color = 'red')
324  {
325  $this->name = $name;
326  $this->color = $color;
327  }
328  };
329 
330  $result = $this->getContainer()->get(PropertyMapper::class)->convert(
331  ['name' => 'foo'],
332  get_class($class)
333  );
334 
335  self::assertSame('foo', $result->name);
336  self::assertSame('red', $result->color);
337  }
338 
343  {
344  static::expectException(Exception::class);
345  static::expectExceptionCode(1297759968);
346  static::expectExceptionMessage('Exception while property mapping at property path "": Missing constructor argument "color" for object of type "');
347 
348  $class = new class('', '') {
349  public $name;
350  public $color;
351  public function __construct(string $name, string $color)
352  {
353  $this->name = $name;
354  $this->color = $color;
355  }
356  };
357 
358  $this->getContainer()->get(PropertyMapper::class)->convert(
359  ['name' => 'foo'],
360  get_class($class)
361  );
362  }
363 
368  {
369  static::expectException(Exception::class);
370  static::expectExceptionCode(1297759968);
371  static::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.');
372 
373  $class = new class() {
374  };
375 
376  $this->getContainer()->get(PropertyMapper::class)->convert(
377  ['__type' => Animal::class],
378  get_class($class)
379  );
380  }
381 
386  {
387  static::expectException(Exception::class);
388  static::expectExceptionCode(1297759968);
389  static::expectExceptionMessage('Exception while property mapping at property path "": The given type "TYPO3\CMS\Extbase\Tests\Functional\Property\Fixtures\Animal" is not a subtype of "');
390 
391  $class = new class() {
392  };
393 
394  $propertyMapperConfiguration = new PropertyMappingConfiguration();
395  $propertyMapperConfiguration->allowAllProperties();
396  $propertyMapperConfiguration->setTypeConverterOption(
397  ObjectConverter::class,
399  true
400  );
401 
402  $this->getContainer()->get(PropertyMapper::class)->convert(
403  ['__type' => Animal::class],
404  get_class($class),
405  $propertyMapperConfiguration
406  );
407  }
408 }
‪TYPO3\CMS\Extbase\Tests\Functional\Property\TypeConverter\ObjectConverterTest\getTargetTypeForSourceThrowsInvalidPropertyMappingConfigurationExceptionIfTargetTypeOverridingIsNotAllowed
‪getTargetTypeForSourceThrowsInvalidPropertyMappingConfigurationExceptionIfTargetTypeOverridingIsNotAllowed()
Definition: ObjectConverterTest.php:366
‪TYPO3\CMS\Extbase\Property\TypeConverter\ObjectConverter\CONFIGURATION_TARGET_TYPE
‪const CONFIGURATION_TARGET_TYPE
Definition: ObjectConverter.php:39
‪TYPO3\CMS\Extbase\Property\Exception
Definition: DuplicateObjectException.php:18
‪TYPO3\CMS\Extbase\Tests\Functional\Property\TypeConverter\ObjectConverterTest\buildObjectUsesDefaultValueOfOptionalConstructorArguments
‪buildObjectUsesDefaultValueOfOptionalConstructorArguments()
Definition: ObjectConverterTest.php:317
‪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:115
‪TYPO3\CMS\Extbase\Tests\Functional\Property\TypeConverter\ObjectConverterTest\getTypeOfChildPropertyThrowsInvalidTargetExceptionIfPropertySetterDoesNotDefineAType
‪getTypeOfChildPropertyThrowsInvalidTargetExceptionIfPropertySetterDoesNotDefineAType()
Definition: ObjectConverterTest.php:259
‪TYPO3\CMS\Extbase\Tests\Functional\Property\TypeConverter\ObjectConverterTest\getTypeOfChildPropertyThrowsInvalidTargetExceptionIfPropertyTypeCannotBeDerivedFromExistingConstructorArgument
‪getTypeOfChildPropertyThrowsInvalidTargetExceptionIfPropertyTypeCannotBeDerivedFromExistingConstructorArgument()
Definition: ObjectConverterTest.php:230
‪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:201
‪TYPO3\CMS\Extbase\Tests\Functional\Property\TypeConverter\ObjectConverterTest\getTypeOfChildPropertyReturnsTypeDefinedByPropertyMappingConfiguration
‪getTypeOfChildPropertyReturnsTypeDefinedByPropertyMappingConfiguration()
Definition: ObjectConverterTest.php:85
‪TYPO3\CMS\Extbase\Tests\Functional\Property\TypeConverter\ObjectConverterTest
Definition: ObjectConverterTest.php:30
‪TYPO3\CMS\Extbase\Tests\Functional\Property\TypeConverter\ObjectConverterTest\convertFromThrowsInvalidTargetExceptionIfPropertiesCannotBeSet
‪convertFromThrowsInvalidTargetExceptionIfPropertiesCannotBeSet()
Definition: ObjectConverterTest.php:285
‪TYPO3\CMS\Extbase\DomainObject\AbstractEntity
Definition: AbstractEntity.php:23
‪TYPO3\CMS\Extbase\Property\TypeConverter\ObjectConverter\CONFIGURATION_OVERRIDE_TARGET_TYPE_ALLOWED
‪const CONFIGURATION_OVERRIDE_TARGET_TYPE_ALLOWED
Definition: ObjectConverter.php:44
‪TYPO3\CMS\Extbase\Tests\Functional\Property\TypeConverter
Definition: ArrayConverterTest.php:18
‪TYPO3\CMS\Extbase\Tests\Functional\Property\TypeConverter\ObjectConverterTest\getTypeOfChildPropertyReturnsTypeDefinedBySetter
‪getTypeOfChildPropertyReturnsTypeDefinedBySetter()
Definition: ObjectConverterTest.php:145
‪TYPO3\CMS\Extbase\Tests\Functional\Property\TypeConverter\ObjectConverterTest\buildObjectThrowsInvalidTargetExceptionIfMandatoryConstructorArgumentIsMissing
‪buildObjectThrowsInvalidTargetExceptionIfMandatoryConstructorArgumentIsMissing()
Definition: ObjectConverterTest.php:341
‪TYPO3\CMS\Extbase\Property\PropertyMappingConfiguration
Definition: PropertyMappingConfiguration.php:22
‪TYPO3\CMS\Extbase\Property\PropertyMapper
Definition: PropertyMapper.php:37
‪TYPO3\CMS\Extbase\Property\TypeConverter\ObjectConverter
Definition: ObjectConverter.php:35
‪TYPO3\CMS\Extbase\Tests\Functional\Property\TypeConverter\ObjectConverterTest\getTypeOfChildPropertyThrowsInvalidTargetExceptionIfPropertyIsNotAccessible
‪getTypeOfChildPropertyThrowsInvalidTargetExceptionIfPropertyIsNotAccessible()
Definition: ObjectConverterTest.php:175
‪TYPO3\CMS\Extbase\Tests\Functional\Property\TypeConverter\ObjectConverterTest\getTargetTypeForSourceThrowsInvalidDataTypeExceptionIfOverriddenTargetTypeIsNotASubtypeOfOriginalTargetType
‪getTargetTypeForSourceThrowsInvalidDataTypeExceptionIfOverriddenTargetTypeIsNotASubtypeOfOriginalTargetType()
Definition: ObjectConverterTest.php:384
‪TYPO3\CMS\Extbase\Tests\Functional\Property\TypeConverter\ObjectConverterTest\convertToObjectViaTypeInArray
‪convertToObjectViaTypeInArray()
Definition: ObjectConverterTest.php:59
‪TYPO3\CMS\Extbase\Tests\Functional\Property\TypeConverter\ObjectConverterTest\convertToObject
‪convertToObject()
Definition: ObjectConverterTest.php:34