[TASK] Use name-resolution instead of strings where possible: 3
[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 /**
25 * Test case
26 */
27 class PropertyMappingConfigurationTest extends \TYPO3\CMS\Core\Tests\UnitTestCase {
28
29 /**
30 * @var \TYPO3\CMS\Extbase\Property\PropertyMappingConfiguration
31 */
32 protected $propertyMappingConfiguration;
33
34 /**
35 * Initialization
36 */
37 public function setUp() {
38 $this->propertyMappingConfiguration = new \TYPO3\CMS\Extbase\Property\PropertyMappingConfiguration();
39 }
40
41 /**
42 * @test
43 * @covers \TYPO3\CMS\Extbase\Property\PropertyMappingConfiguration::getTargetPropertyName
44 */
45 public function getTargetPropertyNameShouldReturnTheUnmodifiedPropertyNameWithoutConfiguration() {
46 $this->assertEquals('someSourceProperty', $this->propertyMappingConfiguration->getTargetPropertyName('someSourceProperty'));
47 $this->assertEquals('someOtherSourceProperty', $this->propertyMappingConfiguration->getTargetPropertyName('someOtherSourceProperty'));
48 }
49
50 /**
51 * @test
52 * @covers \TYPO3\CMS\Extbase\Property\PropertyMappingConfiguration::shouldMap
53 */
54 public function shouldMapReturnsFalseByDefault() {
55 $this->assertFalse($this->propertyMappingConfiguration->shouldMap('someSourceProperty'));
56 $this->assertFalse($this->propertyMappingConfiguration->shouldMap('someOtherSourceProperty'));
57 }
58
59 /**
60 * @test
61 * @covers \TYPO3\CMS\Extbase\Property\PropertyMappingConfiguration::shouldMap
62 */
63 public function shouldMapReturnsTrueIfConfigured() {
64 $this->propertyMappingConfiguration->allowAllProperties();
65 $this->assertTrue($this->propertyMappingConfiguration->shouldMap('someSourceProperty'));
66 $this->assertTrue($this->propertyMappingConfiguration->shouldMap('someOtherSourceProperty'));
67 }
68
69 /**
70 * @test
71 * @covers \TYPO3\CMS\Extbase\Property\PropertyMappingConfiguration::shouldMap
72 */
73 public function shouldMapReturnsTrueForAllowedProperties() {
74 $this->propertyMappingConfiguration->allowProperties('someSourceProperty', 'someOtherProperty');
75 $this->assertTrue($this->propertyMappingConfiguration->shouldMap('someSourceProperty'));
76 $this->assertTrue($this->propertyMappingConfiguration->shouldMap('someOtherProperty'));
77 }
78
79 /**
80 * @test
81 * @covers \TYPO3\CMS\Extbase\Property\PropertyMappingConfiguration::shouldMap
82 */
83 public function shouldMapReturnsFalseForBlacklistedProperties() {
84 $this->propertyMappingConfiguration->allowAllPropertiesExcept('someSourceProperty', 'someOtherProperty');
85 $this->assertFalse($this->propertyMappingConfiguration->shouldMap('someSourceProperty'));
86 $this->assertFalse($this->propertyMappingConfiguration->shouldMap('someOtherProperty'));
87
88 $this->assertTrue($this->propertyMappingConfiguration->shouldMap('someOtherPropertyWhichHasNotBeenConfigured'));
89 }
90
91 /**
92 * @test
93 * @covers \TYPO3\CMS\Extbase\Property\PropertyMappingConfiguration::shouldSkip
94 */
95 public function shouldSkipReturnsFalseByDefault() {
96 $this->assertFalse($this->propertyMappingConfiguration->shouldSkip('someSourceProperty'));
97 $this->assertFalse($this->propertyMappingConfiguration->shouldSkip('someOtherSourceProperty'));
98 }
99
100 /**
101 * @test
102 * @covers \TYPO3\CMS\Extbase\Property\PropertyMappingConfiguration::shouldSkip
103 */
104 public function shouldSkipReturnsTrueIfConfigured() {
105 $this->propertyMappingConfiguration->skipProperties('someSourceProperty', 'someOtherSourceProperty');
106 $this->assertTrue($this->propertyMappingConfiguration->shouldSkip('someSourceProperty'));
107 $this->assertTrue($this->propertyMappingConfiguration->shouldSkip('someOtherSourceProperty'));
108 }
109
110 /**
111 * @test
112 */
113 public function setTypeConverterOptionsCanBeRetrievedAgain() {
114 $mockTypeConverterClass = $this->getMockClass(\TYPO3\CMS\Extbase\Property\TypeConverterInterface::class);
115
116 $this->propertyMappingConfiguration->setTypeConverterOptions($mockTypeConverterClass, array('k1' => 'v1', 'k2' => 'v2'));
117 $this->assertEquals('v1', $this->propertyMappingConfiguration->getConfigurationValue($mockTypeConverterClass, 'k1'));
118 $this->assertEquals('v2', $this->propertyMappingConfiguration->getConfigurationValue($mockTypeConverterClass, 'k2'));
119 }
120
121 /**
122 * @test
123 */
124 public function nonexistentTypeConverterOptionsReturnNull() {
125 $this->assertNull($this->propertyMappingConfiguration->getConfigurationValue('foo', 'bar'));
126 }
127
128 /**
129 * @test
130 */
131 public function setTypeConverterOptionsShouldOverrideAlreadySetOptions() {
132 $mockTypeConverterClass = $this->getMockClass(\TYPO3\CMS\Extbase\Property\TypeConverterInterface::class);
133 $this->propertyMappingConfiguration->setTypeConverterOptions($mockTypeConverterClass, array('k1' => 'v1', 'k2' => 'v2'));
134 $this->propertyMappingConfiguration->setTypeConverterOptions($mockTypeConverterClass, array('k3' => 'v3'));
135
136 $this->assertEquals('v3', $this->propertyMappingConfiguration->getConfigurationValue($mockTypeConverterClass, 'k3'));
137 $this->assertNull($this->propertyMappingConfiguration->getConfigurationValue($mockTypeConverterClass, 'k2'));
138 }
139
140 /**
141 * @test
142 */
143 public function setTypeConverterOptionShouldOverrideAlreadySetOptions() {
144 $mockTypeConverterClass = $this->getMockClass(\TYPO3\CMS\Extbase\Property\TypeConverterInterface::class);
145 $this->propertyMappingConfiguration->setTypeConverterOptions($mockTypeConverterClass, array('k1' => 'v1', 'k2' => 'v2'));
146 $this->propertyMappingConfiguration->setTypeConverterOption($mockTypeConverterClass, 'k1', 'v3');
147
148 $this->assertEquals('v3', $this->propertyMappingConfiguration->getConfigurationValue($mockTypeConverterClass, 'k1'));
149 $this->assertEquals('v2', $this->propertyMappingConfiguration->getConfigurationValue($mockTypeConverterClass, 'k2'));
150 }
151
152 /**
153 * @test
154 */
155 public function getTypeConverterReturnsNullIfNoTypeConverterSet() {
156 $this->assertNull($this->propertyMappingConfiguration->getTypeConverter());
157 }
158
159 /**
160 * @test
161 */
162 public function getTypeConverterReturnsTypeConverterIfItHasBeenSet() {
163 $mockTypeConverter = $this->getMock(\TYPO3\CMS\Extbase\Property\TypeConverterInterface::class);
164 $this->propertyMappingConfiguration->setTypeConverter($mockTypeConverter);
165 $this->assertSame($mockTypeConverter, $this->propertyMappingConfiguration->getTypeConverter());
166 }
167
168 /**
169 * @return \TYPO3\CMS\Extbase\Property\PropertyMappingConfiguration
170 */
171 protected function buildChildConfigurationForSingleProperty() {
172 $childConfiguration = $this->propertyMappingConfiguration->forProperty('key1.key2');
173 $childConfiguration->setTypeConverterOption('someConverter', 'foo', 'specialChildConverter');
174
175 return $childConfiguration;
176 }
177
178 /**
179 * @test
180 */
181 public function getTargetPropertyNameShouldRespectMapping() {
182 $this->propertyMappingConfiguration->setMapping('k1', 'k1a');
183 $this->assertEquals('k1a', $this->propertyMappingConfiguration->getTargetPropertyName('k1'));
184 $this->assertEquals('k2', $this->propertyMappingConfiguration->getTargetPropertyName('k2'));
185 }
186
187 /**
188 * @return array Signature: $methodToTestForFluentInterface [, $argumentsForMethod = array() ]
189 */
190 public function fluentInterfaceMethodsDataProvider() {
191 $mockTypeConverterClass = $this->getMockClass(\TYPO3\CMS\Extbase\Property\TypeConverterInterface::class);
192
193 return array(
194 array('allowAllProperties'),
195 array('allowProperties'),
196 array('allowAllPropertiesExcept'),
197 array('setMapping', array('k1', 'k1a')),
198 array('setTypeConverterOptions', array($mockTypeConverterClass, array('k1' => 'v1', 'k2' => 'v2'))),
199 array('setTypeConverterOption', array($mockTypeConverterClass, 'k1', 'v3')),
200 array('setTypeConverter', array($this->getMock(\TYPO3\CMS\Extbase\Property\TypeConverterInterface::class))),
201 );
202 }
203
204 /**
205 * @test
206 * @dataProvider fluentInterfaceMethodsDataProvider
207 */
208 public function respectiveMethodsProvideFluentInterface($methodToTestForFluentInterface, array $argumentsForMethod = array()) {
209 $actualResult = call_user_func_array(array($this->propertyMappingConfiguration, $methodToTestForFluentInterface), $argumentsForMethod);
210 $this->assertSame($this->propertyMappingConfiguration, $actualResult);
211 }
212
213 /**
214 * @test
215 */
216 public function forPropertyWithAsteriskAllowsArbitraryPropertyNamesWithGetConfigurationFor() {
217 // using stdClass so that class_parents() in getTypeConvertersWithParentClasses() is happy
218 $this->propertyMappingConfiguration->forProperty('items.*')->setTypeConverterOptions('stdClass', array('k1' => 'v1'));
219
220 $configuration = $this->propertyMappingConfiguration->getConfigurationFor('items')->getConfigurationFor('6');
221 $this->assertSame('v1', $configuration->getConfigurationValue('stdClass', 'k1'));
222 }
223
224 /**
225 * @test
226 */
227 public function forPropertyWithAsteriskAllowsArbitraryPropertyNamesWithForProperty() {
228 // using stdClass so that class_parents() in getTypeConvertersWithParentClasses() is happy
229 $this->propertyMappingConfiguration->forProperty('items.*.foo')->setTypeConverterOptions('stdClass', array('k1' => 'v1'));
230
231 $configuration = $this->propertyMappingConfiguration->forProperty('items.6.foo');
232 $this->assertSame('v1', $configuration->getConfigurationValue('stdClass', 'k1'));
233 }
234
235 /**
236 * @test
237 */
238 public function forPropertyWithAsteriskAllowsArbitraryPropertyNamesWithShouldMap() {
239 $this->propertyMappingConfiguration->forProperty('items.*')->setTypeConverterOptions('stdClass', array('k1' => 'v1'));
240
241 $configuration = $this->propertyMappingConfiguration->forProperty('items');
242 $this->assertTrue($configuration->shouldMap(6));
243 }
244
245 }