Skip to content

Commit

Permalink
Merge pull request #8 from lion-packages/new
Browse files Browse the repository at this point in the history
Extended support for dependency injection in Subclasses
  • Loading branch information
Sleon4 authored Mar 3, 2024
2 parents 72a0aca + dff25be commit 63e443d
Show file tree
Hide file tree
Showing 5 changed files with 104 additions and 9 deletions.
10 changes: 8 additions & 2 deletions src/DependencyInjection/Container.php
Original file line number Diff line number Diff line change
Expand Up @@ -120,12 +120,18 @@ private function getParameters(ReflectionFunctionAbstract $method, array $params

foreach ($method->getParameters() as $parameter) {
if ($parameter->isDefaultValueAvailable()) {
$args[] = $parameter->getDefaultValue();
if (!empty($params[$parameter->getName()])) {
$args[] = $params[$parameter->getName()];
} else {
$args[] = $parameter->getDefaultValue();
}
} else {
if (!empty($params[$parameter->getName()])) {
$args[] = $params[$parameter->getName()];
} else {
$args[] = $this->container->get($this->getParameterClassName($parameter));
$args[] = $this->injectDependencies(
$this->container->get($this->getParameterClassName($parameter))
);
}
}
}
Expand Down
53 changes: 46 additions & 7 deletions tests/ContainerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ class ContainerTest extends Test
use ContainerProviderTrait;

const STR = 'test';
const DEFAULT_VALUE = 'default-value';
const FOLDER = './tests/';
const PATH_FILE = './Provider/CustomClass.php';
const FILES = [
Expand All @@ -29,7 +30,8 @@ class ContainerTest extends Test
'./tests/Provider/ContainerProviderTrait.php',
'./tests/Provider/CustomClass.php',
'./tests/Provider/ExtendsProvider.php',
'./tests/Provider/FactoryProvider.php'
'./tests/Provider/FactoryProvider.php',
'./tests/Provider/SubClassProvider.php'
];
const REFLECTION_PARAMETERS = [CustomClass::class, 'setFactoryProvider'];

Expand Down Expand Up @@ -81,27 +83,48 @@ public function testGetParameters(): void
{
$parameters = $this->getPrivateMethod(
'getParameters',
[new ReflectionMethod(new CustomClass(), 'setFactoryProvider')
]);
[new ReflectionMethod(new CustomClass(), 'setFactoryProvider')]
);

$this->assertIsArray($parameters);
$this->assertInstanceOf(FactoryProvider::class, reset($parameters));
}

public function testGetParametersWithDefaultValue(): void
{
$parameters = $this->getPrivateMethod(
'getParameters',
[new ReflectionMethod(new CustomClass(), 'setMultiple')
]);
[new ReflectionMethod(new CustomClass(), 'setMultiple')]
);

$this->assertIsArray($parameters);

$first = reset($parameters);
$second = end($parameters);

$this->assertInstanceOf(FactoryProvider::class, $first);
$this->assertInstanceOf(FactoryProvider::class, reset($parameters));
$this->assertIsString($second);
$this->assertSame(self::STR, $second);
}

public function testGetParametersWithDefaultDeclaredValue(): void
{
$parameters = $this->getPrivateMethod(
'getParameters',
[
new ReflectionMethod(new CustomClass(), 'setMultiple'),
['str' => self::DEFAULT_VALUE]
]
);

$this->assertIsArray($parameters);

$second = end($parameters);

$this->assertInstanceOf(FactoryProvider::class, reset($parameters));
$this->assertIsString($second);
$this->assertSame(self::DEFAULT_VALUE, $second);
}

public function testInjectDependenciesMethod(): void
{
/** @var FactoryProvider $factoryProvider */
Expand Down Expand Up @@ -163,6 +186,22 @@ public function testInjectDependenciesWithExtendsClass(): void
$this->assertInstanceOf(FactoryProvider::class, $classProvider->getFactoryProvider());
}

public function testInjectDependenciesWithSubDependencies(): void
{
/** @var ClassProvider $classProvider */
$classProvider = $this->container->injectDependencies(new ClassProvider);

$str = $classProvider
->getSubClassProvider()
->getExtendsProvider()
->getFactoryProviderExtends()
->setStr(self::STR)
->getStr();

$this->assertIsString($str);
$this->assertSame(self::STR, $str);
}

public function testGetParameterClassName()
{
$reflectionParameter = new ReflectionParameter(self::REFLECTION_PARAMETERS, 'factoryProvider');
Expand Down
18 changes: 18 additions & 0 deletions tests/Provider/ClassProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,27 @@
namespace Tests\Provider;

use Tests\Provider\ExtendsProvider;
use Tests\Provider\SubClassProvider;

class ClassProvider extends ExtendsProvider
{
private SubClassProvider $subClassProvider;

/**
* @required
*/
public function setSubClassProvider(SubClassProvider $subClassProvider): ClassProvider
{
$this->subClassProvider = $subClassProvider;

return $this;
}

public function getSubClassProvider(): SubClassProvider
{
return $this->subClassProvider;
}

public function getFactoryProvider(): FactoryProvider
{
return parent::getFactoryProvider();
Expand Down
5 changes: 5 additions & 0 deletions tests/Provider/ExtendsProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@ public function setFactoryProvider(FactoryProvider $factoryProvider): void
$this->factoryProvider = $factoryProvider;
}

public function getFactoryProviderExtends(): FactoryProvider
{
return $this->factoryProvider;
}

protected function getFactoryProvider(): FactoryProvider
{
return $this->factoryProvider;
Expand Down
27 changes: 27 additions & 0 deletions tests/Provider/SubClassProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

declare(strict_types=1);

namespace Tests\Provider;

use Tests\Provider\ExtendsProvider;

class SubClassProvider
{
private ExtendsProvider $extendsProvider;

/**
* @required
*/
public function setExtendsProvider(ExtendsProvider $extendsProvider): SubClassProvider
{
$this->extendsProvider = $extendsProvider;

return $this;
}

public function getExtendsProvider(): ExtendsProvider
{
return $this->extendsProvider;
}
}

0 comments on commit 63e443d

Please sign in to comment.