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