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