diff --git a/test/Faker/Extension/ContainerTest.php b/test/Faker/Extension/ContainerTest.php index a4ed7b233f..3a6932905b 100644 --- a/test/Faker/Extension/ContainerTest.php +++ b/test/Faker/Extension/ContainerTest.php @@ -5,8 +5,10 @@ namespace Faker\Test\Extension; use Faker\Container\Container; +use Faker\Container\ContainerException; use Faker\Core\File; use Faker\Extension\Extension; +use Faker\Test; use PHPUnit\Framework\TestCase; use Psr\Container\ContainerExceptionInterface; use Psr\Container\NotFoundExceptionInterface; @@ -16,6 +18,15 @@ */ final class ContainerTest extends TestCase { + public function testHasThrowsInvalidArgumentExceptionWhenIdentifierIsNotAString(): void + { + $container = new Container([]); + + $this->expectException(\InvalidArgumentException::class); + + $container->has(false); + } + public function testHasReturnsFalseWhenContainerDoesNotHaveDefinitionForService(): void { $container = new Container([]); @@ -23,6 +34,15 @@ public function testHasReturnsFalseWhenContainerDoesNotHaveDefinitionForService( self::assertFalse($container->has('foo')); } + public function testGetThrowsInvalidArgumentExceptionWhenIdentifierIsNotAString(): void + { + $container = new Container([]); + + $this->expectException(\InvalidArgumentException::class); + + $container->get(false); + } + public function testGetThrowsNotFoundExceptionWhenContainerDoesNotHaveDefinitionForService(): void { $container = new Container([]); @@ -43,6 +63,42 @@ public function testGetFromString(): void self::assertInstanceOf(File::class, $object); } + public function testGetThrowsRuntimeExceptionWhenServiceCouldNotBeResolvedFromCallable(): void + { + $id = 'foo'; + + $container = new Container([ + $id => static function (): void { + throw new \RuntimeException(); + }, + ]); + + $this->expectException(ContainerException::class); + $this->expectExceptionMessage(sprintf( + 'Error while invoking callable for "%s"', + $id, + )); + + $container->get($id); + } + + public function testGetThrowsRuntimeExceptionWhenServiceCouldNotBeResolvedFromClass(): void + { + $id = 'foo'; + + $container = new Container([ + $id => Test\Fixture\Container\UnconstructableClass::class, + ]); + + $this->expectException(ContainerException::class); + $this->expectExceptionMessage(sprintf( + 'Could not instantiate class "%s"', + $id, + )); + + $container->get($id); + } + /** * @dataProvider provideDefinitionThatDoesNotResolveToExtension */ diff --git a/test/Fixture/Container/UnconstructableClass.php b/test/Fixture/Container/UnconstructableClass.php new file mode 100644 index 0000000000..6fcb403b11 --- /dev/null +++ b/test/Fixture/Container/UnconstructableClass.php @@ -0,0 +1,15 @@ +