can modify DI container services at runtime
This commit is contained in:
parent
b1f8b5dc64
commit
10e0afa20f
@ -5,6 +5,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
|
|||||||
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
|
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
|
||||||
|
|
||||||
## [Unreleased](https://git.d3data.de/D3Public/TestingTools/compare/1.0.0.0...rel_1.x)
|
## [Unreleased](https://git.d3data.de/D3Public/TestingTools/compare/1.0.0.0...rel_1.x)
|
||||||
|
- can modify DI container services in runtime
|
||||||
|
|
||||||
## [1.0.0.0](https://git.d3data.de/D3Public/TestingTools/releases/tag/1.0.0.0) - 2022-11-11
|
## [1.0.0.0](https://git.d3data.de/D3Public/TestingTools/releases/tag/1.0.0.0) - 2022-11-11
|
||||||
### Added
|
### Added
|
||||||
|
@ -17,6 +17,8 @@ declare(strict_types=1);
|
|||||||
|
|
||||||
namespace D3\TestingTools\Development;
|
namespace D3\TestingTools\Development;
|
||||||
|
|
||||||
|
use OxidEsales\EshopCommunity\Internal\Container\ContainerBuilderFactory;
|
||||||
|
use OxidEsales\EshopCommunity\Internal\Container\ContainerFactory;
|
||||||
use PHPUnit\Framework\MockObject\MockObject;
|
use PHPUnit\Framework\MockObject\MockObject;
|
||||||
use ReflectionClass;
|
use ReflectionClass;
|
||||||
use ReflectionException;
|
use ReflectionException;
|
||||||
@ -109,4 +111,40 @@ trait CanAccessRestricted
|
|||||||
$property->setAccessible(true);
|
$property->setAccessible(true);
|
||||||
return $property->getValue($object);
|
return $property->getValue($object);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* use \OxidEsales\EshopCommunity\Internal\Container\ContainerFactory::resetContainer() to undo these modifications
|
||||||
|
* @param array<string, object> $services
|
||||||
|
* @return void
|
||||||
|
* @throws ReflectionException
|
||||||
|
*/
|
||||||
|
public function addServiceMocks(array $services): void
|
||||||
|
{
|
||||||
|
$builder = (new ContainerBuilderFactory())->create()->getContainer();
|
||||||
|
|
||||||
|
array_walk($services, function ($service, $serviceId) use ($builder) {
|
||||||
|
if ($builder->has($serviceId)) {
|
||||||
|
$builder->set($serviceId, $service);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
$builder->compile();
|
||||||
|
$container = ContainerFactory::getInstance();
|
||||||
|
$reflection = new ReflectionClass($container);
|
||||||
|
$property = $reflection->getProperty($this->getDIContainerPropertyName($reflection));
|
||||||
|
$property->setValue($container, $builder);
|
||||||
|
}
|
||||||
|
|
||||||
|
protected function getDIContainerPropertyName(ReflectionClass $containerReflection): string
|
||||||
|
{
|
||||||
|
$property = current(array_filter($containerReflection->getProperties(), function (ReflectionProperty $property) {
|
||||||
|
return stristr($property->getName(), 'container');
|
||||||
|
}));
|
||||||
|
|
||||||
|
if (!is_object($property) || !$property instanceof ReflectionProperty) {
|
||||||
|
throw new \RuntimeException("can't find container property");
|
||||||
|
}
|
||||||
|
|
||||||
|
return $property->getName();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user