From 29645e77cf8b666bf7ab08a59be38c3a30884399 Mon Sep 17 00:00:00 2001 From: Alejandro Celaya Date: Fri, 13 Oct 2017 12:27:20 +0200 Subject: [PATCH] Created DottedAccessConfigAbstractFactory --- .../DottedAccessConfigAbstractFactoryTest.php | 91 +++++++++++++++++++ 1 file changed, 91 insertions(+) create mode 100644 module/Common/test/Factory/DottedAccessConfigAbstractFactoryTest.php diff --git a/module/Common/test/Factory/DottedAccessConfigAbstractFactoryTest.php b/module/Common/test/Factory/DottedAccessConfigAbstractFactoryTest.php new file mode 100644 index 00000000..59bc08e3 --- /dev/null +++ b/module/Common/test/Factory/DottedAccessConfigAbstractFactoryTest.php @@ -0,0 +1,91 @@ +factory = new DottedAccessConfigAbstractFactory(); + } + + /** + * @param string $serviceName + * @param bool $canCreate + * + * @test + * @dataProvider provideDotNames + */ + public function canCreateOnlyServicesWithDot(string $serviceName, bool $canCreate) + { + $this->assertEquals($canCreate, $this->factory->canCreate(new ServiceManager(), $serviceName)); + } + + public function provideDotNames(): array + { + return [ + ['foo.bar', true], + ['config.something', true], + ['config_something', false], + ['foo', false], + ]; + } + + /** + * @test + */ + public function throwsExceptionWhenFirstPartOfTheServiceIsNotRegistered() + { + $this->expectException(ServiceNotCreatedException::class); + $this->expectExceptionMessage( + 'Defined service "foo" could not be found in container after resolving dotted expression "foo.bar"' + ); + + $this->factory->__invoke(new ServiceManager(), 'foo.bar'); + } + + /** + * @test + */ + public function dottedNotationIsRecursivelyResolvedUntilLastValueIsFoundAndReturned() + { + $expected = 'this is the result'; + + $result = $this->factory->__invoke(new ServiceManager(['services' => [ + 'foo' => [ + 'bar' => ['baz' => $expected], + ], + ]]), 'foo.bar.baz'); + + $this->assertEquals($expected, $result); + } + + /** + * @test + */ + public function exceptionIsThrownIfAnyStepCannotBeResolved() + { + $this->expectException(InvalidArgumentException::class); + $this->expectExceptionMessage( + 'The key "baz" provided in the dotted notation could not be found in the array service' + ); + + $this->factory->__invoke(new ServiceManager(['services' => [ + 'foo' => [ + 'bar' => ['something' => 123], + ], + ]]), 'foo.bar.baz'); + } +}