[TASK] Replace inject methods with @inject
[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->getAccessibleMock('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->_set('dataMapFactory', $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->_set('identityMap', $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 /** @var \TYPO3\CMS\Core\Tests\AccessibleObjectInterface|\TYPO3\CMS\Extbase\Reflection\ClassSchema $classSchema */
89 $classSchema = $this->getAccessibleMock('TYPO3\CMS\Extbase\Reflection\ClassSchema', array('dummy'), array($className));
90 $classSchema->_set('typeHandlingService', new \TYPO3\CMS\Extbase\Service\TypeHandlingService());
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', array('getClassSchema'));
98 $mockReflectionService->expects($this->any())->method('getClassSchema')->will($this->returnValue($classSchema));
99 $dataMapper = $this->getAccessibleMock('TYPO3\\CMS\\Extbase\\Persistence\\Generic\\Mapper\\DataMapper', array('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
109 /**
110 * Test if fetchRelatedEager method returns NULL when $fieldValue = '' and relation type == RELATION_HAS_ONE
111 *
112 * @test
113 */
114 public function fetchRelatedEagerReturnsNullForEmptyRelationHasOne() {
115 $columnMap = new \TYPO3\CMS\Extbase\Persistence\Generic\Mapper\ColumnMap('columnName', 'propertyName');
116 $columnMap->setTypeOfRelation(\TYPO3\CMS\Extbase\Persistence\Generic\Mapper\ColumnMap::RELATION_HAS_ONE);
117 $dataMap = $this->getMock('TYPO3\\CMS\\Extbase\\Persistence\\Generic\\Mapper\\DataMap', array('getColumnMap'), array(), '', FALSE);
118 $dataMap->expects($this->any())->method('getColumnMap')->will($this->returnValue($columnMap));
119 $dataMapper = $this->getAccessibleMock('TYPO3\\CMS\\Extbase\\Persistence\\Generic\\Mapper\\DataMapper', array('getDataMap'));
120 $dataMapper->expects($this->any())->method('getDataMap')->will($this->returnValue($dataMap));
121 $result = $dataMapper->_call('fetchRelatedEager', $this->getMock('TYPO3\\CMS\\Extbase\\DomainObject\\AbstractEntity'), 'SomeName', '');
122 $this->assertEquals(NULL, $result);
123 }
124
125 /**
126 * Test if fetchRelatedEager method returns empty array when $fieldValue = '' and relation type != RELATION_HAS_ONE
127 *
128 * @test
129 */
130 public function fetchRelatedEagerReturnsEmptyArrayForEmptyRelationNotHasOne() {
131 $columnMap = new \TYPO3\CMS\Extbase\Persistence\Generic\Mapper\ColumnMap('columnName', 'propertyName');
132 $columnMap->setTypeOfRelation(\TYPO3\CMS\Extbase\Persistence\Generic\Mapper\ColumnMap::RELATION_BELONGS_TO_MANY);
133 $dataMap = $this->getMock('TYPO3\\CMS\\Extbase\\Persistence\\Generic\\Mapper\\DataMap', array('getColumnMap'), array(), '', FALSE);
134 $dataMap->expects($this->any())->method('getColumnMap')->will($this->returnValue($columnMap));
135 $dataMapper = $this->getAccessibleMock('TYPO3\\CMS\\Extbase\\Persistence\\Generic\\Mapper\\DataMapper', array('getDataMap'));
136 $dataMapper->expects($this->any())->method('getDataMap')->will($this->returnValue($dataMap));
137 $result = $dataMapper->_call('fetchRelatedEager', $this->getMock('TYPO3\\CMS\\Extbase\\DomainObject\\AbstractEntity'), 'SomeName', '');
138 $this->assertEquals(array(), $result);
139 }
140
141 /**
142 * Data provider for date checks. Date will be stored based on UTC in
143 * the database. That's why it's not possible to check for explicit date
144 * strings but using the date('c') conversion instead, which considers the
145 * current local timezone setting.
146 *
147 * @return array
148 */
149 public function mapDateTimeHandlesDifferentFieldEvaluationsDataProvider() {
150 return array(
151 'nothing' => array(NULL, NULL, NULL),
152 'timestamp' => array(1, NULL, date('c', 1)),
153 'empty date' => array('0000-00-00', 'date', NULL),
154 'valid date' => array('2013-01-01', 'date', date('c', strtotime('2013-01-01T00:00:00+00:00'))),
155 'empty datetime' => array('0000-00-00 00:00:00', 'datetime', NULL),
156 'valid datetime' => array('2013-01-01 01:02:03', 'datetime', date('c', strtotime('2013-01-01T01:02:03+00:00'))),
157 );
158 }
159
160 /**
161 * @param NULL|string|integer $value
162 * @param NULL|string $storageFormat
163 * @param NULL|string $expectedValue
164 * @test
165 * @dataProvider mapDateTimeHandlesDifferentFieldEvaluationsDataProvider
166 */
167 public function mapDateTimeHandlesDifferentFieldEvaluations($value, $storageFormat, $expectedValue) {
168 $accessibleClassName = $this->buildAccessibleProxy('TYPO3\\CMS\\Extbase\\Persistence\\Generic\\Mapper\\DataMapper');
169 $accessibleDataMapFactory = new $accessibleClassName();
170
171 /** @var $dateTime NULL|\DateTime */
172 $dateTime = $accessibleDataMapFactory->_callRef('mapDateTime', $value, $storageFormat);
173
174 if ($expectedValue === NULL) {
175 $this->assertNull($dateTime);
176 } else {
177 $this->assertEquals($expectedValue, $dateTime->format('c'));
178 }
179 }
180 }
181
182 ?>