[!!!][+TASK] Extbase (Object): Rewritten Object Container
[Packages/TYPO3.CMS.git] / typo3 / sysext / extbase / Tests / Unit / Object / Container / ContainerTest.php
1 <?php
2 /***************************************************************
3 * Copyright notice
4 * (c) 2010 Daniel Pötzinger
5 * (c) 2010 Bastian Waidelich <bastian@typo3.org>
6 * All rights reserved
7 *
8 * This script is part of the TYPO3 project. The TYPO3 project is
9 * free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * The GNU General Public License can be found at
15 * http://www.gnu.org/copyleft/gpl.html.
16 *
17 * This script is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
21 *
22 * This copyright notice MUST APPEAR in all copies of the script!
23 ***************************************************************/
24
25 require_once(t3lib_extMgm::extPath('extbase') . 'Tests/Unit/Object/Container/Fixtures/Testclasses.php');
26
27 /**
28 * Testcase for class t3lib_object_Container.
29 *
30 * @author Daniel Pötzinger
31 * @author Bastian Waidelich <bastian@typo3.org>
32 * @package TYPO3
33 * @subpackage t3lib
34 */
35 class Tx_Extbase_Object_Container_ContainerTest extends Tx_Extbase_Tests_Unit_BaseTestCase {
36
37 private $container;
38
39 public function setUp() {
40 $this->container = new Tx_Extbase_Object_Container_Container();
41
42 }
43
44 /**
45 * @test
46 */
47 public function getInstanceReturnsInstanceOfSimpleClass() {
48 $object = $this->container->getInstance('t3lib_object_tests_c');
49 $this->assertType('t3lib_object_tests_c', $object);
50 }
51
52 /**
53 * @test
54 */
55 public function getInstanceReturnsInstanceOfAClassWithConstructorInjection() {
56 $object = $this->container->getInstance('t3lib_object_tests_b');
57 $this->assertType('t3lib_object_tests_b', $object);
58 $this->assertType('t3lib_object_tests_c', $object->c);
59 }
60
61 /**
62 * @test
63 */
64 public function getInstanceReturnsInstanceOfAClassWithTwoLevelDependency() {
65 $object = $this->container->getInstance('t3lib_object_tests_a');
66 $this->assertType('t3lib_object_tests_a', $object);
67 $this->assertType('t3lib_object_tests_c', $object->b->c);
68 }
69
70 /**
71 * @test
72 */
73 public function getInstanceReturnsInstanceOfAClassWithMixedSimpleTypeAndConstructorInjection() {
74 $object = $this->container->getInstance('t3lib_object_tests_amixed_array');
75 $this->assertType('t3lib_object_tests_amixed_array', $object);
76 $this->assertEquals(array('some' => 'default'), $object->myvalue);
77 }
78
79 /**
80 * @test
81 * @expectedException Tx_Extbase_Object_Exception
82 */
83 public function getInstanceThrowsExceptionWhenTryingToInstanciateASingletonWithConstructorParameters() {
84 $this->container->getInstance('t3lib_object_tests_amixed_array_singleton', array('somevalue'));
85 }
86
87 /**
88 * @test
89 */
90 public function getInstanceReturnsInstanceOfAClassWithConstructorInjectionAndDefaultConstructorParameters() {
91 $object = $this->container->getInstance('t3lib_object_tests_amixed_array');
92 $this->assertType('t3lib_object_tests_b', $object->b);
93 $this->assertType('t3lib_object_tests_c', $object->c);
94 $this->assertEquals(array('some' => 'default'), $object->myvalue);
95 }
96
97 /**
98 * @test
99 */
100 public function getInstancePassesGivenParameterToTheNewObject() {
101 $mockObject = $this->getMock('t3lib_object_tests_c');
102
103 $object = $this->container->getInstance('t3lib_object_tests_a', array($mockObject));
104 $this->assertType('t3lib_object_tests_a', $object);
105 $this->assertSame($mockObject, $object->c);
106 }
107
108 /**
109 * @test
110 */
111 public function getInstanceReturnsAFreshInstanceIfObjectIsNoSingleton() {
112 $object1 = $this->container->getInstance('t3lib_object_tests_a');
113 $object2 = $this->container->getInstance('t3lib_object_tests_a');
114
115 $this->assertNotSame($object1, $object2);
116 }
117
118 /**
119 * @test
120 */
121 public function getInstanceReturnsSameInstanceInstanceIfObjectIsSingleton() {
122 $object1 = $this->container->getInstance('t3lib_object_tests_singleton');
123 $object2 = $this->container->getInstance('t3lib_object_tests_singleton');
124
125 $this->assertSame($object1, $object2);
126 }
127
128 /**
129 * @test
130 * @expectedException Tx_Extbase_Object_Exception
131 */
132 public function getInstanceThrowsExceptionIfObjectContainsCyclicDependencyAndIsNoSingleton() {
133 $this->container->getInstance('t3lib_object_tests_cyclic1');
134 }
135
136
137 /**
138 * @test
139 * @expectedException Tx_Extbase_Object_Exception
140 */
141 public function getInstanceThrowsExceptionIfObjectContainsCyclicDependency() {
142 $this->container->getInstance('t3lib_object_tests_cyclic1');
143 }
144
145 /**
146 * @test
147 * @expectedException Tx_Extbase_Object_Exception
148 */
149 public function getInstanceThrowsExceptionIfClassWasNotFound() {
150 $this->container->getInstance('nonextistingclass_bla');
151
152 }
153
154 /**
155 * @test
156 */
157 public function test_canGetChildClass() {
158 $object = $this->container->getInstance('t3lib_object_tests_b_child');
159 $this->assertType('t3lib_object_tests_b_child', $object);
160 }
161
162 /**
163 * @test
164 */
165 public function test_canInjectInterfaceInClass() {
166 $this->container->registerImplementation('t3lib_object_tests_someinterface', 't3lib_object_tests_someimplementation');
167 $object = $this->container->getInstance('t3lib_object_tests_needsinterface');
168 $this->assertType('t3lib_object_tests_needsinterface', $object);
169 }
170
171 /**
172 * @test
173 */
174 public function test_canBuildCyclicDependenciesOfSingletonsWithSetter() {
175 $object = $this->container->getInstance('t3lib_object_tests_resolveablecyclic1');
176 $this->assertType('t3lib_object_tests_resolveablecyclic1', $object);
177 $this->assertType('t3lib_object_tests_resolveablecyclic1', $object->o2->o3->o1);
178 }
179
180
181
182 }
183
184
185 ?>