Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 178b5cb

Browse files
committed
Merge pull request #3 from weaverryan/dunglas-autowiring-setter
Enhancing test example and clarity
2 parents f690258 + d0d1e57 commit 178b5cb

1 file changed

Lines changed: 75 additions & 14 deletions

File tree

src/Symfony/Component/DependencyInjection/Tests/Compiler/AutowirePassTest.php

Lines changed: 75 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -416,26 +416,52 @@ public function testOptionalScalarArgsNotPassedIfLast()
416416
public function testSetterInjection()
417417
{
418418
$container = new ContainerBuilder();
419-
$container->register('a', A::class);
420-
$aArguments = array(new Reference('a'));
419+
$container->register('app_foo', Foo::class);
420+
$container->register('app_a', A::class);
421+
$container->register('app_collision_a', CollisionA::class);
422+
$container->register('app_collision_b', CollisionB::class);
423+
424+
// manually configure *one* call, to override autowiring
421425
$container
422426
->register('setter_injection', SetterInjection::class)
423427
->setAutowired(true)
424-
->addMethodCall('setA', $aArguments)
428+
->addMethodCall('setWithCallsConfigured', array('manual_arg1', 'manual_arg2'))
425429
;
426430

427431
$pass = new AutowirePass();
428432
$pass->process($container);
429433

430434
$methodCalls = $container->getDefinition('setter_injection')->getMethodCalls();
431435

432-
$this->assertCount(3, $methodCalls);
433-
$this->assertEquals(array('setA', $aArguments), $methodCalls[0]);
434-
$this->assertEquals('setFoo', $methodCalls[1][0]);
435-
$this->assertEquals('autowired.symfony\component\dependencyinjection\tests\compiler\foo', $methodCalls[1][1][0]->__toString());
436-
$this->assertEquals('setDependencies', $methodCalls[2][0]);
437-
$this->assertEquals('autowired.symfony\component\dependencyinjection\tests\compiler\foo', $methodCalls[2][1][0]->__toString());
438-
$this->assertEquals('a', $methodCalls[2][1][1]->__toString());
436+
// grab the call method names
437+
$actualMethodNameCalls = array_map(function($call) {
438+
return $call[0];
439+
}, $methodCalls);
440+
$this->assertEquals(
441+
array('setWithCallsConfigured', 'setFoo', 'setDependencies', 'setOptionalArgNoAutowireable'),
442+
$actualMethodNameCalls
443+
);
444+
445+
// test setWithCallsConfigured args
446+
$this->assertEquals(
447+
array('manual_arg1', 'manual_arg2'),
448+
$methodCalls[0][1]
449+
);
450+
// test setFoo args
451+
$this->assertEquals(
452+
array(new Reference('app_foo')),
453+
$methodCalls[1][1]
454+
);
455+
// test setDependencies args
456+
$this->assertEquals(
457+
array(new Reference('app_foo'), new Reference('app_a')),
458+
$methodCalls[2][1]
459+
);
460+
// test setOptionalArgNoAutowireable args
461+
$this->assertEquals(
462+
array(new Reference('app_a'), 'default_val'),
463+
$methodCalls[3][1]
464+
);
439465
}
440466
}
441467

@@ -591,25 +617,60 @@ class SetterInjection
591617
{
592618
public function setFoo(Foo $foo)
593619
{
594-
}
595-
596-
public function setA(A $a)
597-
{
620+
// should be called
598621
}
599622

600623
public function setDependencies(Foo $foo, A $a)
601624
{
625+
// should be called
602626
}
603627

604628
public function setBar()
605629
{
630+
// should not be called
606631
}
607632

608633
public function setNotAutowireable(NotARealClass $n)
609634
{
635+
// should not be called
636+
// ???
637+
}
638+
639+
public function setArgCannotAutowire($foo)
640+
{
641+
// should not be called
610642
}
611643

612644
public function setOptionalNotAutowireable(NotARealClass $n = null)
613645
{
646+
// should not be called
647+
}
648+
649+
public function setOptionalNoTypeHint($foo = null)
650+
{
651+
// should not be called
652+
}
653+
654+
public function setMultipleAutowiringMultipleInstancesForOneArg(A $a, CollisionInterface $collision)
655+
{
656+
// The CollisionInterface cannot be autowired - there are multiple
657+
658+
// should not be called
659+
}
660+
661+
public function setOptionalArgNoAutowireable(A $a, $other = 'default_val')
662+
{
663+
// should be called, but only 1 argument is passed
664+
}
665+
666+
public function setCannotAutowireAllArguments(A $a, $other)
667+
{
668+
// should not be called
669+
}
670+
671+
public function setWithCallsConfigured(A $a)
672+
{
673+
// this method has a calls configured on it
674+
// should not be called
614675
}
615676
}

0 commit comments

Comments
 (0)