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