Skip to content
This repository has been archived by the owner on Feb 6, 2020. It is now read-only.

Commit

Permalink
Allows the use of ArrayObject config service with ConfigAbstractFactory
Browse files Browse the repository at this point in the history
  • Loading branch information
Bryan Folliot committed Jan 24, 2017
1 parent 9247ee5 commit 7efae58
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 2 deletions.
5 changes: 3 additions & 2 deletions src/AbstractFactory/ConfigAbstractFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

namespace Zend\ServiceManager\AbstractFactory;

use ArrayObject;
use Zend\ServiceManager\Exception\ServiceNotCreatedException;
use Zend\ServiceManager\Factory\AbstractFactoryInterface;

Expand Down Expand Up @@ -40,8 +41,8 @@ public function __invoke(\Interop\Container\ContainerInterface $container, $requ

$config = $container->get('config');

if (! is_array($config)) {
throw new ServiceNotCreatedException('Config must be an array');
if (! (is_array($config) || $config instanceof ArrayObject)) {
throw new ServiceNotCreatedException('Config must be an array or an instance of ArrayObject');
}

if (! array_key_exists(self::class, $config)) {
Expand Down
34 changes: 34 additions & 0 deletions test/AbstractFactory/ConfigAbstractFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

namespace ZendTest\ServiceManager\AbstractFactory;

use ArrayObject;
use Zend\ServiceManager\AbstractFactory\ConfigAbstractFactory;
use Zend\ServiceManager\Exception\ServiceNotCreatedException;
use Zend\ServiceManager\ServiceManager;
Expand Down Expand Up @@ -76,6 +77,39 @@ public function testCanCreate()
self::assertFalse($abstractFactory->canCreate($serviceManager, ServiceManager::class));
}

public function testCanCreateReturnsTrueWhenConfigIsAnArrayObject()
{
$abstractFactory = new ConfigAbstractFactory();
$serviceManager = new ServiceManager();
$serviceManager->setService(
'config',
new ArrayObject([
ConfigAbstractFactory::class => [
InvokableObject::class => [],
]
])
);

self::assertTrue($abstractFactory->canCreate($serviceManager, InvokableObject::class));
self::assertFalse($abstractFactory->canCreate($serviceManager, ServiceManager::class));
}

public function testFactoryCanCreateInstancesWhenConfigIsAnArrayObject()
{
$abstractFactory = new ConfigAbstractFactory();
$serviceManager = new ServiceManager();
$serviceManager->setService(
'config',
new ArrayObject([
ConfigAbstractFactory::class => [
InvokableObject::class => [],
]
])
);

self::assertInstanceOf(InvokableObject::class, $abstractFactory($serviceManager, InvokableObject::class));
}

public function testInvokeWithInvokableClass()
{
$abstractFactory = new ConfigAbstractFactory();
Expand Down

0 comments on commit 7efae58

Please sign in to comment.