TYPO3 CMS  TYPO3_7-6
DataMapperTest.php
Go to the documentation of this file.
1 <?php
3 
4 /*
5  * This file is part of the TYPO3 CMS project.
6  *
7  * It is free software; you can redistribute it and/or modify it under
8  * the terms of the GNU General Public License, either version 2
9  * of the License, or any later version.
10  *
11  * For the full copyright and license information, please read the
12  * LICENSE.txt file that was distributed with this source code.
13  *
14  * The TYPO3 project - inspiring people to share!
15  */
16 
21 
26 {
30  public function mapMapsArrayToObjectByCallingmapToObject()
31  {
32  $rows = [['uid' => '1234']];
33  $object = new \stdClass();
35  $dataMapper = $this->getAccessibleMock(\TYPO3\CMS\Extbase\Persistence\Generic\Mapper\DataMapper::class, ['mapSingleRow', 'getTargetType']);
36  $dataMapper->expects($this->any())->method('getTargetType')->will($this->returnArgument(1));
37  $dataMapFactory = $this->getMock(\TYPO3\CMS\Extbase\Persistence\Generic\Mapper\DataMapFactory::class);
38  $dataMapper->_set('dataMapFactory', $dataMapFactory);
39  $dataMapper->expects($this->once())->method('mapSingleRow')->with($rows[0])->will($this->returnValue($object));
40  $dataMapper->map(get_class($object), $rows);
41  }
42 
47  {
48  $row = ['uid' => '1234'];
49  $object = new \stdClass();
50  $persistenceSession = $this->getMock(\TYPO3\CMS\Extbase\Persistence\Generic\Session::class);
51  $persistenceSession->expects($this->once())->method('hasIdentifier')->with('1234')->will($this->returnValue(true));
52  $persistenceSession->expects($this->once())->method('getObjectByIdentifier')->with('1234')->will($this->returnValue($object));
53  $dataMapper = $this->getAccessibleMock(\TYPO3\CMS\Extbase\Persistence\Generic\Mapper\DataMapper::class, ['dummy']);
54  $dataMapper->_set('persistenceSession', $persistenceSession);
55  $dataMapper->_call('mapSingleRow', get_class($object), $row);
56  }
57 
61  public function thawPropertiesSetsPropertyValues()
62  {
63  $className = $this->getUniqueId('Class');
64  $classNameWithNS = __NAMESPACE__ . '\\' . $className;
65  eval('namespace ' . __NAMESPACE__ . '; class ' . $className . ' extends \\' . \TYPO3\CMS\Extbase\DomainObject\AbstractEntity::class . ' {
66  public $firstProperty; public $secondProperty; public $thirdProperty; public $fourthProperty;
67  }'
68  );
69  $object = new $classNameWithNS();
70  $row = [
71  'uid' => '1234',
72  'firstProperty' => 'firstValue',
73  'secondProperty' => 1234,
74  'thirdProperty' => 1.234,
75  'fourthProperty' => false
76  ];
77  $columnMaps = [
78  'uid' => new ColumnMap('uid', 'uid'),
79  'pid' => new ColumnMap('pid', 'pid'),
80  'firstProperty' => new ColumnMap('firstProperty', 'firstProperty'),
81  'secondProperty' => new ColumnMap('secondProperty', 'secondProperty'),
82  'thirdProperty' => new ColumnMap('thirdProperty', 'thirdProperty')
83  ];
84  $dataMap = $this->getAccessibleMock(\TYPO3\CMS\Extbase\Persistence\Generic\Mapper\DataMap::class, ['dummy'], [$className, $className]);
85  $dataMap->_set('columnMaps', $columnMaps);
86  $dataMaps = [
87  $classNameWithNS => $dataMap
88  ];
90  $classSchema = $this->getAccessibleMock(\TYPO3\CMS\Extbase\Reflection\ClassSchema::class, ['dummy'], [$classNameWithNS]);
91  $classSchema->addProperty('pid', 'integer');
92  $classSchema->addProperty('uid', 'integer');
93  $classSchema->addProperty('firstProperty', 'string');
94  $classSchema->addProperty('secondProperty', 'integer');
95  $classSchema->addProperty('thirdProperty', 'float');
96  $classSchema->addProperty('fourthProperty', 'boolean');
97  $mockReflectionService = $this->getMock(\TYPO3\CMS\Extbase\Reflection\ReflectionService::class, ['getClassSchema']);
98  $mockReflectionService->expects($this->any())->method('getClassSchema')->will($this->returnValue($classSchema));
99  $dataMapper = $this->getAccessibleMock(\TYPO3\CMS\Extbase\Persistence\Generic\Mapper\DataMapper::class, ['dummy']);
100  $dataMapper->_set('dataMaps', $dataMaps);
101  $dataMapper->_set('reflectionService', $mockReflectionService);
102  $dataMapper->_call('thawProperties', $object, $row);
103  $this->assertAttributeEquals('firstValue', 'firstProperty', $object);
104  $this->assertAttributeEquals(1234, 'secondProperty', $object);
105  $this->assertAttributeEquals(1.234, 'thirdProperty', $object);
106  $this->assertAttributeEquals(false, 'fourthProperty', $object);
107  }
108 
115  {
116  $columnMap = new ColumnMap('columnName', 'propertyName');
117  $columnMap->setTypeOfRelation(ColumnMap::RELATION_HAS_ONE);
118  $dataMap = $this->getMock(\TYPO3\CMS\Extbase\Persistence\Generic\Mapper\DataMap::class, ['getColumnMap'], [], '', false);
119  $dataMap->expects($this->any())->method('getColumnMap')->will($this->returnValue($columnMap));
120  $dataMapper = $this->getAccessibleMock(\TYPO3\CMS\Extbase\Persistence\Generic\Mapper\DataMapper::class, ['getDataMap']);
121  $dataMapper->expects($this->any())->method('getDataMap')->will($this->returnValue($dataMap));
122  $result = $dataMapper->_call('fetchRelatedEager', $this->getMock(\TYPO3\CMS\Extbase\DomainObject\AbstractEntity::class), 'SomeName', '');
123  $this->assertEquals(null, $result);
124  }
125 
132  {
133  $columnMap = new ColumnMap('columnName', 'propertyName');
134  $columnMap->setTypeOfRelation(ColumnMap::RELATION_BELONGS_TO_MANY);
135  $dataMap = $this->getMock(\TYPO3\CMS\Extbase\Persistence\Generic\Mapper\DataMap::class, ['getColumnMap'], [], '', false);
136  $dataMap->expects($this->any())->method('getColumnMap')->will($this->returnValue($columnMap));
137  $dataMapper = $this->getAccessibleMock(\TYPO3\CMS\Extbase\Persistence\Generic\Mapper\DataMapper::class, ['getDataMap']);
138  $dataMapper->expects($this->any())->method('getDataMap')->will($this->returnValue($dataMap));
139  $result = $dataMapper->_call('fetchRelatedEager', $this->getMock(\TYPO3\CMS\Extbase\DomainObject\AbstractEntity::class), 'SomeName', '');
140  $this->assertEquals([], $result);
141  }
142 
150  {
151  $columnMap = new ColumnMap('columnName', 'propertyName');
152  $columnMap->setTypeOfRelation(ColumnMap::RELATION_HAS_ONE);
153  $dataMap = $this->getMock(\TYPO3\CMS\Extbase\Persistence\Generic\Mapper\DataMap::class, ['getColumnMap'], [], '', false);
154  $dataMap->expects($this->any())->method('getColumnMap')->will($this->returnValue($columnMap));
155  $dataMapper = $this->getAccessibleMock(\TYPO3\CMS\Extbase\Persistence\Generic\Mapper\DataMapper::class, ['getDataMap', 'fetchRelated']);
156  $dataMapper->expects($this->any())->method('getDataMap')->will($this->returnValue($dataMap));
157  $dataMapper->expects($this->never())->method('fetchRelated');
158  $result = $dataMapper->_call('mapObjectToClassProperty', $this->getMock(\TYPO3\CMS\Extbase\DomainObject\AbstractEntity::class), 'SomeName', '');
159  $this->assertEquals(null, $result);
160  }
161 
169  public function mapObjectToClassPropertyReturnsExistingObjectWithoutCallingFetchRelated()
170  {
171  $columnMap = new ColumnMap('columnName', 'propertyName');
172  $columnMap->setTypeOfRelation(ColumnMap::RELATION_HAS_ONE);
173  $dataMap = $this->getMock(\TYPO3\CMS\Extbase\Persistence\Generic\Mapper\DataMap::class, ['getColumnMap'], [], '', false);
174 
175  $className = $this->getUniqueId('Class1');
176  $classNameWithNS = __NAMESPACE__ . '\\' . $className;
177  eval('namespace ' . __NAMESPACE__ . '; class ' . $className . ' extends \\' . \TYPO3\CMS\Extbase\DomainObject\AbstractEntity::class . ' { public $relationProperty; }');
178  $object = new $classNameWithNS();
179 
180  $className2 = $this->getUniqueId('Class2');
181  $className2WithNS = __NAMESPACE__ . '\\' . $className2;
182  eval('namespace ' . __NAMESPACE__ . '; class ' . $className2 . ' extends \\' . \TYPO3\CMS\Extbase\DomainObject\AbstractEntity::class . ' { }');
183  $child = new $className2WithNS();
184  $classSchema2 = $this->getAccessibleMock(\TYPO3\CMS\Extbase\Reflection\ClassSchema::class, ['dummy'], [$className2WithNS]);
185 
187  $classSchema1 = $this->getAccessibleMock(\TYPO3\CMS\Extbase\Reflection\ClassSchema::class, ['dummy'], [$classNameWithNS]);
188  $classSchema1->addProperty('relationProperty', $className2WithNS);
189  $identifier = 1;
190 
191  $session = new \TYPO3\CMS\Extbase\Persistence\Generic\Session();
192  $session->registerObject($child, $identifier);
193 
194  $mockReflectionService = $this->getMock(\TYPO3\CMS\Extbase\Reflection\ReflectionService::class, ['getClassSchema'], [], '', false);
195  $mockReflectionService->expects($this->any())->method('getClassSchema')->will($this->returnValue($classSchema1));
196 
197  $dataMap->expects($this->any())->method('getColumnMap')->will($this->returnValue($columnMap));
198  $dataMapper = $this->getAccessibleMock(\TYPO3\CMS\Extbase\Persistence\Generic\Mapper\DataMapper::class, ['getDataMap', 'getNonEmptyRelationValue']);
199  $dataMapper->_set('reflectionService', $mockReflectionService);
200  $dataMapper->_set('persistenceSession', $session);
201  $dataMapper->expects($this->any())->method('getDataMap')->will($this->returnValue($dataMap));
202  $dataMapper->expects($this->never())->method('getNonEmptyRelationValue');
203  $result = $dataMapper->_call('mapObjectToClassProperty', $object, 'relationProperty', $identifier);
204  $this->assertEquals($child, $result);
205  }
206 
216  {
217  return [
218  'nothing' => [null, null, null],
219  'timestamp' => [1, null, date('c', 1)],
220  'empty date' => ['0000-00-00', 'date', null],
221  'valid date' => ['2013-01-01', 'date', date('c', strtotime('2013-01-01T00:00:00+00:00'))],
222  'empty datetime' => ['0000-00-00 00:00:00', 'datetime', null],
223  'valid datetime' => ['2013-01-01 01:02:03', 'datetime', date('c', strtotime('2013-01-01T01:02:03+00:00'))],
224  ];
225  }
226 
234  public function mapDateTimeHandlesDifferentFieldEvaluations($value, $storageFormat, $expectedValue)
235  {
236  $accessibleClassName = $this->buildAccessibleProxy(\TYPO3\CMS\Extbase\Persistence\Generic\Mapper\DataMapper::class);
237 
239  $accessibleDataMapFactory = new $accessibleClassName();
240 
242  $dateTime = $accessibleDataMapFactory->_callRef('mapDateTime', $value, $storageFormat);
243 
244  if ($expectedValue === null) {
245  $this->assertNull($dateTime);
246  } else {
247  $this->assertEquals($expectedValue, $dateTime->format('c'));
248  }
249  }
250 
254  public function mapDateTimeHandlesSubclassesOfDateTime()
255  {
256  $accessibleClassName = $this->buildAccessibleProxy(\TYPO3\CMS\Extbase\Persistence\Generic\Mapper\DataMapper::class);
257 
259  $accessibleDataMapFactory = new $accessibleClassName();
260  $targetType = 'TYPO3\CMS\Extbase\Tests\Unit\Persistence\Fixture\Model\CustomDateTime';
261  $date = '2013-01-01 01:02:03';
262  $storageFormat = 'datetime';
263 
265  $dateTime = $accessibleDataMapFactory->_callRef('mapDateTime', $date, $storageFormat, $targetType);
266 
267  $this->assertInstanceOf($targetType, $dateTime);
268  }
269 
274  {
275  $subject = new DataMapper();
276  $columnMap = new ColumnMap('column_name', 'propertyName');
277  $columnMap->setDateTimeStorageFormat('datetime');
278  $datetimeAsString = '2013-04-15 09:30:00';
279  $input = new \DateTime($datetimeAsString, new \DateTimeZone('UTC'));
280  $this->assertEquals('2013-04-15 09:30:00', $subject->getPlainValue($input, $columnMap));
281  $columnMap->setDateTimeStorageFormat('date');
282  $this->assertEquals('2013-04-15', $subject->getPlainValue($input, $columnMap));
283  }
284 
289  public function getPlainValueReturnsExpectedValues($expectedValue, $input)
290  {
291  $dataMapper = new DataMapper();
292  $this->assertSame($expectedValue, $dataMapper->getPlainValue($input));
293  }
294 
299  {
300  $traversableDomainObject = $this->prophesize()
301  ->willImplement(\Iterator::class)
302  ->willImplement(DomainObjectInterface::class);
303  $traversableDomainObject->getUid()->willReturn(1);
304 
305  return [
306  'datetime to timestamp' => ['1365866253', new \DateTime('@1365866253')],
307  'boolean true to 1' => [1, true],
308  'boolean false to 0' => [0, false],
309  'NULL is handled' => ['NULL', null],
310  'plain value is returned unchanged' => ['RANDOM string', 'RANDOM string'],
311  'array is flattened' => ['a,b,c', ['a', 'b', 'c']],
312  'deep array is flattened' => ['a,b,c', [['a', 'b'], 'c']],
313  'traversable domain object to identifier' => [1, $traversableDomainObject->reveal()],
314  ];
315  }
316 
322  {
323  $dataMapper = new DataMapper();
324  $input = $this->getMock(
325  \TYPO3\CMS\Extbase\Persistence\Generic\LazyLoadingProxy::class,
326  [],
327  [],
328  '',
329  false
330  );
331  $input->expects($this->once())->method('_loadRealInstance')->will($this->returnValue($dataMapper));
332  $dataMapper->getPlainValue($input);
333  }
334 
339  {
340  $dataMapper = new DataMapper();
341  $input = $this->getMock(
342  \TYPO3\CMS\Extbase\DomainObject\DomainObjectInterface::class,
343  [],
344  [],
345  '',
346  false
347  );
348  $input->expects($this->once())->method('getUid')->will($this->returnValue(23));
349  $this->assertSame(23, $dataMapper->getPlainValue($input));
350  }
351 }
getAccessibleMock( $originalClassName, $methods=[], array $arguments=[], $mockClassName='', $callOriginalConstructor=true, $callOriginalClone=true, $callAutoload=true)