[BUGFIX] Omit constructor injection for optional parameters
[Packages/TYPO3.CMS.git] / typo3 / sysext / extbase / Tests / Unit / Object / Container / ContainerTest.php
1 <?php
2 namespace TYPO3\CMS\Extbase\Tests\Unit\Object\Container;
3
4 /***************************************************************
5 * Copyright notice
6 * (c) 2010 Daniel Pötzinger
7 * (c) 2010 Bastian Waidelich <bastian@typo3.org>
8 * All rights reserved
9 *
10 * This script is part of the TYPO3 project. The TYPO3 project is
11 * free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * The GNU General Public License can be found at
17 * http://www.gnu.org/copyleft/gpl.html.
18 *
19 * This script is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
23 *
24 * This copyright notice MUST APPEAR in all copies of the script!
25 ***************************************************************/
26
27 require_once 'Fixtures/Testclasses.php';
28 require_once 'Fixtures/ContainerConstructorInjectionTestFixtures.php';
29
30 /**
31 * Testcase for class t3lib_object_Container.
32 *
33 * @author Daniel Pötzinger
34 * @author Bastian Waidelich <bastian@typo3.org>
35 */
36 class ContainerTest extends \TYPO3\CMS\Extbase\Tests\Unit\BaseTestCase {
37
38 /**
39 * @var \TYPO3\CMS\Extbase\Object\Container\Container
40 */
41 private $container;
42
43 /**
44 * @var \TYPO3\CMS\Extbase\Object\Container\ClassInfo
45 */
46 private $cachedClassInfo;
47
48 public function setUp() {
49 //our mocked cache will allways indicate that he has nothing in the cache to force that we get the real classinfo
50 $mockedCache = $this->getMock('TYPO3\\CMS\\Extbase\\Object\\Container\\ClassInfoCache', array('get'));
51 $mockedCache->expects($this->any())->method('get')->will($this->returnValue(FALSE));
52 $mockedCache->expects($this->never())->method('has');
53 $this->container = $this->getMock('TYPO3\\CMS\\Extbase\\Object\\Container\\Container', array('log', 'getClassInfoCache'));
54 $this->container->expects($this->any())->method('getClassInfoCache')->will($this->returnValue($mockedCache));
55 }
56
57 /**
58 * @test
59 */
60 public function getInstanceReturnsInstanceOfSimpleClass() {
61 $object = $this->container->getInstance('t3lib_object_tests_c');
62 $this->assertInstanceOf('t3lib_object_tests_c', $object);
63 }
64
65 /**
66 * @test
67 */
68 public function getInstanceReturnsInstanceOfSimpleNamespacedClass() {
69 $object = $this->container->getInstance('TYPO3\\CMS\\Extbase\\Tests\\Unit\\Object\\Container\\Fixtures\\NamespacedClass');
70 $this->assertInstanceOf('TYPO3\\CMS\\Extbase\\Tests\\Unit\\Object\\Container\\Fixtures\\NamespacedClass', $object);
71 }
72
73 /**
74 * @test
75 */
76 public function getInstanceReturnsInstanceOfAClassWithConstructorInjection() {
77 $object = $this->container->getInstance('t3lib_object_tests_b');
78 $this->assertInstanceOf('t3lib_object_tests_b', $object);
79 $this->assertInstanceOf('t3lib_object_tests_c', $object->c);
80 }
81
82 /**
83 * @test
84 */
85 public function getInstanceReturnsInstanceOfAClassWithTwoLevelDependency() {
86 $object = $this->container->getInstance('t3lib_object_tests_a');
87 $this->assertInstanceOf('t3lib_object_tests_a', $object);
88 $this->assertInstanceOf('t3lib_object_tests_c', $object->b->c);
89 }
90
91 /**
92 * @test
93 */
94 public function getInstanceReturnsInstanceOfAClassWithMixedSimpleTypeAndConstructorInjection() {
95 $object = $this->container->getInstance('t3lib_object_tests_amixed_array');
96 $this->assertInstanceOf('t3lib_object_tests_amixed_array', $object);
97 $this->assertEquals(array('some' => 'default'), $object->myvalue);
98 }
99
100 /**
101 * @test
102 */
103 public function getInstanceReturnsInstanceOfAClassWithMixedSimpleTypeAndConstructorInjectionWithNullDefaultValue() {
104 $object = $this->container->getInstance('t3lib_object_tests_amixed_null');
105 $this->assertInstanceOf('t3lib_object_tests_amixed_null', $object);
106 $this->assertNull($object->myvalue);
107 }
108
109 /**
110 * @test
111 * @expectedException \TYPO3\CMS\Extbase\Object\Exception
112 */
113 public function getInstanceThrowsExceptionWhenTryingToInstanciateASingletonWithConstructorParameters() {
114 $this->container->getInstance('t3lib_object_tests_amixed_array_singleton', array('somevalue'));
115 }
116
117 /**
118 * @test
119 */
120 public function getInstanceReturnsInstanceOfAClassWithConstructorInjectionAndDefaultConstructorParameters() {
121 $object = $this->container->getInstance('t3lib_object_tests_amixed_array');
122 $this->assertInstanceOf('t3lib_object_tests_b', $object->b);
123 $this->assertInstanceOf('t3lib_object_tests_c', $object->c);
124 $this->assertEquals(array('some' => 'default'), $object->myvalue);
125 }
126
127 /**
128 * @test
129 */
130 public function getInstancePassesGivenParameterToTheNewObject() {
131 $mockObject = $this->getMock('t3lib_object_tests_c');
132 $object = $this->container->getInstance('t3lib_object_tests_a', array($mockObject));
133 $this->assertInstanceOf('t3lib_object_tests_a', $object);
134 $this->assertSame($mockObject, $object->c);
135 }
136
137 /**
138 * @test
139 */
140 public function getInstanceReturnsAFreshInstanceIfObjectIsNoSingleton() {
141 $object1 = $this->container->getInstance('t3lib_object_tests_a');
142 $object2 = $this->container->getInstance('t3lib_object_tests_a');
143 $this->assertNotSame($object1, $object2);
144 }
145
146 /**
147 * @test
148 */
149 public function getInstanceReturnsSameInstanceInstanceIfObjectIsSingleton() {
150 $object1 = $this->container->getInstance('t3lib_object_tests_singleton');
151 $object2 = $this->container->getInstance('t3lib_object_tests_singleton');
152 $this->assertSame($object1, $object2);
153 }
154
155 /**
156 * @test
157 * @expectedException \TYPO3\CMS\Extbase\Object\Exception\CannotBuildObjectException
158 */
159 public function getInstanceThrowsExceptionIfPrototypeObjectsWiredViaConstructorInjectionContainCyclicDependencies() {
160 $this->container->getInstance('t3lib_object_tests_cyclic1WithSetterDependency');
161 }
162
163 /**
164 * @test
165 * @expectedException \TYPO3\CMS\Extbase\Object\Exception\CannotBuildObjectException
166 */
167 public function getInstanceThrowsExceptionIfPrototypeObjectsWiredViaSetterInjectionContainCyclicDependencies() {
168 $this->container->getInstance('t3lib_object_tests_cyclic1');
169 }
170
171 /**
172 * @test
173 * @expectedException \TYPO3\CMS\Extbase\Object\Exception
174 */
175 public function getInstanceThrowsExceptionIfClassWasNotFound() {
176 $this->container->getInstance('nonextistingclass_bla');
177 }
178
179 /**
180 * @test
181 */
182 public function getInstanceUsesClassNameMd5AsCacheKey() {
183 $className = 'TYPO3\\CMS\\Extbase\\Tests\\Unit\\Object\\Container\\Fixtures\\NamespacedClass';
184 $classNameHash = md5($className);
185 $mockedCache = $this->getMock('TYPO3\\CMS\\Extbase\\Object\\Container\\ClassInfoCache', array('has', 'set', 'get'));
186 $container = $this->getMock('TYPO3\\CMS\\Extbase\\Object\\Container\\Container', array('log', 'getClassInfoCache'));
187 $container->expects($this->any())->method('getClassInfoCache')->will($this->returnValue($mockedCache));
188 $mockedCache->expects($this->never())->method('has');
189 $mockedCache->expects($this->once())->method('get')->with($classNameHash)->will($this->returnValue(FALSE));
190 $mockedCache->expects($this->once())->method('set')->with($classNameHash, $this->anything())->will($this->returnCallback(array($this, 'setClassInfoCacheCallback')));
191 $container->getInstance($className);
192 $this->assertInstanceOf('TYPO3\\CMS\\Extbase\\Object\\Container\\ClassInfo', $this->cachedClassInfo);
193 $this->assertEquals($className, $this->cachedClassInfo->getClassName());
194 }
195
196 /**
197 * Callback for getInstanceUsesClassNameSha1AsCacheKey
198 *
199 * @param string $id
200 * @param \TYPO3\CMS\Extbase\Object\Container\ClassInfo $value
201 * @return void
202 */
203 public function setClassInfoCacheCallback($id, \TYPO3\CMS\Extbase\Object\Container\ClassInfo $value) {
204 $this->cachedClassInfo = $value;
205 }
206
207 /**
208 * @test
209 */
210 public function getEmptyObjectReturnsInstanceOfSimpleClass() {
211 $object = $this->container->getEmptyObject('t3lib_object_tests_c');
212 $this->assertInstanceOf('t3lib_object_tests_c', $object);
213 }
214
215 /**
216 * @test
217 */
218 public function test_canGetChildClass() {
219 $object = $this->container->getInstance('t3lib_object_tests_b_child');
220 $this->assertInstanceOf('t3lib_object_tests_b_child', $object);
221 }
222
223 /**
224 * @test
225 */
226 public function test_canInjectInterfaceInClass() {
227 $this->container->registerImplementation('t3lib_object_tests_someinterface', 't3lib_object_tests_someimplementation');
228 $object = $this->container->getInstance('t3lib_object_tests_needsinterface');
229 $this->assertInstanceOf('t3lib_object_tests_needsinterface', $object);
230 $this->assertInstanceOf('t3lib_object_tests_someinterface', $object->dependency);
231 $this->assertInstanceOf('t3lib_object_tests_someimplementation', $object->dependency);
232 }
233
234 /**
235 * @test
236 */
237 public function test_canBuildCyclicDependenciesOfSingletonsWithSetter() {
238 $object = $this->container->getInstance('t3lib_object_tests_resolveablecyclic1');
239 $this->assertInstanceOf('t3lib_object_tests_resolveablecyclic1', $object);
240 $this->assertInstanceOf('t3lib_object_tests_resolveablecyclic1', $object->o2->o3->o1);
241 }
242
243 /**
244 * @test
245 */
246 public function singletonWhichRequiresPrototypeViaSetterInjectionWorksAndAddsDebugMessage() {
247 $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);
248 $object = $this->container->getInstance('t3lib_object_singletonNeedsPrototype');
249 $this->assertInstanceOf('t3lib_object_prototype', $object->dependency);
250 }
251
252 /**
253 * @test
254 */
255 public function singletonWhichRequiresSingletonViaSetterInjectionWorks() {
256 $this->container->expects($this->never())->method('log');
257 $object = $this->container->getInstance('t3lib_object_singletonNeedsSingleton');
258 $this->assertInstanceOf('t3lib_object_singleton', $object->dependency);
259 }
260
261 /**
262 * @test
263 */
264 public function prototypeWhichRequiresPrototypeViaSetterInjectionWorks() {
265 $this->container->expects($this->never())->method('log');
266 $object = $this->container->getInstance('t3lib_object_prototypeNeedsPrototype');
267 $this->assertInstanceOf('t3lib_object_prototype', $object->dependency);
268 }
269
270 /**
271 * @test
272 */
273 public function prototypeWhichRequiresSingletonViaSetterInjectionWorks() {
274 $this->container->expects($this->never())->method('log');
275 $object = $this->container->getInstance('t3lib_object_prototypeNeedsSingleton');
276 $this->assertInstanceOf('t3lib_object_singleton', $object->dependency);
277 }
278
279 /**
280 * @test
281 */
282 public function singletonWhichRequiresPrototypeViaConstructorInjectionWorksAndAddsDebugMessage() {
283 $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);
284 $object = $this->container->getInstance('t3lib_object_singletonNeedsPrototypeInConstructor');
285 $this->assertInstanceOf('t3lib_object_prototype', $object->dependency);
286 }
287
288 /**
289 * @test
290 */
291 public function singletonWhichRequiresSingletonViaConstructorInjectionWorks() {
292 $this->container->expects($this->never())->method('log');
293 $object = $this->container->getInstance('t3lib_object_singletonNeedsSingletonInConstructor');
294 $this->assertInstanceOf('t3lib_object_singleton', $object->dependency);
295 }
296
297 /**
298 * @test
299 */
300 public function prototypeWhichRequiresPrototypeViaConstructorInjectionWorks() {
301 $this->container->expects($this->never())->method('log');
302 $object = $this->container->getInstance('t3lib_object_prototypeNeedsPrototypeInConstructor');
303 $this->assertInstanceOf('t3lib_object_prototype', $object->dependency);
304 }
305
306 /**
307 * @test
308 */
309 public function prototypeWhichRequiresSingletonViaConstructorInjectionWorks() {
310 $this->container->expects($this->never())->method('log');
311 $object = $this->container->getInstance('t3lib_object_prototypeNeedsSingletonInConstructor');
312 $this->assertInstanceOf('t3lib_object_singleton', $object->dependency);
313 }
314
315 /**
316 * @test
317 */
318 public function isSingletonReturnsTrueForSingletonInstancesAndFalseForPrototypes() {
319 $this->assertTrue($this->container->isSingleton('TYPO3\CMS\Extbase\Object\Container\Container'));
320 $this->assertFalse($this->container->isSingleton('TYPO3\CMS\Extbase\Core\Bootstrap'));
321 }
322
323 /**
324 * @test
325 */
326 public function isPrototypeReturnsFalseForSingletonInstancesAndTrueForPrototypes() {
327 $this->assertFalse($this->container->isPrototype('TYPO3\CMS\Extbase\Object\Container\Container'));
328 $this->assertTrue($this->container->isPrototype('TYPO3\CMS\Extbase\Core\Bootstrap'));
329 }
330
331 /************************************************
332 * Test regarding constructor argument injection
333 ************************************************/
334
335 /**
336 * test class SimpleTypeConstructorArgument
337 * @test
338 */
339 public function getInstanceGivesSimpleConstructorArgumentToClassInstance() {
340 $object = $this->container->getInstance(
341 'TYPO3\\CMS\\Extbase\\Tests\\Unit\\Object\\Container\\Fixtures\\SimpleTypeConstructorArgument',
342 array(TRUE)
343 );
344 $this->assertTrue($object->foo);
345 }
346
347 /**
348 * test class SimpleTypeConstructorArgument
349 * @test
350 */
351 public function getInstanceDoesNotInfluenceSimpleTypeConstructorArgumentIfNotGiven() {
352 $object = $this->container->getInstance(
353 'TYPO3\\CMS\\Extbase\\Tests\\Unit\\Object\\Container\\Fixtures\\SimpleTypeConstructorArgument'
354 );
355 $this->assertFalse($object->foo);
356 }
357
358 /**
359 * test class MandatoryConstructorArgument
360 * @test
361 */
362 public function getInstanceGivesExistingConstructorArgumentToClassInstance() {
363 $argumentTestClass = new Fixtures\ArgumentTestClass();
364 $object = $this->container->getInstance(
365 'TYPO3\\CMS\\Extbase\\Tests\\Unit\\Object\\Container\\Fixtures\\MandatoryConstructorArgument',
366 array($argumentTestClass)
367 );
368 $this->assertInstanceOf(
369 'TYPO3\\CMS\\Extbase\\Tests\\Unit\\Object\\Container\\Fixtures\\MandatoryConstructorArgument',
370 $object
371 );
372 $this->assertSame($argumentTestClass, $object->argumentTestClass);
373 }
374
375 /**
376 * test class MandatoryConstructorArgument
377 * @test
378 */
379 public function getInstanceInjectsNewInstanceOfClassToClassIfArgumentIsMandatory() {
380 $object = $this->container->getInstance(
381 'TYPO3\\CMS\\Extbase\\Tests\\Unit\\Object\\Container\\Fixtures\\MandatoryConstructorArgument'
382 );
383 $this->assertInstanceOf(
384 'TYPO3\\CMS\\Extbase\\Tests\\Unit\\Object\\Container\\Fixtures\\MandatoryConstructorArgument',
385 $object
386 );
387 $this->assertInstanceOf(
388 'TYPO3\\CMS\\Extbase\\Tests\\Unit\\Object\\Container\\Fixtures\\ArgumentTestClass',
389 $object->argumentTestClass
390 );
391 }
392
393 /**
394 * test class OptionalConstructorArgument
395 * @test
396 */
397 public function getInstanceDoesNotInjectAnOptionalArgumentIfNotGiven() {
398 $object = $this->container->getInstance(
399 'TYPO3\\CMS\\Extbase\\Tests\\Unit\\Object\\Container\\Fixtures\\OptionalConstructorArgument'
400 );
401 $this->assertInstanceOf(
402 'TYPO3\\CMS\\Extbase\\Tests\\Unit\\Object\\Container\\Fixtures\\OptionalConstructorArgument',
403 $object
404 );
405 $this->assertNull($object->argumentTestClass);
406 }
407
408 /**
409 * test class OptionalConstructorArgument
410 * @test
411 */
412 public function getInstanceDoesNotInjectAnOptionalArgumentIfGivenArgumentIsNull() {
413 $object = $this->container->getInstance(
414 'TYPO3\\CMS\\Extbase\\Tests\\Unit\\Object\\Container\\Fixtures\\OptionalConstructorArgument',
415 array(NULL)
416 );
417 $this->assertInstanceOf(
418 'TYPO3\\CMS\\Extbase\\Tests\\Unit\\Object\\Container\\Fixtures\\OptionalConstructorArgument',
419 $object
420 );
421 $this->assertNull($object->argumentTestClass);
422 }
423
424 /**
425 * test class OptionalConstructorArgument
426 * @test
427 */
428 public function getInstanceGivesExistingConstructorArgumentToClassInstanceIfArgumentIsGiven() {
429 $argumentTestClass = new Fixtures\ArgumentTestClass();
430 $object = $this->container->getInstance(
431 'TYPO3\\CMS\\Extbase\\Tests\\Unit\\Object\\Container\\Fixtures\\OptionalConstructorArgument',
432 array($argumentTestClass)
433 );
434 $this->assertInstanceOf(
435 'TYPO3\\CMS\\Extbase\\Tests\\Unit\\Object\\Container\\Fixtures\\OptionalConstructorArgument',
436 $object
437 );
438 $this->assertSame($argumentTestClass, $object->argumentTestClass);
439 }
440
441 /**
442 * test class MandatoryConstructorArgumentTwo
443 * @test
444 */
445 public function getInstanceGivesTwoArgumentsToClassConstructor() {
446 $firstArgument = new Fixtures\ArgumentTestClass();
447 $secondArgument = new Fixtures\ArgumentTestClass();
448 $object = $this->container->getInstance(
449 'TYPO3\\CMS\\Extbase\\Tests\\Unit\\Object\\Container\\Fixtures\\MandatoryConstructorArgumentTwo',
450 array($firstArgument, $secondArgument)
451 );
452 $this->assertInstanceOf(
453 'TYPO3\\CMS\\Extbase\\Tests\\Unit\\Object\\Container\\Fixtures\\MandatoryConstructorArgumentTwo',
454 $object
455 );
456 $this->assertSame(
457 $firstArgument,
458 $object->argumentTestClass
459 );
460 $this->assertSame(
461 $secondArgument,
462 $object->argumentTestClassTwo
463 );
464 }
465
466 /**
467 * test class MandatoryConstructorArgumentTwo
468 * @test
469 */
470 public function getInstanceInjectsTwoMandatoryArguments() {
471 $object = $this->container->getInstance(
472 'TYPO3\\CMS\\Extbase\\Tests\\Unit\\Object\\Container\\Fixtures\\MandatoryConstructorArgumentTwo'
473 );
474 $this->assertInstanceOf(
475 'TYPO3\\CMS\\Extbase\\Tests\\Unit\\Object\\Container\\Fixtures\\MandatoryConstructorArgumentTwo',
476 $object
477 );
478 $this->assertInstanceOf(
479 'TYPO3\\CMS\\Extbase\\Tests\\Unit\\Object\\Container\\Fixtures\\ArgumentTestClass',
480 $object->argumentTestClass
481 );
482 $this->assertInstanceOf(
483 'TYPO3\\CMS\\Extbase\\Tests\\Unit\\Object\\Container\\Fixtures\\ArgumentTestClass',
484 $object->argumentTestClassTwo
485 );
486 $this->assertNotSame(
487 $object->argumentTestClass,
488 $object->argumentTestClassTwo
489 );
490 }
491
492 /**
493 * test class MandatoryConstructorArgumentTwo
494 * @test
495 */
496 public function getInstanceInjectsSecondMandatoryArgumentIfFirstIsGiven() {
497 $firstArgument = new Fixtures\ArgumentTestClass();
498 $object = $this->container->getInstance(
499 'TYPO3\\CMS\\Extbase\\Tests\\Unit\\Object\\Container\\Fixtures\\MandatoryConstructorArgumentTwo',
500 array($firstArgument)
501 );
502 $this->assertInstanceOf(
503 'TYPO3\\CMS\\Extbase\\Tests\\Unit\\Object\\Container\\Fixtures\\MandatoryConstructorArgumentTwo',
504 $object
505 );
506 $this->assertSame(
507 $firstArgument,
508 $object->argumentTestClass
509 );
510 $this->assertInstanceOf(
511 'TYPO3\\CMS\\Extbase\\Tests\\Unit\\Object\\Container\\Fixtures\\ArgumentTestClass',
512 $object->argumentTestClassTwo
513 );
514 $this->assertNotSame(
515 $object->argumentTestClass,
516 $object->argumentTestClassTwo
517 );
518 }
519
520 /**
521 * test class MandatoryConstructorArgumentTwo
522 * @test
523 */
524 public function getInstanceInjectsFirstMandatoryArgumentIfSecondIsGiven() {
525 $secondArgument = new Fixtures\ArgumentTestClass();
526 $object = $this->container->getInstance(
527 'TYPO3\\CMS\\Extbase\\Tests\\Unit\\Object\\Container\\Fixtures\\MandatoryConstructorArgumentTwo',
528 array(NULL, $secondArgument)
529 );
530 $this->assertInstanceOf(
531 'TYPO3\\CMS\\Extbase\\Tests\\Unit\\Object\\Container\\Fixtures\\MandatoryConstructorArgumentTwo',
532 $object
533 );
534 $this->assertInstanceOf(
535 'TYPO3\\CMS\\Extbase\\Tests\\Unit\\Object\\Container\\Fixtures\\ArgumentTestClass',
536 $object->argumentTestClass
537 );
538 $this->assertInstanceOf(
539 'TYPO3\\CMS\\Extbase\\Tests\\Unit\\Object\\Container\\Fixtures\\ArgumentTestClass',
540 $object->argumentTestClassTwo
541 );
542 $this->assertSame(
543 $secondArgument,
544 $object->argumentTestClassTwo
545 );
546 $this->assertNotSame(
547 $object->argumentTestClass,
548 $object->argumentTestClassTwo
549 );
550 }
551
552 /**
553 * test class TwoConstructorArgumentsSecondOptional
554 * @test
555 */
556 public function getInstanceGivesTwoArgumentsToClassConstructorIfSecondIsOptional() {
557 $firstArgument = new Fixtures\ArgumentTestClass();
558 $secondArgument = new Fixtures\ArgumentTestClass();
559 $object = $this->container->getInstance(
560 'TYPO3\\CMS\\Extbase\\Tests\\Unit\\Object\\Container\\Fixtures\\TwoConstructorArgumentsSecondOptional',
561 array($firstArgument, $secondArgument)
562 );
563 $this->assertInstanceOf(
564 'TYPO3\\CMS\\Extbase\\Tests\\Unit\\Object\\Container\\Fixtures\\TwoConstructorArgumentsSecondOptional',
565 $object
566 );
567 $this->assertSame(
568 $firstArgument,
569 $object->argumentTestClass
570 );
571 $this->assertSame(
572 $secondArgument,
573 $object->argumentTestClassTwo
574 );
575 }
576
577 /**
578 * test class TwoConstructorArgumentsSecondOptional
579 * @test
580 */
581 public function getInstanceInjectsFirstMandatoryArgumentIfSecondIsOptionalAndNoneAreGiven() {
582 $object = $this->container->getInstance(
583 'TYPO3\\CMS\\Extbase\\Tests\\Unit\\Object\\Container\\Fixtures\\TwoConstructorArgumentsSecondOptional'
584 );
585 $this->assertInstanceOf(
586 'TYPO3\\CMS\\Extbase\\Tests\\Unit\\Object\\Container\\Fixtures\\TwoConstructorArgumentsSecondOptional',
587 $object
588 );
589 $this->assertInstanceOf(
590 'TYPO3\\CMS\\Extbase\\Tests\\Unit\\Object\\Container\\Fixtures\\ArgumentTestClass',
591 $object->argumentTestClass
592 );
593 $this->assertNull($object->argumentTestClassTwo);
594 }
595
596 /**
597 * test class TwoConstructorArgumentsSecondOptional
598 * @test
599 */
600 public function getInstanceInjectsFirstMandatoryArgumentIfSecondIsOptionalAndBothAreGivenAsNull() {
601 $object = $this->container->getInstance(
602 'TYPO3\\CMS\\Extbase\\Tests\\Unit\\Object\\Container\\Fixtures\\TwoConstructorArgumentsSecondOptional',
603 array(NULL, NULL)
604 );
605 $this->assertInstanceOf(
606 'TYPO3\\CMS\\Extbase\\Tests\\Unit\\Object\\Container\\Fixtures\\TwoConstructorArgumentsSecondOptional',
607 $object
608 );
609 $this->assertInstanceOf(
610 'TYPO3\\CMS\\Extbase\\Tests\\Unit\\Object\\Container\\Fixtures\\ArgumentTestClass',
611 $object->argumentTestClass
612 );
613 $this->assertNull($object->argumentTestClassTwo);
614 }
615
616 /**
617 * test class TwoConstructorArgumentsSecondOptional
618 * @test
619 */
620 public function getInstanceGivesFirstArgumentToConstructorIfSecondIsOptionalAndFirstIsGiven() {
621 $firstArgument = new Fixtures\ArgumentTestClass();
622 $object = $this->container->getInstance(
623 'TYPO3\\CMS\\Extbase\\Tests\\Unit\\Object\\Container\\Fixtures\\TwoConstructorArgumentsSecondOptional',
624 array($firstArgument)
625 );
626 $this->assertInstanceOf(
627 'TYPO3\\CMS\\Extbase\\Tests\\Unit\\Object\\Container\\Fixtures\\TwoConstructorArgumentsSecondOptional',
628 $object
629 );
630 $this->assertSame(
631 $firstArgument,
632 $object->argumentTestClass
633 );
634 $this->assertNull($object->argumentTestClassTwo);
635 }
636
637 /**
638 * test class TwoConstructorArgumentsSecondOptional
639 * @test
640 */
641 public function getInstanceGivesFirstArgumentToConstructorIfSecondIsOptionalFirstIsGivenAndSecondIsGivenNull() {
642 $firstArgument = new Fixtures\ArgumentTestClass();
643 $object = $this->container->getInstance(
644 'TYPO3\\CMS\\Extbase\\Tests\\Unit\\Object\\Container\\Fixtures\\TwoConstructorArgumentsSecondOptional',
645 array($firstArgument, NULL)
646 );
647 $this->assertInstanceOf(
648 'TYPO3\\CMS\\Extbase\\Tests\\Unit\\Object\\Container\\Fixtures\\TwoConstructorArgumentsSecondOptional',
649 $object
650 );
651 $this->assertSame(
652 $firstArgument,
653 $object->argumentTestClass
654 );
655 $this->assertNull($object->argumentTestClassTwo);
656 }
657
658 /**
659 * test class TwoConstructorArgumentsFirstOptional
660 *
661 * @test
662 */
663 public function getInstanceOnFirstOptionalAndSecondMandatoryInjectsOnlySecondArgumentAndFirstArgumentIsNullIfNoArgumentsGiven() {
664 $object = $this->container->getInstance(
665 'TYPO3\\CMS\\Extbase\\Tests\\Unit\\Object\\Container\\Fixtures\\TwoConstructorArgumentsFirstOptional'
666 );
667 $this->assertInstanceOf(
668 'TYPO3\\CMS\\Extbase\\Tests\\Unit\\Object\\Container\\Fixtures\\TwoConstructorArgumentsFirstOptional',
669 $object
670 );
671 $this->assertNull($object->argumentTestClass);
672 $this->assertInstanceOf(
673 'TYPO3\\CMS\\Extbase\\Tests\\Unit\\Object\\Container\\Fixtures\\ArgumentTestClass',
674 $object->argumentTestClassTwo
675 );
676 }
677
678 /**
679 * test class TwoConstructorArgumentsFirstOptional
680 *
681 * @test
682 */
683 public function getInstanceOnFirstOptionalAndSecondMandatoryInjectsSecondArgumentIfFirstIsGivenAsNull() {
684 $object = $this->container->getInstance(
685 'TYPO3\\CMS\\Extbase\\Tests\\Unit\\Object\\Container\\Fixtures\\TwoConstructorArgumentsFirstOptional',
686 array(NULL)
687 );
688 $this->assertInstanceOf(
689 'TYPO3\\CMS\\Extbase\\Tests\\Unit\\Object\\Container\\Fixtures\\TwoConstructorArgumentsFirstOptional',
690 $object
691 );
692 $this->assertNull($object->argumentTestClass);
693 $this->assertInstanceOf(
694 'TYPO3\\CMS\\Extbase\\Tests\\Unit\\Object\\Container\\Fixtures\\ArgumentTestClass',
695 $object->argumentTestClassTwo
696 );
697 }
698
699 /**
700 * test class TwoConstructorArgumentsFirstOptional
701 * @test
702 */
703 public function getInstanceOnFirstOptionalAndSecondMandatoryGivesTwoGivenArgumentsToConstructor() {
704 $first = new Fixtures\ArgumentTestClass();
705 $second = new Fixtures\ArgumentTestClass();
706 $object = $this->container->getInstance(
707 'TYPO3\\CMS\\Extbase\\Tests\\Unit\\Object\\Container\\Fixtures\\TwoConstructorArgumentsFirstOptional',
708 array($first, $second)
709 );
710 $this->assertInstanceOf(
711 'TYPO3\\CMS\\Extbase\\Tests\\Unit\\Object\\Container\\Fixtures\\TwoConstructorArgumentsFirstOptional',
712 $object
713 );
714 $this->assertSame(
715 $first,
716 $object->argumentTestClass
717 );
718 $this->assertSame(
719 $second,
720 $object->argumentTestClassTwo
721 );
722 }
723
724 /**
725 * test class TwoConstructorArgumentsFirstOptional
726 * @test
727 */
728 public function getInstanceOnFirstOptionalAndSecondMandatoryInjectsSecondArgumentIfFirstIsGiven() {
729 $first = new Fixtures\ArgumentTestClass();
730 $object = $this->container->getInstance(
731 'TYPO3\\CMS\\Extbase\\Tests\\Unit\\Object\\Container\\Fixtures\\TwoConstructorArgumentsFirstOptional',
732 array($first)
733 );
734 $this->assertInstanceOf(
735 'TYPO3\\CMS\\Extbase\\Tests\\Unit\\Object\\Container\\Fixtures\\TwoConstructorArgumentsFirstOptional',
736 $object
737 );
738 $this->assertSame(
739 $first,
740 $object->argumentTestClass
741 );
742 $this->assertInstanceOf(
743 'TYPO3\\CMS\\Extbase\\Tests\\Unit\\Object\\Container\\Fixtures\\ArgumentTestClass',
744 $object->argumentTestClassTwo
745 );
746 $this->assertNotSame(
747 $object->argumentTestClass,
748 $object->argumentTestClassTwo
749 );
750 }
751
752 /**
753 * test class TwoConstructorArgumentsFirstOptional
754 *
755 * @test
756 */
757 public function getInstanceOnFirstOptionalAndSecondMandatoryGivesSecondArgumentAsIsIfFirstIsGivenAsNullAndSecondIsGiven() {
758 $second = new Fixtures\ArgumentTestClass();
759 $object = $this->container->getInstance(
760 'TYPO3\\CMS\\Extbase\\Tests\\Unit\\Object\\Container\\Fixtures\\TwoConstructorArgumentsFirstOptional',
761 array(NULL, $second)
762 );
763 $this->assertInstanceOf(
764 'TYPO3\\CMS\\Extbase\\Tests\\Unit\\Object\\Container\\Fixtures\\TwoConstructorArgumentsFirstOptional',
765 $object
766 );
767 $this->assertNull($object->argumentTestClass);
768 $this->assertSame(
769 $second,
770 $object->argumentTestClassTwo
771 );
772 }
773
774 /**
775 * test class TwoConstructorArgumentsFirstOptional
776 *
777 * @test
778 */
779 public function getInstanceOnFirstOptionalAndSecondMandatoryInjectsSecondArgumentIfFirstIsGivenAsNullAndSecondIsNull() {
780 $object = $this->container->getInstance(
781 'TYPO3\\CMS\\Extbase\\Tests\\Unit\\Object\\Container\\Fixtures\\TwoConstructorArgumentsFirstOptional',
782 array(NULL, NULL)
783 );
784 $this->assertInstanceOf(
785 'TYPO3\\CMS\\Extbase\\Tests\\Unit\\Object\\Container\\Fixtures\\TwoConstructorArgumentsFirstOptional',
786 $object
787 );
788 $this->assertNull($object->argumentTestClass);
789 $this->assertInstanceOf(
790 'TYPO3\\CMS\\Extbase\\Tests\\Unit\\Object\\Container\\Fixtures\\ArgumentTestClass',
791 $object->argumentTestClassTwo
792 );
793 }
794
795 /**
796 * test class TwoConstructorArgumentsBothOptional
797 * @test
798 */
799 public function getInstanceOnTwoOptionalGivesTwoGivenArgumentsToConstructor() {
800 $first = new Fixtures\ArgumentTestClass();
801 $second = new Fixtures\ArgumentTestClass();
802 $object = $this->container->getInstance(
803 'TYPO3\\CMS\\Extbase\\Tests\\Unit\\Object\\Container\\Fixtures\\TwoConstructorArgumentsBothOptional',
804 array($first, $second)
805 );
806 $this->assertInstanceOf(
807 'TYPO3\\CMS\\Extbase\\Tests\\Unit\\Object\\Container\\Fixtures\\TwoConstructorArgumentsBothOptional',
808 $object
809 );
810 $this->assertSame(
811 $first,
812 $object->argumentTestClass
813 );
814 $this->assertSame(
815 $second,
816 $object->argumentTestClassTwo
817 );
818 }
819
820 /**
821 * test class TwoConstructorArgumentsBothOptional
822 * @test
823 */
824 public function getInstanceOnTwoOptionalGivesNoArgumentsToConstructorIfArgumentsAreNull() {
825 $object = $this->container->getInstance(
826 'TYPO3\\CMS\\Extbase\\Tests\\Unit\\Object\\Container\\Fixtures\\TwoConstructorArgumentsBothOptional',
827 array(NULL, NULL)
828 );
829 $this->assertInstanceOf(
830 'TYPO3\\CMS\\Extbase\\Tests\\Unit\\Object\\Container\\Fixtures\\TwoConstructorArgumentsBothOptional',
831 $object
832 );
833 $this->assertNull($object->argumentTestClass);
834 $this->assertNull($object->argumentTestClassTwo);
835 }
836
837 /**
838 * test class TwoConstructorArgumentsBothOptional
839 * @test
840 */
841 public function getInstanceOnTwoOptionalGivesNoArgumentsToConstructorIfNoneAreGiven() {
842 $object = $this->container->getInstance(
843 'TYPO3\\CMS\\Extbase\\Tests\\Unit\\Object\\Container\\Fixtures\\TwoConstructorArgumentsBothOptional');
844 $this->assertInstanceOf(
845 'TYPO3\\CMS\\Extbase\\Tests\\Unit\\Object\\Container\\Fixtures\\TwoConstructorArgumentsBothOptional',
846 $object
847 );
848 $this->assertNull($object->argumentTestClass);
849 $this->assertNull($object->argumentTestClassTwo);
850 }
851
852 /**
853 * test class TwoConstructorArgumentsBothOptional
854 * @test
855 */
856 public function getInstanceOnTwoOptionalGivesOneArgumentToConstructorIfFirstIsObjectAndSecondIsNotGiven() {
857 $first = new Fixtures\ArgumentTestClass();
858 $object = $this->container->getInstance(
859 'TYPO3\\CMS\\Extbase\\Tests\\Unit\\Object\\Container\\Fixtures\\TwoConstructorArgumentsBothOptional',
860 array($first)
861 );
862 $this->assertInstanceOf(
863 'TYPO3\\CMS\\Extbase\\Tests\\Unit\\Object\\Container\\Fixtures\\TwoConstructorArgumentsBothOptional',
864 $object
865 );
866 $this->assertSame(
867 $first,
868 $object->argumentTestClass
869 );
870 $this->assertNull($object->argumentTestClassTwo);
871 }
872
873 /**
874 * test class TwoConstructorArgumentsBothOptional
875 * @test
876 */
877 public function getInstanceOnTwoOptionalGivesOneArgumentToConstructorIfFirstIsObjectAndSecondIsNull() {
878 $first = new Fixtures\ArgumentTestClass();
879 $object = $this->container->getInstance(
880 'TYPO3\\CMS\\Extbase\\Tests\\Unit\\Object\\Container\\Fixtures\\TwoConstructorArgumentsBothOptional',
881 array($first, NULL)
882 );
883 $this->assertInstanceOf(
884 'TYPO3\\CMS\\Extbase\\Tests\\Unit\\Object\\Container\\Fixtures\\TwoConstructorArgumentsBothOptional',
885 $object
886 );
887 $this->assertSame(
888 $first,
889 $object->argumentTestClass
890 );
891 $this->assertNull($object->argumentTestClassTwo);
892 }
893
894 /**
895 * test class TwoConstructorArgumentsBothOptional
896 * @test
897 */
898 public function getInstanceOnTwoOptionalGivesOneArgumentToConstructorIfFirstIsNullAndSecondIsObject() {
899 $second = new Fixtures\ArgumentTestClass();
900 $object = $this->container->getInstance(
901 'TYPO3\\CMS\\Extbase\\Tests\\Unit\\Object\\Container\\Fixtures\\TwoConstructorArgumentsBothOptional',
902 array(NULL, $second)
903 );
904 $this->assertInstanceOf(
905 'TYPO3\\CMS\\Extbase\\Tests\\Unit\\Object\\Container\\Fixtures\\TwoConstructorArgumentsBothOptional',
906 $object
907 );
908 $this->assertNull($object->argumentTestClass);
909 $this->assertSame(
910 $second,
911 $object->argumentTestClassTwo
912 );
913 }
914 }