Skip to content

Commit

Permalink
[bug] fix using UnionParameter on parameters with default values
Browse files Browse the repository at this point in the history
  • Loading branch information
kbond committed Oct 18, 2021
1 parent a4fb33d commit 29d8277
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/Callback/Parameter/UnionParameter.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ protected function valueFor(Argument $argument)
{
foreach ($this->parameters as $parameter) {
try {
return $parameter->resolve($argument);
return $parameter->valueFor($argument);
} catch (UnresolveableArgument $e) {
continue;
}
Expand Down
43 changes: 43 additions & 0 deletions tests/CallbackTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -581,6 +581,49 @@ public function argument_allows(): void
$this->assertFalse($callback2->argument(0)->allows(new Object1()));
$this->assertFalse($callback2->argument(0)->allows(new Object3()));
}

/**
* @test
*/
public function invoke_all_union_parameter_with_defaults(): void
{
$callback = Callback::createFor(function(string $a, ?\DateTimeInterface $b = null, $c = null) { return [$a, $b, $c]; });

$ret = $callback->invokeAll(Parameter::union(
Parameter::typed('string', 'a')
));

$this->assertSame(['a', null, null], $ret);

$ret = $callback->invokeAll(Parameter::union(
Parameter::typed('string', 'a'),
Parameter::typed(\DateTime::class, $b = new \DateTime())
));

$this->assertSame(['a', $b, null], $ret);

$ret = $callback->invokeAll(Parameter::union(
Parameter::typed('string', 'a'),
Parameter::untyped('c')
));

$this->assertSame(['a', null, 'c'], $ret);

$ret = $callback->invokeAll(Parameter::union(
Parameter::untyped('c'),
Parameter::typed('string', 'a'),
Parameter::typed(\DateTime::class, $b = new \DateTime())
));

$this->assertSame(['a', $b, 'c'], $ret);

$ret = $callback->invokeAll(Parameter::union(
Parameter::typed('string', 'a'),
Parameter::typed(\DateTime::class, Parameter::factory(function() use ($b) { return $b; }))
));

$this->assertSame(['a', $b, null], $ret);
}
}

class Object1
Expand Down

0 comments on commit 29d8277

Please sign in to comment.