c225add6b904818dd5fc0165ade17a0ea1a406e0
[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 * Testcase for the Property Mapper
25 *
26 * @covers \TYPO3\CMS\Extbase\Property\PropertyMappingConfiguration
27 */
28 class PropertyMappingConfigurationTest extends \TYPO3\CMS\Extbase\Tests\Unit\BaseTestCase {
29
30 /**
31 * @var \TYPO3\CMS\Extbase\Property\PropertyMappingConfiguration
32 */
33 protected $propertyMappingConfiguration;
34
35 public function setUp() {
36 $this->propertyMappingConfiguration = new \TYPO3\CMS\Extbase\Property\PropertyMappingConfiguration();
37 }
38
39 /**
40 * @test
41 * @covers \TYPO3\CMS\Extbase\Property\PropertyMappingConfiguration::getTargetPropertyName
42 */
43 public function getTargetPropertyNameShouldReturnTheUnmodifiedPropertyNameWithoutConfiguration() {
44 $this->assertEquals('someSourceProperty', $this->propertyMappingConfiguration->getTargetPropertyName('someSourceProperty'));
45 $this->assertEquals('someOtherSourceProperty', $this->propertyMappingConfiguration->getTargetPropertyName('someOtherSourceProperty'));
46 }
47
48 /**
49 * @test
50 * @covers \TYPO3\CMS\Extbase\Property\PropertyMappingConfiguration::shouldMap
51 */
52 public function shouldMapReturnsFalseByDefault() {
53 $this->assertFalse($this->propertyMappingConfiguration->shouldMap('someSourceProperty'));
54 $this->assertFalse($this->propertyMappingConfiguration->shouldMap('someOtherSourceProperty'));
55 }
56
57 /**
58 * @test
59 * @covers \TYPO3\CMS\Extbase\Property\PropertyMappingConfiguration::shouldMap
60 */
61 public function shouldMapReturnsTrueIfConfigured() {
62 $this->propertyMappingConfiguration->allowAllProperties();
63 $this->assertTrue($this->propertyMappingConfiguration->shouldMap('someSourceProperty'));
64 $this->assertTrue($this->propertyMappingConfiguration->shouldMap('someOtherSourceProperty'));
65 }
66
67 /**
68 * @test
69 * @covers \TYPO3\CMS\Extbase\Property\PropertyMappingConfiguration::shouldMap
70 */
71 public function shouldMapReturnsTrueForAllowedProperties() {
72 $this->propertyMappingConfiguration->allowProperties('someSourceProperty', 'someOtherProperty');
73 $this->assertTrue($this->propertyMappingConfiguration->shouldMap('someSourceProperty'));
74 $this->assertTrue($this->propertyMappingConfiguration->shouldMap('someOtherProperty'));
75 }
76
77 /**
78 * @test
79 * @covers \TYPO3\CMS\Extbase\Property\PropertyMappingConfiguration::shouldMap
80 */
81 public function shouldMapReturnsFalseForBlacklistedProperties() {
82 $this->propertyMappingConfiguration->allowAllPropertiesExcept('someSourceProperty', 'someOtherProperty');
83 $this->assertFalse($this->propertyMappingConfiguration->shouldMap('someSourceProperty'));
84 $this->assertFalse($this->propertyMappingConfiguration->shouldMap('someOtherProperty'));
85 $this->assertTrue($this->propertyMappingConfiguration->shouldMap('someOtherPropertyWhichHasNotBeenConfigured'));
86 }
87
88 /**
89 * DataProvider for typeConfiguration tests
90 * @return array
91 */
92 public function provideTypeConverterSettings () {
93 return array(
94 // kept for historical reasons, this test was the initial one
95 'dummy data' => array(
96 'someConverter',
97 'someConverter',
98 array('k1' => 'v1', 'k2' => 'v2')
99 ),
100 'typeConverterName oldschool' => array(
101 'Tx_Extbase_Property_TypeConverter_DateTimeConverter',
102 'TYPO3\\CMS\\Extbase\\Property\\TypeConverter\\DateTimeConverter',
103 array('k1' => 'v1', 'k2' => 'v2')
104 ),
105 'typeConverterName namespaced' => array(
106 'TYPO3\\CMS\\Extbase\\Property\\TypeConverter\\DateTimeConverter',
107 'TYPO3\\CMS\\Extbase\\Property\\TypeConverter\\DateTimeConverter',
108 array('k1' => 'v1', 'k2' => 'v2')
109 )
110 );
111 }
112
113 /**
114 * @dataProvider provideTypeConverterSettings
115 * @test
116 */
117 public function setTypeConverterOptionsCanBeRetrievedAgain($converterName, $converterClass, $values) {
118 $this->propertyMappingConfiguration->setTypeConverterOptions($converterName, $values);
119 $this->assertEquals($values['k1'], $this->propertyMappingConfiguration->getConfigurationValue($converterClass, 'k1'));
120 $this->assertEquals($values['k2'], $this->propertyMappingConfiguration->getConfigurationValue($converterClass, 'k2'));
121 }
122
123 /**
124 * @test
125 */
126 public function inexistentTypeConverterOptionsReturnNull() {
127 $this->assertNull($this->propertyMappingConfiguration->getConfigurationValue('foo', 'bar'));
128 }
129
130 /**
131 * @test
132 */
133 public function setTypeConverterOptionsShouldOverrideAlreadySetOptions() {
134 $this->propertyMappingConfiguration->setTypeConverterOptions('someConverter', array('k1' => 'v1', 'k2' => 'v2'));
135 $this->propertyMappingConfiguration->setTypeConverterOptions('someConverter', array('k3' => 'v3'));
136 $this->assertEquals('v3', $this->propertyMappingConfiguration->getConfigurationValue('someConverter', 'k3'));
137 $this->assertNull($this->propertyMappingConfiguration->getConfigurationValue('someConverter', 'k2'));
138 }
139
140 /**
141 * @test
142 */
143 public function setTypeConverterOptionShouldOverrideAlreadySetOptions() {
144 $this->propertyMappingConfiguration->setTypeConverterOptions('someConverter', array('k1' => 'v1', 'k2' => 'v2'));
145 $this->propertyMappingConfiguration->setTypeConverterOption('someConverter', 'k1', 'v3');
146 $this->assertEquals('v3', $this->propertyMappingConfiguration->getConfigurationValue('someConverter', 'k1'));
147 $this->assertEquals('v2', $this->propertyMappingConfiguration->getConfigurationValue('someConverter', 'k2'));
148 }
149
150 /**
151 * @test
152 */
153 public function getTypeConverterReturnsNullIfNoTypeConverterSet() {
154 $this->assertNull($this->propertyMappingConfiguration->getTypeConverter());
155 }
156
157 /**
158 * @test
159 */
160 public function getTypeConverterReturnsTypeConverterIfItHasBeenSet() {
161 $mockTypeConverter = $this->getMock('TYPO3\\CMS\\Extbase\\Property\\TypeConverterInterface');
162 $this->propertyMappingConfiguration->setTypeConverter($mockTypeConverter);
163 $this->assertSame($mockTypeConverter, $this->propertyMappingConfiguration->getTypeConverter());
164 }
165
166 /**
167 * @return \TYPO3\CMS\Extbase\Property\PropertyMappingConfiguration
168 */
169 protected function buildChildConfigurationForSingleProperty() {
170 $childConfiguration = $this->propertyMappingConfiguration->forProperty('key1.key2');
171 $childConfiguration->setTypeConverterOption('someConverter', 'foo', 'specialChildConverter');
172 return $childConfiguration;
173 }
174
175 /**
176 * @test
177 */
178 public function getTargetPropertyNameShouldRespectMapping() {
179 $this->propertyMappingConfiguration->setMapping('k1', 'k1a');
180 $this->assertEquals('k1a', $this->propertyMappingConfiguration->getTargetPropertyName('k1'));
181 $this->assertEquals('k2', $this->propertyMappingConfiguration->getTargetPropertyName('k2'));
182 }
183
184 /**
185 * @test
186 */
187 public function shouldMapAllowsArbitraryPropertiesWhenConfiguredWithAsterisk() {
188 $this->propertyMappingConfiguration->forProperty('items.*')->setTypeConverterOptions('someConverter', array('k1' => 'v1'));
189 $configuration = $this->propertyMappingConfiguration->forProperty('items');
190 $this->assertTrue($configuration->shouldMap(uniqid()));
191 }
192 }
193
194 ?>