‪TYPO3CMS  10.4
DataMapperTest.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 Psr\Container\ContainerInterface;
21 use Psr\EventDispatcher\EventDispatcherInterface;
41 use TYPO3\TestingFramework\Core\AccessibleObjectInterface;
42 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
43 
47 class ‪DataMapperTest extends UnitTestCase
48 {
57  {
58  $rows = [['uid' => '1234']];
59  $object = new \stdClass();
60 
62  $dataMapper = $this->getMockBuilder(DataMapper::class)
63  ->setConstructorArgs([
64  $this->createMock(ReflectionService::class),
65  $this->createMock(QueryObjectModelFactory::class),
66  $this->createMock(Session::class),
67  $this->createMock(DataMapFactory::class),
68  $this->createMock(QueryFactoryInterface::class),
69  $this->createMock(ObjectManagerInterface::class),
70  $this->createMock(EventDispatcherInterface::class),
71  ])
72  ->setMethods(['mapSingleRow', 'getTargetType'])
73  ->getMock();
74 
75  $dataMapper->expects(self::any())->method('getTargetType')->willReturnArgument(1);
76  $dataMapper->expects(self::once())->method('mapSingleRow')->with($rows[0])->willReturn($object);
77 
78  $dataMapper->map(get_class($object), $rows);
79  }
80 
89  {
90  $row = ['uid' => '1234'];
91  $object = new \stdClass();
92  $persistenceSession = $this->createMock(Session::class);
93  $persistenceSession->expects(self::once())->method('hasIdentifier')->with('1234')->willReturn(true);
94  $persistenceSession->expects(self::once())->method('getObjectByIdentifier')->with('1234')->willReturn($object);
95 
96  $dataMapper = $this->getAccessibleMock(
97  DataMapper::class,
98  ['dummy'],
99  [
100  $this->createMock(ReflectionService::class),
101  $this->createMock(QueryObjectModelFactory::class),
102  $persistenceSession,
103  $this->createMock(DataMapFactory::class),
104  $this->createMock(QueryFactoryInterface::class),
105  $this->createMock(ObjectManagerInterface::class),
106  $this->createMock(EventDispatcherInterface::class),
107  ]
108  );
109 
110  $dataMapper->_call('mapSingleRow', get_class($object), $row);
111  }
112 
121  {
122  $className = DummyEntity::class;
123  $object = new ‪DummyEntity();
124  $row = [
125  'uid' => '1234',
126  'firstProperty' => 'firstValue',
127  'secondProperty' => 1234,
128  'thirdProperty' => 1.234,
129  'fourthProperty' => false
130  ];
131  $columnMaps = [
132  'uid' => new ‪ColumnMap('uid', 'uid'),
133  'pid' => new ‪ColumnMap('pid', 'pid'),
134  'firstProperty' => new ‪ColumnMap('firstProperty', 'firstProperty'),
135  'secondProperty' => new ‪ColumnMap('secondProperty', 'secondProperty'),
136  'thirdProperty' => new ‪ColumnMap('thirdProperty', 'thirdProperty'),
137  'fourthProperty' => new ‪ColumnMap('fourthProperty', 'fourthProperty'),
138  ];
139  $dataMap = $this->getAccessibleMock(DataMap::class, ['dummy'], [$className, $className]);
140  $dataMap->_set('columnMaps', $columnMaps);
141  $dataMaps = [
142  $className => $dataMap
143  ];
145  $classSchema = new ‪ClassSchema($className);
146  $mockReflectionService = $this->getMockBuilder(ReflectionService::class)
147  ->onlyMethods(['getClassSchema'])
148  ->getMock();
149  $mockReflectionService->expects(self::any())->method('getClassSchema')->willReturn($classSchema);
150  $dataMapFactory = $this->getAccessibleMock(DataMapFactory::class, ['dummy'], [], '', false);
151  $dataMapFactory->_set('dataMaps', $dataMaps);
152  $dataMapper = $this->getAccessibleMock(
153  DataMapper::class,
154  ['dummy'],
155  [
156  $mockReflectionService,
157  $this->createMock(QueryObjectModelFactory::class),
158  $this->createMock(Session::class),
159  $dataMapFactory,
160  $this->createMock(QueryFactoryInterface::class),
161  $this->createMock(ObjectManagerInterface::class),
162  $this->createMock(EventDispatcherInterface::class),
163  ]
164  );
165  $dataMapper->_call('thawProperties', $object, $row);
166 
167  self::assertEquals('firstValue', $object->firstProperty);
168  self::assertEquals(1234, $object->secondProperty);
169  self::assertEquals(1.234, $object->thirdProperty);
170  self::assertFalse($object->fourthProperty);
171  }
172 
177  {
178  $className = DummyEntity::class;
179  $object = new ‪DummyEntity();
180  $row = [
181  'uid' => '1234',
182  'unknownType' => 'What am I?'
183  ];
184  $columnMaps = [
185  'unknownType' => new ‪ColumnMap('unknownType', 'unknownType'),
186  ];
187  $dataMap = $this->getAccessibleMock(DataMap::class, ['dummy'], [$className, $className]);
188  $dataMap->_set('columnMaps', $columnMaps);
189  $dataMaps = [
190  $className => $dataMap
191  ];
193  $classSchema = new ‪ClassSchema($className);
194  $mockReflectionService = $this->getMockBuilder(ReflectionService::class)
195  ->onlyMethods(['getClassSchema'])
196  ->getMock();
197  $mockReflectionService->method('getClassSchema')->willReturn($classSchema);
198  $dataMapFactory = $this->getAccessibleMock(DataMapFactory::class, ['dummy'], [], '', false);
199  $dataMapFactory->_set('dataMaps', $dataMaps);
200  $dataMapper = $this->getAccessibleMock(
201  DataMapper::class,
202  ['dummy'],
203  [
204  $mockReflectionService,
205  $this->createMock(QueryObjectModelFactory::class),
206  $this->createMock(Session::class),
207  $dataMapFactory,
208  $this->createMock(QueryFactoryInterface::class),
209  $this->createMock(ObjectManagerInterface::class),
210  $this->createMock(EventDispatcherInterface::class),
211  ]
212  );
213  $this->expectException(UnknownPropertyTypeException::class);
214  $dataMapper->_call('thawProperties', $object, $row);
215  }
216 
226  {
227  $columnMap = new ‪ColumnMap('columnName', 'propertyName');
228  $columnMap->setTypeOfRelation(‪ColumnMap::RELATION_HAS_ONE);
229  $dataMap = $this->getMockBuilder(DataMap::class)
230  ->setMethods(['getColumnMap'])
231  ->disableOriginalConstructor()
232  ->getMock();
233  $dataMap->expects(self::any())->method('getColumnMap')->willReturn($columnMap);
234  $dataMapper = $this->getAccessibleMock(DataMapper::class, ['getDataMap'], [], '', false);
235  $dataMapper->expects(self::any())->method('getDataMap')->willReturn($dataMap);
236  $result = $dataMapper->_call('fetchRelatedEager', $this->createMock(AbstractEntity::class), 'SomeName', '');
237  self::assertNull($result);
238  }
239 
249  {
250  $columnMap = new ‪ColumnMap('columnName', 'propertyName');
251  $columnMap->setTypeOfRelation(‪ColumnMap::RELATION_BELONGS_TO_MANY);
252  $dataMap = $this->getMockBuilder(DataMap::class)
253  ->setMethods(['getColumnMap'])
254  ->disableOriginalConstructor()
255  ->getMock();
256  $dataMap->expects(self::any())->method('getColumnMap')->willReturn($columnMap);
257  $dataMapper = $this->getAccessibleMock(DataMapper::class, ['getDataMap'], [], '', false);
258  $dataMapper->expects(self::any())->method('getDataMap')->willReturn($dataMap);
259  $result = $dataMapper->_call('fetchRelatedEager', $this->createMock(AbstractEntity::class), 'SomeName', '');
260  self::assertEquals([], $result);
261  }
262 
273  {
274  $columnMap = new ‪ColumnMap('columnName', 'propertyName');
275  $columnMap->setTypeOfRelation(‪ColumnMap::RELATION_HAS_ONE);
276  $dataMap = $this->getMockBuilder(DataMap::class)
277  ->setMethods(['getColumnMap'])
278  ->disableOriginalConstructor()
279  ->getMock();
280  $dataMap->expects(self::any())->method('getColumnMap')->willReturn($columnMap);
281  $dataMapper = $this->getAccessibleMock(DataMapper::class, ['getDataMap', 'fetchRelated'], [], '', false);
282  $dataMapper->expects(self::any())->method('getDataMap')->willReturn($dataMap);
283  $dataMapper->expects(self::never())->method('fetchRelated');
284  $result = $dataMapper->_call('mapObjectToClassProperty', $this->createMock(AbstractEntity::class), 'SomeName', '');
285  self::assertNull($result);
286  }
287 
299  {
300  $columnMap = new ‪ColumnMap('columnName', 'propertyName');
301  $columnMap->setTypeOfRelation(‪ColumnMap::RELATION_HAS_ONE);
302  $dataMap = $this->getMockBuilder(DataMap::class)
303  ->setMethods(['getColumnMap'])
304  ->disableOriginalConstructor()
305  ->getMock();
306 
307  $object = new ‪DummyParentEntity();
308  $child = new ‪DummyChildEntity();
309 
311  $classSchema1 = new ‪ClassSchema(DummyParentEntity::class);
312  $identifier = 1;
313 
314  $psrContainer = $this->getMockBuilder(ContainerInterface::class)
315  ->setMethods(['has', 'get'])
316  ->disableOriginalConstructor()
317  ->getMock();
318  $psrContainer->expects(self::any())->method('has')->willReturn(false);
319  $container = new ‪Container($psrContainer);
320 
321  $session = new ‪Session(new ‪Container($psrContainer));
322  $session->registerObject($child, $identifier);
323 
324  $mockReflectionService = $this->getMockBuilder(ReflectionService::class)
325  ->setMethods(['getClassSchema'])
326  ->disableOriginalConstructor()
327  ->getMock();
328  $mockReflectionService->expects(self::any())->method('getClassSchema')->willReturn($classSchema1);
329 
330  $dataMap->expects(self::any())->method('getColumnMap')->willReturn($columnMap);
331 
332  $dataMapper = $this->getAccessibleMock(
333  DataMapper::class,
334  ['getDataMap', 'getNonEmptyRelationValue'],
335  [
336  $mockReflectionService,
337  $this->createMock(QueryObjectModelFactory::class),
338  $session,
339  $this->createMock(DataMapFactory::class),
340  $this->createMock(QueryFactoryInterface::class),
341  $this->createMock(ObjectManagerInterface::class),
342  $this->createMock(EventDispatcherInterface::class),
343  ]
344  );
345  $dataMapper->expects(self::any())->method('getDataMap')->willReturn($dataMap);
346  $dataMapper->expects(self::never())->method('getNonEmptyRelationValue');
347  $result = $dataMapper->_call('mapObjectToClassProperty', $object, 'relationProperty', $identifier);
348  self::assertEquals($child, $result);
349  }
350 
360  {
361  return [
362  'nothing' => [null, null, null],
363  'timestamp' => [1, null, date('c', 1)],
364  'empty date' => ['0000-00-00', 'date', null],
365  'valid date' => ['2013-01-01', 'date', date('c', strtotime('2013-01-01T00:00:00+00:00'))],
366  'empty datetime' => ['0000-00-00 00:00:00', 'datetime', null],
367  'valid datetime' => ['2013-01-01 01:02:03', 'datetime', date('c', strtotime('2013-01-01T01:02:03+00:00'))],
368  ];
369  }
370 
378  public function ‪mapDateTimeHandlesDifferentFieldEvaluations($value, $storageFormat, $expectedValue)
379  {
381  $accessibleDataMapFactory = $this->getAccessibleMock(DataMapper::class, ['dummy'], [], '', false);
382 
384  $dateTime = $accessibleDataMapFactory->_call('mapDateTime', $value, $storageFormat);
385 
386  if ($expectedValue === null) {
387  self::assertNull($dateTime);
388  } else {
389  self::assertEquals($expectedValue, $dateTime->format('c'));
390  }
391  }
392 
397  {
399  $accessibleDataMapFactory = $this->getAccessibleMock(DataMapper::class, ['dummy'], [], '', false);
400  $targetType = 'TYPO3\CMS\Extbase\Tests\Unit\Persistence\Fixture\Model\CustomDateTime';
401  $date = '2013-01-01 01:02:03';
402  $storageFormat = 'datetime';
403 
405  $dateTime = $accessibleDataMapFactory->_call('mapDateTime', $date, $storageFormat, $targetType);
406 
407  self::assertInstanceOf($targetType, $dateTime);
408  }
409 
414  {
416  $subject = $this->createPartialMock(DataMapper::class, []);
417 
418  $columnMap = new ‪ColumnMap('column_name', 'propertyName');
419  $columnMap->setDateTimeStorageFormat('datetime');
420  $datetimeAsString = '2013-04-15 09:30:00';
421  $input = new \DateTime($datetimeAsString, new \DateTimeZone('UTC'));
422  self::assertEquals('2013-04-15 09:30:00', $subject->getPlainValue($input, $columnMap));
423  $columnMap->setDateTimeStorageFormat('date');
424  self::assertEquals('2013-04-15', $subject->getPlainValue($input, $columnMap));
425  }
426 
431  public function ‪getPlainValueReturnsExpectedValues($expectedValue, $input)
432  {
434  $dataMapper = $this->createPartialMock(DataMapper::class, []);
435 
436  self::assertSame($expectedValue, $dataMapper->getPlainValue($input));
437  }
438 
443  {
444  $traversableDomainObject = $this->prophesize()
445  ->willImplement(\Iterator::class)
446  ->willImplement(DomainObjectInterface::class);
447  $traversableDomainObject->getUid()->willReturn(1);
448 
449  return [
450  'datetime to timestamp' => ['1365866253', new \DateTime('@1365866253')],
451  'boolean true to 1' => [1, true],
452  'boolean false to 0' => [0, false],
453  'NULL is handled as string' => ['NULL', null],
454  'string value is returned unchanged' => ['RANDOM string', 'RANDOM string'],
455  'array is flattened' => ['a,b,c', ['a', 'b', 'c']],
456  'deep array is flattened' => ['a,b,c', [['a', 'b'], 'c']],
457  'traversable domain object to identifier' => [1, $traversableDomainObject->reveal()],
458  'integer value is returned unchanged' => [1234, 1234],
459  'float is converted to string' => ['1234.56', 1234.56],
460  ];
461  }
462 
467  {
468  $this->expectException(UnexpectedTypeException::class);
469  $this->expectExceptionCode(1274799934);
470 
472  $dataMapper = $this->createPartialMock(DataMapper::class, []);
473  $input = $this->createMock(LazyLoadingProxy::class);
474  $input->expects(self::once())->method('_loadRealInstance')->willReturn($dataMapper);
475  $dataMapper->getPlainValue($input);
476  }
477 
482  {
484  $dataMapper = $this->createPartialMock(DataMapper::class, []);
485  $input = $this->createMock(DomainObjectInterface::class);
486 
487  $input->expects(self::once())->method('getUid')->willReturn(23);
488  self::assertSame(23, $dataMapper->getPlainValue($input));
489  }
490 }
‪TYPO3\CMS\Extbase\Persistence\Generic\Mapper\ColumnMap\RELATION_BELONGS_TO_MANY
‪const RELATION_BELONGS_TO_MANY
Definition: ColumnMap.php:47
‪TYPO3\CMS\Extbase\Object\Container\Container
Definition: Container.php:39
‪TYPO3\CMS\Extbase\Persistence\Generic\Mapper\DataMap
Definition: DataMap.php:23
‪TYPO3\CMS\Extbase\Tests\Unit\Persistence\Generic\Mapper\DataMapperTest\thawPropertiesThrowsExceptionOnUnknownPropertyType
‪thawPropertiesThrowsExceptionOnUnknownPropertyType()
Definition: DataMapperTest.php:176
‪TYPO3\CMS\Extbase\Tests\Unit\Persistence\Generic\Mapper\DataMapperTest\MapObjectToClassPropertyReturnsNullForEmptyRelationHasOne
‪MapObjectToClassPropertyReturnsNullForEmptyRelationHasOne()
Definition: DataMapperTest.php:272
‪TYPO3\CMS\Extbase\Tests\Unit\Persistence\Generic\Mapper\DataMapperTest\thawPropertiesSetsPropertyValues
‪thawPropertiesSetsPropertyValues()
Definition: DataMapperTest.php:120
‪TYPO3\CMS\Extbase\Persistence\Generic\Mapper\ColumnMap
Definition: ColumnMap.php:28
‪TYPO3\CMS\Extbase\Persistence\Generic\Mapper\Exception\UnknownPropertyTypeException
Definition: UnknownPropertyTypeException.php:23
‪TYPO3\CMS\Extbase\Tests\Unit\Persistence\Generic\Mapper\DataMapperTest\mapDateTimeHandlesDifferentFieldEvaluations
‪mapDateTimeHandlesDifferentFieldEvaluations($value, $storageFormat, $expectedValue)
Definition: DataMapperTest.php:378
‪TYPO3\CMS\Extbase\Persistence\Generic\Qom\QueryObjectModelFactory
Definition: QueryObjectModelFactory.php:27
‪TYPO3\CMS\Extbase\Tests\Unit\Persistence\Generic\Mapper\DataMapperTest\getPlainValueCallsGetUidOnDomainObjectInterfaceInput
‪getPlainValueCallsGetUidOnDomainObjectInterfaceInput()
Definition: DataMapperTest.php:481
‪TYPO3\CMS\Extbase\Persistence\Generic\Mapper\DataMapper
Definition: DataMapper.php:52
‪TYPO3\CMS\Extbase\Tests\Unit\Persistence\Generic\Mapper\DataMapperTest\fetchRelatedEagerReturnsEmptyArrayForEmptyRelationNotHasOne
‪fetchRelatedEagerReturnsEmptyArrayForEmptyRelationNotHasOne()
Definition: DataMapperTest.php:248
‪TYPO3\CMS\Extbase\Tests\Unit\Persistence\Generic\Mapper\DataMapperTest\mapSingleRowReturnsObjectFromPersistenceSessionIfAvailable
‪mapSingleRowReturnsObjectFromPersistenceSessionIfAvailable()
Definition: DataMapperTest.php:88
‪TYPO3\CMS\Extbase\DomainObject\AbstractEntity
Definition: AbstractEntity.php:23
‪TYPO3\CMS\Extbase\Tests\Unit\Persistence\Generic\Mapper
Definition: DataMapFactoryTest.php:16
‪TYPO3\CMS\Extbase\Object\ObjectManagerInterface
Definition: ObjectManagerInterface.php:26
‪TYPO3\CMS\Extbase\Persistence\Generic\Mapper\ColumnMap\RELATION_HAS_ONE
‪const RELATION_HAS_ONE
Definition: ColumnMap.php:37
‪TYPO3\CMS\Extbase\Persistence\Generic\Mapper\DataMapFactory
Definition: DataMapFactory.php:42
‪TYPO3\CMS\Extbase\Reflection\ReflectionService
Definition: ReflectionService.php:31
‪TYPO3\CMS\Extbase\Reflection\ClassSchema
‪TYPO3\CMS\Extbase\Tests\Unit\Persistence\Generic\Mapper\Fixture\DummyChildEntity
Definition: DummyChildEntity.php:26
‪TYPO3\CMS\Extbase\Persistence\Generic\QueryFactoryInterface
Definition: QueryFactoryInterface.php:22
‪TYPO3\CMS\Extbase\DomainObject\DomainObjectInterface
Definition: DomainObjectInterface.php:29
‪TYPO3\CMS\Extbase\Tests\Unit\Persistence\Generic\Mapper\Fixture\DummyEntity
Definition: DummyEntity.php:26
‪TYPO3\CMS\Extbase\Tests\Unit\Persistence\Generic\Mapper\DataMapperTest\getPlainValueReturnsExpectedValuesDataProvider
‪array getPlainValueReturnsExpectedValuesDataProvider()
Definition: DataMapperTest.php:442
‪TYPO3\CMS\Extbase\Persistence\Generic\Session
Definition: Session.php:27
‪TYPO3\CMS\Extbase\Tests\Unit\Persistence\Generic\Mapper\DataMapperTest\getPlainValueCallsGetRealInstanceOnInputIfInputIsInstanceOfLazyLoadingProxy
‪getPlainValueCallsGetRealInstanceOnInputIfInputIsInstanceOfLazyLoadingProxy()
Definition: DataMapperTest.php:466
‪TYPO3\CMS\Extbase\Tests\Unit\Persistence\Generic\Mapper\DataMapperTest
Definition: DataMapperTest.php:48
‪TYPO3\CMS\Extbase\Persistence\Generic\Exception\UnexpectedTypeException
Definition: UnexpectedTypeException.php:26
‪TYPO3\CMS\Extbase\Tests\Unit\Persistence\Generic\Mapper\DataMapperTest\fetchRelatedEagerReturnsNullForEmptyRelationHasOne
‪fetchRelatedEagerReturnsNullForEmptyRelationHasOne()
Definition: DataMapperTest.php:225
‪TYPO3\CMS\Extbase\Tests\Unit\Persistence\Generic\Mapper\DataMapperTest\getPlainValueReturnsCorrectDateTimeFormat
‪getPlainValueReturnsCorrectDateTimeFormat()
Definition: DataMapperTest.php:413
‪TYPO3\CMS\Extbase\Tests\Unit\Persistence\Generic\Mapper\DataMapperTest\mapMapsArrayToObjectByCallingmapToObject
‪mapMapsArrayToObjectByCallingmapToObject()
Definition: DataMapperTest.php:56
‪TYPO3\CMS\Extbase\Tests\Unit\Persistence\Generic\Mapper\DataMapperTest\mapObjectToClassPropertyReturnsExistingObjectWithoutCallingFetchRelated
‪mapObjectToClassPropertyReturnsExistingObjectWithoutCallingFetchRelated()
Definition: DataMapperTest.php:298
‪TYPO3\CMS\Extbase\Persistence\Generic\LazyLoadingProxy
Definition: LazyLoadingProxy.php:29
‪TYPO3\CMS\Extbase\Tests\Unit\Persistence\Generic\Mapper\DataMapperTest\testMapDateTimeHandlesSubclassesOfDateTime
‪testMapDateTimeHandlesSubclassesOfDateTime()
Definition: DataMapperTest.php:396
‪TYPO3\CMS\Extbase\Tests\Unit\Persistence\Generic\Mapper\Fixture\DummyParentEntity
Definition: DummyParentEntity.php:26
‪TYPO3\CMS\Extbase\Reflection\ClassSchema
Definition: ClassSchema.php:55
‪TYPO3\CMS\Extbase\Tests\Unit\Persistence\Generic\Mapper\DataMapperTest\getPlainValueReturnsExpectedValues
‪getPlainValueReturnsExpectedValues($expectedValue, $input)
Definition: DataMapperTest.php:431
‪TYPO3\CMS\Extbase\Tests\Unit\Persistence\Generic\Mapper\DataMapperTest\mapDateTimeHandlesDifferentFieldEvaluationsDataProvider
‪array mapDateTimeHandlesDifferentFieldEvaluationsDataProvider()
Definition: DataMapperTest.php:359