|
12 | 12 | use Psr\Container\ContainerInterface;
|
13 | 13 | use ReflectionObject;
|
14 | 14 | use stdClass;
|
| 15 | +use Zend\ServiceManager\Exception\ServiceNotCreatedException; |
| 16 | +use Zend\ServiceManager\Exception\ServiceNotFoundException; |
15 | 17 | use Zend\ServiceManager\Factory\AbstractFactoryInterface;
|
| 18 | +use Zend\ServiceManager\Factory\ContainerException; |
| 19 | +use Zend\ServiceManager\Factory\DelegatorFactoryInterface; |
16 | 20 | use Zend\ServiceManager\Factory\FactoryInterface;
|
17 | 21 | use Zend\ServiceManager\Factory\InvokableFactory;
|
| 22 | +use Zend\ServiceManager\Proxy\LazyServiceFactory; |
18 | 23 | use Zend\ServiceManager\ServiceManager;
|
19 | 24 | use ZendTest\ServiceManager\TestAsset\InvokableObject;
|
20 | 25 | use ZendTest\ServiceManager\TestAsset\SimpleServiceManager;
|
21 | 26 |
|
22 | 27 | use function get_class;
|
23 | 28 |
|
| 29 | +interface DecorableInterface |
| 30 | +{} |
| 31 | +class DecorableClass implements DecorableInterface |
| 32 | +{ |
| 33 | + |
| 34 | +} |
| 35 | + |
24 | 36 | /**
|
25 | 37 | * @covers \Zend\ServiceManager\ServiceManager
|
26 | 38 | */
|
@@ -371,27 +383,46 @@ public function testResolvedAliasNoMatchingAbstractFactoryReturnsFalse()
|
371 | 383 | */
|
372 | 384 | public function testConfigureMultipleTimes()
|
373 | 385 | {
|
| 386 | + $delegatorFactory = new class { |
| 387 | + public function __invoke( |
| 388 | + ContainerInterface $container, |
| 389 | + $name, |
| 390 | + callable $callback |
| 391 | + ) { |
| 392 | + /** @var InvokableObject $instance */ |
| 393 | + $instance = $callback(); |
| 394 | + $inc = $instance->getOptions()['inc'] ?? 0; |
| 395 | + |
| 396 | + return new InvokableObject(['inc' => ++$inc]); |
| 397 | + } |
| 398 | + }; |
| 399 | + |
374 | 400 | $config = [
|
| 401 | + 'factories' => [ |
| 402 | + 'Foo' => function () { |
| 403 | + return new InvokableObject(); |
| 404 | + }, |
| 405 | + ], |
375 | 406 | 'delegators' => [
|
376 |
| - 'Foo' => 'FooDelegator', |
| 407 | + 'Foo' => [ |
| 408 | + $delegatorFactory, |
| 409 | + LazyServiceFactory::class, |
| 410 | + ], |
377 | 411 | ],
|
378 | 412 | 'lazy_services' => [
|
379 | 413 | 'class_map' => [
|
380 |
| - 'Foo' => 'Foo', |
| 414 | + 'Foo' => InvokableObject::class, |
381 | 415 | ],
|
382 |
| - ] |
| 416 | + ], |
383 | 417 | ];
|
384 | 418 |
|
385 | 419 | $serviceManager = new ServiceManager($config);
|
386 | 420 | $serviceManager->configure($config);
|
387 | 421 |
|
388 |
| - $ref = new ReflectionObject($serviceManager); |
389 |
| - $delegatorsProperty = $ref->getProperty('delegators'); |
390 |
| - $delegatorsProperty->setAccessible(true); |
391 |
| - $lazyServicesProperty = $ref->getProperty('lazyServices'); |
392 |
| - $lazyServicesProperty->setAccessible(true); |
| 422 | + /** @var InvokableObject $instance */ |
| 423 | + $instance = $serviceManager->get('Foo'); |
393 | 424 |
|
394 |
| - self::assertSame($config['delegators'], $delegatorsProperty->getValue($serviceManager)); |
395 |
| - self::assertSame($config['lazy_services'], $lazyServicesProperty->getValue($serviceManager)); |
| 425 | + self::assertInstanceOf(InvokableObject::class, $instance); |
| 426 | + self::assertSame(1, $instance->getOptions()['inc']); |
396 | 427 | }
|
397 | 428 | }
|
0 commit comments