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

Commit 2fa25c9

Browse files
committed
Added more exception rules, more config checking, and more tests
1 parent a0ce3b3 commit 2fa25c9

File tree

2 files changed

+156
-4
lines changed

2 files changed

+156
-4
lines changed

src/AbstractFactory/ConfigAbstractFactory.php

Lines changed: 35 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
namespace Zend\ServiceManager\AbstractFactory;
1111

12+
use Zend\ServiceManager\Exception\ServiceNotCreatedException;
1213
use Zend\ServiceManager\Factory\AbstractFactoryInterface;
1314

1415
final class ConfigAbstractFactory implements AbstractFactoryInterface
@@ -27,18 +28,48 @@ public function canCreate(\Interop\Container\ContainerInterface $container, $req
2728
$config = $container->get('config');
2829
$dependencies = $config[self::class];
2930

30-
return is_array($dependencies) && array_key_exists($requestedName, $dependencies) && is_array(
31-
$dependencies[$requestedName]
32-
);
31+
// config must be array, and have a key for the requested name that's value is also an array
32+
if (!is_array($dependencies)
33+
|| !array_key_exists($requestedName, $dependencies)
34+
|| !is_array($dependencies[$requestedName])
35+
) {
36+
return false;
37+
}
38+
39+
// we can only create this service if the config is an array of strings
40+
return $dependencies[$requestedName] === array_values(array_map('strval', $dependencies[$requestedName]));
41+
3342
}
3443

3544
/**
3645
* {@inheritDoc}
3746
*/
3847
public function __invoke(\Interop\Container\ContainerInterface $container, $requestedName, array $options = null)
3948
{
49+
if (!$container->has('config')) {
50+
throw new ServiceNotCreatedException('Cannot find a config array in the container');
51+
}
52+
4053
$config = $container->get('config');
41-
$dependencies = $config[self::class][$requestedName];
54+
55+
if (!is_array($config)) {
56+
throw new ServiceNotCreatedException('Config must be an array');
57+
}
58+
59+
if (!array_key_exists(self::class, $config)) {
60+
throw new ServiceNotCreatedException('Cannot find a `' . self::class . '` key in the config array');
61+
}
62+
63+
$dependencies = $config[self::class];
64+
65+
if (!is_array($dependencies)
66+
|| !array_key_exists($requestedName, $dependencies)
67+
|| !is_array($dependencies[$requestedName])
68+
|| $dependencies[$requestedName] !== array_values(array_map('strval', $dependencies[$requestedName]))
69+
) {
70+
throw new ServiceNotCreatedException('Dependencies config must exist and be an array of strings');
71+
}
72+
$dependencies = $dependencies[$requestedName];
4273
$arguments = array_map([$container, 'get'], $dependencies);
4374

4475
return new $requestedName(...$arguments);

test/AbstractFactory/ConfigAbstractFactoryTest.php

Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
use ProxyManager\Factory\LazyLoadingValueHolderFactory;
1313
use Zend\ServiceManager\AbstractFactory\ConfigAbstractFactory;
14+
use Zend\ServiceManager\Exception\ServiceNotCreatedException;
1415
use Zend\ServiceManager\Proxy\LazyServiceFactory;
1516
use Zend\ServiceManager\ServiceManager;
1617
use ZendTest\ServiceManager\TestAsset\ComplexDependencyObject;
@@ -43,7 +44,22 @@ public function testCanCreateReturnsFalseIfDependencyNotArrays()
4344
]
4445
]
4546
);
47+
self::assertFalse($abstractFactory->canCreate($serviceManager, InvokableObject::class));
4648

49+
$serviceManager->setAllowOverride(true);
50+
$serviceManager->setService(
51+
'config',
52+
[
53+
ConfigAbstractFactory::class => [
54+
InvokableObject::class => [
55+
'Jabba',
56+
'Gandalf',
57+
'Blofeld',
58+
42
59+
],
60+
]
61+
]
62+
);
4763
self::assertFalse($abstractFactory->canCreate($serviceManager, InvokableObject::class));
4864

