[CLEANUP] Adjust code to coding guidelines
[Packages/TYPO3.CMS.git] / typo3 / sysext / extbase / Tests / Unit / Reflection / ObjectAccessTest.php
1 <?php
2 namespace TYPO3\CMS\Extbase\Tests\Unit\Reflection;
3
4 /***************************************************************
5 * Copyright notice
6 *
7 * All rights reserved
8 *
9 * This class is a backport of the corresponding class of TYPO3 Flow.
10 * All credits go to the TYPO3 Flow team.
11 *
12 * This script is part of the TYPO3 project. The TYPO3 project is
13 * free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 2 of the License, or
16 * (at your option) any later version.
17 *
18 * The GNU General Public License can be found at
19 * http://www.gnu.org/copyleft/gpl.html.
20 *
21 * This script is distributed in the hope that it will be useful,
22 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 * GNU General Public License for more details.
25 *
26 * This copyright notice MUST APPEAR in all copies of the script!
27 ***************************************************************/
28 require_once 'Fixture/DummyClassWithGettersAndSetters.php';
29 require_once 'Fixture/ArrayAccessClass.php';
30
31 /**
32 * Test Unit Test Base Class
33 */
34 class ObjectAccessTest extends \TYPO3\CMS\Extbase\Tests\Unit\BaseTestCase {
35
36 protected $dummyObject;
37
38 public function setUp() {
39 $this->dummyObject = new \TYPO3\CMS\Extbase\Tests\Unit\Reflection\Fixture\DummyClassWithGettersAndSetters();
40 $this->dummyObject->setProperty('string1');
41 $this->dummyObject->setAnotherProperty(42);
42 $this->dummyObject->shouldNotBePickedUp = TRUE;
43 }
44
45 /**
46 * @test
47 */
48 public function getPropertyReturnsExpectedValueForGetterProperty() {
49 $property = \TYPO3\CMS\Extbase\Reflection\ObjectAccess::getProperty($this->dummyObject, 'property');
50 $this->assertEquals($property, 'string1');
51 }
52
53 /**
54 * @test
55 */
56 public function getPropertyReturnsExpectedValueForPublicProperty() {
57 $property = \TYPO3\CMS\Extbase\Reflection\ObjectAccess::getProperty($this->dummyObject, 'publicProperty2');
58 $this->assertEquals($property, 42, 'A property of a given object was not returned correctly.');
59 }
60
61 /**
62 * @test
63 */
64 public function getPropertyReturnsExpectedValueForUnexposedPropertyIfForceDirectAccessIsTrue() {
65 $property = \TYPO3\CMS\Extbase\Reflection\ObjectAccess::getProperty($this->dummyObject, 'unexposedProperty', TRUE);
66 $this->assertEquals($property, 'unexposed', 'A property of a given object was not returned correctly.');
67 }
68
69 /**
70 * @test
71 */
72 public function getPropertyReturnsExpectedValueForUnknownPropertyIfForceDirectAccessIsTrue() {
73 $this->dummyObject->unknownProperty = 'unknown';
74 $property = \TYPO3\CMS\Extbase\Reflection\ObjectAccess::getProperty($this->dummyObject, 'unknownProperty', TRUE);
75 $this->assertEquals($property, 'unknown', 'A property of a given object was not returned correctly.');
76 }
77
78 /**
79 * @test
80 * @expectedException \TYPO3\CMS\Extbase\Reflection\Exception\PropertyNotAccessibleException
81 */
82 public function getPropertyReturnsPropertyNotAccessibleExceptionForNotExistingPropertyIfForceDirectAccessIsTrue() {
83 $property = \TYPO3\CMS\Extbase\Reflection\ObjectAccess::getProperty($this->dummyObject, 'notExistingProperty', TRUE);
84 }
85
86 /**
87 * @test
88 * @expectedException \TYPO3\CMS\Extbase\Reflection\Exception\PropertyNotAccessibleException
89 */
90 public function getPropertyReturnsThrowsExceptionIfPropertyDoesNotExist() {
91 \TYPO3\CMS\Extbase\Reflection\ObjectAccess::getProperty($this->dummyObject, 'notExistingProperty');
92 }
93
94 /**
95 * @test
96 * @expectedException \TYPO3\CMS\Extbase\Reflection\Exception\PropertyNotAccessibleException
97 */
98 public function getPropertyReturnsThrowsExceptionIfArrayKeyDoesNotExist() {
99 \TYPO3\CMS\Extbase\Reflection\ObjectAccess::getProperty(array(), 'notExistingProperty');
100 }
101
102 /**
103 * @test
104 */
105 public function getPropertyTriesToCallABooleanGetterMethodIfItExists() {
106 $property = \TYPO3\CMS\Extbase\Reflection\ObjectAccess::getProperty($this->dummyObject, 'booleanProperty');
107 $this->assertTrue($property);
108 }
109
110 /**
111 * @test
112 * @expectedException \InvalidArgumentException
113 */
114 public function getPropertyThrowsExceptionIfThePropertyNameIsNotAString() {
115 \TYPO3\CMS\Extbase\Reflection\ObjectAccess::getProperty($this->dummyObject, new \ArrayObject());
116 }
117
118 /**
119 * @test
120 * @expectedException \InvalidArgumentException
121 */
122 public function setPropertyThrowsExceptionIfThePropertyNameIsNotAString() {
123 \TYPO3\CMS\Extbase\Reflection\ObjectAccess::setProperty($this->dummyObject, new \ArrayObject(), 42);
124 }
125
126 /**
127 * @test
128 */
129 public function setPropertyReturnsFalseIfPropertyIsNotAccessible() {
130 $this->assertFalse(\TYPO3\CMS\Extbase\Reflection\ObjectAccess::setProperty($this->dummyObject, 'protectedProperty', 42));
131 }
132
133 /**
134 * @test
135 */
136 public function setPropertySetsValueIfPropertyIsNotAccessibleWhenForceDirectAccessIsTrue() {
137 $this->assertTrue(\TYPO3\CMS\Extbase\Reflection\ObjectAccess::setProperty($this->dummyObject, 'unexposedProperty', 'was set anyway', TRUE));
138 $this->assertAttributeEquals('was set anyway', 'unexposedProperty', $this->dummyObject);
139 }
140
141 /**
142 * @test
143 */
144 public function setPropertySetsValueIfPropertyDoesNotExistWhenForceDirectAccessIsTrue() {
145 $this->assertTrue(\TYPO3\CMS\Extbase\Reflection\ObjectAccess::setProperty($this->dummyObject, 'unknownProperty', 'was set anyway', TRUE));
146 $this->assertAttributeEquals('was set anyway', 'unknownProperty', $this->dummyObject);
147 }
148
149 /**
150 * @test
151 */
152 public function setPropertyCallsASetterMethodToSetThePropertyValueIfOneIsAvailable() {
153 \TYPO3\CMS\Extbase\Reflection\ObjectAccess::setProperty($this->dummyObject, 'property', 4242);
154 $this->assertEquals($this->dummyObject->getProperty(), 4242, 'setProperty does not work with setter.');
155 }
156
157 /**
158 * @test
159 */
160 public function setPropertyWorksWithPublicProperty() {
161 \TYPO3\CMS\Extbase\Reflection\ObjectAccess::setProperty($this->dummyObject, 'publicProperty', 4242);
162 $this->assertEquals($this->dummyObject->publicProperty, 4242, 'setProperty does not work with public property.');
163 }
164
165 /**
166 * @test
167 */
168 public function setPropertyCanDirectlySetValuesInAnArrayObjectOrArray() {
169 $arrayObject = new \ArrayObject();
170 $array = array();
171 \TYPO3\CMS\Extbase\Reflection\ObjectAccess::setProperty($arrayObject, 'publicProperty', 4242);
172 \TYPO3\CMS\Extbase\Reflection\ObjectAccess::setProperty($array, 'key', 'value');
173 $this->assertEquals(4242, $arrayObject['publicProperty']);
174 $this->assertEquals('value', $array['key']);
175 }
176
177 /**
178 * @test
179 */
180 public function getPropertyCanAccessPropertiesOfAnArrayObject() {
181 $arrayObject = new \ArrayObject(array('key' => 'value'));
182 $expected = \TYPO3\CMS\Extbase\Reflection\ObjectAccess::getProperty($arrayObject, 'key');
183 $this->assertEquals($expected, 'value', 'getProperty does not work with ArrayObject property.');
184 }
185
186 /**
187 * @test
188 */
189 public function getPropertyCanAccessPropertiesOfAnObjectImplementingArrayAccess() {
190 $arrayAccessInstance = new \TYPO3\CMS\Extbase\Tests\Unit\Reflection\Fixture\ArrayAccessClass(array('key' => 'value'));
191 $expected = \TYPO3\CMS\Extbase\Reflection\ObjectAccess::getProperty($arrayAccessInstance, 'key');
192 $this->assertEquals($expected, 'value', 'getPropertyPath does not work with Array Access property.');
193 }
194
195 /**
196 * @test
197 */
198 public function getPropertyCanAccessPropertiesOfAnArray() {
199 $array = array('key' => 'value');
200 $expected = \TYPO3\CMS\Extbase\Reflection\ObjectAccess::getProperty($array, 'key');
201 $this->assertEquals($expected, 'value', 'getProperty does not work with Array property.');
202 }
203
204 /**
205 * @test
206 */
207 public function getPropertyPathCanAccessPropertiesOfAnArray() {
208 $array = array('parent' => array('key' => 'value'));
209 $expected = \TYPO3\CMS\Extbase\Reflection\ObjectAccess::getPropertyPath($array, 'parent.key');
210 $this->assertEquals($expected, 'value', 'getPropertyPath does not work with Array property.');
211 }
212
213 /**
214 * @test
215 */
216 public function getPropertyPathCanAccessPropertiesOfAnObjectImplementingArrayAccess() {
217 $array = array('parent' => new \ArrayObject(array('key' => 'value')));
218 $expected = \TYPO3\CMS\Extbase\Reflection\ObjectAccess::getPropertyPath($array, 'parent.key');
219 $this->assertEquals($expected, 'value', 'getPropertyPath does not work with Array Access property.');
220 }
221
222 /**
223 * @test
224 */
225 public function getGettablePropertyNamesReturnsAllPropertiesWhichAreAvailable() {
226 $gettablePropertyNames = \TYPO3\CMS\Extbase\Reflection\ObjectAccess::getGettablePropertyNames($this->dummyObject);
227 $expectedPropertyNames = array('anotherProperty', 'booleanProperty', 'property', 'property2', 'publicProperty', 'publicProperty2');
228 $this->assertEquals($gettablePropertyNames, $expectedPropertyNames, 'getGettablePropertyNames returns not all gettable properties.');
229 }
230
231 /**
232 * @test
233 */
234 public function getSettablePropertyNamesReturnsAllPropertiesWhichAreAvailable() {
235 $settablePropertyNames = \TYPO3\CMS\Extbase\Reflection\ObjectAccess::getSettablePropertyNames($this->dummyObject);
236 $expectedPropertyNames = array('anotherProperty', 'property', 'property2', 'publicProperty', 'publicProperty2', 'writeOnlyMagicProperty');
237 $this->assertEquals($settablePropertyNames, $expectedPropertyNames, 'getSettablePropertyNames returns not all settable properties.');
238 }
239
240 /**
241 * @test
242 */
243 public function getSettablePropertyNamesReturnsPropertyNamesOfStdClass() {
244 $stdClassObject = new \stdClass();
245 $stdClassObject->property = 'string1';
246 $stdClassObject->property2 = NULL;
247 $settablePropertyNames = \TYPO3\CMS\Extbase\Reflection\ObjectAccess::getSettablePropertyNames($stdClassObject);
248 $expectedPropertyNames = array('property', 'property2');
249 $this->assertEquals($expectedPropertyNames, $settablePropertyNames, 'getSettablePropertyNames returns not all settable properties.');
250 }
251
252 /**
253 * @test
254 */
255 public function getGettablePropertiesReturnsTheCorrectValuesForAllProperties() {
256 $allProperties = \TYPO3\CMS\Extbase\Reflection\ObjectAccess::getGettableProperties($this->dummyObject);
257 $expectedProperties = array(
258 'anotherProperty' => 42,
259 'booleanProperty' => TRUE,
260 'property' => 'string1',
261 'property2' => NULL,
262 'publicProperty' => NULL,
263 'publicProperty2' => 42
264 );
265 $this->assertEquals($allProperties, $expectedProperties, 'expectedProperties did not return the right values for the properties.');
266 }
267
268 /**
269 * @test
270 */
271 public function getGettablePropertiesReturnsPropertiesOfStdClass() {
272 $stdClassObject = new \stdClass();
273 $stdClassObject->property = 'string1';
274 $stdClassObject->property2 = NULL;
275 $stdClassObject->publicProperty2 = 42;
276 $allProperties = \TYPO3\CMS\Extbase\Reflection\ObjectAccess::getGettableProperties($stdClassObject);
277 $expectedProperties = array(
278 'property' => 'string1',
279 'property2' => NULL,
280 'publicProperty2' => 42
281 );
282 $this->assertEquals($expectedProperties, $allProperties, 'expectedProperties did not return the right values for the properties.');
283 }
284
285 /**
286 * @test
287 */
288 public function isPropertySettableTellsIfAPropertyCanBeSet() {
289 $this->assertTrue(\TYPO3\CMS\Extbase\Reflection\ObjectAccess::isPropertySettable($this->dummyObject, 'writeOnlyMagicProperty'));
290 $this->assertTrue(\TYPO3\CMS\Extbase\Reflection\ObjectAccess::isPropertySettable($this->dummyObject, 'publicProperty'));
291 $this->assertTrue(\TYPO3\CMS\Extbase\Reflection\ObjectAccess::isPropertySettable($this->dummyObject, 'property'));
292 $this->assertFalse(\TYPO3\CMS\Extbase\Reflection\ObjectAccess::isPropertySettable($this->dummyObject, 'privateProperty'));
293 $this->assertFalse(\TYPO3\CMS\Extbase\Reflection\ObjectAccess::isPropertySettable($this->dummyObject, 'shouldNotBePickedUp'));
294 }
295
296 /**
297 * @test
298 */
299 public function isPropertySettableWorksOnStdClass() {
300 $stdClassObject = new \stdClass();
301 $stdClassObject->property = 'foo';
302 $this->assertTrue(\TYPO3\CMS\Extbase\Reflection\ObjectAccess::isPropertySettable($stdClassObject, 'property'));
303 $this->assertFalse(\TYPO3\CMS\Extbase\Reflection\ObjectAccess::isPropertySettable($stdClassObject, 'undefinedProperty'));
304 }
305
306 /**
307 * @test
308 */
309 public function isPropertyGettableTellsIfAPropertyCanBeRetrieved() {
310 $this->assertTrue(\TYPO3\CMS\Extbase\Reflection\ObjectAccess::isPropertyGettable($this->dummyObject, 'publicProperty'));
311 $this->assertTrue(\TYPO3\CMS\Extbase\Reflection\ObjectAccess::isPropertyGettable($this->dummyObject, 'property'));
312 $this->assertTrue(\TYPO3\CMS\Extbase\Reflection\ObjectAccess::isPropertyGettable($this->dummyObject, 'booleanProperty'));
313 $this->assertFalse(\TYPO3\CMS\Extbase\Reflection\ObjectAccess::isPropertyGettable($this->dummyObject, 'privateProperty'));
314 $this->assertFalse(\TYPO3\CMS\Extbase\Reflection\ObjectAccess::isPropertyGettable($this->dummyObject, 'writeOnlyMagicProperty'));
315 $this->assertFalse(\TYPO3\CMS\Extbase\Reflection\ObjectAccess::isPropertyGettable($this->dummyObject, 'shouldNotBePickedUp'));
316 }
317
318 /**
319 * @test
320 */
321 public function isPropertyGettableWorksOnArrayAccessObjects() {
322 $arrayObject = new \ArrayObject();
323 $arrayObject['key'] = 'v';
324 $this->assertTrue(\TYPO3\CMS\Extbase\Reflection\ObjectAccess::isPropertyGettable($arrayObject, 'key'));
325 $this->assertFalse(\TYPO3\CMS\Extbase\Reflection\ObjectAccess::isPropertyGettable($arrayObject, 'undefinedKey'));
326 }
327
328 /**
329 * @test
330 */
331 public function isPropertyGettableWorksOnStdClass() {
332 $stdClassObject = new \stdClass();
333 $stdClassObject->property = 'foo';
334 $this->assertTrue(\TYPO3\CMS\Extbase\Reflection\ObjectAccess::isPropertyGettable($stdClassObject, 'property'));
335 $this->assertFalse(\TYPO3\CMS\Extbase\Reflection\ObjectAccess::isPropertyGettable($stdClassObject, 'undefinedProperty'));
336 }
337
338 /**
339 * @test
340 */
341 public function getPropertyPathCanRecursivelyGetPropertiesOfAnObject() {
342 $alternativeObject = new \TYPO3\CMS\Extbase\Tests\Unit\Reflection\Fixture\DummyClassWithGettersAndSetters();
343 $alternativeObject->setProperty('test');
344 $this->dummyObject->setProperty2($alternativeObject);
345 $expected = 'test';
346 $actual = \TYPO3\CMS\Extbase\Reflection\ObjectAccess::getPropertyPath($this->dummyObject, 'property2.property');
347 $this->assertEquals($expected, $actual);
348 }
349
350 /**
351 * @test
352 */
353 public function getPropertyPathReturnsNullForNonExistingPropertyPath() {
354 $alternativeObject = new \TYPO3\CMS\Extbase\Tests\Unit\Reflection\Fixture\DummyClassWithGettersAndSetters();
355 $alternativeObject->setProperty(new \stdClass());
356 $this->dummyObject->setProperty2($alternativeObject);
357 $this->assertNull(\TYPO3\CMS\Extbase\Reflection\ObjectAccess::getPropertyPath($this->dummyObject, 'property2.property.not.existing'));
358 }
359
360 /**
361 * @test
362 */
363 public function getPropertyPathReturnsNullIfSubjectIsNoObject() {
364 $string = 'Hello world';
365 $this->assertNull(\TYPO3\CMS\Extbase\Reflection\ObjectAccess::getPropertyPath($string, 'property2'));
366 }
367
368 /**
369 * @test
370 */
371 public function getPropertyPathReturnsNullIfSubjectOnPathIsNoObject() {
372 $object = new \stdClass();
373 $object->foo = 'Hello World';
374 $this->assertNull(\TYPO3\CMS\Extbase\Reflection\ObjectAccess::getPropertyPath($object, 'foo.bar'));
375 }
376 }
377
378 ?>