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