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