[TASK] Backport Flow property mapper
[Packages/TYPO3.CMS.git] / typo3 / sysext / extbase / Tests / Unit / Property / PropertyMappingConfigurationTest.php
1 <?php
2 namespace TYPO3\CMS\Extbase\Tests\Unit\Property;
3
4 /* *
5 * This script belongs to the Extbase framework. *
6 * *
7 * It is free software; you can redistribute it and/or modify it under *
8 * the terms of the GNU Lesser General Public License as published by the *
9 * Free Software Foundation, either version 3 of the License, or (at your *
10 * option) any later version. *
11 * *
12 * This script is distributed in the hope that it will be useful, but *
13 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHAN- *
14 * TABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser *
15 * General Public License for more details. *
16 * *
17 * You should have received a copy of the GNU Lesser General Public *
18 * License along with the script. *
19 * If not, see http://www.gnu.org/licenses/lgpl.html *
20 * *
21 * The TYPO3 project - inspiring people to share! *
22 * */
23
24 require_once (__DIR__ . '/../../Fixture/ClassWithSetters.php');
25
26 /**
27 * Testcase for the Property Mapper
28 *
29 * @covers \TYPO3\CMS\Extbase\Property\PropertyMappingConfiguration
30 */
31 class PropertyMappingConfigurationTest extends \TYPO3\CMS\Extbase\Tests\Unit\BaseTestCase {
32
33 /**
34 * @var \TYPO3\CMS\Extbase\Property\PropertyMappingConfiguration
35 */
36 protected $propertyMappingConfiguration;
37
38 /**
39 * Initialization
40 */
41 public function setUp() {
42 $this->propertyMappingConfiguration = new \TYPO3\CMS\Extbase\Property\PropertyMappingConfiguration();
43 }
44
45 /**
46 * @test
47 * @covers \TYPO3\CMS\Extbase\Property\PropertyMappingConfiguration::getTargetPropertyName
48 */
49 public function getTargetPropertyNameShouldReturnTheUnmodifiedPropertyNameWithoutConfiguration() {
50 $this->assertEquals('someSourceProperty', $this->propertyMappingConfiguration->getTargetPropertyName('someSourceProperty'));
51 $this->assertEquals('someOtherSourceProperty', $this->propertyMappingConfiguration->getTargetPropertyName('someOtherSourceProperty'));
52 }
53
54 /**
55 * @test
56 * @covers \TYPO3\CMS\Extbase\Property\PropertyMappingConfiguration::shouldMap
57 */
58 public function shouldMapReturnsFalseByDefault() {
59 $this->assertFalse($this->propertyMappingConfiguration->shouldMap('someSourceProperty'));
60 $this->assertFalse($this->propertyMappingConfiguration->shouldMap('someOtherSourceProperty'));
61 }
62
63 /**
64 * @test
65 * @covers \TYPO3\CMS\Extbase\Property\PropertyMappingConfiguration::shouldMap
66 */
67 public function shouldMapReturnsTrueIfConfigured() {
68 $this->propertyMappingConfiguration->allowAllProperties();
69 $this->assertTrue($this->propertyMappingConfiguration->shouldMap('someSourceProperty'));
70 $this->assertTrue($this->propertyMappingConfiguration->shouldMap('someOtherSourceProperty'));
71 }
72
73 /**
74 * @test
75 * @covers \TYPO3\CMS\Extbase\Property\PropertyMappingConfiguration::shouldMap
76 */
77 public function shouldMapReturnsTrueForAllowedProperties() {
78 $this->propertyMappingConfiguration->allowProperties('someSourceProperty', 'someOtherProperty');
79 $this->assertTrue($this->propertyMappingConfiguration->shouldMap('someSourceProperty'));
80 $this->assertTrue($this->propertyMappingConfiguration->shouldMap('someOtherProperty'));
81 }
82
83 /**
84 * @test
85 * @covers \TYPO3\CMS\Extbase\Property\PropertyMappingConfiguration::shouldMap
86 */
87 public function shouldMapReturnsFalseForBlacklistedProperties() {
88 $this->propertyMappingConfiguration->allowAllPropertiesExcept('someSourceProperty', 'someOtherProperty');
89 $this->assertFalse($this->propertyMappingConfiguration->shouldMap('someSourceProperty'));
90 $this->assertFalse($this->propertyMappingConfiguration->shouldMap('someOtherProperty'));
91
92 $this->assertTrue($this->propertyMappingConfiguration->shouldMap('someOtherPropertyWhichHasNotBeenConfigured'));
93 }
94
95 /**
96 * @test
97 * @covers \TYPO3\CMS\Extbase\Property\PropertyMappingConfiguration::shouldSkip
98 */
99 public function shouldSkipReturnsFalseByDefault() {
100 $this->assertFalse($this->propertyMappingConfiguration->shouldSkip('someSourceProperty'));
101 $this->assertFalse($this->propertyMappingConfiguration->shouldSkip('someOtherSourceProperty'));
102 }
103
104 /**
105 * @test
106 * @covers \TYPO3\CMS\Extbase\Property\PropertyMappingConfiguration::shouldSkip
107 */
108 public function shouldSkipReturnsTrueIfConfigured() {
109 $this->propertyMappingConfiguration->skipProperties('someSourceProperty', 'someOtherSourceProperty');
110 $this->assertTrue($this->propertyMappingConfiguration->shouldSkip('someSourceProperty'));
111 $this->assertTrue($this->propertyMappingConfiguration->shouldSkip('someOtherSourceProperty'));
112 }
113
114 /**
115 * @test
116 */
117 public function setTypeConverterOptionsCanBeRetrievedAgain() {
118 $mockTypeConverterClass = $this->getMockClass('TYPO3\CMS\Extbase\Property\TypeConverterInterface');
119
120 $this->propertyMappingConfiguration->setTypeConverterOptions($mockTypeConverterClass, array('k1' => 'v1', 'k2' => 'v2'));
121 $this->assertEquals('v1', $this->propertyMappingConfiguration->getConfigurationValue($mockTypeConverterClass, 'k1'));
122 $this->assertEquals('v2', $this->propertyMappingConfiguration->getConfigurationValue($mockTypeConverterClass, 'k2'));
123 }
124
125 /**
126 * @test
127 */
128 public function nonexistentTypeConverterOptionsReturnNull() {
129 $this->assertNull($this->propertyMappingConfiguration->getConfigurationValue('foo', 'bar'));
130 }
131
132 /**
133 * @test
134 */
135 public function setTypeConverterOptionsShouldOverrideAlreadySetOptions() {
136 $mockTypeConverterClass = $this->getMockClass('TYPO3\CMS\Extbase\Property\TypeConverterInterface');
137 $this->propertyMappingConfiguration->setTypeConverterOptions($mockTypeConverterClass, array('k1' => 'v1', 'k2' => 'v2'));
138 $this->propertyMappingConfiguration->setTypeConverterOptions($mockTypeConverterClass, array('k3' => 'v3'));
139
140 $this->assertEquals('v3', $this->propertyMappingConfiguration->getConfigurationValue($mockTypeConverterClass, 'k3'));
141 $this->assertNull($this->propertyMappingConfiguration->getConfigurationValue($mockTypeConverterClass, 'k2'));
142 }
143
144 /**
145 * @test
146 */
147 public function setTypeConverterOptionShouldOverrideAlreadySetOptions() {
148 $mockTypeConverterClass = $this->getMockClass('TYPO3\CMS\Extbase\Property\TypeConverterInterface');
149 $this->propertyMappingConfiguration->setTypeConverterOptions($mockTypeConverterClass, array('k1' => 'v1', 'k2' => 'v2'));
150 $this->propertyMappingConfiguration->setTypeConverterOption($mockTypeConverterClass, 'k1', 'v3');
151
152 $this->assertEquals('v3', $this->propertyMappingConfiguration->getConfigurationValue($mockTypeConverterClass, 'k1'));
153 $this->assertEquals('v2', $this->propertyMappingConfiguration->getConfigurationValue($mockTypeConverterClass, 'k2'));
154 }
155
156 /**
157 * @test
158 */
159 public function getTypeConverterReturnsNullIfNoTypeConverterSet() {
160 $this->assertNull($this->propertyMappingConfiguration->getTypeConverter());
161 }
162
163 /**
164 * @test
165 */
166 public function getTypeConverterReturnsTypeConverterIfItHasBeenSet() {
167 $mockTypeConverter = $this->getMock('TYPO3\\CMS\\Extbase\\Property\\TypeConverterInterface');
168 $this->propertyMappingConfiguration->setTypeConverter($mockTypeConverter);
169 $this->assertSame($mockTypeConverter, $this->propertyMappingConfiguration->getTypeConverter());
170 }
171
172 /**
173 * @return \TYPO3\CMS\Extbase\Property\PropertyMappingConfiguration
174 */
175 protected function buildChildConfigurationForSingleProperty() {
176 $childConfiguration = $this->propertyMappingConfiguration->forProperty('key1.key2');
177 $childConfiguration->setTypeConverterOption('someConverter', 'foo', 'specialChildConverter');
178
179 return $childConfiguration;
180 }
181
182 /**
183 * @test
184 */
185 public function getTargetPropertyNameShouldRespectMapping() {
186 $this->propertyMappingConfiguration->setMapping('k1', 'k1a');
187 $this->assertEquals('k1a', $this->propertyMappingConfiguration->getTargetPropertyName('k1'));
188 $this->assertEquals('k2', $this->propertyMappingConfiguration->getTargetPropertyName('k2'));
189 }
190
191 /**
192 * @return array Signature: $methodToTestForFluentInterface [, $argumentsForMethod = array() ]
193 */
194 public function fluentInterfaceMethodsDataProvider() {
195 $mockTypeConverterClass = $this->getMockClass('TYPO3\CMS\Extbase\Property\TypeConverterInterface');
196
197 return array(
198 array('allowAllProperties'),
199 array('allowProperties'),
200 array('allowAllPropertiesExcept'),
201 array('setMapping', array('k1', 'k1a')),
202 array('setTypeConverterOptions', array($mockTypeConverterClass, array('k1' => 'v1', 'k2' => 'v2'))),
203 array('setTypeConverterOption', array($mockTypeConverterClass, 'k1', 'v3')),
204 array('setTypeConverter', array($this->getMock('TYPO3\CMS\Extbase\Property\TypeConverterInterface'))),
205 );
206 }
207
208 /**
209 * @test
210 * @dataProvider fluentInterfaceMethodsDataProvider
211 */
212 public function respectiveMethodsProvideFluentInterface($methodToTestForFluentInterface, array $argumentsForMethod = array()) {
213 $actualResult = call_user_func_array(array($this->propertyMappingConfiguration, $methodToTestForFluentInterface), $argumentsForMethod);
214 $this->assertSame($this->propertyMappingConfiguration, $actualResult);
215 }
216
217 /**
218 * @test
219 */
220 public function forPropertyWithAsteriskAllowsArbitraryPropertyNamesWithGetConfigurationFor() {
221 // using stdClass so that class_parents() in getTypeConvertersWithParentClasses() is happy
222 $this->propertyMappingConfiguration->forProperty('items.*')->setTypeConverterOptions('stdClass', array('k1' => 'v1'));
223
224 $configuration = $this->propertyMappingConfiguration->getConfigurationFor('items')->getConfigurationFor('6');
225 $this->assertSame('v1', $configuration->getConfigurationValue('stdClass', 'k1'));
226 }
227
228 /**
229 * @test
230 */
231 public function forPropertyWithAsteriskAllowsArbitraryPropertyNamesWithForProperty() {
232 // using stdClass so that class_parents() in getTypeConvertersWithParentClasses() is happy
233 $this->propertyMappingConfiguration->forProperty('items.*.foo')->setTypeConverterOptions('stdClass', array('k1' => 'v1'));
234
235 $configuration = $this->propertyMappingConfiguration->forProperty('items.6.foo');
236 $this->assertSame('v1', $configuration->getConfigurationValue('stdClass', 'k1'));
237 }
238
239 /**
240 * @test
241 */
242 public function forPropertyWithAsteriskAllowsArbitraryPropertyNamesWithShouldMap() {
243 $this->propertyMappingConfiguration->forProperty('items.*')->setTypeConverterOptions('stdClass', array('k1' => 'v1'));
244
245 $configuration = $this->propertyMappingConfiguration->forProperty('items');
246 $this->assertTrue($configuration->shouldMap(6));
247 }
248
249 }
250 ?>