Skip to content

Commit

Permalink
Resolve parameters default values
Browse files Browse the repository at this point in the history
  • Loading branch information
jm42 committed Jun 29, 2017
1 parent d67ad28 commit c302c9c
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 8 deletions.
1 change: 1 addition & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
- [BC-BREAK] Factories are given an instance of delegate instead it's own.
- [BC-BREAK] Factory call to make without parameters gets service from it's own instead of delegate.
- [BC-BREAK] Call a function will resolve parameters from it's own instead of delegate.
- [BC-BREAK] Resolve parameters default values.

### 0.11.0

Expand Down
2 changes: 1 addition & 1 deletion library/Registry.php
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ private function buildContext(self $self, array $params, array $args): array {
} elseif ($param->hasType() && !$param->getType()->isBuiltin() && isset($self[strval($param->getType())])) {
$context[$index] = $self[strval($param->getType())];
} elseif ($param->isDefaultValueAvailable()) {
$context[$index] = $param->getDefaultValue();
$context[$index] = $this->resolve($self, $param->getDefaultValue());
} elseif ($param->isOptional() && !$param->isVariadic()) {
$context[$index] = null;
} else {
Expand Down
17 changes: 10 additions & 7 deletions spec/Suite/RegistrySpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
interface Engine {}

class V8 implements Engine {
function __invoke() {
return 8;
function __invoke(string $prefix = '$prefix') {
return "${prefix}World";
}
}

Expand Down Expand Up @@ -318,32 +318,35 @@ function __construct(Engine $engine, string $color='red') {

describe('__invoke', function() {
it('calls callable when object that has __invoke method is given', function() {
expect((new Registry)->__invoke(new V8))->toBe(8);
expect((new Registry)->__invoke(new V8, ['prefix' => '']))->toBe('World');
});

it('calls callable when class that is in container and has __invoke method is given', function() {
$di = new Registry;
$di->offsetSet('prefix', 'Hello ');
$di->offsetSet(Engine::class, V8::class);

expect($di->__invoke(Engine::class))->toBe(8);
expect($di->__invoke(Engine::class))->toBe('Hello World');
});

it('calls callable when class that is in container and method split by :: is given', function() {
$di = new Registry;
$di->offsetSet('prefix', 'Hello ');
$di->offsetSet(Engine::class, V8::class);

expect($di->__invoke(Engine::class . '::__invoke'))->toBe(8);
expect($di->__invoke(Engine::class . '::__invoke'))->toBe('Hello World');
});

it('calls callable when array of object and method is given', function() {
expect((new Registry)->__invoke([new V8, '__invoke']))->toBe(8);
expect((new Registry)->__invoke([new V8, '__invoke'], ['prefix' => '']))->toBe('World');
});

it('calls callable when array of class that is in container and method is given', function() {
$di = new Registry;
$di->offsetSet('prefix', 'Hello ');
$di->offsetSet(Engine::class, V8::class);

expect($di->__invoke([Engine::class, '__invoke']))->toBe(8);
expect($di->__invoke([Engine::class, '__invoke']))->toBe('Hello World');
});

it('calls callable when string of closure that is in the container is given', function() {
Expand Down

0 comments on commit c302c9c

Please sign in to comment.