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