[TASK] Re-work/simplify copyright header in PHP files - Part 8
[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 * 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 ObjectAccessTest extends \TYPO3\CMS\Core\Tests\UnitTestCase {
21
22 protected $dummyObject;
23
24 public function setUp() {
25 $this->dummyObject = new \TYPO3\CMS\Extbase\Tests\Unit\Reflection\Fixture\DummyClassWithGettersAndSetters();
26 $this->dummyObject->setProperty('string1');
27 $this->dummyObject->setAnotherProperty(42);
28 $this->dummyObject->shouldNotBePickedUp = TRUE;
29 }
30
31 /**
32 * @test
33 */
34 public function getPropertyReturnsExpectedValueForGetterProperty() {
35 $property = \TYPO3\CMS\Extbase\Reflection\ObjectAccess::getProperty($this->dummyObject, 'property');
36 $this->assertEquals($property, 'string1');
37 }
38
39 /**
40 * @test
41 */
42 public function getPropertyReturnsExpectedValueForPublicProperty() {
43 $property = \TYPO3\CMS\Extbase\Reflection\ObjectAccess::getProperty($this->dummyObject, 'publicProperty2');
44 $this->assertEquals($property, 42, 'A property of a given object was not returned correctly.');
45 }
46
47 /**
48 * @test
49 */
50 public function getPropertyReturnsExpectedValueForUnexposedPropertyIfForceDirectAccessIsTrue() {
51 $property = \TYPO3\CMS\Extbase\Reflection\ObjectAccess::getProperty($this->dummyObject, 'unexposedProperty', TRUE);
52 $this->assertEquals($property, 'unexposed', 'A property of a given object was not returned correctly.');
53 }
54
55 /**
56 * @test
57 */
58 public function getPropertyReturnsExpectedValueForUnknownPropertyIfForceDirectAccessIsTrue() {
59 $this->dummyObject->unknownProperty = 'unknown';
60 $property = \TYPO3\CMS\Extbase\Reflection\ObjectAccess::getProperty($this->dummyObject, 'unknownProperty', TRUE);
61 $this->assertEquals($property, 'unknown', 'A property of a given object was not returned correctly.');
62 }
63
64 /**
65 * @test
66 * @expectedException \TYPO3\CMS\Extbase\Reflection\Exception\PropertyNotAccessibleException
67 */
68 public function getPropertyReturnsPropertyNotAccessibleExceptionForNotExistingPropertyIfForceDirectAccessIsTrue() {
69 $property = \TYPO3\CMS\Extbase\Reflection\ObjectAccess::getProperty($this->dummyObject, 'notExistingProperty', TRUE);
70 }
71
72 /**
73 * @test
74 * @expectedException \TYPO3\CMS\Extbase\Reflection\Exception\PropertyNotAccessibleException
75 */
76 public function getPropertyReturnsThrowsExceptionIfPropertyDoesNotExist() {
77 \TYPO3\CMS\Extbase\Reflection\ObjectAccess::getProperty($this->dummyObject, 'notExistingProperty');
78 }
79
80 /**
81 * @test
82 * @expectedException \TYPO3\CMS\Extbase\Reflection\Exception\PropertyNotAccessibleException
83 */
84 public function getPropertyReturnsThrowsExceptionIfArrayKeyDoesNotExist() {
85 \TYPO3\CMS\Extbase\Reflection\ObjectAccess::getProperty(array(), 'notExistingProperty');
86 }
87
88 /**
89 * @test
90 */
91 public function getPropertyTriesToCallABooleanGetterMethodIfItExists() {
92 $property = \TYPO3\CMS\Extbase\Reflection\ObjectAccess::getProperty($this->dummyObject, 'booleanProperty');
93 $this->assertTrue($property);
94 }
95
96 /**
97 * @test
98 * @expectedException \InvalidArgumentException
99 */
100 public function getPropertyThrowsExceptionIfThePropertyNameIsNotAString() {
101 \TYPO3\CMS\Extbase\Reflection\ObjectAccess::getProperty($this->dummyObject, new \ArrayObject());
102 }
103
104 /**
105 * @test
106 * @expectedException \InvalidArgumentException
107 */
108 public function setPropertyThrowsExceptionIfThePropertyNameIsNotAString() {
109 \TYPO3\CMS\Extbase\Reflection\ObjectAccess::setProperty($this->dummyObject, new \ArrayObject(), 42);
110 }
111
112 /**
113 * @test
114 */
115 public function setPropertyReturnsFalseIfPropertyIsNotAccessible() {
116 $this->assertFalse(\TYPO3\CMS\Extbase\Reflection\ObjectAccess::setProperty($this->dummyObject, 'protectedProperty', 42));
117 }
118
119 /**
120 * @test
121 */
122 public function setPropertySetsValueIfPropertyIsNotAccessibleWhenForceDirectAccessIsTrue() {
123 $this->assertTrue(\TYPO3\CMS\Extbase\Reflection\ObjectAccess::setProperty($this->dummyObject, 'unexposedProperty', 'was set anyway', TRUE));
124 $this->assertAttributeEquals('was set anyway', 'unexposedProperty', $this->dummyObject);
125 }
126
127 /**
128 * @test
129 */
130 public function setPropertySetsValueIfPropertyDoesNotExistWhenForceDirectAccessIsTrue() {
131 $this->assertTrue(\TYPO3\CMS\Extbase\Reflection\ObjectAccess::setProperty($this->dummyObject, 'unknownProperty', 'was set anyway', TRUE));
132 $this->assertAttributeEquals('was set anyway', 'unknownProperty', $this->dummyObject);
133 }
134
135 /**
136 * @test
137 */
138 public function setPropertyCallsASetterMethodToSetThePropertyValueIfOneIsAvailable() {
139 \TYPO3\CMS\Extbase\Reflection\ObjectAccess::setProperty($this->dummyObject, 'property', 4242);
140 $this->assertEquals($this->dummyObject->getProperty(), 4242, 'setProperty does not work with setter.');
141 }
142
143 /**
144 * @test
145 */
146 public function setPropertyWorksWithPublicProperty() {
147 \TYPO3\CMS\Extbase\Reflection\ObjectAccess::setProperty($this->dummyObject, 'publicProperty', 4242);
148 $this->assertEquals($this->dummyObject->publicProperty, 4242, 'setProperty does not work with public property.');
149 }
150
151 /**
152 * @test
153 */
154 public function setPropertyCanDirectlySetValuesInAnArrayObjectOrArray() {
155 $arrayObject = new \ArrayObject();
156 $array = array();
157 \TYPO3\CMS\Extbase\Reflection\ObjectAccess::setProperty($arrayObject, 'publicProperty', 4242);
158 \TYPO3\CMS\Extbase\Reflection\ObjectAccess::setProperty($array, 'key', 'value');
159 $this->assertEquals(4242, $arrayObject['publicProperty']);
160 $this->assertEquals('value', $array['key']);
161 }
162
163 /**
164 * @test
165 */
166 public function getPropertyCanAccessPropertiesOfAnArrayObject() {
167 $arrayObject = new \ArrayObject(array('key' => 'value'));
168 $actual = \TYPO3\CMS\Extbase\Reflection\ObjectAccess::getProperty($arrayObject, 'key');
169 $this->assertEquals('value', $actual, 'getProperty does not work with ArrayObject property.');
170 }
171
172 /**
173 * @test
174 */
175 public function getPropertyCanAccessPropertiesOfAnObjectImplementingArrayAccess() {
176 $arrayAccessInstance = new \TYPO3\CMS\Extbase\Tests\Unit\Reflection\Fixture\ArrayAccessClass(array('key' => 'value'));
177 $actual = \TYPO3\CMS\Extbase\Reflection\ObjectAccess::getProperty($arrayAccessInstance, 'key');
178 $this->assertEquals('value', $actual, 'getProperty does not work with Array Access property.');
179 }
180
181 /**
182 * @test
183 */
184 public function getPropertyCanAccessPropertiesOfAnArray() {
185 $array = array('key' => 'value');
186 $expected = \TYPO3\CMS\Extbase\Reflection\ObjectAccess::getProperty($array, 'key');
187 $this->assertEquals($expected, 'value', 'getProperty does not work with Array property.');
188 }
189
190 /**
191 * @test
192 */
193 public function getPropertyPathCanAccessPropertiesOfAnArray() {
194 $array = array('parent' => array('key' => 'value'));
195 $actual = \TYPO3\CMS\Extbase\Reflection\ObjectAccess::getPropertyPath($array, 'parent.key');
196 $this->assertEquals('value', $actual, 'getPropertyPath does not work with Array property.');
197 }
198
199 /**
200 * @test
201 */
202 public function getPropertyPathCanAccessPropertiesOfAnObjectImplementingArrayAccess() {
203 $array = array('parent' => new \ArrayObject(array('key' => 'value')));
204 $actual = \TYPO3\CMS\Extbase\Reflection\ObjectAccess::getPropertyPath($array, 'parent.key');
205 $this->assertEquals('value', $actual, 'getPropertyPath does not work with Array Access property.');
206 }
207
208 /**
209 * @test
210 */
211 public function getPropertyPathCanAccessPropertiesOfAnExtbaseObjectStorageObject() {
212 $objectStorage = new \TYPO3\CMS\Extbase\Persistence\ObjectStorage();
213 $exampleObject = new \stdClass();
214 $exampleObject->key = 'value';
215 $exampleObject2 = new \stdClass();
216 $exampleObject2->key = 'value2';
217 $objectStorage->attach($exampleObject);
218 $objectStorage->attach($exampleObject2);
219 $array = array(
220 'parent' => $objectStorage,
221 );
222 $this->assertSame('value', \TYPO3\CMS\Extbase\Reflection\ObjectAccess::getPropertyPath($array, 'parent.0.key'));
223 $this->assertSame('value2', \TYPO3\CMS\Extbase\Reflection\ObjectAccess::getPropertyPath($array, 'parent.1.key'));
224 }
225
226 /**
227 * @test
228 */
229 public function getPropertyPathCanAccessPropertiesOfAnSplObjectStorageObject() {
230 $objectStorage = new \SplObjectStorage();
231 $exampleObject = new \stdClass();
232 $exampleObject->key = 'value';
233 $exampleObject2 = new \stdClass();
234 $exampleObject2->key = 'value2';
235 $objectStorage->attach($exampleObject);
236 $objectStorage->attach($exampleObject2);
237 $array = array(
238 'parent' => $objectStorage,
239 );
240 $this->assertSame('value', \TYPO3\CMS\Extbase\Reflection\ObjectAccess::getPropertyPath($array, 'parent.0.key'));
241 $this->assertSame('value2', \TYPO3\CMS\Extbase\Reflection\ObjectAccess::getPropertyPath($array, 'parent.1.key'));
242 }
243
244 /**
245 * @test
246 */
247 public function getGettablePropertyNamesReturnsAllPropertiesWhichAreAvailable() {
248 $gettablePropertyNames = \TYPO3\CMS\Extbase\Reflection\ObjectAccess::getGettablePropertyNames($this->dummyObject);
249 $expectedPropertyNames = array('anotherProperty', 'booleanProperty', 'property', 'property2', 'publicProperty', 'publicProperty2');
250 $this->assertEquals($gettablePropertyNames, $expectedPropertyNames, 'getGettablePropertyNames returns not all gettable properties.');
251 }
252
253 /**
254 * @test
255 */
256 public function getSettablePropertyNamesReturnsAllPropertiesWhichAreAvailable() {
257 $settablePropertyNames = \TYPO3\CMS\Extbase\Reflection\ObjectAccess::getSettablePropertyNames($this->dummyObject);
258 $expectedPropertyNames = array('anotherProperty', 'property', 'property2', 'publicProperty', 'publicProperty2', 'writeOnlyMagicProperty');
259 $this->assertEquals($settablePropertyNames, $expectedPropertyNames, 'getSettablePropertyNames returns not all settable properties.');
260 }
261
262 /**
263 * @test
264 */
265 public function getSettablePropertyNamesReturnsPropertyNamesOfStdClass() {
266 $stdClassObject = new \stdClass();
267 $stdClassObject->property = 'string1';
268 $stdClassObject->property2 = NULL;
269 $settablePropertyNames = \TYPO3\CMS\Extbase\Reflection\ObjectAccess::getSettablePropertyNames($stdClassObject);
270 $expectedPropertyNames = array('property', 'property2');
271 $this->assertEquals($expectedPropertyNames, $settablePropertyNames, 'getSettablePropertyNames returns not all settable properties.');
272 }
273
274 /**
275 * @test
276 */
277 public function getGettablePropertiesReturnsTheCorrectValuesForAllProperties() {
278 $allProperties = \TYPO3\CMS\Extbase\Reflection\ObjectAccess::getGettableProperties($this->dummyObject);
279 $expectedProperties = array(
280 'anotherProperty' => 42,
281 'booleanProperty' => TRUE,
282 'property' => 'string1',
283 'property2' => NULL,
284 'publicProperty' => NULL,
285 'publicProperty2' => 42
286 );
287 $this->assertEquals($allProperties, $expectedProperties, 'expectedProperties did not return the right values for the properties.');
288 }
289
290 /**
291 * @test
292 */
293 public function getGettablePropertiesReturnsPropertiesOfStdClass() {
294 $stdClassObject = new \stdClass();
295 $stdClassObject->property = 'string1';
296 $stdClassObject->property2 = NULL;
297 $stdClassObject->publicProperty2 = 42;
298 $allProperties = \TYPO3\CMS\Extbase\Reflection\ObjectAccess::getGettableProperties($stdClassObject);
299 $expectedProperties = array(
300 'property' => 'string1',
301 'property2' => NULL,
302 'publicProperty2' => 42
303 );
304 $this->assertEquals($expectedProperties, $allProperties, 'expectedProperties did not return the right values for the properties.');
305 }
306
307 /**
308 * @test
309 */
310 public function isPropertySettableTellsIfAPropertyCanBeSet() {
311 $this->assertTrue(\TYPO3\CMS\Extbase\Reflection\ObjectAccess::isPropertySettable($this->dummyObject, 'writeOnlyMagicProperty'));
312 $this->assertTrue(\TYPO3\CMS\Extbase\Reflection\ObjectAccess::isPropertySettable($this->dummyObject, 'publicProperty'));
313 $this->assertTrue(\TYPO3\CMS\Extbase\Reflection\ObjectAccess::isPropertySettable($this->dummyObject, 'property'));
314 $this->assertFalse(\TYPO3\CMS\Extbase\Reflection\ObjectAccess::isPropertySettable($this->dummyObject, 'privateProperty'));
315 $this->assertFalse(\TYPO3\CMS\Extbase\Reflection\ObjectAccess::isPropertySettable($this->dummyObject, 'shouldNotBePickedUp'));
316 }
317
318 /**
319 * @test
320 */
321 public function isPropertySettableWorksOnStdClass() {
322 $stdClassObject = new \stdClass();
323 $stdClassObject->property = 'foo';
324 $this->assertTrue(\TYPO3\CMS\Extbase\Reflection\ObjectAccess::isPropertySettable($stdClassObject, 'property'));
325 $this->assertFalse(\TYPO3\CMS\Extbase\Reflection\ObjectAccess::isPropertySettable($stdClassObject, 'undefinedProperty'));
326 }
327
328 /**
329 * @test
330 */
331 public function isPropertyGettableTellsIfAPropertyCanBeRetrieved() {
332 $this->assertTrue(\TYPO3\CMS\Extbase\Reflection\ObjectAccess::isPropertyGettable($this->dummyObject, 'publicProperty'));
333 $this->assertTrue(\TYPO3\CMS\Extbase\Reflection\ObjectAccess::isPropertyGettable($this->dummyObject, 'property'));
334 $this->assertTrue(\TYPO3\CMS\Extbase\Reflection\ObjectAccess::isPropertyGettable($this->dummyObject, 'booleanProperty'));
335 $this->assertFalse(\TYPO3\CMS\Extbase\Reflection\ObjectAccess::isPropertyGettable($this->dummyObject, 'privateProperty'));
336 $this->assertFalse(\TYPO3\CMS\Extbase\Reflection\ObjectAccess::isPropertyGettable($this->dummyObject, 'writeOnlyMagicProperty'));
337 $this->assertFalse(\TYPO3\CMS\Extbase\Reflection\ObjectAccess::isPropertyGettable($this->dummyObject, 'shouldNotBePickedUp'));
338 }
339
340 /**
341 * @test
342 */
343 public function isPropertyGettableWorksOnArrayAccessObjects() {
344 $arrayObject = new \ArrayObject();
345 $arrayObject['key'] = 'v';
346 $this->assertTrue(\TYPO3\CMS\Extbase\Reflection\ObjectAccess::isPropertyGettable($arrayObject, 'key'));
347 $this->assertFalse(\TYPO3\CMS\Extbase\Reflection\ObjectAccess::isPropertyGettable($arrayObject, 'undefinedKey'));
348 }
349
350 /**
351 * @test
352 */
353 public function isPropertyGettableWorksOnStdClass() {
354 $stdClassObject = new \stdClass();
355 $stdClassObject->property = 'foo';
356 $this->assertTrue(\TYPO3\CMS\Extbase\Reflection\ObjectAccess::isPropertyGettable($stdClassObject, 'property'));
357 $this->assertFalse(\TYPO3\CMS\Extbase\Reflection\ObjectAccess::isPropertyGettable($stdClassObject, 'undefinedProperty'));
358 }
359
360 /**
361 * @test
362 */
363 public function getPropertyPathCanRecursivelyGetPropertiesOfAnObject() {
364 $alternativeObject = new \TYPO3\CMS\Extbase\Tests\Unit\Reflection\Fixture\DummyClassWithGettersAndSetters();
365 $alternativeObject->setProperty('test');
366 $this->dummyObject->setProperty2($alternativeObject);
367 $expected = 'test';
368 $actual = \TYPO3\CMS\Extbase\Reflection\ObjectAccess::getPropertyPath($this->dummyObject, 'property2.property');
369 $this->assertEquals($expected, $actual);
370 }
371
372 /**
373 * @test
374 */
375 public function getPropertyPathReturnsNullForNonExistingPropertyPath() {
376 $alternativeObject = new \TYPO3\CMS\Extbase\Tests\Unit\Reflection\Fixture\DummyClassWithGettersAndSetters();
377 $alternativeObject->setProperty(new \stdClass());
378 $this->dummyObject->setProperty2($alternativeObject);
379 $this->assertNull(\TYPO3\CMS\Extbase\Reflection\ObjectAccess::getPropertyPath($this->dummyObject, 'property2.property.not.existing'));
380 }
381
382 /**
383 * @test
384 */
385 public function getPropertyPathReturnsNullIfSubjectIsNoObject() {
386 $string = 'Hello world';
387 $this->assertNull(\TYPO3\CMS\Extbase\Reflection\ObjectAccess::getPropertyPath($string, 'property2'));
388 }
389
390 /**
391 * @test
392 */
393 public function getPropertyPathReturnsNullIfSubjectOnPathIsNoObject() {
394 $object = new \stdClass();
395 $object->foo = 'Hello World';
396 $this->assertNull(\TYPO3\CMS\Extbase\Reflection\ObjectAccess::getPropertyPath($object, 'foo.bar'));
397 }
398 }