Skip to content

Commit

Permalink
FIX: non built type with default value
Browse files Browse the repository at this point in the history
  • Loading branch information
gravataLonga committed May 12, 2020
1 parent 240d06a commit 3e81fd3
Show file tree
Hide file tree
Showing 5 changed files with 110 additions and 6 deletions.
26 changes: 25 additions & 1 deletion README.md
Expand Up @@ -178,7 +178,7 @@ $container->set(FooBar::class, function () {
$container->get(Test::class); // FooBar it will inject into Test class.
```

**Note:** We only support resolving auto wiring argument on construction if they is binded into container. Otherwise it will throw an exception.
**Note:** We only support resolving auto wiring argument on construction if they is binded into container. Otherwise it will throw an exception if can't find entry into container.

### Using Built in type

Expand Down Expand Up @@ -221,6 +221,30 @@ $container = new Container();
$container->get(Test::class); // null it will inject into Test class.
```

In also attempt to resolve auto wiring of construction by it's default value, it will check default value of `__construction` and it will pass that default value.

First case, if value is a simple built in type value.
```php
use Gravatalonga\Container\Container;

class Test
{
/**
* @var string
*/
private $name;

public function __construct($name = 'Jonathan Fontes')
{
$this->name = $name;
}
}

$container = new Container();

$container->get(Test::class); // 'Jonathan Fontes' it will pass into container...
```

## Change log

Please see [CHANGELOG](CHANGELOG.md) for more information on what has changed recently.
Expand Down
10 changes: 7 additions & 3 deletions src/Container.php
Expand Up @@ -124,8 +124,8 @@ public static function getInstance()
public function has($id)
{
return array_key_exists($id, $this->bindings) ||
array_key_exists($id, $this->share) ||
array_key_exists($id, $this->aliases);
array_key_exists($id, $this->share) ||
array_key_exists($id, $this->aliases);
}

/**
Expand Down Expand Up @@ -253,7 +253,11 @@ function (ReflectionParameter $param) use ($arguments) {
return $this->get($param->getName());
}

throw ContainerException::findType(null);
if (false === $param->isOptional()) {
throw ContainerException::findType(null);
}

return $param->getDefaultValue();
}

if (true === $type->isBuiltin()) {
Expand Down
49 changes: 47 additions & 2 deletions tests/ResolveConstructionTest.php
Expand Up @@ -14,6 +14,8 @@
use Tests\Stub\FooBarClass;
use Tests\Stub\FooBarWithNullClass;
use Tests\Stub\FooBarWithoutBuiltInTypeClass;
use Tests\Stub\FooBarWithoutBuiltInTypeNullableClass;
use Tests\Stub\FooBarWithoutBuiltInTypeStringDefaultValue;
use Tests\Stub\FooInterface;

/**
Expand Down Expand Up @@ -102,15 +104,58 @@ public function testGotExceptionFromVariableNameAndBuiltTypeNotIntoContainer()
$this->expectException(ContainerException::class);
$this->expectExceptionMessage('Unable to find type hint (string)');
$container = new Container();
$class = $container->get(FooBarClass::class);
$container->get(FooBarClass::class);
}

public function testGotExceptionFromVariableNameNotIntoContainer()
{
$this->expectException(ContainerException::class);
$this->expectExceptionMessage('Unable to find type hint ()');
$container = new Container();
$class = $container->get(FooBarWithoutBuiltInTypeClass::class);
$container->get(FooBarWithoutBuiltInTypeClass::class);
}

/**
* @test
*/
public function testCanResolveClassIfConstructorAcceptNullable()
{
// FooBarWithoutBuiltInTypeNullableClass
$container = new Container();

$class = $container->get(FooBarWithoutBuiltInTypeNullableClass::class);

self::assertInstanceOf(FooBarWithoutBuiltInTypeNullableClass::class, $class);
self::assertNull($class->name);
}

/**
* @test
*/
public function testCanPassOtherDefaultValueToConstructionsIfNotIsNullable()
{
$container = new Container();
$class = $container->get(FooBarWithoutBuiltInTypeStringDefaultValue::class);

self::assertInstanceOf(FooBarWithoutBuiltInTypeStringDefaultValue::class, $class);
self::assertNotNull($class->name);
self::assertEquals('hello world', $class->name);
}

/**
* @test
*/
public function canPassOtherDefaultValueToConstructions()
{
$container = new Container();
$container->set('name', function () {
return 'Jonathan Fontes';
});
$class2 = $container->get(FooBarWithoutBuiltInTypeStringDefaultValue::class);

self::assertInstanceOf(FooBarWithoutBuiltInTypeStringDefaultValue::class, $class2);
self::assertNotNull($class2->name);
self::assertEquals('Jonathan Fontes', $class2->name);
}

public function testIfParamIsNullableButWeHaveValueFromContainer()
Expand Down
15 changes: 15 additions & 0 deletions tests/Stub/FooBarWithoutBuiltInTypeNullableClass.php
@@ -0,0 +1,15 @@
<?php

declare(strict_types=1);

namespace Tests\Stub;

class FooBarWithoutBuiltInTypeNullableClass
{
public $name;

public function __construct($name = null)
{
$this->name = $name;
}
}
16 changes: 16 additions & 0 deletions tests/Stub/FooBarWithoutBuiltInTypeStringDefaultValue.php
@@ -0,0 +1,16 @@
<?php

namespace Tests\Stub;

class FooBarWithoutBuiltInTypeStringDefaultValue
{
/**
* @var string
*/
public $name;

public function __construct($name = 'hello world')
{
$this->name = $name;
}
}

0 comments on commit 3e81fd3

Please sign in to comment.