Neos\Flow\Tests\BaseTestCase::inject PHP Метод

inject() защищенный Метод

This is a convenience method for setting a protected or private property in a test subject for the purpose of injecting a dependency.
protected inject ( object $target, string $name, mixed $dependency ) : void
$target object The instance which needs the dependency
$name string Name of the property to be injected
$dependency mixed The dependency to inject – usually an object but can also be any other type
Результат void
    protected function inject($target, $name, $dependency)
    {
        if (!is_object($target)) {
            throw new \InvalidArgumentException('Wrong type for argument $target, must be object.');
        }
        $objectReflection = new \ReflectionObject($target);
        $methodNamePart = strtoupper($name[0]) . substr($name, 1);
        if ($objectReflection->hasMethod('set' . $methodNamePart)) {
            $methodName = 'set' . $methodNamePart;
            $target->{$methodName}($dependency);
        } elseif ($objectReflection->hasMethod('inject' . $methodNamePart)) {
            $methodName = 'inject' . $methodNamePart;
            $target->{$methodName}($dependency);
        } elseif ($objectReflection->hasProperty($name)) {
            $property = $objectReflection->getProperty($name);
            $property->setAccessible(true);
            $property->setValue($target, $dependency);
        } else {
            throw new \RuntimeException('Could not inject ' . $name . ' into object of type ' . get_class($target));
        }
    }