Skip to content

Commit

Permalink
Forward port zendframework#2997
Browse files Browse the repository at this point in the history
  • Loading branch information
Maks3w committed Nov 17, 2012
2 parents 2428736 + 15cb09f commit 7af2761
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 1 deletion.
2 changes: 1 addition & 1 deletion library/Zend/ServiceManager/ServiceManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -591,7 +591,7 @@ public function canCreateFromAbstractFactory($cName, $rName)
foreach ($this->abstractFactories as $index => $abstractFactory) {
// Support string abstract factory class names
if (is_string($abstractFactory) && class_exists($abstractFactory, true)) {
$this->abstractFactory[$index] = $abstractFactory = new $abstractFactory();
$this->abstractFactories[$index] = $abstractFactory = new $abstractFactory();
}

if (
Expand Down
16 changes: 16 additions & 0 deletions tests/ZendTest/ServiceManager/ServiceManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
use Zend\ServiceManager\ServiceManager;
use Zend\ServiceManager\Config;

use ZendTest\ServiceManager\TestAsset\FooCounterAbstractFactory;

class ServiceManagerTest extends \PHPUnit_Framework_TestCase
{

Expand Down Expand Up @@ -599,4 +601,18 @@ public function testCanonicalizeName()
$this->assertEquals(true, $this->serviceManager->has('foo/bar'));
$this->assertEquals(true, $this->serviceManager->has('foo bar'));
}

/**
* @covers Zend\ServiceManager\ServiceManager::canCreateFromAbstractFactory
*/
public function testWanCreateFromAbstractFactoryWillNotInstantiateAbstractFactoryOnce()
{
$count = FooCounterAbstractFactory::$instantiationCount;
$this->serviceManager->addAbstractFactory(__NAMESPACE__ . '\TestAsset\FooCounterAbstractFactory');

$this->serviceManager->canCreateFromAbstractFactory('foo', 'foo');
$this->serviceManager->canCreateFromAbstractFactory('foo', 'foo');

$this->assertSame($count + 1, FooCounterAbstractFactory::$instantiationCount);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php
/**
* Zend Framework (http://framework.zend.com/)
*
* @link http://github.com/zendframework/zf2 for the canonical source repository
* @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
* @package Zend_ServiceManager
*/

namespace ZendTest\ServiceManager\TestAsset;

use Zend\ServiceManager\AbstractFactoryInterface;
use Zend\ServiceManager\ServiceLocatorInterface;

/**
* Abstract factory that keeps track of the number of times it is instantiated
*/
class FooCounterAbstractFactory implements AbstractFactoryInterface
{
/**
* @var int
*/
public static $instantiationCount = 0;

/**
* Increments instantiation count
*/
public function __construct()
{
self::$instantiationCount += 1;
}

/**
* {@inheritDoc}
*/
public function canCreateServiceWithName(ServiceLocatorInterface $serviceLocator, $name, $requestedName)
{
if ($name == 'foo') {
return true;
}
}

/**
* {@inheritDoc}
*/
public function createServiceWithName(ServiceLocatorInterface $serviceLocator, $name, $requestedName)
{
return new Foo;
}
}

0 comments on commit 7af2761

Please sign in to comment.