[TASK] Use name-resolution instead of strings where possible: 3
[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 public 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 * Callback for getInstanceUsesClassNameSha1AsCacheKey
185 *
186 * @param string $id
187 * @param \TYPO3\CMS\Extbase\Object\Container\ClassInfo $value
188 * @return void
189 */
190 public function setClassInfoCacheCallback($id, \TYPO3\CMS\Extbase\Object\Container\ClassInfo $value) {
191 $this->cachedClassInfo = $value;
192 }
193
194 /**
195 * @test
196 */
197 public function getEmptyObjectReturnsInstanceOfSimpleClass() {
198 $object = $this->container->getEmptyObject('t3lib_object_tests_c');
199 $this->assertInstanceOf('t3lib_object_tests_c', $object);
200 }
201
202 /**
203 * @test
204 */
205 public function test_canGetChildClass() {
206 $object = $this->container->getInstance('t3lib_object_tests_b_child');
207 $this->assertInstanceOf('t3lib_object_tests_b_child', $object);
208 }
209
210 /**
211 * @test
212 */
213 public function test_canInjectInterfaceInClass() {
214 $this->container->registerImplementation('t3lib_object_tests_someinterface', 't3lib_object_tests_someimplementation');
215 $object = $this->container->getInstance('t3lib_object_tests_needsinterface');
216 $this->assertInstanceOf('t3lib_object_tests_needsinterface', $object);
217 $this->assertInstanceOf('t3lib_object_tests_someinterface', $object->dependency);
218 $this->assertInstanceOf('t3lib_object_tests_someimplementation', $object->dependency);
219 }
220
221 /**
222 * @test
223 */
224 public function test_canBuildCyclicDependenciesOfSingletonsWithSetter() {
225 $object = $this->container->getInstance('t3lib_object_tests_resolveablecyclic1');
226 $this->assertInstanceOf('t3lib_object_tests_resolveablecyclic1', $object);
227 $this->assertInstanceOf('t3lib_object_tests_resolveablecyclic1', $object->o2->o3->o1);
228 }
229
230 /**
231 * @test
232 */
233 public function singletonWhichRequiresPrototypeViaSetterInjectionWorksAndAddsDebugMessage() {
234 $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);
235 $object = $this->container->getInstance('t3lib_object_singletonNeedsPrototype');
236 $this->assertInstanceOf('t3lib_object_prototype', $object->dependency);
237 }
238
239 /**
240 * @test
241 */
242 public function singletonWhichRequiresSingletonViaSetterInjectionWorks() {
243 $this->container->expects($this->never())->method('log');
244 $object = $this->container->getInstance('t3lib_object_singletonNeedsSingleton');
245 $this->assertInstanceOf('t3lib_object_singleton', $object->dependency);
246 }
247
248 /**
249 * @test
250 */
251 public function prototypeWhichRequiresPrototypeViaSetterInjectionWorks() {
252 $this->container->expects($this->never())->method('log');
253 $object = $this->container->getInstance('t3lib_object_prototypeNeedsPrototype');
254 $this->assertInstanceOf('t3lib_object_prototype', $object->dependency);
255 }
256
257 /**
258 * @test
259 */
260 public function prototypeWhichRequiresSingletonViaSetterInjectionWorks() {
261 $this->container->expects($this->never())->method('log');
262 $object = $this->container->getInstance('t3lib_object_prototypeNeedsSingleton');
263 $this->assertInstanceOf('t3lib_object_singleton', $object->dependency);
264 }
265
266 /**
267 * @test
268 */
269 public function singletonWhichRequiresPrototypeViaConstructorInjectionWorksAndAddsDebugMessage() {
270 $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);
271 $object = $this->container->getInstance('t3lib_object_singletonNeedsPrototypeInConstructor');
272 $this->assertInstanceOf('t3lib_object_prototype', $object->dependency);
273 }
274
275 /**
276 * @test
277 */
278 public function singletonWhichRequiresSingletonViaConstructorInjectionWorks() {
279 $this->container->expects($this->never())->method('log');
280 $object = $this->container->getInstance('t3lib_object_singletonNeedsSingletonInConstructor');
281 $this->assertInstanceOf('t3lib_object_singleton', $object->dependency);
282 }
283
284 /**
285 * @test
286 */
287 public function prototypeWhichRequiresPrototypeViaConstructorInjectionWorks() {
288 $this->container->expects($this->never())->method('log');
289 $object = $this->container->getInstance('t3lib_object_prototypeNeedsPrototypeInConstructor');
290 $this->assertInstanceOf('t3lib_object_prototype', $object->dependency);
291 }
292
293 /**
294 * @test
295 */
296 public function prototypeWhichRequiresSingletonViaConstructorInjectionWorks() {
297 $this->container->expects($this->never())->method('log');
298 $object = $this->container->getInstance('t3lib_object_prototypeNeedsSingletonInConstructor');
299 $this->assertInstanceOf('t3lib_object_singleton', $object->dependency);
300 }
301
302 /**
303 * @test
304 */
305 public function isSingletonReturnsTrueForSingletonInstancesAndFalseForPrototypes() {
306 $this->assertTrue($this->container->isSingleton(\TYPO3\CMS\Extbase\Object\Container\Container::class));
307 $this->assertFalse($this->container->isSingleton(\TYPO3\CMS\Extbase\Core\Bootstrap::class));
308 }
309
310 /**
311 * @test
312 */
313 public function isPrototypeReturnsFalseForSingletonInstancesAndTrueForPrototypes() {
314 $this->assertFalse($this->container->isPrototype(\TYPO3\CMS\Extbase\Object\Container\Container::class));
315 $this->assertTrue($this->container->isPrototype(\TYPO3\CMS\Extbase\Core\Bootstrap::class));
316 }
317
318 /************************************************
319 * Test regarding constructor argument injection
320 ************************************************/
321
322 /**
323 * test class SimpleTypeConstructorArgument
324 * @test
325 */
326 public function getInstanceGivesSimpleConstructorArgumentToClassInstance() {
327 $object = $this->container->getInstance(
328 \TYPO3\CMS\Extbase\Tests\Unit\Object\Container\Fixtures\SimpleTypeConstructorArgument::class,
329 array(TRUE)
330 );
331 $this->assertTrue($object->foo);
332 }
333
334 /**
335 * test class SimpleTypeConstructorArgument
336 * @test
337 */
338 public function getInstanceDoesNotInfluenceSimpleTypeConstructorArgumentIfNotGiven() {
339 $object = $this->container->getInstance(
340 \TYPO3\CMS\Extbase\Tests\Unit\Object\Container\Fixtures\SimpleTypeConstructorArgument::class
341 );
342 $this->assertFalse($object->foo);
343 }
344
345 /**
346 * test class MandatoryConstructorArgument
347 * @test
348 */
349 public function getInstanceGivesExistingConstructorArgumentToClassInstance() {
350 $argumentTestClass = new Fixtures\ArgumentTestClass();
351 $object = $this->container->getInstance(
352 \TYPO3\CMS\Extbase\Tests\Unit\Object\Container\Fixtures\MandatoryConstructorArgument::class,
353 array($argumentTestClass)
354 );
355 $this->assertInstanceOf(
356 \TYPO3\CMS\Extbase\Tests\Unit\Object\Container\Fixtures\MandatoryConstructorArgument::class,
357 $object
358 );
359 $this->assertSame($argumentTestClass, $object->argumentTestClass);
360 }
361
362 /**
363 * test class MandatoryConstructorArgument
364 * @test
365 */
366 public function getInstanceInjectsNewInstanceOfClassToClassIfArgumentIsMandatory() {
367 $object = $this->container->getInstance(
368 \TYPO3\CMS\Extbase\Tests\Unit\Object\Container\Fixtures\MandatoryConstructorArgument::class
369 );
370 $this->assertInstanceOf(
371 \TYPO3\CMS\Extbase\Tests\Unit\Object\Container\Fixtures\MandatoryConstructorArgument::class,
372 $object
373 );
374 $this->assertInstanceOf(
375 \TYPO3\CMS\Extbase\Tests\Unit\Object\Container\Fixtures\ArgumentTestClass::class,
376 $object->argumentTestClass
377 );
378 }
379
380 /**
381 * test class OptionalConstructorArgument
382 * @test
383 */
384 public function getInstanceDoesNotInjectAnOptionalArgumentIfNotGiven() {
385 $object = $this->container->getInstance(
386 \TYPO3\CMS\Extbase\Tests\Unit\Object\Container\Fixtures\OptionalConstructorArgument::class
387 );
388 $this->assertInstanceOf(
389 \TYPO3\CMS\Extbase\Tests\Unit\Object\Container\Fixtures\OptionalConstructorArgument::class,
390 $object
391 );
392 $this->assertNull($object->argumentTestClass);
393 }
394
395 /**
396 * test class OptionalConstructorArgument
397 * @test
398 */
399 public function getInstanceDoesNotInjectAnOptionalArgumentIfGivenArgumentIsNull() {
400 $object = $this->container->getInstance(
401 \TYPO3\CMS\Extbase\Tests\Unit\Object\Container\Fixtures\OptionalConstructorArgument::class,
402 array(NULL)
403 );
404 $this->assertInstanceOf(
405 \TYPO3\CMS\Extbase\Tests\Unit\Object\Container\Fixtures\OptionalConstructorArgument::class,
406 $object
407 );
408 $this->assertNull($object->argumentTestClass);
409 }
410
411 /**
412 * test class OptionalConstructorArgument
413 * @test
414 */
415 public function getInstanceGivesExistingConstructorArgumentToClassInstanceIfArgumentIsGiven() {
416 $argumentTestClass = new Fixtures\ArgumentTestClass();
417 $object = $this->container->getInstance(
418 \TYPO3\CMS\Extbase\Tests\Unit\Object\Container\Fixtures\OptionalConstructorArgument::class,
419 array($argumentTestClass)
420 );
421 $this->assertInstanceOf(
422 \TYPO3\CMS\Extbase\Tests\Unit\Object\Container\Fixtures\OptionalConstructorArgument::class,
423 $object
424 );
425 $this->assertSame($argumentTestClass, $object->argumentTestClass);
426 }
427
428 /**
429 * test class MandatoryConstructorArgumentTwo
430 * @test
431 */
432 public function getInstanceGivesTwoArgumentsToClassConstructor() {
433 $firstArgument = new Fixtures\ArgumentTestClass();
434 $secondArgument = new Fixtures\ArgumentTestClass();
435 $object = $this->container->getInstance(
436 \TYPO3\CMS\Extbase\Tests\Unit\Object\Container\Fixtures\MandatoryConstructorArgumentTwo::class,
437 array($firstArgument, $secondArgument)
438 );
439 $this->assertInstanceOf(
440 \TYPO3\CMS\Extbase\Tests\Unit\Object\Container\Fixtures\MandatoryConstructorArgumentTwo::class,
441 $object
442 );
443 $this->assertSame(
444 $firstArgument,
445 $object->argumentTestClass
446 );
447 $this->assertSame(
448 $secondArgument,
449 $object->argumentTestClassTwo
450 );
451 }
452
453 /**
454 * test class MandatoryConstructorArgumentTwo
455 * @test
456 */
457 public function getInstanceInjectsTwoMandatoryArguments() {
458 $object = $this->container->getInstance(
459 \TYPO3\CMS\Extbase\Tests\Unit\Object\Container\Fixtures\MandatoryConstructorArgumentTwo::class
460 );
461 $this->assertInstanceOf(
462 \TYPO3\CMS\Extbase\Tests\Unit\Object\Container\Fixtures\MandatoryConstructorArgumentTwo::class,
463 $object
464 );
465 $this->assertInstanceOf(
466 \TYPO3\CMS\Extbase\Tests\Unit\Object\Container\Fixtures\ArgumentTestClass::class,
467 $object->argumentTestClass
468 );
469 $this->assertInstanceOf(
470 \TYPO3\CMS\Extbase\Tests\Unit\Object\Container\Fixtures\ArgumentTestClass::class,
471 $object->argumentTestClassTwo
472 );
473 $this->assertNotSame(
474 $object->argumentTestClass,
475 $object->argumentTestClassTwo
476 );
477 }
478
479 /**
480 * test class MandatoryConstructorArgumentTwo
481 * @test
482 */
483 public function getInstanceInjectsSecondMandatoryArgumentIfFirstIsGiven() {
484 $firstArgument = new Fixtures\ArgumentTestClass();
485 $object = $this->container->getInstance(
486 \TYPO3\CMS\Extbase\Tests\Unit\Object\Container\Fixtures\MandatoryConstructorArgumentTwo::class,
487 array($firstArgument)
488 );
489 $this->assertInstanceOf(
490 \TYPO3\CMS\Extbase\Tests\Unit\Object\Container\Fixtures\MandatoryConstructorArgumentTwo::class,
491 $object
492 );
493 $this->assertSame(
494 $firstArgument,
495 $object->argumentTestClass
496 );
497 $this->assertInstanceOf(
498 \TYPO3\CMS\Extbase\Tests\Unit\Object\Container\Fixtures\ArgumentTestClass::class,
499 $object->argumentTestClassTwo
500 );
501 $this->assertNotSame(
502 $object->argumentTestClass,
503 $object->argumentTestClassTwo
504 );
505 }
506
507 /**
508 * test class MandatoryConstructorArgumentTwo
509 * @test
510 */
511 public function getInstanceInjectsFirstMandatoryArgumentIfSecondIsGiven() {
512 $secondArgument = new Fixtures\ArgumentTestClass();
513 $object = $this->container->getInstance(
514 \TYPO3\CMS\Extbase\Tests\Unit\Object\Container\Fixtures\MandatoryConstructorArgumentTwo::class,
515 array(NULL, $secondArgument)
516 );
517 $this->assertInstanceOf(
518 \TYPO3\CMS\Extbase\Tests\Unit\Object\Container\Fixtures\MandatoryConstructorArgumentTwo::class,
519 $object
520 );
521 $this->assertInstanceOf(
522 \TYPO3\CMS\Extbase\Tests\Unit\Object\Container\Fixtures\ArgumentTestClass::class,
523 $object->argumentTestClass
524 );
525 $this->assertInstanceOf(
526 \TYPO3\CMS\Extbase\Tests\Unit\Object\Container\Fixtures\ArgumentTestClass::class,
527 $object->argumentTestClassTwo
528 );
529 $this->assertSame(
530 $secondArgument,
531 $object->argumentTestClassTwo
532 );
533 $this->assertNotSame(
534 $object->argumentTestClass,
535 $object->argumentTestClassTwo
536 );
537 }
538
539 /**
540 * test class TwoConstructorArgumentsSecondOptional
541 * @test
542 */
543 public function getInstanceGivesTwoArgumentsToClassConstructorIfSecondIsOptional() {
544 $firstArgument = new Fixtures\ArgumentTestClass();
545 $secondArgument = new Fixtures\ArgumentTestClass();
546 $object = $this->container->getInstance(
547 \TYPO3\CMS\Extbase\Tests\Unit\Object\Container\Fixtures\TwoConstructorArgumentsSecondOptional::class,
548 array($firstArgument, $secondArgument)
549 );
550 $this->assertInstanceOf(
551 \TYPO3\CMS\Extbase\Tests\Unit\Object\Container\Fixtures\TwoConstructorArgumentsSecondOptional::class,
552 $object
553 );
554 $this->assertSame(
555 $firstArgument,
556 $object->argumentTestClass
557 );
558 $this->assertSame(
559 $secondArgument,
560 $object->argumentTestClassTwo
561 );
562 }
563
564 /**
565 * test class TwoConstructorArgumentsSecondOptional
566 * @test
567 */
568 public function getInstanceInjectsFirstMandatoryArgumentIfSecondIsOptionalAndNoneAreGiven() {
569 $object = $this->container->getInstance(
570 \TYPO3\CMS\Extbase\Tests\Unit\Object\Container\Fixtures\TwoConstructorArgumentsSecondOptional::class
571 );
572 $this->assertInstanceOf(
573 \TYPO3\CMS\Extbase\Tests\Unit\Object\Container\Fixtures\TwoConstructorArgumentsSecondOptional::class,
574 $object
575 );
576 $this->assertInstanceOf(
577 \TYPO3\CMS\Extbase\Tests\Unit\Object\Container\Fixtures\ArgumentTestClass::class,
578 $object->argumentTestClass
579 );
580 $this->assertNull($object->argumentTestClassTwo);
581 }
582
583 /**
584 * test class TwoConstructorArgumentsSecondOptional
585 * @test
586 */
587 public function getInstanceInjectsFirstMandatoryArgumentIfSecondIsOptionalAndBothAreGivenAsNull() {
588 $object = $this->container->getInstance(
589 \TYPO3\CMS\Extbase\Tests\Unit\Object\Container\Fixtures\TwoConstructorArgumentsSecondOptional::class,
590 array(NULL, NULL)
591 );
592 $this->assertInstanceOf(
593 \TYPO3\CMS\Extbase\Tests\Unit\Object\Container\Fixtures\TwoConstructorArgumentsSecondOptional::class,
594 $object
595 );
596 $this->assertInstanceOf(
597 \TYPO3\CMS\Extbase\Tests\Unit\Object\Container\Fixtures\ArgumentTestClass::class,
598 $object->argumentTestClass
599 );
600 $this->assertNull($object->argumentTestClassTwo);
601 }
602
603 /**
604 * test class TwoConstructorArgumentsSecondOptional
605 * @test
606 */
607 public function getInstanceGivesFirstArgumentToConstructorIfSecondIsOptionalAndFirstIsGiven() {
608 $firstArgument = new Fixtures\ArgumentTestClass();
609 $object = $this->container->getInstance(
610 \TYPO3\CMS\Extbase\Tests\Unit\Object\Container\Fixtures\TwoConstructorArgumentsSecondOptional::class,
611 array($firstArgument)
612 );
613 $this->assertInstanceOf(
614 \TYPO3\CMS\Extbase\Tests\Unit\Object\Container\Fixtures\TwoConstructorArgumentsSecondOptional::class,
615 $object
616 );
617 $this->assertSame(
618 $firstArgument,
619 $object->argumentTestClass
620 );
621 $this->assertNull($object->argumentTestClassTwo);
622 }
623
624 /**
625 * test class TwoConstructorArgumentsSecondOptional
626 * @test
627 */
628 public function getInstanceGivesFirstArgumentToConstructorIfSecondIsOptionalFirstIsGivenAndSecondIsGivenNull() {
629 $firstArgument = new Fixtures\ArgumentTestClass();
630 $object = $this->container->getInstance(
631 \TYPO3\CMS\Extbase\Tests\Unit\Object\Container\Fixtures\TwoConstructorArgumentsSecondOptional::class,
632 array($firstArgument, NULL)
633 );
634 $this->assertInstanceOf(
635 \TYPO3\CMS\Extbase\Tests\Unit\Object\Container\Fixtures\TwoConstructorArgumentsSecondOptional::class,
636 $object
637 );
638 $this->assertSame(
639 $firstArgument,
640 $object->argumentTestClass
641 );
642 $this->assertNull($object->argumentTestClassTwo);
643 }
644
645 /**
646 * test class TwoConstructorArgumentsFirstOptional
647 *
648 * @test
649 */
650 public function getInstanceOnFirstOptionalAndSecondMandatoryInjectsSecondArgumentIfFirstIsGivenAsNull() {
651 $object = $this->container->getInstance(
652 \TYPO3\CMS\Extbase\Tests\Unit\Object\Container\Fixtures\TwoConstructorArgumentsFirstOptional::class,
653 array(NULL)
654 );
655 $this->assertInstanceOf(
656 \TYPO3\CMS\Extbase\Tests\Unit\Object\Container\Fixtures\TwoConstructorArgumentsFirstOptional::class,
657 $object
658 );
659 $this->assertInstanceOf(
660 \TYPO3\CMS\Extbase\Tests\Unit\Object\Container\Fixtures\ArgumentTestClass::class,
661 $object->argumentTestClassTwo
662 );
663 }
664
665 /**
666 * test class TwoConstructorArgumentsFirstOptional
667 * @test
668 */
669 public function getInstanceOnFirstOptionalAndSecondMandatoryGivesTwoGivenArgumentsToConstructor() {
670 $first = new Fixtures\ArgumentTestClass();
671 $second = new Fixtures\ArgumentTestClass();
672 $object = $this->container->getInstance(
673 \TYPO3\CMS\Extbase\Tests\Unit\Object\Container\Fixtures\TwoConstructorArgumentsFirstOptional::class,
674 array($first, $second)
675 );
676 $this->assertInstanceOf(
677 \TYPO3\CMS\Extbase\Tests\Unit\Object\Container\Fixtures\TwoConstructorArgumentsFirstOptional::class,
678 $object
679 );
680 $this->assertSame(
681 $first,
682 $object->argumentTestClass
683 );
684 $this->assertSame(
685 $second,
686 $object->argumentTestClassTwo
687 );
688 }
689
690 /**
691 * test class TwoConstructorArgumentsFirstOptional
692 * @test
693 */
694 public function getInstanceOnFirstOptionalAndSecondMandatoryInjectsSecondArgumentIfFirstIsGiven() {
695 $first = new Fixtures\ArgumentTestClass();
696 $object = $this->container->getInstance(
697 \TYPO3\CMS\Extbase\Tests\Unit\Object\Container\Fixtures\TwoConstructorArgumentsFirstOptional::class,
698 array($first)
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->assertInstanceOf(
709 \TYPO3\CMS\Extbase\Tests\Unit\Object\Container\Fixtures\ArgumentTestClass::class,
710 $object->argumentTestClassTwo
711 );
712 $this->assertNotSame(
713 $object->argumentTestClass,
714 $object->argumentTestClassTwo
715 );
716 }
717
718 /**
719 * test class TwoConstructorArgumentsFirstOptional
720 *
721 * @test
722 */
723 public function getInstanceOnFirstOptionalAndSecondMandatoryGivesSecondArgumentAsIsIfFirstIsGivenAsNullAndSecondIsGiven() {
724 $second = new Fixtures\ArgumentTestClass();
725 $object = $this->container->getInstance(
726 \TYPO3\CMS\Extbase\Tests\Unit\Object\Container\Fixtures\TwoConstructorArgumentsFirstOptional::class,
727 array(NULL, $second)
728 );
729 $this->assertInstanceOf(
730 \TYPO3\CMS\Extbase\Tests\Unit\Object\Container\Fixtures\TwoConstructorArgumentsFirstOptional::class,
731 $object
732 );
733 $this->assertSame(
734 $second,
735 $object->argumentTestClassTwo
736 );
737 }
738
739 /**
740 * test class TwoConstructorArgumentsFirstOptional
741 *
742 * @test
743 */
744 public function getInstanceOnFirstOptionalAndSecondMandatoryInjectsSecondArgumentIfFirstIsGivenAsNullAndSecondIsNull() {
745 $object = $this->container->getInstance(
746 \TYPO3\CMS\Extbase\Tests\Unit\Object\Container\Fixtures\TwoConstructorArgumentsFirstOptional::class,
747 array(NULL, NULL)
748 );
749 $this->assertInstanceOf(
750 \TYPO3\CMS\Extbase\Tests\Unit\Object\Container\Fixtures\TwoConstructorArgumentsFirstOptional::class,
751 $object
752 );
753 $this->assertInstanceOf(
754 \TYPO3\CMS\Extbase\Tests\Unit\Object\Container\Fixtures\ArgumentTestClass::class,
755 $object->argumentTestClassTwo
756 );
757 }
758
759 /**
760 * test class TwoConstructorArgumentsBothOptional
761 * @test
762 */
763 public function getInstanceOnTwoOptionalGivesTwoGivenArgumentsToConstructor() {
764 $first = new Fixtures\ArgumentTestClass();
765 $second = new Fixtures\ArgumentTestClass();
766 $object = $this->container->getInstance(
767 \TYPO3\CMS\Extbase\Tests\Unit\Object\Container\Fixtures\TwoConstructorArgumentsBothOptional::class,
768 array($first, $second)
769 );
770 $this->assertInstanceOf(
771 \TYPO3\CMS\Extbase\Tests\Unit\Object\Container\Fixtures\TwoConstructorArgumentsBothOptional::class,
772 $object
773 );
774 $this->assertSame(
775 $first,
776 $object->argumentTestClass
777 );
778 $this->assertSame(
779 $second,
780 $object->argumentTestClassTwo
781 );
782 }
783
784 /**
785 * test class TwoConstructorArgumentsBothOptional
786 * @test
787 */
788 public function getInstanceOnTwoOptionalGivesNoArgumentsToConstructorIfArgumentsAreNull() {
789 $object = $this->container->getInstance(
790 \TYPO3\CMS\Extbase\Tests\Unit\Object\Container\Fixtures\TwoConstructorArgumentsBothOptional::class,
791 array(NULL, NULL)
792 );
793 $this->assertInstanceOf(
794 \TYPO3\CMS\Extbase\Tests\Unit\Object\Container\Fixtures\TwoConstructorArgumentsBothOptional::class,
795 $object
796 );
797 $this->assertNull($object->argumentTestClass);
798 $this->assertNull($object->argumentTestClassTwo);
799 }
800
801 /**
802 * test class TwoConstructorArgumentsBothOptional
803 * @test
804 */
805 public function getInstanceOnTwoOptionalGivesNoArgumentsToConstructorIfNoneAreGiven() {
806 $object = $this->container->getInstance(
807 \TYPO3\CMS\Extbase\Tests\Unit\Object\Container\Fixtures\TwoConstructorArgumentsBothOptional::class);
808 $this->assertInstanceOf(
809 \TYPO3\CMS\Extbase\Tests\Unit\Object\Container\Fixtures\TwoConstructorArgumentsBothOptional::class,
810 $object
811 );
812 $this->assertNull($object->argumentTestClass);
813 $this->assertNull($object->argumentTestClassTwo);
814 }
815
816 /**
817 * test class TwoConstructorArgumentsBothOptional
818 * @test
819 */
820 public function getInstanceOnTwoOptionalGivesOneArgumentToConstructorIfFirstIsObjectAndSecondIsNotGiven() {
821 $first = new Fixtures\ArgumentTestClass();
822 $object = $this->container->getInstance(
823 \TYPO3\CMS\Extbase\Tests\Unit\Object\Container\Fixtures\TwoConstructorArgumentsBothOptional::class,
824 array($first)
825 );
826 $this->assertInstanceOf(
827 \TYPO3\CMS\Extbase\Tests\Unit\Object\Container\Fixtures\TwoConstructorArgumentsBothOptional::class,
828 $object
829 );
830 $this->assertSame(
831 $first,
832 $object->argumentTestClass
833 );
834 $this->assertNull($object->argumentTestClassTwo);
835 }
836
837 /**
838 * test class TwoConstructorArgumentsBothOptional
839 * @test
840 */
841 public function getInstanceOnTwoOptionalGivesOneArgumentToConstructorIfFirstIsObjectAndSecondIsNull() {
842 $first = new Fixtures\ArgumentTestClass();
843 $object = $this->container->getInstance(
844 \TYPO3\CMS\Extbase\Tests\Unit\Object\Container\Fixtures\TwoConstructorArgumentsBothOptional::class,
845 array($first, NULL)
846 );
847 $this->assertInstanceOf(
848 \TYPO3\CMS\Extbase\Tests\Unit\Object\Container\Fixtures\TwoConstructorArgumentsBothOptional::class,
849 $object
850 );
851 $this->assertSame(
852 $first,
853 $object->argumentTestClass
854 );
855 $this->assertNull($object->argumentTestClassTwo);
856 }
857
858 /**
859 * test class TwoConstructorArgumentsBothOptional
860 * @test
861 */
862 public function getInstanceOnTwoOptionalGivesOneArgumentToConstructorIfFirstIsNullAndSecondIsObject() {
863 $second = new Fixtures\ArgumentTestClass();
864 $object = $this->container->getInstance(
865 \TYPO3\CMS\Extbase\Tests\Unit\Object\Container\Fixtures\TwoConstructorArgumentsBothOptional::class,
866 array(NULL, $second)
867 );
868 $this->assertInstanceOf(
869 \TYPO3\CMS\Extbase\Tests\Unit\Object\Container\Fixtures\TwoConstructorArgumentsBothOptional::class,
870 $object
871 );
872 $this->assertNull($object->argumentTestClass);
873 $this->assertSame(
874 $second,
875 $object->argumentTestClassTwo
876 );
877 }
878 }