TYPO3 CMS  TYPO3_7-6
ObjectAccessTest.php
Go to the documentation of this file.
1 <?php
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 
21 {
22  protected $dummyObject;
23 
24  protected function setUp()
25  {
26  $this->dummyObject = new \TYPO3\CMS\Extbase\Tests\Unit\Reflection\Fixture\DummyClassWithGettersAndSetters();
27  $this->dummyObject->setProperty('string1');
28  $this->dummyObject->setAnotherProperty(42);
29  $this->dummyObject->shouldNotBePickedUp = true;
30  }
31 
36  {
37  $property = \TYPO3\CMS\Extbase\Reflection\ObjectAccess::getProperty($this->dummyObject, 'property');
38  $this->assertEquals($property, 'string1');
39  }
40 
45  {
46  $property = \TYPO3\CMS\Extbase\Reflection\ObjectAccess::getProperty($this->dummyObject, 'publicProperty2');
47  $this->assertEquals($property, 42, 'A property of a given object was not returned correctly.');
48  }
49 
54  {
55  $property = \TYPO3\CMS\Extbase\Reflection\ObjectAccess::getProperty($this->dummyObject, 'unexposedProperty', true);
56  $this->assertEquals($property, 'unexposed', 'A property of a given object was not returned correctly.');
57  }
58 
63  {
64  $this->dummyObject->unknownProperty = 'unknown';
65  $property = \TYPO3\CMS\Extbase\Reflection\ObjectAccess::getProperty($this->dummyObject, 'unknownProperty', true);
66  $this->assertEquals($property, 'unknown', 'A property of a given object was not returned correctly.');
67  }
68 
74  {
75  $property = \TYPO3\CMS\Extbase\Reflection\ObjectAccess::getProperty($this->dummyObject, 'notExistingProperty', true);
76  }
77 
83  {
84  \TYPO3\CMS\Extbase\Reflection\ObjectAccess::getProperty($this->dummyObject, 'notExistingProperty');
85  }
86 
92  {
94  }
95 
100  {
101  $property = \TYPO3\CMS\Extbase\Reflection\ObjectAccess::getProperty($this->dummyObject, 'booleanProperty');
102  $this->assertTrue($property);
103  }
104 
110  {
111  \TYPO3\CMS\Extbase\Reflection\ObjectAccess::getProperty($this->dummyObject, new \ArrayObject());
112  }
113 
119  {
120  \TYPO3\CMS\Extbase\Reflection\ObjectAccess::setProperty($this->dummyObject, new \ArrayObject(), 42);
121  }
122 
127  {
128  $this->assertFalse(\TYPO3\CMS\Extbase\Reflection\ObjectAccess::setProperty($this->dummyObject, 'protectedProperty', 42));
129  }
130 
135  {
136  $this->assertTrue(\TYPO3\CMS\Extbase\Reflection\ObjectAccess::setProperty($this->dummyObject, 'unexposedProperty', 'was set anyway', true));
137  $this->assertAttributeEquals('was set anyway', 'unexposedProperty', $this->dummyObject);
138  }
139 
144  {
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 
153  {
154  \TYPO3\CMS\Extbase\Reflection\ObjectAccess::setProperty($this->dummyObject, 'property', 4242);
155  $this->assertEquals($this->dummyObject->getProperty(), 4242, 'setProperty does not work with setter.');
156  }
157 
162  {
163  \TYPO3\CMS\Extbase\Reflection\ObjectAccess::setProperty($this->dummyObject, 'publicProperty', 4242);
164  $this->assertEquals($this->dummyObject->publicProperty, 4242, 'setProperty does not work with public property.');
165  }
166 
171  {
172  $arrayObject = new \ArrayObject();
173  $array = [];
174  \TYPO3\CMS\Extbase\Reflection\ObjectAccess::setProperty($arrayObject, 'publicProperty', 4242);
176  $this->assertEquals(4242, $arrayObject['publicProperty']);
177  $this->assertEquals('value', $array['key']);
178  }
179 
184  {
185  $arrayObject = new \ArrayObject(['key' => 'value']);
186  $actual = \TYPO3\CMS\Extbase\Reflection\ObjectAccess::getProperty($arrayObject, 'key');
187  $this->assertEquals('value', $actual, 'getProperty does not work with ArrayObject property.');
188  }
189 
194  {
195  $objectStorage = new \TYPO3\CMS\Extbase\Persistence\ObjectStorage();
196  $objectStorage->key = 'value';
197  $actual = \TYPO3\CMS\Extbase\Reflection\ObjectAccess::getProperty($objectStorage, 'key');
198  $this->assertEquals('value', $actual, 'getProperty does not work with ObjectStorage property.');
199  }
200 
205  {
206  $arrayAccessInstance = new \TYPO3\CMS\Extbase\Tests\Unit\Reflection\Fixture\ArrayAccessClass(['key' => 'value']);
207  $actual = \TYPO3\CMS\Extbase\Reflection\ObjectAccess::getProperty($arrayAccessInstance, 'key');
208  $this->assertEquals('value', $actual, 'getProperty does not work with Array Access property.');
209  }
210 
215  {
216  $array = ['key' => 'value'];
218  $this->assertEquals($expected, 'value', 'getProperty does not work with Array property.');
219  }
220 
225  {
226  $array = ['parent' => ['key' => 'value']];
227  $actual = \TYPO3\CMS\Extbase\Reflection\ObjectAccess::getPropertyPath($array, 'parent.key');
228  $this->assertEquals('value', $actual, 'getPropertyPath does not work with Array property.');
229  }
230 
235  {
236  $array = ['parent' => new \ArrayObject(['key' => 'value'])];
237  $actual = \TYPO3\CMS\Extbase\Reflection\ObjectAccess::getPropertyPath($array, 'parent.key');
238  $this->assertEquals('value', $actual, 'getPropertyPath does not work with Array Access property.');
239  }
240 
245  {
246  $objectStorage = new \TYPO3\CMS\Extbase\Persistence\ObjectStorage();
247  $exampleObject = new \stdClass();
248  $exampleObject->key = 'value';
249  $exampleObject2 = new \stdClass();
250  $exampleObject2->key = 'value2';
251  $objectStorage->attach($exampleObject);
252  $objectStorage->attach($exampleObject2);
253  $array = [
254  'parent' => $objectStorage,
255  ];
256  $this->assertSame('value', \TYPO3\CMS\Extbase\Reflection\ObjectAccess::getPropertyPath($array, 'parent.0.key'));
257  $this->assertSame('value2', \TYPO3\CMS\Extbase\Reflection\ObjectAccess::getPropertyPath($array, 'parent.1.key'));
258  }
259 
264  {
265  $objectStorage = new \SplObjectStorage();
266  $exampleObject = new \stdClass();
267  $exampleObject->key = 'value';
268  $exampleObject2 = new \stdClass();
269  $exampleObject2->key = 'value2';
270  $objectStorage->attach($exampleObject);
271  $objectStorage->attach($exampleObject2);
272  $array = [
273  'parent' => $objectStorage,
274  ];
275  $this->assertSame('value', \TYPO3\CMS\Extbase\Reflection\ObjectAccess::getPropertyPath($array, 'parent.0.key'));
276  $this->assertSame('value2', \TYPO3\CMS\Extbase\Reflection\ObjectAccess::getPropertyPath($array, 'parent.1.key'));
277  }
278 
283  {
284  $gettablePropertyNames = \TYPO3\CMS\Extbase\Reflection\ObjectAccess::getGettablePropertyNames($this->dummyObject);
285  $expectedPropertyNames = ['anotherBooleanProperty', 'anotherProperty', 'booleanProperty', 'property', 'property2', 'publicProperty', 'publicProperty2', 'someValue'];
286  $this->assertEquals($gettablePropertyNames, $expectedPropertyNames, 'getGettablePropertyNames returns not all gettable properties.');
287  }
288 
293  {
294  $dateTimeZone = new \DateTimeZone('+2');
295  $gettablePropertyNames = \TYPO3\CMS\Extbase\Reflection\ObjectAccess::getGettablePropertyNames($dateTimeZone);
296  $expectedPropertyNames = ['location', 'name'];
297  $this->assertArraySubset($expectedPropertyNames, $gettablePropertyNames);
298  }
299 
304  {
305  $settablePropertyNames = \TYPO3\CMS\Extbase\Reflection\ObjectAccess::getSettablePropertyNames($this->dummyObject);
306  $expectedPropertyNames = ['anotherBooleanProperty', 'anotherProperty', 'property', 'property2', 'publicProperty', 'publicProperty2', 'writeOnlyMagicProperty'];
307  $this->assertEquals($settablePropertyNames, $expectedPropertyNames, 'getSettablePropertyNames returns not all settable properties.');
308  }
309 
314  {
315  $stdClassObject = new \stdClass();
316  $stdClassObject->property = 'string1';
317  $stdClassObject->property2 = null;
318  $settablePropertyNames = \TYPO3\CMS\Extbase\Reflection\ObjectAccess::getSettablePropertyNames($stdClassObject);
319  $expectedPropertyNames = ['property', 'property2'];
320  $this->assertEquals($expectedPropertyNames, $settablePropertyNames, 'getSettablePropertyNames returns not all settable properties.');
321  }
322 
327  {
328  $allProperties = \TYPO3\CMS\Extbase\Reflection\ObjectAccess::getGettableProperties($this->dummyObject);
329  $expectedProperties = [
330  'anotherBooleanProperty' => true,
331  'anotherProperty' => 42,
332  'booleanProperty' => true,
333  'property' => 'string1',
334  'property2' => null,
335  'publicProperty' => null,
336  'publicProperty2' => 42,
337  'someValue' => true,
338  ];
339  $this->assertEquals($allProperties, $expectedProperties, 'expectedProperties did not return the right values for the properties.');
340  }
341 
346  {
347  $stdClassObject = new \stdClass();
348  $stdClassObject->property = 'string1';
349  $stdClassObject->property2 = null;
350  $stdClassObject->publicProperty2 = 42;
352  $expectedProperties = [
353  'property' => 'string1',
354  'property2' => null,
355  'publicProperty2' => 42
356  ];
357  $this->assertEquals($expectedProperties, $allProperties, 'expectedProperties did not return the right values for the properties.');
358  }
359 
364  {
365  $this->assertTrue(\TYPO3\CMS\Extbase\Reflection\ObjectAccess::isPropertySettable($this->dummyObject, 'writeOnlyMagicProperty'));
366  $this->assertTrue(\TYPO3\CMS\Extbase\Reflection\ObjectAccess::isPropertySettable($this->dummyObject, 'publicProperty'));
367  $this->assertTrue(\TYPO3\CMS\Extbase\Reflection\ObjectAccess::isPropertySettable($this->dummyObject, 'property'));
368  $this->assertFalse(\TYPO3\CMS\Extbase\Reflection\ObjectAccess::isPropertySettable($this->dummyObject, 'privateProperty'));
369  $this->assertFalse(\TYPO3\CMS\Extbase\Reflection\ObjectAccess::isPropertySettable($this->dummyObject, 'shouldNotBePickedUp'));
370  }
371 
376  {
377  $stdClassObject = new \stdClass();
378  $stdClassObject->property = 'foo';
379  $this->assertTrue(\TYPO3\CMS\Extbase\Reflection\ObjectAccess::isPropertySettable($stdClassObject, 'property'));
380  $this->assertFalse(\TYPO3\CMS\Extbase\Reflection\ObjectAccess::isPropertySettable($stdClassObject, 'undefinedProperty'));
381  }
382 
387  {
388  $this->assertTrue(\TYPO3\CMS\Extbase\Reflection\ObjectAccess::isPropertyGettable($this->dummyObject, 'publicProperty'));
389  $this->assertTrue(\TYPO3\CMS\Extbase\Reflection\ObjectAccess::isPropertyGettable($this->dummyObject, 'property'));
390  $this->assertTrue(\TYPO3\CMS\Extbase\Reflection\ObjectAccess::isPropertyGettable($this->dummyObject, 'booleanProperty'));
391  $this->assertFalse(\TYPO3\CMS\Extbase\Reflection\ObjectAccess::isPropertyGettable($this->dummyObject, 'privateProperty'));
392  $this->assertFalse(\TYPO3\CMS\Extbase\Reflection\ObjectAccess::isPropertyGettable($this->dummyObject, 'writeOnlyMagicProperty'));
393  $this->assertFalse(\TYPO3\CMS\Extbase\Reflection\ObjectAccess::isPropertyGettable($this->dummyObject, 'shouldNotBePickedUp'));
394  $this->assertTrue(\TYPO3\CMS\Extbase\Reflection\ObjectAccess::isPropertyGettable($this->dummyObject, 'anotherBooleanProperty'));
395  }
396 
401  {
402  $arrayObject = new \ArrayObject();
403  $arrayObject['key'] = 'v';
404  $this->assertTrue(\TYPO3\CMS\Extbase\Reflection\ObjectAccess::isPropertyGettable($arrayObject, 'key'));
405  $this->assertFalse(\TYPO3\CMS\Extbase\Reflection\ObjectAccess::isPropertyGettable($arrayObject, 'undefinedKey'));
406  }
407 
412  {
413  $stdClassObject = new \stdClass();
414  $stdClassObject->property = 'foo';
415  $this->assertTrue(\TYPO3\CMS\Extbase\Reflection\ObjectAccess::isPropertyGettable($stdClassObject, 'property'));
416  $this->assertFalse(\TYPO3\CMS\Extbase\Reflection\ObjectAccess::isPropertyGettable($stdClassObject, 'undefinedProperty'));
417  }
418 
423  {
424  $alternativeObject = new \TYPO3\CMS\Extbase\Tests\Unit\Reflection\Fixture\DummyClassWithGettersAndSetters();
425  $alternativeObject->setProperty('test');
426  $this->dummyObject->setProperty2($alternativeObject);
427  $expected = 'test';
428  $actual = \TYPO3\CMS\Extbase\Reflection\ObjectAccess::getPropertyPath($this->dummyObject, 'property2.property');
429  $this->assertEquals($expected, $actual);
430  }
431 
436  {
437  $alternativeObject = new \TYPO3\CMS\Extbase\Tests\Unit\Reflection\Fixture\DummyClassWithGettersAndSetters();
438  $alternativeObject->setProperty(new \stdClass());
439  $this->dummyObject->setProperty2($alternativeObject);
440  $this->assertNull(\TYPO3\CMS\Extbase\Reflection\ObjectAccess::getPropertyPath($this->dummyObject, 'property2.property.not.existing'));
441  }
442 
447  {
448  $string = 'Hello world';
449  $this->assertNull(\TYPO3\CMS\Extbase\Reflection\ObjectAccess::getPropertyPath($string, 'property2'));
450  }
451 
456  {
457  $object = new \stdClass();
458  $object->foo = 'Hello World';
459  $this->assertNull(\TYPO3\CMS\Extbase\Reflection\ObjectAccess::getPropertyPath($object, 'foo.bar'));
460  }
461 }
static getProperty($subject, $propertyName, $forceDirectAccess=false)
static getPropertyPath($subject, $propertyPath)
static setProperty(&$subject, $propertyName, $propertyValue, $forceDirectAccess=false)