diff --git a/src/Di.php b/src/Di.php index 611543fb..a943e071 100644 --- a/src/Di.php +++ b/src/Di.php @@ -150,7 +150,8 @@ public function get($name, array $params = array()) return $im->getSharedInstance($name, $params); } } - $instance = $this->newInstance($name, $params); + $config = $im->getConfig($name); + $instance = $this->newInstance($name, $params, $config['shared']); array_pop($this->instanceContext); return $instance; diff --git a/test/DiTest.php b/test/DiTest.php index f718f01d..981179b1 100644 --- a/test/DiTest.php +++ b/test/DiTest.php @@ -48,14 +48,6 @@ public function testDiConstructorCanTakeDependencies() $this->assertSame($im, $di->instanceManager()); } - public function testPassingInvalidDefinitionRaisesException() - { - $di = new Di(); - - $this->setExpectedException('PHPUnit_Framework_Error'); - $di->setDefinitionList(array('foo')); - } - public function testGetRetrievesObjectWithMatchingClassDefinition() { $di = new Di(); @@ -65,7 +57,14 @@ public function testGetRetrievesObjectWithMatchingClassDefinition() public function testGetRetrievesSameInstanceOnSubsequentCalls() { - $di = new Di(); + $config = new Config(array( + 'instance' => array( + 'ZendTest\Di\TestAsset\BasicClass' => array( + 'shared' => true, + ), + ), + )); + $di = new Di(null, null, $config); $obj1 = $di->get('ZendTest\Di\TestAsset\BasicClass'); $obj2 = $di->get('ZendTest\Di\TestAsset\BasicClass'); $this->assertInstanceOf('ZendTest\Di\TestAsset\BasicClass', $obj1); @@ -73,6 +72,23 @@ public function testGetRetrievesSameInstanceOnSubsequentCalls() $this->assertSame($obj1, $obj2); } + public function testGetRetrievesDifferentInstanceOnSubsequentCallsIfSharingDisabled() + { + $config = new Config(array( + 'instance' => array( + 'ZendTest\Di\TestAsset\BasicClass' => array( + 'shared' => false, + ), + ), + )); + $di = new Di(null, null, $config); + $obj1 = $di->get('ZendTest\Di\TestAsset\BasicClass'); + $obj2 = $di->get('ZendTest\Di\TestAsset\BasicClass'); + $this->assertInstanceOf('ZendTest\Di\TestAsset\BasicClass', $obj1); + $this->assertInstanceOf('ZendTest\Di\TestAsset\BasicClass', $obj2); + $this->assertNotSame($obj1, $obj2); + } + public function testGetThrowsExceptionWhenUnknownClassIsUsed() { $di = new Di();