diff --git a/tests/GenieCoreTest.php b/tests/GenieCoreTest.php index 8667596..042ffc4 100644 --- a/tests/GenieCoreTest.php +++ b/tests/GenieCoreTest.php @@ -5,6 +5,8 @@ namespace Dakujem\Wire\Tests; use Dakujem\Sleeve; +use Dakujem\Wire\Exceptions\Unresolvable; +use Dakujem\Wire\Exceptions\UnresolvableCallArguments; use Dakujem\Wire\Genie; use PHPUnit\Framework\TestCase; @@ -14,6 +16,7 @@ class GenieCoreTest extends TestCase { use WithStuff; + use AssertsErrors; public function testGenieProvisionsCallArguments() { @@ -70,4 +73,42 @@ public function testEmploy() $this->with(new Genie($c, $core), $contains); $this->with(Genie::employ($c, $core), $contains); } + + public function testInvocationTypeSwitch() + { + $g = new Genie($c = new Sleeve([ + Constant::class => new Constant(0.0), + Coefficient::class => new Coefficient(1.0), + Offset::class => function ($c) { + return new Offset($c[Constant::class], $c[Coefficient::class]); + }, + ])); + + $o1 = $g(Offset::class); + $o2 = $g(function (Offset $offset) { + return $offset; + }); + + $this->assertSame($c[Offset::class], $o2); + $this->assertInstanceOf(Offset::class, $o1); + $this->assertNotSame($c[Offset::class], $o1); // must not be the same, one is created, other one is from the container + $self = $this; + $this->with($o1, function () use ($self, $c) { + // assert that the services contained inside the newly created Offset instance come from the container + $self->assertSame($c[Constant::class], $this->absolute); + $self->assertSame($c[Coefficient::class], $this->relative); + }); + } + + public function testUnresolvable() + { + $g = new Genie(new Sleeve([]), function () { + throw new UnresolvableCallArguments('foo'); + }); + + $this->assertException(function () use ($g) { + $g(function () { + }); + }, Unresolvable::class); + } }