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

Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Enhancing test example and clarity
I want the expected behavior to be unmistakably clear
  • Loading branch information
weaverryan committed Mar 6, 2016
commit d0d1e57d01fb950b6466ceec8de406308b215955
Original file line number Diff line number Diff line change
Expand Up @@ -416,26 +416,52 @@ public function testOptionalScalarArgsNotPassedIfLast()
public function testSetterInjection()
{
$container = new ContainerBuilder();
$container->register('a', A::class);
$aArguments = array(new Reference('a'));
$container->register('app_foo', Foo::class);
$container->register('app_a', A::class);
$container->register('app_collision_a', CollisionA::class);
$container->register('app_collision_b', CollisionB::class);

// manually configure *one* call, to override autowiring
$container
->register('setter_injection', SetterInjection::class)
->setAutowired(true)
->addMethodCall('setA', $aArguments)
->addMethodCall('setWithCallsConfigured', array('manual_arg1', 'manual_arg2'))
;

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

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

$this->assertCount(3, $methodCalls);
$this->assertEquals(array('setA', $aArguments), $methodCalls[0]);
$this->assertEquals('setFoo', $methodCalls[1][0]);
$this->assertEquals('autowired.symfony\component\dependencyinjection\tests\compiler\foo', $methodCalls[1][1][0]->__toString());
$this->assertEquals('setDependencies', $methodCalls[2][0]);
$this->assertEquals('autowired.symfony\component\dependencyinjection\tests\compiler\foo', $methodCalls[2][1][0]->__toString());
$this->assertEquals('a', $methodCalls[2][1][1]->__toString());
// grab the call method names
$actualMethodNameCalls = array_map(function($call) {
return $call[0];
}, $methodCalls);
$this->assertEquals(
array('setWithCallsConfigured', 'setFoo', 'setDependencies', 'setOptionalArgNoAutowireable'),
$actualMethodNameCalls
);

// test setWithCallsConfigured args
$this->assertEquals(
array('manual_arg1', 'manual_arg2'),
$methodCalls[0][1]
);
// test setFoo args
$this->assertEquals(
array(new Reference('app_foo')),
$methodCalls[1][1]
);
// test setDependencies args
$this->assertEquals(
array(new Reference('app_foo'), new Reference('app_a')),
$methodCalls[2][1]
);
// test setOptionalArgNoAutowireable args
$this->assertEquals(
array(new Reference('app_a'), 'default_val'),
$methodCalls[3][1]
);
}
}

Expand Down Expand Up @@ -591,25 +617,60 @@ class SetterInjection
{
public function setFoo(Foo $foo)
{
}

public function setA(A $a)
{
// should be called
}

public function setDependencies(Foo $foo, A $a)
{
// should be called
}

public function setBar()
{
// should not be called
}

public function setNotAutowireable(NotARealClass $n)
{
// should not be called
// ???
}

public function setArgCannotAutowire($foo)
{
// should not be called
}

public function setOptionalNotAutowireable(NotARealClass $n = null)
{
// should not be called
}

public function setOptionalNoTypeHint($foo = null)
{
// should not be called
}

public function setMultipleAutowiringMultipleInstancesForOneArg(A $a, CollisionInterface $collision)
{
// The CollisionInterface cannot be autowired - there are multiple

// should not be called
}

public function setOptionalArgNoAutowireable(A $a, $other = 'default_val')
{
// should be called, but only 1 argument is passed
}

public function setCannotAutowireAllArguments(A $a, $other)
{
// should not be called
}

public function setWithCallsConfigured(A $a)
{
// this method has a calls configured on it
// should not be called
}
}