TYPO3 API  SVNRelease
ContainerTest.php
Go to the documentation of this file.
00001 <?php
00002 /***************************************************************
00003 *  Copyright notice
00004 *  (c) 2010 Daniel Pötzinger
00005 *  (c) 2010 Bastian Waidelich <bastian@typo3.org>
00006 *  All rights reserved
00007 *
00008 *  This script is part of the TYPO3 project. The TYPO3 project is
00009 *  free software; you can redistribute it and/or modify
00010 *  it under the terms of the GNU General Public License as published by
00011 *  the Free Software Foundation; either version 2 of the License, or
00012 *  (at your option) any later version.
00013 *
00014 *  The GNU General Public License can be found at
00015 *  http://www.gnu.org/copyleft/gpl.html.
00016 *
00017 *  This script is distributed in the hope that it will be useful,
00018 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00019 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00020 *  GNU General Public License for more details.
00021 *
00022 *  This copyright notice MUST APPEAR in all copies of the script!
00023 ***************************************************************/
00024 
00025 require_once(t3lib_extMgm::extPath('extbase') . 'Tests/Unit/Object/Container/Fixtures/Testclasses.php');
00026 
00027 /**
00028  * Testcase for class t3lib_object_Container.
00029  *
00030  * @author Daniel Pötzinger
00031  * @author Bastian Waidelich <bastian@typo3.org>
00032  * @package TYPO3
00033  * @subpackage t3lib
00034  */
00035 class Tx_Extbase_Tests_Unit_Object_Container_ContainerTest extends Tx_Extbase_Tests_Unit_BaseTestCase {
00036 
00037     private $container;
00038 
00039     public function setUp() {
00040         $this->container = $this->getMock('Tx_Extbase_Object_Container_Container', array('log'));
00041     }
00042 
00043     /**
00044      * @test
00045      */
00046     public function getInstanceReturnsInstanceOfSimpleClass() {
00047         $object = $this->container->getInstance('t3lib_object_tests_c');
00048         $this->assertType('t3lib_object_tests_c', $object);
00049     }
00050 
00051     /**
00052      * @test
00053      */
00054     public function getInstanceReturnsInstanceOfAClassWithConstructorInjection() {
00055         $object = $this->container->getInstance('t3lib_object_tests_b');
00056         $this->assertType('t3lib_object_tests_b', $object);
00057         $this->assertType('t3lib_object_tests_c', $object->c);
00058     }
00059 
00060     /**
00061      * @test
00062      */
00063     public function getInstanceReturnsInstanceOfAClassWithTwoLevelDependency() {
00064         $object = $this->container->getInstance('t3lib_object_tests_a');
00065         $this->assertType('t3lib_object_tests_a', $object);
00066         $this->assertType('t3lib_object_tests_c', $object->b->c);
00067     }
00068 
00069     /**
00070      * @test
00071      */
00072     public function getInstanceReturnsInstanceOfAClassWithMixedSimpleTypeAndConstructorInjection() {
00073         $object = $this->container->getInstance('t3lib_object_tests_amixed_array');
00074         $this->assertType('t3lib_object_tests_amixed_array', $object);
00075         $this->assertEquals(array('some' => 'default'), $object->myvalue);
00076     }
00077 
00078     /**
00079      * @test
00080      */
00081     public function getInstanceReturnsInstanceOfAClassWithMixedSimpleTypeAndConstructorInjectionWithNullDefaultValue() {
00082         $object = $this->container->getInstance('t3lib_object_tests_amixed_null');
00083         $this->assertType('t3lib_object_tests_amixed_null', $object);
00084         $this->assertNull($object->myvalue);
00085     }
00086 
00087     /**
00088      * @test
00089      * @expectedException Tx_Extbase_Object_Exception
00090      */
00091     public function getInstanceThrowsExceptionWhenTryingToInstanciateASingletonWithConstructorParameters() {
00092         $this->container->getInstance('t3lib_object_tests_amixed_array_singleton', array('somevalue'));
00093     }
00094 
00095     /**
00096      * @test
00097      */
00098     public function getInstanceReturnsInstanceOfAClassWithConstructorInjectionAndDefaultConstructorParameters() {
00099         $object = $this->container->getInstance('t3lib_object_tests_amixed_array');
00100         $this->assertType('t3lib_object_tests_b', $object->b);
00101         $this->assertType('t3lib_object_tests_c', $object->c);
00102         $this->assertEquals(array('some' => 'default'), $object->myvalue);
00103     }
00104 
00105     /**
00106      * @test
00107      */
00108     public function getInstancePassesGivenParameterToTheNewObject() {
00109         $mockObject = $this->getMock('t3lib_object_tests_c');
00110 
00111         $object = $this->container->getInstance('t3lib_object_tests_a', array($mockObject));
00112         $this->assertType('t3lib_object_tests_a', $object);
00113         $this->assertSame($mockObject, $object->c);
00114     }
00115 
00116     /**
00117      * @test
00118      */
00119     public function getInstanceReturnsAFreshInstanceIfObjectIsNoSingleton() {
00120         $object1 = $this->container->getInstance('t3lib_object_tests_a');
00121         $object2 = $this->container->getInstance('t3lib_object_tests_a');
00122 
00123         $this->assertNotSame($object1, $object2);
00124     }
00125 
00126     /**
00127      * @test
00128      */
00129     public function getInstanceReturnsSameInstanceInstanceIfObjectIsSingleton() {
00130         $object1 = $this->container->getInstance('t3lib_object_tests_singleton');
00131         $object2 = $this->container->getInstance('t3lib_object_tests_singleton');
00132 
00133         $this->assertSame($object1, $object2);
00134     }
00135 
00136     /**
00137      * @test
00138      * @expectedException Tx_Extbase_Object_Exception_CannotBuildObject
00139      */
00140     public function getInstanceThrowsExceptionIfPrototypeObjectsWiredViaConstructorInjectionContainCyclicDependencies() {
00141         $this->container->getInstance('t3lib_object_tests_cyclic1WithSetterDependency');
00142     }
00143 
00144     /**
00145      * @test
00146      * @expectedException Tx_Extbase_Object_Exception_CannotBuildObject
00147      */
00148     public function getInstanceThrowsExceptionIfPrototypeObjectsWiredViaSetterInjectionContainCyclicDependencies() {
00149         $this->container->getInstance('t3lib_object_tests_cyclic1');
00150     }
00151 
00152     /**
00153      * @test
00154      * @expectedException Tx_Extbase_Object_Exception
00155      */
00156     public function getInstanceThrowsExceptionIfClassWasNotFound() {
00157         $this->container->getInstance('nonextistingclass_bla');
00158     }
00159 
00160     /**
00161      * @test
00162      */
00163     public function test_canGetChildClass() {
00164         $object = $this->container->getInstance('t3lib_object_tests_b_child');
00165         $this->assertType('t3lib_object_tests_b_child', $object);
00166     }
00167 
00168     /**
00169      * @test
00170      */
00171     public function test_canInjectInterfaceInClass() {
00172         $this->container->registerImplementation('t3lib_object_tests_someinterface', 't3lib_object_tests_someimplementation');
00173         $object = $this->container->getInstance('t3lib_object_tests_needsinterface');
00174         $this->assertType('t3lib_object_tests_needsinterface', $object);
00175 
00176         $this->assertType('t3lib_object_tests_someinterface', $object->dependency);
00177         $this->assertType('t3lib_object_tests_someimplementation', $object->dependency);
00178     }
00179 
00180     /**
00181      * @test
00182      */
00183     public function test_canBuildCyclicDependenciesOfSingletonsWithSetter() {
00184         $object = $this->container->getInstance('t3lib_object_tests_resolveablecyclic1');
00185         $this->assertType('t3lib_object_tests_resolveablecyclic1', $object);
00186         $this->assertType('t3lib_object_tests_resolveablecyclic1', $object->o2->o3->o1);
00187     }
00188 
00189     /**
00190      * @test
00191      */
00192     public function singletonWhichRequiresPrototypeViaSetterInjectionWorksAndAddsDebugMessage() {
00193         $this->container->expects($this->once())->method('log')->with('The singleton "t3lib_object_singletonNeedsPrototype" needs a prototype in "injectDependency". This is often a bad code smell; often you rather want to inject a singleton.', 1);
00194 
00195         $object = $this->container->getInstance('t3lib_object_singletonNeedsPrototype');
00196         $this->assertType('t3lib_object_prototype', $object->dependency);
00197     }
00198 
00199     /**
00200      * @test
00201      */
00202     public function singletonWhichRequiresSingletonViaSetterInjectionWorks() {
00203         $this->container->expects($this->never())->method('log');
00204 
00205         $object = $this->container->getInstance('t3lib_object_singletonNeedsSingleton');
00206         $this->assertType('t3lib_object_singleton', $object->dependency);
00207     }
00208 
00209     /**
00210      * @test
00211      */
00212     public function prototypeWhichRequiresPrototypeViaSetterInjectionWorks() {
00213         $this->container->expects($this->never())->method('log');
00214 
00215         $object = $this->container->getInstance('t3lib_object_prototypeNeedsPrototype');
00216         $this->assertType('t3lib_object_prototype', $object->dependency);
00217     }
00218 
00219     /**
00220      * @test
00221      */
00222     public function prototypeWhichRequiresSingletonViaSetterInjectionWorks() {
00223         $this->container->expects($this->never())->method('log');
00224 
00225         $object = $this->container->getInstance('t3lib_object_prototypeNeedsSingleton');
00226         $this->assertType('t3lib_object_singleton', $object->dependency);
00227     }
00228 
00229     /**
00230      * @test
00231      */
00232     public function singletonWhichRequiresPrototypeViaConstructorInjectionWorksAndAddsDebugMessage() {
00233         $this->container->expects($this->once())->method('log')->with('The singleton "t3lib_object_singletonNeedsPrototypeInConstructor" needs a prototype in the constructor. This is often a bad code smell; often you rather want to inject a singleton.', 1);
00234 
00235         $object = $this->container->getInstance('t3lib_object_singletonNeedsPrototypeInConstructor');
00236         $this->assertType('t3lib_object_prototype', $object->dependency);
00237     }
00238 
00239     /**
00240      * @test
00241      */
00242     public function singletonWhichRequiresSingletonViaConstructorInjectionWorks() {
00243         $this->container->expects($this->never())->method('log');
00244 
00245         $object = $this->container->getInstance('t3lib_object_singletonNeedsSingletonInConstructor');
00246         $this->assertType('t3lib_object_singleton', $object->dependency);
00247     }
00248 
00249     /**
00250      * @test
00251      */
00252     public function prototypeWhichRequiresPrototypeViaConstructorInjectionWorks() {
00253         $this->container->expects($this->never())->method('log');
00254 
00255         $object = $this->container->getInstance('t3lib_object_prototypeNeedsPrototypeInConstructor');
00256         $this->assertType('t3lib_object_prototype', $object->dependency);
00257     }
00258 
00259     /**
00260      * @test
00261      */
00262     public function prototypeWhichRequiresSingletonViaConstructorInjectionWorks() {
00263         $this->container->expects($this->never())->method('log');
00264 
00265         $object = $this->container->getInstance('t3lib_object_prototypeNeedsSingletonInConstructor');
00266         $this->assertType('t3lib_object_singleton', $object->dependency);
00267     }
00268 }
00269 ?>