804bd9bf99e91596ae916e3ef45522cb14a3a212
[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 = Tx_Extbase_Object_Container_Container::getContainer();
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');
85 }
86
87 /**
88 * @test
89 */
90 public function getInstancePassesGivenParameterToTheNewObject() {
91 $mockObject = $this->getMock('t3lib_object_tests_c');
92
93 $object = $this->container->getInstance('t3lib_object_tests_a', $mockObject);
94 $this->assertType('t3lib_object_tests_a', $object);
95 $this->assertSame($mockObject, $object->c);
96 }
97
98 /**
99 * @test
100 */
101 public function getInstanceReturnsAFreshInstanceIfObjectIsNoSingleton() {
102 $object1 = $this->container->getInstance('t3lib_object_tests_a');
103 $object2 = $this->container->getInstance('t3lib_object_tests_a');
104
105 $this->assertNotSame($object1, $object2);
106 }
107
108 /**
109 * @test
110 */
111 public function getInstanceReturnsSameInstanceInstanceIfObjectIsSingleton() {
112 $object1 = $this->container->getInstance('t3lib_object_tests_singleton');
113 $object2 = $this->container->getInstance('t3lib_object_tests_singleton');
114
115 $this->assertSame($object1, $object2);
116 }
117
118 /**
119 * @test
120 * @expectedException Tx_Extbase_Object_Exception
121 */
122 public function getInstanceThrowsExceptionIfObjectContainsCyclicDependency() {
123 $this->container->getInstance('t3lib_object_tests_cyclic1');
124
125 }
126
127 /**
128 * @test
129 * @expectedException Tx_Extbase_Object_Exception
130 */
131 public function getInstanceThrowsExceptionIfClassWasNotFound() {
132 $this->container->getInstance('nonextistingclass_bla');
133
134 }
135
136 /**
137 * @test
138 */
139 public function test_canGetChildClass() {
140 $object = $this->container->getInstance('t3lib_object_tests_b_child');
141 $this->assertType('t3lib_object_tests_b_child', $object);
142 }
143
144 /**
145 * @test
146 */
147 public function test_canInjectInterfaceInClass() {
148 $this->container->registerImplementation('t3lib_object_tests_someinterface', 't3lib_object_tests_someimplementation');
149 $object = $this->container->getInstance('t3lib_object_tests_needsinterface');
150 $this->assertType('t3lib_object_tests_needsinterface', $object);
151 }
152
153 /**
154 * @test
155 */
156 public function test_canBuildCyclicDependenciesWithSetter() {
157 $object = $this->container->getInstance('t3lib_object_tests_resolveablecyclic1');
158 $this->assertType('t3lib_object_tests_resolveablecyclic1', $object);
159 $this->assertType('t3lib_object_tests_resolveablecyclic1', $object->o2->o3->o1);
160 }
161
162
163
164 }
165
166
167 ?>