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

Commit 224a028

Browse files
committed
Improved test coverage
Ran coverage reports and identified uncovered paths. Also, modified `createLazyServiceDelegatorFactory()` to raise ServiceNotCreatedException for missing class_map, as this method is called during actual service creation.
1 parent e2eb462 commit 224a028

File tree

3 files changed

+129
-9
lines changed

3 files changed

+129
-9
lines changed

src/ServiceManager.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -498,6 +498,8 @@ private function doCreate($resolvedName, array $options = null)
498498
* configuration present.
499499
*
500500
* @return Proxy\LazyServiceFactory
501+
* @throws ServiceNotCreatedException when the lazy service class_map
502+
* configuration is missing
501503
*/
502504
private function createLazyServiceDelegatorFactory()
503505
{
@@ -506,7 +508,7 @@ private function createLazyServiceDelegatorFactory()
506508
}
507509

508510
if (! isset($this->lazyServices['class_map'])) {
509-
throw new Exception\InvalidArgumentException('Missing "class_map" config key in "lazy_services"');
511+
throw new ServiceNotCreatedException('Missing "class_map" config key in "lazy_services"');
510512
}
511513

512514
$factoryConfig = new ProxyConfiguration();

test/CommonServiceLocatorBehaviorsTrait.php

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -471,13 +471,22 @@ public function invalidFactories()
471471
];
472472
}
473473

