From bf468b36e699dad1e9b6d79ef59329ce4cc0a9a1 Mon Sep 17 00:00:00 2001 From: Sergei Predvoditelev Date: Thu, 26 Oct 2023 14:02:23 +0300 Subject: [PATCH 1/2] ContainerAttributeResolverFactoryTest --- .../ContainerAttributeResolverFactoryTest.php | 50 +++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 tests/AttributeHandling/ResolverFactory/ContainerAttributeResolverFactoryTest.php diff --git a/tests/AttributeHandling/ResolverFactory/ContainerAttributeResolverFactoryTest.php b/tests/AttributeHandling/ResolverFactory/ContainerAttributeResolverFactoryTest.php new file mode 100644 index 0000000..39551f3 --- /dev/null +++ b/tests/AttributeHandling/ResolverFactory/ContainerAttributeResolverFactoryTest.php @@ -0,0 +1,50 @@ + $resolver]); + $factory = new ContainerAttributeResolverFactory($container); + + $result = $factory->create($attribute); + + $this->assertSame($resolver, $result); + } + + public function testResolverIsAttributeItself(): void + { + $attribute = new CustomValue('test'); + $container = new SimpleContainer(); + $factory = new ContainerAttributeResolverFactory($container); + + $result = $factory->create($attribute); + + $this->assertSame($attribute, $result); + } + + public function testResolverNotExists(): void + { + $attribute = new Counter('test'); + $container = new SimpleContainer(); + $factory = new ContainerAttributeResolverFactory($container); + + $this->expectException(AttributeResolverNonInstantiableException::class); + $this->expectExceptionMessage('Class "' . CounterResolver::class . '" does not exist.'); + $factory->create($attribute); + } +} From 4f9b6780bb3fd90d5ba5abe2dc5bf5a6181a94c6 Mon Sep 17 00:00:00 2001 From: Sergei Predvoditelev Date: Thu, 26 Oct 2023 14:25:18 +0300 Subject: [PATCH 2/2] More tests --- .../ReflectionAttributeResolverFactory.php | 2 +- ...ReflectionAttributeResolverFactoryTest.php | 83 +++++++++++++++++++ tests/Support/Attribute/AbstractResolver.php | 11 +++ .../Support/Attribute/CustomResolverAttr.php | 21 +++++ .../Attribute/ParameterizedResolver.php | 25 ++++++ .../Attribute/PrivateConstructorResolver.php | 25 ++++++ 6 files changed, 166 insertions(+), 1 deletion(-) create mode 100644 tests/AttributeHandling/ResolverFactory/ReflectionAttributeResolverFactoryTest.php create mode 100644 tests/Support/Attribute/AbstractResolver.php create mode 100644 tests/Support/Attribute/CustomResolverAttr.php create mode 100644 tests/Support/Attribute/ParameterizedResolver.php create mode 100644 tests/Support/Attribute/PrivateConstructorResolver.php diff --git a/src/AttributeHandling/ResolverFactory/ReflectionAttributeResolverFactory.php b/src/AttributeHandling/ResolverFactory/ReflectionAttributeResolverFactory.php index e0c646f..c5c1c04 100644 --- a/src/AttributeHandling/ResolverFactory/ReflectionAttributeResolverFactory.php +++ b/src/AttributeHandling/ResolverFactory/ReflectionAttributeResolverFactory.php @@ -33,7 +33,7 @@ public function create(DataAttributeInterface|ParameterAttributeInterface $attri if ($reflectionClass->isAbstract()) { throw new AttributeResolverNonInstantiableException( sprintf( - '%s is not instantiable because it is abstract.', + '"%s" is not instantiable because it is abstract.', $reflectionClass->getName(), ), ); diff --git a/tests/AttributeHandling/ResolverFactory/ReflectionAttributeResolverFactoryTest.php b/tests/AttributeHandling/ResolverFactory/ReflectionAttributeResolverFactoryTest.php new file mode 100644 index 0000000..d55d1fa --- /dev/null +++ b/tests/AttributeHandling/ResolverFactory/ReflectionAttributeResolverFactoryTest.php @@ -0,0 +1,83 @@ +create($attribute); + + $this->assertInstanceOf(CounterResolver::class, $result); + } + + public function testResolverIsAttributeItself(): void + { + $attribute = new CustomValue('test'); + $factory = new ReflectionAttributeResolverFactory(); + + $result = $factory->create($attribute); + + $this->assertSame($attribute, $result); + } + + public function testNonExistClass(): void + { + $attribute = new CustomResolverAttr('NonExistClass'); + $factory = new ReflectionAttributeResolverFactory(); + + $this->expectException(AttributeResolverNonInstantiableException::class); + $this->expectExceptionMessage('Class "NonExistClass" does not exist.'); + $factory->create($attribute); + } + + public function testAbstractClass(): void + { + $attribute = new CustomResolverAttr(AbstractResolver::class); + $factory = new ReflectionAttributeResolverFactory(); + + $this->expectException(AttributeResolverNonInstantiableException::class); + $this->expectExceptionMessage('"' . AbstractResolver::class . '" is not instantiable because it is abstract.'); + $factory->create($attribute); + } + + public function testNonPublicConstructor(): void + { + $attribute = new CustomResolverAttr(PrivateConstructorResolver::class); + $factory = new ReflectionAttributeResolverFactory(); + + $this->expectException(AttributeResolverNonInstantiableException::class); + $this->expectExceptionMessage( + 'Class "' . PrivateConstructorResolver::class . '" is not instantiable because contain non-public constructor.' + ); + $factory->create($attribute); + } + + public function testConstructorWithParameters(): void + { + $attribute = new CustomResolverAttr(ParameterizedResolver::class); + $factory = new ReflectionAttributeResolverFactory(); + + $this->expectException(AttributeResolverNonInstantiableException::class); + $this->expectExceptionMessage( + 'Class "' . ParameterizedResolver::class . '" cannot be instantiated because it has 1 required parameters in constructor.' + ); + $factory->create($attribute); + } +} diff --git a/tests/Support/Attribute/AbstractResolver.php b/tests/Support/Attribute/AbstractResolver.php new file mode 100644 index 0000000..532ee4e --- /dev/null +++ b/tests/Support/Attribute/AbstractResolver.php @@ -0,0 +1,11 @@ +resolver; + } +} diff --git a/tests/Support/Attribute/ParameterizedResolver.php b/tests/Support/Attribute/ParameterizedResolver.php new file mode 100644 index 0000000..5498040 --- /dev/null +++ b/tests/Support/Attribute/ParameterizedResolver.php @@ -0,0 +1,25 @@ +result); + } +} diff --git a/tests/Support/Attribute/PrivateConstructorResolver.php b/tests/Support/Attribute/PrivateConstructorResolver.php new file mode 100644 index 0000000..ad19872 --- /dev/null +++ b/tests/Support/Attribute/PrivateConstructorResolver.php @@ -0,0 +1,25 @@ +