Skip to content

Commit

Permalink
Fix #31: Add support for objects as default parameter values
Browse files Browse the repository at this point in the history
  • Loading branch information
vjik committed Dec 19, 2021
1 parent 6d3e469 commit 511dd14
Show file tree
Hide file tree
Showing 9 changed files with 87 additions and 3 deletions.
File renamed without changes.
1 change: 1 addition & 0 deletions .github/workflows/build.yml
Expand Up @@ -24,6 +24,7 @@ jobs:
php:
- "7.4"
- "8.0"
- "8.1"

steps:
- name: Checkout
Expand Down
1 change: 1 addition & 0 deletions .github/workflows/static.yml
Expand Up @@ -20,6 +20,7 @@ jobs:
php:
- "7.4"
- "8.0"
- "8.1"

steps:
- name: Checkout
Expand Down
4 changes: 2 additions & 2 deletions CHANGELOG.md
Expand Up @@ -2,8 +2,8 @@

## 1.0.1 under development

- no changes in this release.
- Bug #31: Add support for objects as default parameter values (vjik)

## 1.0.0 November 30, 2021

- Initial release.
- Initial release.
1 change: 1 addition & 0 deletions phpunit.xml.dist
Expand Up @@ -19,6 +19,7 @@
<testsuite name="Yii Definitions tests">
<directory>./tests/Unit</directory>
<directory phpVersion="8" phpVersionOperator=">=">./tests/Php8</directory>
<directory phpVersion="8.1" phpVersionOperator=">=">./tests/Php8_1</directory>
</testsuite>
</testsuites>

Expand Down
2 changes: 1 addition & 1 deletion src/ParameterDefinition.php
Expand Up @@ -84,7 +84,7 @@ public function resolve(ContainerInterface $container)
$result = $container->get($typeName);
} catch (Throwable $t) {
if ($this->parameter->isOptional()) {
return null;
return $this->parameter->getDefaultValue();
}
throw $t;
}
Expand Down
63 changes: 63 additions & 0 deletions tests/Php8_1/Helpers/DefinitionExtractorTest.php
@@ -0,0 +1,63 @@
<?php

declare(strict_types=1);

namespace Yiisoft\Definitions\Tests\Php8_1\Helpers;

use PHPUnit\Framework\TestCase;
use ReflectionFunction;
use Yiisoft\Definitions\Helpers\DefinitionExtractor;
use Yiisoft\Definitions\Tests\Support\Chair;
use Yiisoft\Definitions\Tests\Support\RedChair;
use Yiisoft\Test\Support\Container\SimpleContainer;

final class DefinitionExtractorTest extends TestCase
{
public function testResolvableDependencyWithDefaultObject(): void
{
$container = new SimpleContainer([
Chair::class => new Chair(),
]);

$definitions = DefinitionExtractor::fromFunction(
new ReflectionFunction(static fn (Chair $chair = new RedChair()) => true)
);

$this->assertInstanceOf(Chair::class, $definitions['chair']->resolve($container));
}

public function testResolvableNullableDependencyWithDefaultObject(): void
{
$container = new SimpleContainer([
Chair::class => new Chair(),
]);

$definitions = DefinitionExtractor::fromFunction(
new ReflectionFunction(static fn (?Chair $chair = new RedChair()) => true)
);

$this->assertInstanceOf(Chair::class, $definitions['chair']->resolve($container));
}

public function testUnresolvableDependencyWithDefaultObject(): void
{
$container = new SimpleContainer();

$definitions = DefinitionExtractor::fromFunction(
new ReflectionFunction(static fn (Chair $chair = new RedChair()) => true)
);

$this->assertInstanceOf(RedChair::class, $definitions['chair']->resolve($container));
}

public function testUnresolvablNullableDependencyWithDefaultObject(): void
{
$container = new SimpleContainer();

$definitions = DefinitionExtractor::fromFunction(
new ReflectionFunction(static fn (?Chair $chair = new RedChair()) => true)
);

$this->assertInstanceOf(RedChair::class, $definitions['chair']->resolve($container));
}
}
9 changes: 9 additions & 0 deletions tests/Support/Chair.php
@@ -0,0 +1,9 @@
<?php

declare(strict_types=1);

namespace Yiisoft\Definitions\Tests\Support;

class Chair
{
}
9 changes: 9 additions & 0 deletions tests/Support/RedChair.php
@@ -0,0 +1,9 @@
<?php

declare(strict_types=1);

namespace Yiisoft\Definitions\Tests\Support;

final class RedChair
{
}

0 comments on commit 511dd14

Please sign in to comment.