16 use Psr\Log\LoggerInterface;
25 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
47 protected function setUp()
49 $this->logger = $this->getMockBuilder(Logger::class)
50 ->setMethods([
'notice'])
51 ->disableOriginalConstructor()
54 $this->subject = $this->getMockBuilder(Container::class)
55 ->setMethods([
'getLogger'])
57 $this->subject->expects($this->any())->method(
'getLogger')->will($this->returnValue($this->logger));
65 $object = $this->subject->getInstance(
't3lib_object_tests_c');
66 $this->assertInstanceOf(
't3lib_object_tests_c', $object);
74 $object = $this->subject->getInstance(\
TYPO3\CMS\
Extbase\Tests\Unit\Object\
Container\Fixtures\NamespacedClass::class);
83 $object = $this->subject->getInstance(
't3lib_object_tests_b');
84 $this->assertInstanceOf(
't3lib_object_tests_b', $object);
85 $this->assertInstanceOf(
't3lib_object_tests_c', $object->c);
93 $object = $this->subject->getInstance(
't3lib_object_tests_a');
94 $this->assertInstanceOf(
't3lib_object_tests_a', $object);
95 $this->assertInstanceOf(
't3lib_object_tests_c', $object->b->c);
103 $object = $this->subject->getInstance(
't3lib_object_tests_amixed_array');
104 $this->assertInstanceOf(
't3lib_object_tests_amixed_array', $object);
105 $this->assertEquals([
'some' =>
'default'], $object->myvalue);
113 $object = $this->subject->getInstance(
't3lib_object_tests_amixed_null');
114 $this->assertInstanceOf(
't3lib_object_tests_amixed_null', $object);
115 $this->assertNull($object->myvalue);
123 $this->expectException(Exception::class);
124 $this->expectExceptionCode(1292858051);
125 $this->subject->getInstance(
't3lib_object_tests_amixed_array_singleton', [
'somevalue']);
133 $object = $this->subject->getInstance(
't3lib_object_tests_amixed_array');
134 $this->assertInstanceOf(
't3lib_object_tests_b', $object->b);
135 $this->assertInstanceOf(
't3lib_object_tests_c', $object->c);
136 $this->assertEquals([
'some' =>
'default'], $object->myvalue);
144 $mockObject = $this->createMock(
't3lib_object_tests_c');
145 $object = $this->subject->getInstance(
't3lib_object_tests_a', [$mockObject]);
146 $this->assertInstanceOf(
't3lib_object_tests_a', $object);
147 $this->assertSame($mockObject, $object->c);
155 $object1 = $this->subject->getInstance(
't3lib_object_tests_a');
156 $object2 = $this->subject->getInstance(
't3lib_object_tests_a');
157 $this->assertNotSame($object1, $object2);
165 $object1 = $this->subject->getInstance(
't3lib_object_tests_singleton');
166 $object2 = $this->subject->getInstance(
't3lib_object_tests_singleton');
167 $this->assertSame($object1, $object2);
175 $this->expectException(CannotBuildObjectException::class);
176 $this->expectExceptionCode(1295611406);
177 $this->subject->getInstance(
't3lib_object_tests_cyclic1WithSetterDependency');
185 $this->expectException(CannotBuildObjectException::class);
186 $this->expectExceptionCode(1295611406);
187 $this->subject->getInstance(
't3lib_object_tests_cyclic1');
195 $this->expectException(UnknownClassException::class);
196 $this->expectExceptionCode(1278450972);
197 $this->subject->getInstance(
'nonextistingclass_bla');
205 $instance = $this->subject->getInstance(
't3lib_object_tests_initializable');
206 $this->assertTrue($instance->isInitialized());
214 $object = $this->subject->getEmptyObject(
't3lib_object_tests_c');
215 $this->assertInstanceOf(
't3lib_object_tests_c', $object);
223 $object = $this->subject->getEmptyObject(
't3lib_object_tests_serializable');
224 $this->assertInstanceOf(
't3lib_object_tests_serializable', $object);
232 $object = $this->subject->getEmptyObject(
't3lib_object_tests_initializable');
233 $this->assertTrue($object->isInitialized());
241 $object = $this->subject->getInstance(
't3lib_object_tests_b_child');
242 $this->assertInstanceOf(
't3lib_object_tests_b_child', $object);
250 $this->subject->registerImplementation(
't3lib_object_tests_someinterface',
't3lib_object_tests_someimplementation');
251 $object = $this->subject->getInstance(
't3lib_object_tests_needsinterface');
252 $this->assertInstanceOf(
't3lib_object_tests_needsinterface', $object);
253 $this->assertInstanceOf(
't3lib_object_tests_someinterface', $object->dependency);
254 $this->assertInstanceOf(
't3lib_object_tests_someimplementation', $object->dependency);
262 $object = $this->subject->getInstance(
't3lib_object_tests_resolveablecyclic1');
263 $this->assertInstanceOf(
't3lib_object_tests_resolveablecyclic1', $object);
264 $this->assertInstanceOf(
't3lib_object_tests_resolveablecyclic1', $object->o2->o3->o1);
272 $this->logger->expects($this->once())->method(
'notice')->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.');
273 $object = $this->subject->getInstance(
't3lib_object_singletonNeedsPrototype');
274 $this->assertInstanceOf(
't3lib_object_prototype', $object->dependency);
282 $this->logger->expects($this->never())->method(
'notice');
283 $object = $this->subject->getInstance(
't3lib_object_singletonNeedsSingleton');
284 $this->assertInstanceOf(
't3lib_object_singleton', $object->dependency);
292 $this->logger->expects($this->never())->method(
'notice');
293 $object = $this->subject->getInstance(
't3lib_object_prototypeNeedsPrototype');
294 $this->assertInstanceOf(
't3lib_object_prototype', $object->dependency);
302 $this->logger->expects($this->never())->method(
'notice');
303 $object = $this->subject->getInstance(
't3lib_object_prototypeNeedsSingleton');
304 $this->assertInstanceOf(
't3lib_object_singleton', $object->dependency);
312 $this->logger->expects($this->once())->method(
'notice')->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.');
313 $object = $this->subject->getInstance(
't3lib_object_singletonNeedsPrototypeInConstructor');
314 $this->assertInstanceOf(
't3lib_object_prototype', $object->dependency);
322 $this->logger->expects($this->never())->method(
'notice');
323 $object = $this->subject->getInstance(
't3lib_object_singletonNeedsSingletonInConstructor');
324 $this->assertInstanceOf(
't3lib_object_singleton', $object->dependency);
332 $this->logger->expects($this->never())->method(
'notice');
333 $object = $this->subject->getInstance(
't3lib_object_prototypeNeedsPrototypeInConstructor');
334 $this->assertInstanceOf(
't3lib_object_prototype', $object->dependency);
342 $this->logger->expects($this->never())->method(
'notice');
343 $object = $this->subject->getInstance(
't3lib_object_prototypeNeedsSingletonInConstructor');
344 $this->assertInstanceOf(
't3lib_object_singleton', $object->dependency);
352 $this->assertTrue($this->subject->isSingleton(Container::class));
353 $this->assertFalse($this->subject->isSingleton(\
TYPO3\CMS\
Extbase\Core\Bootstrap::class));
361 $this->assertFalse($this->subject->isPrototype(Container::class));
362 $this->assertTrue($this->subject->isPrototype(\
TYPO3\CMS\
Extbase\Core\Bootstrap::class));
375 $object = $this->subject->getInstance(
379 $this->assertTrue($object->foo);
388 $object = $this->subject->getInstance(
391 $this->assertFalse($object->foo);
401 $object = $this->subject->getInstance(
405 $this->assertInstanceOf(
409 $this->assertSame($argumentTestClass, $object->argumentTestClass);
418 $object = $this->subject->getInstance(
421 $this->assertInstanceOf(
425 $this->assertInstanceOf(
427 $object->argumentTestClass
437 $object = $this->subject->getInstance(
440 $this->assertInstanceOf(
444 $this->assertNull($object->argumentTestClass);
453 $object = $this->subject->getInstance(
457 $this->assertInstanceOf(
461 $this->assertNull($object->argumentTestClass);
471 $object = $this->subject->getInstance(
475 $this->assertInstanceOf(
479 $this->assertSame($argumentTestClass, $object->argumentTestClass);
490 $object = $this->subject->getInstance(
492 [$firstArgument, $secondArgument]
494 $this->assertInstanceOf(
500 $object->argumentTestClass
504 $object->argumentTestClassTwo
514 $object = $this->subject->getInstance(
517 $this->assertInstanceOf(
521 $this->assertInstanceOf(
523 $object->argumentTestClass
525 $this->assertInstanceOf(
527 $object->argumentTestClassTwo
529 $this->assertNotSame(
530 $object->argumentTestClass,
531 $object->argumentTestClassTwo
542 $object = $this->subject->getInstance(
546 $this->assertInstanceOf(
552 $object->argumentTestClass
554 $this->assertInstanceOf(
556 $object->argumentTestClassTwo
558 $this->assertNotSame(
559 $object->argumentTestClass,
560 $object->argumentTestClassTwo
571 $object = $this->subject->getInstance(
573 [
null, $secondArgument]
575 $this->assertInstanceOf(
579 $this->assertInstanceOf(
581 $object->argumentTestClass
583 $this->assertInstanceOf(
585 $object->argumentTestClassTwo
589 $object->argumentTestClassTwo
591 $this->assertNotSame(
592 $object->argumentTestClass,
593 $object->argumentTestClassTwo
605 $object = $this->subject->getInstance(
607 [$firstArgument, $secondArgument]
609 $this->assertInstanceOf(
615 $object->argumentTestClass
619 $object->argumentTestClassTwo
629 $object = $this->subject->getInstance(
632 $this->assertInstanceOf(
636 $this->assertInstanceOf(
638 $object->argumentTestClass
640 $this->assertNull($object->argumentTestClassTwo);
649 $object = $this->subject->getInstance(
653 $this->assertInstanceOf(
657 $this->assertInstanceOf(
659 $object->argumentTestClass
661 $this->assertNull($object->argumentTestClassTwo);
671 $object = $this->subject->getInstance(
675 $this->assertInstanceOf(
681 $object->argumentTestClass
683 $this->assertNull($object->argumentTestClassTwo);
693 $object = $this->subject->getInstance(
695 [$firstArgument,
null]
697 $this->assertInstanceOf(
703 $object->argumentTestClass
705 $this->assertNull($object->argumentTestClassTwo);
715 $object = $this->subject->getInstance(
719 $this->assertInstanceOf(
723 $this->assertInstanceOf(
725 $object->argumentTestClassTwo
737 $object = $this->subject->getInstance(
741 $this->assertInstanceOf(
747 $object->argumentTestClass
751 $object->argumentTestClassTwo
762 $object = $this->subject->getInstance(
766 $this->assertInstanceOf(
772 $object->argumentTestClass
774 $this->assertInstanceOf(
776 $object->argumentTestClassTwo
778 $this->assertNotSame(
779 $object->argumentTestClass,
780 $object->argumentTestClassTwo
792 $object = $this->subject->getInstance(
796 $this->assertInstanceOf(
802 $object->argumentTestClassTwo
813 $object = $this->subject->getInstance(
817 $this->assertInstanceOf(
821 $this->assertInstanceOf(
823 $object->argumentTestClassTwo
835 $object = $this->subject->getInstance(
839 $this->assertInstanceOf(
845 $object->argumentTestClass
849 $object->argumentTestClassTwo
859 $object = $this->subject->getInstance(
863 $this->assertInstanceOf(
867 $this->assertNull($object->argumentTestClass);
868 $this->assertNull($object->argumentTestClassTwo);
877 $object = $this->subject->getInstance(
880 $this->assertInstanceOf(
884 $this->assertNull($object->argumentTestClass);
885 $this->assertNull($object->argumentTestClassTwo);
895 $object = $this->subject->getInstance(
899 $this->assertInstanceOf(
905 $object->argumentTestClass
907 $this->assertNull($object->argumentTestClassTwo);
917 $object = $this->subject->getInstance(
921 $this->assertInstanceOf(
927 $object->argumentTestClass
929 $this->assertNull($object->argumentTestClassTwo);
939 $object = $this->subject->getInstance(
943 $this->assertInstanceOf(
947 $this->assertNull($object->argumentTestClass);
950 $object->argumentTestClassTwo
960 $object = $container->getInstance(PublicPropertyInjectClass::class);
961 self::assertInstanceOf(ArgumentTestClassForPublicPropertyInjection::class, $object->foo);
970 $object = $container->getInstance(ProtectedPropertyInjectClass::class);
971 self::assertInstanceOf(ArgumentTestClassForPublicPropertyInjection::class, $object->getFoo());