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

Commit 255d814

Browse files
committed
Provides additional delegator tests for expected behaviors with non-existent/invalid service definitions
- When an invokable mapping is invalid, but the `$callback` provided to a delegator is never called, the return value of the delegator is expected. - When an invokable mapping is invalid, and the `$callback` provided to a delegator is called, an exception is expected. - When an factory for a given service is invalid, but the `$callback` provided to a delegator is never called, the return value of the delegator is expected. - When a factory for a given service is invalid, and the `$callback` provided to a delegator is called, an exception is expected.
1 parent b3067f8 commit 255d814

File tree

1 file changed

+202
-0
lines changed

1 file changed

+202
-0
lines changed

src/DelegatorTestTrait.php

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

1010
namespace Zend\ContainerConfigTest;
1111

12+
use Error;
1213
use Generator;
1314
use Psr\Container\ContainerExceptionInterface;
1415
use Throwable;
@@ -479,4 +480,205 @@ public function testDelegatorFactoriesTriggerForFactoryBackedServicesUsingAnyFac
479480
// Retrieving a second time should retrieve the same instance.
480481
self::assertSame($instance, $container->get('service'));
481482
}
483+
484+
// @codingStandardsIgnoreStart
485+
public function testWhenInvokableWithDelegatorsResolvesToNonExistentClassNoExceptionIsRaisedIfCallbackNeverInvoked()
486+
{
487+
// @codingStandardsIgnoreEnd
488+
$container = $this->createContainer([
489+
'invokables' => [
490+
TestAsset\NonExistent::class,
491+
],
492+
'delegators' => [
493+
TestAsset\NonExistent::class => [
494+
TestAsset\DelegatorFactory::class,
495+
],
496+
],
497+
]);
498+
499+
self::assertTrue($container->has(TestAsset\NonExistent::class));
500+
$instance = $container->get(TestAsset\NonExistent::class);
501+
self::assertInstanceOf(TestAsset\Delegator::class, $instance);
502+
}
503+
504+
// @codingStandardsIgnoreStart
505+
public function testWhenInvokableWithDelegatorsResolvesToInvalidClassAnExceptionIsRaisedIfCallbackNeverInvoked()
506+
{
507+
// @codingStandardsIgnoreEnd
508+
$container = $this->createContainer([
509+
'invokables' => [
510+
TestAsset\FactoryWithRequiredParameters::class,
511+
],
512+
'delegators' => [
513+
TestAsset\FactoryWithRequiredParameters::class => [
514+
TestAsset\DelegatorFactory::class,
515+
],
516+
],
517+
]);
518+
519+
self::assertTrue($container->has(TestAsset\FactoryWithRequiredParameters::class));
520+
$instance = $container->get(TestAsset\FactoryWithRequiredParameters::class);
521+
self::assertInstanceOf(TestAsset\Delegator::class, $instance);
522+
}
523+
524+
public function testWhenInvokableWithDelegatorsResolvesToNonExistentClassAnExceptionIsRaisedWhenCallbackIsInvoked()
525+
{
526+
$container = $this->createContainer([
527+
'invokables' => [
528+
TestAsset\NonExistent::class,
529+
],
530+
'delegators' => [
531+
TestAsset\NonExistent::class => [
532+
TestAsset\Delegator1Factory::class,
533+
],
534+
],
535+
]);
536+
537+
self::assertTrue($container->has(TestAsset\NonExistent::class));
538+
539+
$caught = false;
540+
try {
541+
$container->get(TestAsset\NonExistent::class);
542+
} catch (Throwable $e) {
543+
if (! $e instanceof Error && ! $e instanceof TypeError && ! $e instanceof ContainerExceptionInterface) {
544+
$this->fail(sprintf(
545+
'Throwable of type %s (%s) was raised; expected Error, TypeError, or %s',
546+
get_class($e),
547+
$e->getMessage(),
548+
ContainerExceptionInterface::class
549+
));
550+
}
551+
$caught = true;
552+
}
553+
554+
$this->assertTrue($caught, 'No TypeError or ContainerExceptionInterface thrown when one was expected');
555+
}
556+
557+
// @codingStandardsIgnoreStart
558+
public function testWhenInvokableWithDelegatorsResolvesToInvalidFactoryClassAnExceptionIsRaisedWhenCallbackIsInvoked()
559+
{
560+
// @codingStandardsIgnoreEnd
561+
$container = $this->createContainer([
562+
'invokables' => [
563+
TestAsset\FactoryWithRequiredParameters::class,
564+
],
565+
'delegators' => [
566+
'service' => [
567+
TestAsset\Delegator1Factory::class,
568+
],
569+
],
570+
]);
571+
572+
self::assertTrue($container->has(TestAsset\FactoryWithRequiredParameters::class));
573+
574+
$caught = false;
575+
try {
576+
$container->get(TestAsset\FactoryWithRequiredParameters::class);
577+
} catch (Throwable $e) {
578+
if (! $e instanceof TypeError && ! $e instanceof ContainerExceptionInterface) {
579+
$this->fail(sprintf(
580+
'Throwable of type %s (%s) was raised; expected TypeError or %s',
581+
get_class($e),
582+
$e->getMessage(),
583+
ContainerExceptionInterface::class
584+
));
585+
}
586+
$caught = true;
587+
}
588+
589+
$this->assertTrue($caught, 'No TypeError or ContainerExceptionInterface thrown when one was expected');
590+
}
591+
592+
// @codingStandardsIgnoreStart
593+
public function testWhenServiceWithDelegatorsResolvesToNonExistentFactoryClassNoExceptionIsRaisedIfCallbackNeverInvoked()
594+
{
595+
// @codingStandardsIgnoreEnd
596+
$container = $this->createContainer([
597+
'factories' => [
598+
'service' => TestAsset\NonExistentFactory::class,
599+
],
600+
'delegators' => [
601+
'service' => [
602+
TestAsset\DelegatorFactory::class,
603+
],
604+
],
605+
]);
606+
607+
self::assertTrue($container->has('service'));
608+
$instance = $container->get('service');
609+
self::assertInstanceOf(TestAsset\Delegator::class, $instance);
610+
}
611+
612+
// @codingStandardsIgnoreStart
613+
public function testWhenServiceWithDelegatorsResolvesToInvalidFactoryClassAnExceptionIsRaisedIfCallbackNeverInvoked()
614+
{
615+
// @codingStandardsIgnoreEnd
616+
$container = $this->createContainer([
617+
'factories' => [
618+
'service' => TestAsset\FactoryWithRequiredParameters::class,
619+
],
620+
'delegators' => [
621+
'service' => [
622+
TestAsset\DelegatorFactory::class,
623+
],
624+
],
625+
]);
626+
627+
self::assertTrue($container->has('service'));
628+
$instance = $container->get('service');
629+
self::assertInstanceOf(TestAsset\Delegator::class, $instance);
630+
}
631+
632+
// @codingStandardsIgnoreStart
633+
public function testWhenServiceWithDelegatorsResolvesToNonExistentFactoryClassAnExceptionIsRaisedWhenCallbackIsInvoked()
634+
{
635+
// @codingStandardsIgnoreEnd
636+
$container = $this->createContainer([
637+
'factories' => [
638+
'service' => TestAsset\NonExistentFactory::class,
639+
],
640+
'delegators' => [
641+
'service' => [
642+
TestAsset\Delegator1Factory::class,
643+
],
644+
],
645+
]);
646+
647+
self::assertTrue($container->has('service'));
648+
$this->expectException(ContainerExceptionInterface::class);
649+
$container->get('service');
650+
}
651+
652+
public function testWhenServiceWithDelegatorsResolvesToInvalidFactoryClassAnExceptionIsRaisedWhenCallbackIsInvoked()
653+
{
654+
$container = $this->createContainer([
655+
'factories' => [
656+
'service' => TestAsset\FactoryWithRequiredParameters::class,
657+
],
658+
'delegators' => [
659+
'service' => [
660+
TestAsset\Delegator1Factory::class,
661+
],
662+
],
663+
]);
664+
665+
self::assertTrue($container->has('service'));
666+
667+
$caught = false;
668+
try {
669+
$container->get('service');
670+
} catch (Throwable $e) {
671+
if (! $e instanceof TypeError && ! $e instanceof ContainerExceptionInterface) {
672+
$this->fail(sprintf(
673+
'Throwable of type %s (%s) was raised; expected TypeError or %s',
674+
get_class($e),
675+
$e->getMessage(),
676+
ContainerExceptionInterface::class
677+
));
678+
}
679+
$caught = true;
680+
}
681+
682+
$this->assertTrue($caught, 'No TypeError or ContainerExceptionInterface thrown when one was expected');
683+
}
482684
}

0 commit comments

Comments
 (0)