d2a0fa816f5c9a8e472734a3611379791f5bb56a
[Packages/TYPO3.CMS.git] / typo3 / sysext / extbase / Tests / Unit / Persistence / Generic / Mapper / DataMapperTest.php
1 <?php
2 namespace TYPO3\CMS\Extbase\Tests\Unit\Persistence\Generic\Mapper;
3
4 /***************************************************************
5 * Copyright notice
6 *
7 * (c) 2012 Tymoteusz Motylewski <t.motylewski@gmail.com>
8 * All rights reserved
9 *
10 * Part of This class is a backport of the corresponding class of TYPO3 Flow.
11 * Credits go to the v5 team.
12 *
13 * This script is part of the TYPO3 project. The TYPO3 project is
14 * free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License as published by
16 * the Free Software Foundation; either version 2 of the License, or
17 * (at your option) any later version.
18 *
19 * The GNU General Public License can be found at
20 * http://www.gnu.org/copyleft/gpl.html.
21 *
22 * This script is distributed in the hope that it will be useful,
23 * but WITHOUT ANY WARRANTY; without even the implied warranty of
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 * GNU General Public License for more details.
26 *
27 * This copyright notice MUST APPEAR in all copies of the script!
28 ***************************************************************/
29 /**
30 * Testcase for \TYPO3\CMS\Extbase\Persistence\Generic\Mapper\DataMapper
31 */
32 class DataMapperTest extends \TYPO3\CMS\Extbase\Tests\Unit\BaseTestCase {
33
34 /**
35 * @test
36 */
37 public function mapMapsArrayToObjectByCallingmapToObject() {
38 $rows = array(array('uid' => '1234'));
39 $object = new \stdClass();
40 $dataMapper = $this->getMock('TYPO3\\CMS\\Extbase\\Persistence\\Generic\\Mapper\\DataMapper', array('mapSingleRow', 'getTargetType'));
41 $dataMapper->expects($this->any())->method('getTargetType')->will($this->returnArgument(1));
42 $dataMapFactory = $this->getMock('TYPO3\\CMS\\Extbase\\Persistence\\Generic\\Mapper\\DataMapFactory');
43 $dataMapper->injectDataMapFactory($dataMapFactory);
44 $dataMapper->expects($this->once())->method('mapSingleRow')->with($rows[0])->will($this->returnValue($object));
45 $dataMapper->map(get_class($object), $rows);
46 }
47
48 /**
49 * @test
50 */
51 public function mapSingleRowReturnsObjectFromIdentityMapIfAvailable() {
52 $row = array('uid' => '1234');
53 $object = new \stdClass();
54 $identityMap = $this->getMock('TYPO3\\CMS\\Extbase\\Persistence\\Generic\\IdentityMap');
55 $identityMap->expects($this->once())->method('hasIdentifier')->with('1234')->will($this->returnValue(TRUE));
56 $identityMap->expects($this->once())->method('getObjectByIdentifier')->with('1234')->will($this->returnValue($object));
57 $dataMapper = $this->getAccessibleMock('TYPO3\\CMS\\Extbase\\Persistence\\Generic\\Mapper\\DataMapper', array('dummy'));
58 $dataMapper->injectIdentityMap($identityMap);
59 $dataMapper->_call('mapSingleRow', get_class($object), $row);
60 }
61
62 /**
63 * @test
64 */
65 public function thawPropertiesSetsPropertyValues() {
66 $className = 'Class' . md5(uniqid(mt_rand(), TRUE));
67 eval('class ' . $className . ' extends TYPO3\\CMS\\Extbase\\DomainObject\\AbstractEntity { public $firstProperty; public $secondProperty; public $thirdProperty; public $fourthProperty; }');
68 $object = new $className();
69 $row = array(
70 'uid' => '1234',
71 'firstProperty' => 'firstValue',
72 'secondProperty' => 1234,
73 'thirdProperty' => 1.234,
74 'fourthProperty' => FALSE
75 );
76 $columnMaps = array(
77 'uid' => new \TYPO3\CMS\Extbase\Persistence\Generic\Mapper\ColumnMap('uid', 'uid'),
78 'pid' => new \TYPO3\CMS\Extbase\Persistence\Generic\Mapper\ColumnMap('pid', 'pid'),
79 'firstProperty' => new \TYPO3\CMS\Extbase\Persistence\Generic\Mapper\ColumnMap('firstProperty', 'firstProperty'),
80 'secondProperty' => new \TYPO3\CMS\Extbase\Persistence\Generic\Mapper\ColumnMap('secondProperty', 'secondProperty'),
81 'thirdProperty' => new \TYPO3\CMS\Extbase\Persistence\Generic\Mapper\ColumnMap('thirdProperty', 'thirdProperty')
82 );
83 $dataMap = $this->getAccessibleMock('TYPO3\\CMS\\Extbase\\Persistence\\Generic\\Mapper\\DataMap', array('dummy'), array($className, $className));
84 $dataMap->_set('columnMaps', $columnMaps);
85 $dataMaps = array(
86 $className => $dataMap
87 );
88 $classSchema = new \TYPO3\CMS\Extbase\Reflection\ClassSchema($className);
89 $classSchema->injectTypeHandlingService(new \TYPO3\CMS\Extbase\Service\TypeHandlingService());
90 $classSchema->addProperty('pid', 'integer');
91 $classSchema->addProperty('uid', 'integer');
92 $classSchema->addProperty('firstProperty', 'string');
93 $classSchema->addProperty('secondProperty', 'integer');
94 $classSchema->addProperty('thirdProperty', 'float');
95 $classSchema->addProperty('fourthProperty', 'boolean');
96 $mockReflectionService = $this->getMock('TYPO3\\CMS\\Extbase\\Reflection\\ReflectionService', array('getClassSchema'));
97 $mockReflectionService->expects($this->any())->method('getClassSchema')->will($this->returnValue($classSchema));
98 $dataMapper = $this->getAccessibleMock('TYPO3\\CMS\\Extbase\\Persistence\\Generic\\Mapper\\DataMapper', array('dummy'));
99 $dataMapper->_set('dataMaps', $dataMaps);
100 $dataMapper->injectReflectionService($mockReflectionService);
101 $dataMapper->_call('thawProperties', $object, $row);
102 $this->assertAttributeEquals('firstValue', 'firstProperty', $object);
103 $this->assertAttributeEquals(1234, 'secondProperty', $object);
104 $this->assertAttributeEquals(1.234, 'thirdProperty', $object);
105 $this->assertAttributeEquals(FALSE, 'fourthProperty', $object);
106 }
107
108 /**
109 * Test if fetchRelatedEager method returns NULL when $fieldValue = '' and relation type == RELATION_HAS_ONE
110 *
111 * @test
112 */
113 public function fetchRelatedEagerReturnsNullForEmptyRelationHasOne() {
114 $columnMap = new \TYPO3\CMS\Extbase\Persistence\Generic\Mapper\ColumnMap('columnName', 'propertyName');
115 $columnMap->setTypeOfRelation(\TYPO3\CMS\Extbase\Persistence\Generic\Mapper\ColumnMap::RELATION_HAS_ONE);
116 $dataMap = $this->getMock('TYPO3\\CMS\\Extbase\\Persistence\\Generic\\Mapper\\DataMap', array('getColumnMap'), array(), '', FALSE);
117 $dataMap->expects($this->any())->method('getColumnMap')->will($this->returnValue($columnMap));
118 $dataMapper = $this->getAccessibleMock('TYPO3\\CMS\\Extbase\\Persistence\\Generic\\Mapper\\DataMapper', array('getDataMap'));
119 $dataMapper->expects($this->any())->method('getDataMap')->will($this->returnValue($dataMap));
120 $result = $dataMapper->_call('fetchRelatedEager', $this->getMock('TYPO3\\CMS\\Extbase\\DomainObject\\AbstractEntity'), 'SomeName', '');
121 $this->assertEquals(NULL, $result);
122 }
123
124 /**
125 * Test if fetchRelatedEager method returns empty array when $fieldValue = '' and relation type != RELATION_HAS_ONE
126 *
127 * @test
128 */
129 public function fetchRelatedEagerReturnsEmptyArrayForEmptyRelationNotHasOne() {
130 $columnMap = new \TYPO3\CMS\Extbase\Persistence\Generic\Mapper\ColumnMap('columnName', 'propertyName');
131 $columnMap->setTypeOfRelation(\TYPO3\CMS\Extbase\Persistence\Generic\Mapper\ColumnMap::RELATION_BELONGS_TO_MANY);
132 $dataMap = $this->getMock('TYPO3\\CMS\\Extbase\\Persistence\\Generic\\Mapper\\DataMap', array('getColumnMap'), array(), '', FALSE);
133 $dataMap->expects($this->any())->method('getColumnMap')->will($this->returnValue($columnMap));
134 $dataMapper = $this->getAccessibleMock('TYPO3\\CMS\\Extbase\\Persistence\\Generic\\Mapper\\DataMapper', array('getDataMap'));
135 $dataMapper->expects($this->any())->method('getDataMap')->will($this->returnValue($dataMap));
136 $result = $dataMapper->_call('fetchRelatedEager', $this->getMock('TYPO3\\CMS\\Extbase\\DomainObject\\AbstractEntity'), 'SomeName', '');
137 $this->assertEquals(array(), $result);
138 }
139
140 /**
141 * Data provider for date checks. Date will be stored based on UTC in
142 * the database. That's why it's not possible to check for explicit date
143 * strings but using the date('c') conversion instead, which considers the
144 * current local timezone setting.
145 *
146 * @return array
147 */
148 public function mapDateTimeHandlesDifferentFieldEvaluationsDataProvider() {
149 return array(
150 'nothing' => array(NULL, NULL, NULL),
151 'timestamp' => array(1, NULL, date('c', 1)),
152 'empty date' => array('0000-00-00', 'date', NULL),
153 'valid date' => array('2013-01-01', 'date', date('c', strtotime('2013-01-01T00:00:00+00:00'))),
154 'empty datetime' => array('0000-00-00 00:00:00', 'datetime', NULL),
155 'valid datetime' => array('2013-01-01 01:02:03', 'datetime', date('c', strtotime('2013-01-01T01:02:03+00:00'))),
156 );
157 }
158
159 /**
160 * @param NULL|string|integer $value
161 * @param NULL|string $storageFormat
162 * @param NULL|string $expectedValue
163 * @test
164 * @dataProvider mapDateTimeHandlesDifferentFieldEvaluationsDataProvider
165 */
166 public function mapDateTimeHandlesDifferentFieldEvaluations($value, $storageFormat, $expectedValue) {
167 $accessibleClassName = $this->buildAccessibleProxy('TYPO3\\CMS\\Extbase\\Persistence\\Generic\\Mapper\\DataMapper');
168 $accessibleDataMapFactory = new $accessibleClassName();
169
170 /** @var $dateTime NULL|\DateTime */
171 $dateTime = $accessibleDataMapFactory->_callRef('mapDateTime', $value, $storageFormat);
172
173 if ($expectedValue === NULL) {
174 $this->assertNull($dateTime);
175 } else {
176 $this->assertEquals($expectedValue, $dateTime->format('c'));
177 }
178 }
179 }
180
181 ?>