diff --git a/src/Di.php b/src/Di.php index 7227a561..378c3124 100644 --- a/src/Di.php +++ b/src/Di.php @@ -208,7 +208,7 @@ public function newInstance($name, array $params = array(), $isShared = true) } } - $this->handleInjectDependencies($instance, $class, $injectionMethods, $supertypeInjectionMethods, $params, $alias); + $this->handleInjectDependencies($instance, $name, $injectionMethods, $supertypeInjectionMethods, $params); array_pop($this->instanceContext); return $instance; @@ -236,11 +236,11 @@ public function injectDependencies($instance, array $params = array()) $superTypeInjectionMethods[$interface] = $definitions->getMethods($interface); } } - $this->handleInjectDependencies($instance, null, $injectionMethods, $superTypeInjectionMethods, $params, null); + $this->handleInjectDependencies($instance, null, $injectionMethods, $superTypeInjectionMethods, $params); } - protected function handleInjectDependencies($instance, $name, $injectionMethods, $supertypeInjectionMethods, $params, $alias) + protected function handleInjectDependencies($instance, $name, $injectionMethods, $supertypeInjectionMethods, $params) { $class = get_class($instance); @@ -251,13 +251,13 @@ protected function handleInjectDependencies($instance, $name, $injectionMethods, if ($injectionMethods || $supertypeInjectionMethods) { foreach ($injectionMethods as $injectionMethod => $methodIsRequired) { if ($injectionMethod !== '__construct'){ - $this->resolveAndCallInjectionMethodForInstance($instance, $injectionMethod, $params, $alias, $methodIsRequired, $class); + $this->resolveAndCallInjectionMethodForInstance($instance, $injectionMethod, $params, $name, $methodIsRequired, $class); } } foreach ($supertypeInjectionMethods as $supertype => $supertypeInjectionMethod) { foreach ($supertypeInjectionMethod as $injectionMethod => $methodIsRequired) { if ($injectionMethod !== '__construct') { - $this->resolveAndCallInjectionMethodForInstance($instance, $injectionMethod, $params, $alias, $methodIsRequired, $supertype); + $this->resolveAndCallInjectionMethodForInstance($instance, $injectionMethod, $params, $name, $methodIsRequired, $supertype); } } } @@ -294,7 +294,7 @@ protected function handleInjectDependencies($instance, $name, $injectionMethods, if ($methodParams) { foreach ($methodParams as $methodParam) { if (get_class($objectToInject) == $methodParam[1] || $this->isSubclassOf(get_class($objectToInject), $methodParam[1])) { - $this->resolveAndCallInjectionMethodForInstance($instance, $injectionMethod, array($methodParam[0] => $objectToInject), $alias, true, get_class($instance)); + $this->resolveAndCallInjectionMethodForInstance($instance, $injectionMethod, array($methodParam[0] => $objectToInject), $name, true, get_class($instance)); continue 3; } } @@ -304,7 +304,7 @@ protected function handleInjectDependencies($instance, $name, $injectionMethods, } if ($methodsToCall) { foreach ($methodsToCall as $methodInfo) { - $this->resolveAndCallInjectionMethodForInstance($instance, $methodInfo['method'], $methodInfo['args'], $alias, true, get_class($instance)); + $this->resolveAndCallInjectionMethodForInstance($instance, $methodInfo['method'], $methodInfo['args'], $name, true, get_class($instance)); } } } diff --git a/test/DiTest.php b/test/DiTest.php index 43763915..1210850f 100644 --- a/test/DiTest.php +++ b/test/DiTest.php @@ -43,18 +43,18 @@ public function testDiConstructorCanTakeDependencies() public function testPassingInvalidDefinitionRaisesException() { $di = new Di(); - + $this->setExpectedException('PHPUnit_Framework_Error'); $di->setDefinitionList(array('foo')); } - + public function testGetRetrievesObjectWithMatchingClassDefinition() { $di = new Di(); $obj = $di->get('ZendTest\Di\TestAsset\BasicClass'); $this->assertInstanceOf('ZendTest\Di\TestAsset\BasicClass', $obj); } - + public function testGetRetrievesSameInstanceOnSubsequentCalls() { $di = new Di(); @@ -64,23 +64,23 @@ public function testGetRetrievesSameInstanceOnSubsequentCalls() $this->assertInstanceOf('ZendTest\Di\TestAsset\BasicClass', $obj2); $this->assertSame($obj1, $obj2); } - + public function testGetThrowsExceptionWhenUnknownClassIsUsed() { $di = new Di(); - + $this->setExpectedException('Zend\Di\Exception\ClassNotFoundException', 'could not be located in'); $obj1 = $di->get('ZendTest\Di\TestAsset\NonExistentClass'); } - + public function testGetThrowsExceptionWhenMissingParametersAreEncountered() { $di = new Di(); - + $this->setExpectedException('Zend\Di\Exception\MissingPropertyException', 'Missing instance/object for '); $obj1 = $di->get('ZendTest\Di\TestAsset\BasicClassWithParam'); } - + public function testNewInstanceReturnsDifferentInstances() { $di = new Di(); @@ -90,7 +90,7 @@ public function testNewInstanceReturnsDifferentInstances() $this->assertInstanceOf('ZendTest\Di\TestAsset\BasicClass', $obj2); $this->assertNotSame($obj1, $obj2); } - + public function testNewInstanceReturnsInstanceThatIsSharedWithGet() { $di = new Di(); @@ -100,7 +100,7 @@ public function testNewInstanceReturnsInstanceThatIsSharedWithGet() $this->assertInstanceOf('ZendTest\Di\TestAsset\BasicClass', $obj2); $this->assertSame($obj1, $obj2); } - + public function testNewInstanceReturnsInstanceThatIsNotSharedWithGet() { $di = new Di(); @@ -204,7 +204,7 @@ public function testGetWillResolveConstructorInjectionDependencies() $this->assertInstanceOf('ZendTest\Di\TestAsset\ConstructorInjection\B', $b); $this->assertInstanceOf('ZendTest\Di\TestAsset\ConstructorInjection\A', $b->a); } - + /** * @group ConstructorInjection */ @@ -214,15 +214,15 @@ public function testGetWillResolveConstructorInjectionDependenciesAndInstanceAre $b = $di->get('ZendTest\Di\TestAsset\ConstructorInjection\B'); $this->assertInstanceOf('ZendTest\Di\TestAsset\ConstructorInjection\B', $b); $this->assertInstanceOf('ZendTest\Di\TestAsset\ConstructorInjection\A', $b->a); - + $b2 = $di->get('ZendTest\Di\TestAsset\ConstructorInjection\B'); $this->assertInstanceOf('ZendTest\Di\TestAsset\ConstructorInjection\B', $b2); $this->assertInstanceOf('ZendTest\Di\TestAsset\ConstructorInjection\A', $b2->a); - + $this->assertSame($b, $b2); $this->assertSame($b->a, $b2->a); } - + /** * @group ConstructorInjection */ @@ -233,48 +233,48 @@ public function testNewInstanceWillResolveConstructorInjectionDependencies() $this->assertInstanceOf('ZendTest\Di\TestAsset\ConstructorInjection\B', $b); $this->assertInstanceOf('ZendTest\Di\TestAsset\ConstructorInjection\A', $b->a); } - + /** * @group ConstructorInjection */ public function testNewInstanceWillResolveConstructorInjectionDependenciesWithProperties() { $di = new Di(); - + $im = $di->instanceManager(); $im->setParameters('ZendTest\Di\TestAsset\ConstructorInjection\X', array('one' => 1, 'two' => 2)); - + $y = $di->newInstance('ZendTest\Di\TestAsset\ConstructorInjection\Y'); $this->assertEquals(1, $y->x->one); $this->assertEquals(2, $y->x->two); } - + /** * @group ConstructorInjection */ public function testNewInstanceWillThrowExceptionOnConstructorInjectionDependencyWithMissingParameter() { $di = new Di(); - + $this->setExpectedException('Zend\Di\Exception\MissingPropertyException', 'Missing instance/object for parameter'); $b = $di->newInstance('ZendTest\Di\TestAsset\ConstructorInjection\X'); } - + /** * @group ConstructorInjection */ public function testNewInstanceWillResolveConstructorInjectionDependenciesWithMoreThan2Dependencies() { $di = new Di(); - + $im = $di->instanceManager(); $im->setParameters('ZendTest\Di\TestAsset\ConstructorInjection\X', array('one' => 1, 'two' => 2)); - + $z = $di->newInstance('ZendTest\Di\TestAsset\ConstructorInjection\Z'); $this->assertInstanceOf('ZendTest\Di\TestAsset\ConstructorInjection\Y', $z->y); $this->assertInstanceOf('ZendTest\Di\TestAsset\ConstructorInjection\X', $z->y->x); } - + /** * @group SetterInjection */ @@ -290,7 +290,7 @@ public function testGetWillResolveSetterInjectionDependencies() $this->assertInstanceOf('ZendTest\Di\TestAsset\SetterInjection\B', $b); $this->assertInstanceOf('ZendTest\Di\TestAsset\SetterInjection\A', $b->a); } - + /** * @group SetterInjection */ @@ -306,16 +306,16 @@ public function testGetWillResolveSetterInjectionDependenciesAndInstanceAreTheSa $b = $di->get('ZendTest\Di\TestAsset\SetterInjection\B'); $this->assertInstanceOf('ZendTest\Di\TestAsset\SetterInjection\B', $b); $this->assertInstanceOf('ZendTest\Di\TestAsset\SetterInjection\A', $b->a); - + $b2 = $di->get('ZendTest\Di\TestAsset\SetterInjection\B'); $this->assertInstanceOf('ZendTest\Di\TestAsset\SetterInjection\B', $b2); $this->assertInstanceOf('ZendTest\Di\TestAsset\SetterInjection\A', $b2->a); - + $this->assertSame($b, $b2); $this->assertSame($b->a, $a); $this->assertSame($b2->a, $a); } - + /** * @group SetterInjection */ @@ -332,7 +332,7 @@ public function testNewInstanceWillResolveSetterInjectionDependencies() $this->assertInstanceOf('ZendTest\Di\TestAsset\SetterInjection\B', $b); $this->assertInstanceOf('ZendTest\Di\TestAsset\SetterInjection\A', $b->a); } - + /** * @todo Setter Injections is not automatic, find a way to test this logically * @@ -341,7 +341,7 @@ public function testNewInstanceWillResolveSetterInjectionDependencies() public function testNewInstanceWillResolveSetterInjectionDependenciesWithProperties() { $di = new Di(); - + $im = $di->instanceManager(); $im->setParameters('ZendTest\Di\TestAsset\SetterInjection\X', array('one' => 1, 'two' => 2)); @@ -351,10 +351,10 @@ public function testNewInstanceWillResolveSetterInjectionDependenciesWithPropert $this->assertEquals(1, $y->x->one); $this->assertEquals(2, $y->x->two); } - + /** * Test for Circular Dependencies (case 1) - * + * * A->B, B->A * @group CircularDependencyCheck */ @@ -365,10 +365,10 @@ public function testNewInstanceThrowsExceptionOnBasicCircularDependency() $this->setExpectedException('Zend\Di\Exception\CircularDependencyException'); $di->newInstance('ZendTest\Di\TestAsset\CircularClasses\A'); } - + /** * Test for Circular Dependencies (case 2) - * + * * C->D, D->E, E->C * @group CircularDependencyCheck */ @@ -382,10 +382,10 @@ public function testNewInstanceThrowsExceptionOnThreeLevelCircularDependency() ); $di->newInstance('ZendTest\Di\TestAsset\CircularClasses\C'); } - + /** * Test for Circular Dependencies (case 2) - * + * * C->D, D->E, E->C * @group CircularDependencyCheck */ @@ -399,10 +399,10 @@ public function testNewInstanceThrowsExceptionWhenEnteringInMiddleOfCircularDepe ); $di->newInstance('ZendTest\Di\TestAsset\CircularClasses\D'); } - + /** * Fix for PHP bug in is_subclass_of - * + * * @see https://bugs.php.net/bug.php?id=53727 */ public function testNewInstanceWillUsePreferredClassForInterfaceHints() @@ -412,7 +412,7 @@ public function testNewInstanceWillUsePreferredClassForInterfaceHints() 'ZendTest\Di\TestAsset\PreferredImplClasses\A', 'ZendTest\Di\TestAsset\PreferredImplClasses\BofA' ); - + $c = $di->get('ZendTest\Di\TestAsset\PreferredImplClasses\C'); $a = $c->a; $this->assertInstanceOf('ZendTest\Di\TestAsset\PreferredImplClasses\BofA', $a); @@ -590,7 +590,7 @@ public function testInjectionForSetterInjectionWillConsultSupertypeDefinitionInC $this->assertInstanceOf('ZendTest\Di\TestAsset\SetterInjection\C', $c); $this->assertInstanceOf('ZendTest\Di\TestAsset\SetterInjection\A', $c->a); } - + /** * @group SharedInstance */ @@ -624,4 +624,20 @@ public function testDiWillInjectDependenciesForInstance() $di->injectDependencies($b); $this->assertInstanceOf('ZendTest\Di\TestAsset\SetterInjection\A', $b->a); } + + public function testDiWillInjectDependenciesForAlias() + { + $di = new Di; + + // for setter injection, the dependency is not required, thus it must be forced + $classDef = new Definition\ClassDefinition('ZendTest\Di\TestAsset\SetterInjection\B'); + $classDef->addMethod('setA', false); + $classDef->addMethodParameter('setA', 'a', array('type' => 'ZendTest\Di\TestAsset\SetterInjection\A', 'required' => false)); + $di->definitions()->addDefinition($classDef, false); + $di->instanceManager()->addAlias('b_alias', 'ZendTest\Di\TestAsset\SetterInjection\B'); + $di->instanceManager()->setInjections('b_alias', array('ZendTest\Di\TestAsset\SetterInjection\A')); + + $b = $di->get('b_alias'); + $this->assertInstanceOf('ZendTest\Di\TestAsset\SetterInjection\A', $b->a); + } }