TYPO3 CMS  TYPO3_6-2
ObjectAccessTest.php
Go to the documentation of this file.
1 <?php
3 
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 
35  $property = \TYPO3\CMS\Extbase\Reflection\ObjectAccess::getProperty($this->dummyObject, 'property');
36  $this->assertEquals($property, 'string1');
37  }
38 
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 
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 
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 
69  $property = \TYPO3\CMS\Extbase\Reflection\ObjectAccess::getProperty($this->dummyObject, 'notExistingProperty', TRUE);
70  }
71 
77  \TYPO3\CMS\Extbase\Reflection\ObjectAccess::getProperty($this->dummyObject, 'notExistingProperty');
78  }
79 
85  \TYPO3\CMS\Extbase\Reflection\ObjectAccess::getProperty(array(), 'notExistingProperty');
86  }
87 
92  $property = \TYPO3\CMS\Extbase\Reflection\ObjectAccess::getProperty($this->dummyObject, 'booleanProperty');
93  $this->assertTrue($property);
94  }
95 
101  \TYPO3\CMS\Extbase\Reflection\ObjectAccess::getProperty($this->dummyObject, new \ArrayObject());
102  }
103 
109  \TYPO3\CMS\Extbase\Reflection\ObjectAccess::setProperty($this->dummyObject, new \ArrayObject(), 42);
110  }
111 
116  $this->assertFalse(\TYPO3\CMS\Extbase\Reflection\ObjectAccess::setProperty($this->dummyObject, 'protectedProperty', 42));
117  }
118 
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 
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 
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 
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 
155  $arrayObject = new \ArrayObject();
156  $array = array();
157  \TYPO3\CMS\Extbase\Reflection\ObjectAccess::setProperty($arrayObject, 'publicProperty', 4242);
159  $this->assertEquals(4242, $arrayObject['publicProperty']);
160  $this->assertEquals('value', $array['key']);
161  }
162 
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 
176  $objectStorage = new \TYPO3\CMS\Extbase\Persistence\ObjectStorage();
177  $objectStorage->key = 'value';
178  $actual = \TYPO3\CMS\Extbase\Reflection\ObjectAccess::getProperty($objectStorage, 'key');
179  $this->assertEquals('value', $actual, 'getProperty does not work with ObjectStorage property.');
180  }
181 
186  $arrayAccessInstance = new \TYPO3\CMS\Extbase\Tests\Unit\Reflection\Fixture\ArrayAccessClass(array('key' => 'value'));
187  $actual = \TYPO3\CMS\Extbase\Reflection\ObjectAccess::getProperty($arrayAccessInstance, 'key');
188  $this->assertEquals('value', $actual, 'getProperty does not work with Array Access property.');
189  }
190 
195  $array = array('key' => 'value');
197  $this->assertEquals($expected, 'value', 'getProperty does not work with Array property.');
198  }
199 
204  $array = array('parent' => array('key' => 'value'));
205  $actual = \TYPO3\CMS\Extbase\Reflection\ObjectAccess::getPropertyPath($array, 'parent.key');
206  $this->assertEquals('value', $actual, 'getPropertyPath does not work with Array property.');
207  }
208 
213  $array = array('parent' => new \ArrayObject(array('key' => 'value')));
214  $actual = \TYPO3\CMS\Extbase\Reflection\ObjectAccess::getPropertyPath($array, 'parent.key');
215  $this->assertEquals('value', $actual, 'getPropertyPath does not work with Array Access property.');
216  }
217 
222  $objectStorage = new \TYPO3\CMS\Extbase\Persistence\ObjectStorage();
223  $exampleObject = new \stdClass();
224  $exampleObject->key = 'value';
225  $exampleObject2 = new \stdClass();
226  $exampleObject2->key = 'value2';
227  $objectStorage->attach($exampleObject);
228  $objectStorage->attach($exampleObject2);
229  $array = array(
230  'parent' => $objectStorage,
231  );
232  $this->assertSame('value', \TYPO3\CMS\Extbase\Reflection\ObjectAccess::getPropertyPath($array, 'parent.0.key'));
233  $this->assertSame('value2', \TYPO3\CMS\Extbase\Reflection\ObjectAccess::getPropertyPath($array, 'parent.1.key'));
234  }
235 
240  $objectStorage = new \SplObjectStorage();
241  $exampleObject = new \stdClass();
242  $exampleObject->key = 'value';
243  $exampleObject2 = new \stdClass();
244  $exampleObject2->key = 'value2';
245  $objectStorage->attach($exampleObject);
246  $objectStorage->attach($exampleObject2);
247  $array = array(
248  'parent' => $objectStorage,
249  );
250  $this->assertSame('value', \TYPO3\CMS\Extbase\Reflection\ObjectAccess::getPropertyPath($array, 'parent.0.key'));
251  $this->assertSame('value2', \TYPO3\CMS\Extbase\Reflection\ObjectAccess::getPropertyPath($array, 'parent.1.key'));
252  }
253 
258  $gettablePropertyNames = \TYPO3\CMS\Extbase\Reflection\ObjectAccess::getGettablePropertyNames($this->dummyObject);
259  $expectedPropertyNames = array('anotherProperty', 'booleanProperty', 'property', 'property2', 'publicProperty', 'publicProperty2');
260  $this->assertEquals($gettablePropertyNames, $expectedPropertyNames, 'getGettablePropertyNames returns not all gettable properties.');
261  }
262 
267  $settablePropertyNames = \TYPO3\CMS\Extbase\Reflection\ObjectAccess::getSettablePropertyNames($this->dummyObject);
268  $expectedPropertyNames = array('anotherProperty', 'property', 'property2', 'publicProperty', 'publicProperty2', 'writeOnlyMagicProperty');
269  $this->assertEquals($settablePropertyNames, $expectedPropertyNames, 'getSettablePropertyNames returns not all settable properties.');
270  }
271 
276  $stdClassObject = new \stdClass();
277  $stdClassObject->property = 'string1';
278  $stdClassObject->property2 = NULL;
279  $settablePropertyNames = \TYPO3\CMS\Extbase\Reflection\ObjectAccess::getSettablePropertyNames($stdClassObject);
280  $expectedPropertyNames = array('property', 'property2');
281  $this->assertEquals($expectedPropertyNames, $settablePropertyNames, 'getSettablePropertyNames returns not all settable properties.');
282  }
283 
288  $allProperties = \TYPO3\CMS\Extbase\Reflection\ObjectAccess::getGettableProperties($this->dummyObject);
289  $expectedProperties = array(
290  'anotherProperty' => 42,
291  'booleanProperty' => TRUE,
292  'property' => 'string1',
293  'property2' => NULL,
294  'publicProperty' => NULL,
295  'publicProperty2' => 42
296  );
297  $this->assertEquals($allProperties, $expectedProperties, 'expectedProperties did not return the right values for the properties.');
298  }
299 
304  $stdClassObject = new \stdClass();
305  $stdClassObject->property = 'string1';
306  $stdClassObject->property2 = NULL;
307  $stdClassObject->publicProperty2 = 42;
309  $expectedProperties = array(
310  'property' => 'string1',
311  'property2' => NULL,
312  'publicProperty2' => 42
313  );
314  $this->assertEquals($expectedProperties, $allProperties, 'expectedProperties did not return the right values for the properties.');
315  }
316 
321  $this->assertTrue(\TYPO3\CMS\Extbase\Reflection\ObjectAccess::isPropertySettable($this->dummyObject, 'writeOnlyMagicProperty'));
322  $this->assertTrue(\TYPO3\CMS\Extbase\Reflection\ObjectAccess::isPropertySettable($this->dummyObject, 'publicProperty'));
323  $this->assertTrue(\TYPO3\CMS\Extbase\Reflection\ObjectAccess::isPropertySettable($this->dummyObject, 'property'));
324  $this->assertFalse(\TYPO3\CMS\Extbase\Reflection\ObjectAccess::isPropertySettable($this->dummyObject, 'privateProperty'));
325  $this->assertFalse(\TYPO3\CMS\Extbase\Reflection\ObjectAccess::isPropertySettable($this->dummyObject, 'shouldNotBePickedUp'));
326  }
327 
332  $stdClassObject = new \stdClass();
333  $stdClassObject->property = 'foo';
334  $this->assertTrue(\TYPO3\CMS\Extbase\Reflection\ObjectAccess::isPropertySettable($stdClassObject, 'property'));
335  $this->assertFalse(\TYPO3\CMS\Extbase\Reflection\ObjectAccess::isPropertySettable($stdClassObject, 'undefinedProperty'));
336  }
337 
342  $this->assertTrue(\TYPO3\CMS\Extbase\Reflection\ObjectAccess::isPropertyGettable($this->dummyObject, 'publicProperty'));
343  $this->assertTrue(\TYPO3\CMS\Extbase\Reflection\ObjectAccess::isPropertyGettable($this->dummyObject, 'property'));
344  $this->assertTrue(\TYPO3\CMS\Extbase\Reflection\ObjectAccess::isPropertyGettable($this->dummyObject, 'booleanProperty'));
345  $this->assertFalse(\TYPO3\CMS\Extbase\Reflection\ObjectAccess::isPropertyGettable($this->dummyObject, 'privateProperty'));
346  $this->assertFalse(\TYPO3\CMS\Extbase\Reflection\ObjectAccess::isPropertyGettable($this->dummyObject, 'writeOnlyMagicProperty'));
347  $this->assertFalse(\TYPO3\CMS\Extbase\Reflection\ObjectAccess::isPropertyGettable($this->dummyObject, 'shouldNotBePickedUp'));
348  }
349 
354  $arrayObject = new \ArrayObject();
355  $arrayObject['key'] = 'v';
356  $this->assertTrue(\TYPO3\CMS\Extbase\Reflection\ObjectAccess::isPropertyGettable($arrayObject, 'key'));
357  $this->assertFalse(\TYPO3\CMS\Extbase\Reflection\ObjectAccess::isPropertyGettable($arrayObject, 'undefinedKey'));
358  }
359 
364  $stdClassObject = new \stdClass();
365  $stdClassObject->property = 'foo';
366  $this->assertTrue(\TYPO3\CMS\Extbase\Reflection\ObjectAccess::isPropertyGettable($stdClassObject, 'property'));
367  $this->assertFalse(\TYPO3\CMS\Extbase\Reflection\ObjectAccess::isPropertyGettable($stdClassObject, 'undefinedProperty'));
368  }
369 
374  $alternativeObject = new \TYPO3\CMS\Extbase\Tests\Unit\Reflection\Fixture\DummyClassWithGettersAndSetters();
375  $alternativeObject->setProperty('test');
376  $this->dummyObject->setProperty2($alternativeObject);
377  $expected = 'test';
378  $actual = \TYPO3\CMS\Extbase\Reflection\ObjectAccess::getPropertyPath($this->dummyObject, 'property2.property');
379  $this->assertEquals($expected, $actual);
380  }
381 
386  $alternativeObject = new \TYPO3\CMS\Extbase\Tests\Unit\Reflection\Fixture\DummyClassWithGettersAndSetters();
387  $alternativeObject->setProperty(new \stdClass());
388  $this->dummyObject->setProperty2($alternativeObject);
389  $this->assertNull(\TYPO3\CMS\Extbase\Reflection\ObjectAccess::getPropertyPath($this->dummyObject, 'property2.property.not.existing'));
390  }
391 
396  $string = 'Hello world';
397  $this->assertNull(\TYPO3\CMS\Extbase\Reflection\ObjectAccess::getPropertyPath($string, 'property2'));
398  }
399 
404  $object = new \stdClass();
405  $object->foo = 'Hello World';
406  $this->assertNull(\TYPO3\CMS\Extbase\Reflection\ObjectAccess::getPropertyPath($object, 'foo.bar'));
407  }
408 }
static setProperty(&$subject, $propertyName, $propertyValue, $forceDirectAccess=FALSE)
static getPropertyPath($subject, $propertyPath)
static getProperty($subject, $propertyName, $forceDirectAccess=FALSE)