Skip to content

Commit

Permalink
Fix adding mock service to container if not found (#16703)
Browse files Browse the repository at this point in the history
Fix mocking services that are provided by runtime containers
like `ReflectionContainer()`.

Fixes #16700
  • Loading branch information
cnizzardini authored and markstory committed Aug 26, 2022
1 parent 9083c65 commit 77e53e3
Showing 1 changed file with 6 additions and 1 deletion.
7 changes: 6 additions & 1 deletion TestSuite/ContainerStubTrait.php
Expand Up @@ -19,6 +19,7 @@
use Cake\Core\ContainerInterface;
use Cake\Event\EventInterface;
use Closure;
use League\Container\Exception\NotFoundException;
use LogicException;

/**
Expand Down Expand Up @@ -144,7 +145,11 @@ public function modifyContainer(EventInterface $event, ContainerInterface $conta
}
foreach ($this->containerServices as $key => $factory) {
if ($container->has($key)) {
$container->extend($key)->setConcrete($factory);
try {
$container->extend($key)->setConcrete($factory);
} catch (NotFoundException $e) {
$container->add($key, $factory);
}
} else {
$container->add($key, $factory);
}
Expand Down

0 comments on commit 77e53e3

Please sign in to comment.