474+
public function invalidAbstractFactories()
475+
{
476+
$factories = $this->invalidFactories();
477+
$factories['non-class-string'] = ['non-callable-string', 'valid class name'];
478+
return $factories;
479+
}
480+
474481
/**
475-
* @dataProvider invalidFactories
482+
* @dataProvider invalidAbstractFactories
476483
* @covers \Zend\ServiceManager\ServiceManager::configure
477484
*/
478-
public function testPassingInvalidAbstractFactoryTypeViaConfigurationRaisesException($factory)
479-
{
480-
$this->setExpectedException(InvalidArgumentException::class, 'invalid abstract factory');
485+
public function testPassingInvalidAbstractFactoryTypeViaConfigurationRaisesException(
486+
$factory,
487+
$contains = 'invalid abstract factory'
488+
) {
489+
$this->setExpectedException(InvalidArgumentException::class, $contains);
481490
$serviceManager = $this->createContainer([
482491
'abstract_factories' => [
483492
$factory,
@@ -502,13 +511,22 @@ public function testCanSpecifyInitializerUsingStringViaConfiguration()
502511
$this->assertEquals('bar', $instance->foo, '"foo" property was not properly injected');
503512
}
504513

514+
public function invalidInitializers()
515+
{
516+
$factories = $this->invalidFactories();
517+
$factories['non-class-string'] = ['non-callable-string', 'valid function name or class name'];
518+
return $factories;
519+
}
520+
505521
/**
506-
* @dataProvider invalidFactories
522+
* @dataProvider invalidInitializers
507523
* @covers \Zend\ServiceManager\ServiceManager::configure
508524
*/
509-
public function testPassingInvalidInitializerTypeViaConfigurationRaisesException($initializer)
510-
{
511-
$this->setExpectedException(InvalidArgumentException::class, 'invalid initializer');
525+
public function testPassingInvalidInitializerTypeViaConfigurationRaisesException(
526+
$initializer,
527+
$contains = 'invalid initializer'
528+
) {
529+
$this->setExpectedException(InvalidArgumentException::class, $contains);
512530
$serviceManager = $this->createContainer([
513531
'initializers' => [
514532
$initializer,

test/LazyServiceIntegrationTest.php

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
use RecursiveIteratorIterator;
1515
use RecursiveRegexIterator;
1616
use RegexIterator;
17+
use Zend\ServiceManager\Exception\ServiceNotCreatedException;
18+
use Zend\ServiceManager\Exception\ServiceNotFoundException;
1719
use Zend\ServiceManager\Factory\InvokableFactory;
1820
use Zend\ServiceManager\Proxy\LazyServiceFactory;
1921
use Zend\ServiceManager\ServiceManager;
@@ -146,4 +148,102 @@ public function testCanUseLazyServiceFactoryFactoryToCreateLazyServiceFactoryToA
146148
);
147149
$this->assertEquals(['foo' => 'bar'], $options, 'Options returned do not match configuration');
148150
}
151+
152+
/**
153+
* @covers \Zend\ServiceManager\ServiceManager::createLazyServiceDelegatorFactory
154+
*/
155+
public function testMissingClassMapRaisesExceptionOnAttemptToRetrieveLazyService()
156+
{
157+
$config = [
158+
'lazy_services' => [
159+
],
160+
'factories' => [
161+
InvokableObject::class => InvokableFactory::class,
162+
],
163+
'delegators' => [
164+
InvokableObject::class => [LazyServiceFactory::class],
165+
],
166+
];
167+
168+
$container = new ServiceManager($config);
169+
$this->setExpectedException(ServiceNotCreatedException::class, 'class_map');
170+
$container->get(InvokableObject::class);
171+
}
172+
173+
/**
174+
* @covers \Zend\ServiceManager\ServiceManager::createLazyServiceDelegatorFactory
175+
*/
176+
public function testWillNotGenerateProxyClassFilesByDefault()
177+
{
178+
$config = [
179+
'lazy_services' => [
180+
'class_map' => [
181+
InvokableObject::class => InvokableObject::class,
182+
],
183+
'proxies_namespace' => 'TestAssetProxy',
184+
],
185+
'factories' => [
186+
InvokableObject::class => InvokableFactory::class,
187+
],
188+
'delegators' => [
189+
InvokableObject::class => [LazyServiceFactory::class],
190+
],
191+
];
192+
193+
$this->assertProxyDirEmpty();
194+
195+
$container = new ServiceManager($config);
196+
$instance = $container->build(InvokableObject::class, ['foo' => 'bar']);
197+
198+
// This is the important test
199+
$this->assertProxyDirEmpty('Expected proxy directory to remain empty when write_proxy_files disabled');
200+
201+
// Test we got a usable proxy
202+
$this->assertInstanceOf(
203+
InvokableObject::class,
204+
$instance,
205+
'Service returned does not extend ' . InvokableObject::class
206+
);
207+
$this->assertContains(
208+
'TestAssetProxy',
209+
get_class($instance),
210+
'Service returned does not contain expected namespace'
211+
);
212+
213+
// Test proxying works as expected
214+
$options = $instance->getOptions();
215+
$this->assertInternalType(
216+
'array',
217+
$options,
218+
sprintf(
219+
'Expected an array of options; %s received',
220+
(is_object($options) ? get_class($options) : gettype($options))
221+
)
222+
);
223+
$this->assertEquals(['foo' => 'bar'], $options, 'Options returned do not match configuration');
224+
}
225+
226+
public function testRaisesServiceNotFoundExceptionIfRequestedLazyServiceIsNotInClassMap()
227+
{
228+
$config = [
229+
'lazy_services' => [
230+
'class_map' => [
231+
stdClass::class => stdClass::class,
232+
],
233+
'proxies_namespace' => 'TestAssetProxy',
234+
],
235+
'factories' => [
236+
InvokableObject::class => InvokableFactory::class,
237+
],
238+
'delegators' => [
239+
InvokableObject::class => [LazyServiceFactory::class],
240+
],
241+
];
242+
243+
$this->assertProxyDirEmpty();
244+
245+
$container = new ServiceManager($config);
246+
$this->setExpectedException(ServiceNotFoundException::class, 'not found in the provided services map');
247+
$instance = $container->build(InvokableObject::class, ['foo' => 'bar']);
248+
}
149249
}

0 commit comments

Comments
 (0)