TYPO3 API  SVNRelease
ObjectAccessTest.php
Go to the documentation of this file.
00001 <?php
00002 /***************************************************************
00003 *  Copyright notice
00004 *
00005 *  All rights reserved
00006 *
00007 *  This class is a backport of the corresponding class of FLOW3.
00008 *  All credits go to the v5 team.
00009 *
00010 *  This script is part of the TYPO3 project. The TYPO3 project is
00011 *  free software; you can redistribute it and/or modify
00012 *  it under the terms of the GNU General Public License as published by
00013 *  the Free Software Foundation; either version 2 of the License, or
00014 *  (at your option) any later version.
00015 *
00016 *  The GNU General Public License can be found at
00017 *  http://www.gnu.org/copyleft/gpl.html.
00018 *
00019 *  This script is distributed in the hope that it will be useful,
00020 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00021 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00022 *  GNU General Public License for more details.
00023 *
00024 *  This copyright notice MUST APPEAR in all copies of the script!
00025 ***************************************************************/
00026 
00027 require_once('Fixture/DummyClassWithGettersAndSetters.php');
00028 require_once('Fixture/ArrayAccessClass.php');
00029 
00030 class Tx_Extbase_Tests_Unit_Reflection_ObjectAccessTest extends Tx_Extbase_Tests_Unit_BaseTestCase {
00031 
00032     protected $dummyObject;
00033 
00034     public function setUp() {
00035         $this->dummyObject = new Tx_Extbase_Tests_Unit_Reflection_Fixture_DummyClassWithGettersAndSetters();
00036         $this->dummyObject->setProperty('string1');
00037         $this->dummyObject->setAnotherProperty(42);
00038         $this->dummyObject->shouldNotBePickedUp = TRUE;
00039     }
00040 
00041     /**
00042      * @test
00043      */
00044     public function getPropertyReturnsExpectedValueForGetterProperty() {
00045         $property = Tx_Extbase_Reflection_ObjectAccess::getProperty($this->dummyObject, 'property');
00046         $this->assertEquals($property, 'string1');
00047     }
00048 
00049     /**
00050      * @test
00051      */
00052     public function getPropertyReturnsExpectedValueForPublicProperty() {
00053         $property = Tx_Extbase_Reflection_ObjectAccess::getProperty($this->dummyObject, 'publicProperty2');
00054         $this->assertEquals($property, 42, 'A property of a given object was not returned correctly.');
00055     }
00056 
00057     /**
00058      * @test
00059      * @expectedException Tx_Extbase_Reflection_Exception_PropertyNotAccessibleException
00060      */
00061     public function getPropertyReturnsThrowsExceptionIfPropertyDoesNotExist() {
00062         Tx_Extbase_Reflection_ObjectAccess::getProperty($this->dummyObject, 'notExistingProperty');
00063     }
00064 
00065     /**
00066      * @test
00067      * @expectedException Tx_Extbase_Reflection_Exception_PropertyNotAccessibleException
00068      */
00069     public function getPropertyReturnsThrowsExceptionIfArrayKeyDoesNotExist() {
00070         Tx_Extbase_Reflection_ObjectAccess::getProperty(array(), 'notExistingProperty');
00071     }
00072 
00073     /**
00074      * @test
00075      */
00076     public function getPropertyTriesToCallABooleanGetterMethodIfItExists() {
00077         $property = Tx_Extbase_Reflection_ObjectAccess::getProperty($this->dummyObject, 'booleanProperty');
00078         $this->assertSame('method called 1', $property);
00079     }
00080 
00081     /**
00082      * @test
00083      * @expectedException InvalidArgumentException
00084      */
00085     public function getPropertyThrowsExceptionIfThePropertyNameIsNotAString() {
00086         Tx_Extbase_Reflection_ObjectAccess::getProperty($this->dummyObject, new ArrayObject());
00087     }
00088 
00089     /**
00090      * @test
00091      * @expectedException InvalidArgumentException
00092      */
00093     public function setPropertyThrowsExceptionIfThePropertyNameIsNotAString() {
00094         Tx_Extbase_Reflection_ObjectAccess::setProperty($this->dummyObject, new ArrayObject(), 42);
00095     }
00096 
00097     /**
00098      * @test
00099      */
00100     public function setPropertyReturnsFalseIfPropertyIsNotAccessible() {
00101         $this->assertFalse(Tx_Extbase_Reflection_ObjectAccess::setProperty($this->dummyObject, 'protectedProperty', 42));
00102     }
00103 
00104     /**
00105      * @test
00106      */
00107     public function setPropertyCallsASetterMethodToSetThePropertyValueIfOneIsAvailable() {
00108         Tx_Extbase_Reflection_ObjectAccess::setProperty($this->dummyObject, 'property', 4242);
00109         $this->assertEquals($this->dummyObject->getProperty(), 4242, 'setProperty does not work with setter.');
00110     }
00111 
00112     /**
00113      * @test
00114      */
00115     public function setPropertyWorksWithPublicProperty() {
00116         Tx_Extbase_Reflection_ObjectAccess::setProperty($this->dummyObject, 'publicProperty', 4242);
00117         $this->assertEquals($this->dummyObject->publicProperty, 4242, 'setProperty does not work with public property.');
00118     }
00119 
00120     /**
00121      * @test
00122      */
00123     public function setPropertyCanDirectlySetValuesInAnArrayObjectOrArray() {
00124         $arrayObject = new ArrayObject();
00125         $array = array();
00126 
00127         Tx_Extbase_Reflection_ObjectAccess::setProperty($arrayObject, 'publicProperty', 4242);
00128         Tx_Extbase_Reflection_ObjectAccess::setProperty($array, 'key', 'value');
00129 
00130         $this->assertEquals(4242, $arrayObject['publicProperty']);
00131         $this->assertEquals('value', $array['key']);
00132     }
00133 
00134     /**
00135      * @test
00136      */
00137     public function getPropertyCanAccessPropertiesOfAnArrayObject() {
00138         $arrayObject = new ArrayObject(array('key' => 'value'));
00139         $expected = Tx_Extbase_Reflection_ObjectAccess::getProperty($arrayObject, 'key');
00140         $this->assertEquals($expected, 'value', 'getProperty does not work with ArrayObject property.');
00141     }
00142 
00143     /**
00144      * @test
00145      */
00146     public function getPropertyCanAccessPropertiesOfAnObjectImplementingArrayAccess() {
00147         $arrayAccessInstance = new Tx_Extbase_Tests_Unit_Reflection_Fixture_ArrayAccessClass(array('key' => 'value'));
00148         $expected = Tx_Extbase_Reflection_ObjectAccess::getProperty($arrayAccessInstance, 'key');
00149         $this->assertEquals($expected, 'value', 'getPropertyPath does not work with Array Access property.');
00150     }
00151 
00152     /**
00153      * @test
00154      */
00155     public function getPropertyCanAccessPropertiesOfAnArray() {
00156         $array = array('key' => 'value');
00157         $expected = Tx_Extbase_Reflection_ObjectAccess::getProperty($array, 'key');
00158         $this->assertEquals($expected, 'value', 'getProperty does not work with Array property.');
00159     }
00160 
00161     /**
00162      * @test
00163      */
00164     public function getPropertyPathCanAccessPropertiesOfAnArray() {
00165         $array = array('parent' => array('key' => 'value'));
00166         $expected = Tx_Extbase_Reflection_ObjectAccess::getPropertyPath($array, 'parent.key');
00167         $this->assertEquals($expected, 'value', 'getPropertyPath does not work with Array property.');
00168     }
00169 
00170     /**
00171      * @test
00172      */
00173     public function getPropertyPathCanAccessPropertiesOfAnObjectImplementingArrayAccess() {
00174         $array = array('parent' => new ArrayObject(array('key' => 'value')));
00175         $expected = Tx_Extbase_Reflection_ObjectAccess::getPropertyPath($array, 'parent.key');
00176         $this->assertEquals($expected, 'value', 'getPropertyPath does not work with Array Access property.');
00177     }
00178 
00179     /**
00180      * @test
00181      */
00182     public function getGettablePropertyNamesReturnsAllPropertiesWhichAreAvailable() {
00183         $gettablePropertyNames = Tx_Extbase_Reflection_ObjectAccess::getGettablePropertyNames($this->dummyObject);
00184         $expectedPropertyNames = array('anotherProperty', 'booleanProperty', 'property', 'property2', 'publicProperty', 'publicProperty2');
00185         $this->assertEquals($gettablePropertyNames, $expectedPropertyNames, 'getGettablePropertyNames returns not all gettable properties.');
00186     }
00187 
00188     /**
00189      * @test
00190      */
00191     public function getSettablePropertyNamesReturnsAllPropertiesWhichAreAvailable() {
00192         $settablePropertyNames = Tx_Extbase_Reflection_ObjectAccess::getSettablePropertyNames($this->dummyObject);
00193         $expectedPropertyNames = array('anotherProperty', 'property', 'property2', 'publicProperty', 'publicProperty2', 'writeOnlyMagicProperty');
00194         $this->assertEquals($settablePropertyNames, $expectedPropertyNames, 'getSettablePropertyNames returns not all settable properties.');
00195     }
00196 
00197     /**
00198      * @test
00199      */
00200     public function getSettablePropertyNamesReturnsPropertyNamesOfStdClass() {
00201         $stdClassObject = new stdClass();
00202         $stdClassObject->property = 'string1';
00203         $stdClassObject->property2 = NULL;
00204 
00205         $settablePropertyNames = Tx_Extbase_Reflection_ObjectAccess::getSettablePropertyNames($stdClassObject);
00206         $expectedPropertyNames = array('property', 'property2');
00207         $this->assertEquals($expectedPropertyNames, $settablePropertyNames, 'getSettablePropertyNames returns not all settable properties.');
00208     }
00209 
00210     /**
00211      * @test
00212      */
00213     public function getGettablePropertiesReturnsTheCorrectValuesForAllProperties() {
00214         $allProperties = Tx_Extbase_Reflection_ObjectAccess::getGettableProperties($this->dummyObject);
00215         $expectedProperties = array(
00216             'anotherProperty' => 42,
00217             'booleanProperty' => TRUE,
00218             'property' => 'string1',
00219             'property2' => NULL,
00220             'publicProperty' => NULL,
00221             'publicProperty2' => 42);
00222         $this->assertEquals($allProperties, $expectedProperties, 'expectedProperties did not return the right values for the properties.');
00223     }
00224 
00225     /**
00226      * @test
00227      */
00228     public function getGettablePropertiesReturnsPropertiesOfStdClass() {
00229         $stdClassObject = new stdClass();
00230         $stdClassObject->property = 'string1';
00231         $stdClassObject->property2 = NULL;
00232         $stdClassObject->publicProperty2 = 42;
00233         $allProperties = Tx_Extbase_Reflection_ObjectAccess::getGettableProperties($stdClassObject);
00234         $expectedProperties = array(
00235             'property' => 'string1',
00236             'property2' => NULL,
00237             'publicProperty2' => 42);
00238         $this->assertEquals($expectedProperties, $allProperties, 'expectedProperties did not return the right values for the properties.');
00239     }
00240 
00241     /**
00242      * @test
00243      */
00244     public function isPropertySettableTellsIfAPropertyCanBeSet() {
00245         $this->assertTrue(Tx_Extbase_Reflection_ObjectAccess::isPropertySettable($this->dummyObject, 'writeOnlyMagicProperty'));
00246         $this->assertTrue(Tx_Extbase_Reflection_ObjectAccess::isPropertySettable($this->dummyObject, 'publicProperty'));
00247         $this->assertTrue(Tx_Extbase_Reflection_ObjectAccess::isPropertySettable($this->dummyObject, 'property'));
00248 
00249         $this->assertFalse(Tx_Extbase_Reflection_ObjectAccess::isPropertySettable($this->dummyObject, 'privateProperty'));
00250         $this->assertFalse(Tx_Extbase_Reflection_ObjectAccess::isPropertySettable($this->dummyObject, 'shouldNotBePickedUp'));
00251     }
00252 
00253     /**
00254      * @test
00255      */
00256     public function isPropertySettableWorksOnStdClass() {
00257         $stdClassObject = new stdClass();
00258         $stdClassObject->property = 'foo';
00259 
00260         $this->assertTrue(Tx_Extbase_Reflection_ObjectAccess::isPropertySettable($stdClassObject, 'property'));
00261 
00262         $this->assertFalse(Tx_Extbase_Reflection_ObjectAccess::isPropertySettable($stdClassObject, 'undefinedProperty'));
00263     }
00264 
00265     /**
00266      * @test
00267      */
00268     public function isPropertyGettableTellsIfAPropertyCanBeRetrieved() {
00269         $this->assertTrue(Tx_Extbase_Reflection_ObjectAccess::isPropertyGettable($this->dummyObject, 'publicProperty'));
00270         $this->assertTrue(Tx_Extbase_Reflection_ObjectAccess::isPropertyGettable($this->dummyObject, 'property'));
00271         $this->assertTrue(Tx_Extbase_Reflection_ObjectAccess::isPropertyGettable($this->dummyObject, 'booleanProperty'));
00272 
00273         $this->assertFalse(Tx_Extbase_Reflection_ObjectAccess::isPropertyGettable($this->dummyObject, 'privateProperty'));
00274         $this->assertFalse(Tx_Extbase_Reflection_ObjectAccess::isPropertyGettable($this->dummyObject, 'writeOnlyMagicProperty'));
00275         $this->assertFalse(Tx_Extbase_Reflection_ObjectAccess::isPropertyGettable($this->dummyObject, 'shouldNotBePickedUp'));
00276     }
00277 
00278     /**
00279      * @test
00280      */
00281     public function isPropertyGettableWorksOnArrayAccessObjects() {
00282         $arrayObject = new ArrayObject();
00283         $arrayObject['key'] = 'v';
00284 
00285         $this->assertTrue(Tx_Extbase_Reflection_ObjectAccess::isPropertyGettable($arrayObject, 'key'));
00286         $this->assertFalse(Tx_Extbase_Reflection_ObjectAccess::isPropertyGettable($arrayObject, 'undefinedKey'));
00287     }
00288 
00289     /**
00290      * @test
00291      */
00292     public function isPropertyGettableWorksOnStdClass() {
00293         $stdClassObject = new stdClass();
00294         $stdClassObject->property = 'foo';
00295 
00296         $this->assertTrue(Tx_Extbase_Reflection_ObjectAccess::isPropertyGettable($stdClassObject, 'property'));
00297 
00298         $this->assertFalse(Tx_Extbase_Reflection_ObjectAccess::isPropertyGettable($stdClassObject, 'undefinedProperty'));
00299     }
00300 
00301     /**
00302      * @test
00303      */
00304     public function getPropertyPathCanRecursivelyGetPropertiesOfAnObject() {
00305         $alternativeObject = new Tx_Extbase_Tests_Unit_Reflection_Fixture_DummyClassWithGettersAndSetters();
00306         $alternativeObject->setProperty('test');
00307         $this->dummyObject->setProperty2($alternativeObject);
00308 
00309         $expected = 'test';
00310         $actual = Tx_Extbase_Reflection_ObjectAccess::getPropertyPath($this->dummyObject, 'property2.property');
00311         $this->assertEquals($expected, $actual);
00312     }
00313 
00314     /**
00315      * @test
00316      */
00317     public function getPropertyPathReturnsNullForNonExistingPropertyPath() {
00318         $alternativeObject = new Tx_Extbase_Tests_Unit_Reflection_Fixture_DummyClassWithGettersAndSetters();
00319         $alternativeObject->setProperty(new stdClass());
00320         $this->dummyObject->setProperty2($alternativeObject);
00321 
00322         $this->assertNull(Tx_Extbase_Reflection_ObjectAccess::getPropertyPath($this->dummyObject, 'property2.property.not.existing'));
00323     }
00324 
00325 }
00326 ?>