|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace flight\tests; |
| 6 | + |
| 7 | +use flight\Container; |
| 8 | +use flight\tests\examples\ExampleAbstractClass; |
| 9 | +use flight\tests\examples\ExampleClassWithBuiltinConstructorParameter; |
| 10 | +use flight\tests\examples\ExampleClassWithoutConstructorParameterTypeHint; |
| 11 | +use flight\tests\examples\ExampleClassWithUnionTypeHint; |
| 12 | +use flight\tests\examples\ExampleInterface; |
| 13 | +use flight\tests\examples\ExampleTrait; |
| 14 | +use PHPUnit\Framework\TestCase; |
| 15 | +use Psr\Container\ContainerExceptionInterface; |
| 16 | +use Psr\Container\NotFoundExceptionInterface; |
| 17 | + |
| 18 | +final class ContainerExceptionsTest extends TestCase |
| 19 | +{ |
| 20 | + public function test_it_throws_an_exception_when_the_class_does_not_exist(): void |
| 21 | + { |
| 22 | + $container = new Container; |
| 23 | + |
| 24 | + self::expectException(NotFoundExceptionInterface::class); |
| 25 | + self::expectExceptionMessage('Class "NonExistentClass" does not exist'); |
| 26 | + |
| 27 | + $container->get('NonExistentClass'); // @phpstan-ignore-line |
| 28 | + } |
| 29 | + |
| 30 | + /** |
| 31 | + * @dataProvider nonInstantiableClassStringsDataProvider |
| 32 | + * @param class-string $class |
| 33 | + */ |
| 34 | + public function test_it_throws_an_exception_when_the_class_is_not_instantiable( |
| 35 | + string $class |
| 36 | + ): void { |
| 37 | + self::expectException(ContainerExceptionInterface::class); |
| 38 | + self::expectExceptionMessage("Class \"$class\" is not instantiable"); |
| 39 | + |
| 40 | + $container = new Container; |
| 41 | + $container->get($class); |
| 42 | + } |
| 43 | + |
| 44 | + public function test_it_throws_an_exception_when_a_constructor_parameter_does_not_have_type_hint(): void |
| 45 | + { |
| 46 | + self::expectException(ContainerExceptionInterface::class); |
| 47 | + |
| 48 | + self::expectExceptionMessage( |
| 49 | + 'Failed to resolve class "' |
| 50 | + . ExampleClassWithoutConstructorParameterTypeHint::class |
| 51 | + . '" because param "parameter" is missing' |
| 52 | + ); |
| 53 | + |
| 54 | + $container = new Container; |
| 55 | + $container->get(ExampleClassWithoutConstructorParameterTypeHint::class); |
| 56 | + } |
| 57 | + |
| 58 | + public function test_it_throws_an_exception_when_the_class_has_built_in_constructor_parameters(): void |
| 59 | + { |
| 60 | + self::expectException(ContainerExceptionInterface::class); |
| 61 | + |
| 62 | + self::expectExceptionMessage( |
| 63 | + 'Failed to resolve class "' |
| 64 | + . ExampleClassWithBuiltinConstructorParameter::class |
| 65 | + . '" because invalid param "parameter"' |
| 66 | + ); |
| 67 | + |
| 68 | + $container = new Container; |
| 69 | + $container->get(ExampleClassWithBuiltinConstructorParameter::class); |
| 70 | + } |
| 71 | + |
| 72 | + public function test_it_throws_an_exception_when_the_class_has_a_union_type_hint(): void |
| 73 | + { |
| 74 | + if (PHP_VERSION < '8.0') { |
| 75 | + self::markTestSkipped('Union types are only available in PHP 8.0 and later'); |
| 76 | + } |
| 77 | + |
| 78 | + self::expectException(ContainerExceptionInterface::class); |
| 79 | + |
| 80 | + self::expectExceptionMessage( |
| 81 | + 'Failed to resolve class "' |
| 82 | + . ExampleClassWithUnionTypeHint::class |
| 83 | + . '" because of union type for param "dateTime"' |
| 84 | + ); |
| 85 | + |
| 86 | + $container = new Container; |
| 87 | + $container->get(ExampleClassWithUnionTypeHint::class); |
| 88 | + } |
| 89 | + |
| 90 | + /** @return array{0: class-string}[] */ |
| 91 | + public static function nonInstantiableClassStringsDataProvider(): array |
| 92 | + { |
| 93 | + return [ |
| 94 | + [ExampleAbstractClass::class], |
| 95 | + [ExampleInterface::class], |
| 96 | + [ExampleTrait::class] |
| 97 | + ]; |
| 98 | + } |
| 99 | +} |
| 100 | + |
| 101 | +if (PHP_VERSION >= '8.0') { |
| 102 | + require_once __DIR__ . '/examples/ExampleClassWithUnionTypeHint.php'; |
| 103 | +} |
0 commit comments