4965
}
@@ -145,4 +161,109 @@ public function testInvokeWithComplexArguments()
145161
$abstractFactory($serviceManager, ComplexDependencyObject::class)
146162
);
147163
}
164+
165+
public function testExceptsWhenConfigNotSet()
166+
{
167+
$abstractFactory = new ConfigAbstractFactory();
168+
$serviceManager = new ServiceManager();
169+
self::expectException(ServiceNotCreatedException::class);
170+
self::expectExceptionMessage('Cannot find a config array in the container');
171+
172+
$abstractFactory($serviceManager, 'Dirk_Gently');
173+
}
174+
175+
public function testExceptsWhenConfigKeyNotSet()
176+
{
177+
$abstractFactory = new ConfigAbstractFactory();
178+
$serviceManager = new ServiceManager();
179+
$serviceManager->setService('config', []);
180+
self::expectException(ServiceNotCreatedException::class);
181+
self::expectExceptionMessage('Cannot find a `' . ConfigAbstractFactory::class . '` key in the config array');
182+
183+
$abstractFactory($serviceManager, 'Dirk_Gently');
184+
}
185+
186+
public function testExceptsWhenConfigIsNotArray()
187+
{
188+
$abstractFactory = new ConfigAbstractFactory();
189+
$serviceManager = new ServiceManager();
190+
$serviceManager->setService('config', 'Holistic');
191+
self::expectException(ServiceNotCreatedException::class);
192+
self::expectExceptionMessage('Config must be an array');
193+
194+
$abstractFactory($serviceManager, 'Dirk_Gently');
195+
}
196+
197+
public function testExceptsWhenServiceConfigIsNotArray()
198+
{
199+
$abstractFactory = new ConfigAbstractFactory();
200+
$serviceManager = new ServiceManager();
201+
$serviceManager->setService(
202+
'config',
203+
[
204+
ConfigAbstractFactory::class => 'Detective_Agency'
205+
]
206+
);
207+
self::expectException(ServiceNotCreatedException::class);
208+
self::expectExceptionMessage('Dependencies config must exist and be an array of strings');
209+
210+
$abstractFactory($serviceManager, 'Dirk_Gently');
211+
}
212+
213+
public function testExceptsWhenServiceConfigDoesNotExist()
214+
{
215+
$abstractFactory = new ConfigAbstractFactory();
216+
$serviceManager = new ServiceManager();
217+
$serviceManager->setService(
218+
'config',
219+
[
220+
ConfigAbstractFactory::class => [],
221+
]
222+
);
223+
self::expectException(ServiceNotCreatedException::class);
224+
self::expectExceptionMessage('Dependencies config must exist and be an array of strings');
225+
226+
$abstractFactory($serviceManager, 'Dirk_Gently');
227+
}
228+
229+
public function testExceptsWhenServiceConfigForRequestedNameIsNotArray()
230+
{
231+
$abstractFactory = new ConfigAbstractFactory();
232+
$serviceManager = new ServiceManager();
233+
$serviceManager->setService(
234+
'config',
235+
[
236+
ConfigAbstractFactory::class => [
237+
'DirkGently' => 'Holistic',
238+
],
239+
]
240+
);
241+
self::expectException(ServiceNotCreatedException::class);
242+
self::expectExceptionMessage('Dependencies config must exist and be an array of strings');
243+
244+
$abstractFactory($serviceManager, 'Dirk_Gently');
245+
}
246+
247+
public function testExceptsWhenServiceConfigForRequestedNameIsNotArrayOfStrings()
248+
{
249+
$abstractFactory = new ConfigAbstractFactory();
250+
$serviceManager = new ServiceManager();
251+
$serviceManager->setService(
252+
'config',
253+
[
254+
ConfigAbstractFactory::class => [
255+
'DirkGently' => [
256+
'Holistic',
257+
'Detective',
258+
'Agency',
259+
42
260+
],
261+
],
262+
]
263+
);
264+
self::expectException(ServiceNotCreatedException::class);
265+
self::expectExceptionMessage('Dependencies config must exist and be an array of strings');
266+
267+
$abstractFactory($serviceManager, 'Dirk_Gently');
268+
}
148269
}

0 commit comments

Comments
 (0)