diff --git a/README.md b/README.md index e6f3219..b00464c 100644 --- a/README.md +++ b/README.md @@ -137,22 +137,34 @@ use CHStudio\Raven\Http\Factory\Resolver\ArrayValueResolver; use CHStudio\Raven\Http\Factory\Resolver\FakerValueResolver; use CHStudio\Raven\Http\Factory\Resolver\PassThroughValueResolver; +//Configure your own faker generator, maybe with some `addProvider` calls. $generator = \Faker\Factory::create(); +//Configure specific resolver logic. +$valueResolver = new ArrayValueResolver( + new FakerValueResolver( + $generator, + new PassThroughValueResolver() + ) +); + //Apply it on the request factory built in the previous section. -$requestFactory = new RequestBodyResolver( - //Configure specific resolver logic. - new ArrayValueResolver( - new FakerValueResolver( - $generator, - new PassThroughValueResolver() - ) - ), - $requestFactory +$requestFactory = new RequestUriParametersResolver( + $valueResolver, + new RequestBodyResolver( + $valueResolver, + $requestFactory + ) ); $request = $requestFactory->fromArray([ - 'uri' => 'http://myhost.com/api/users/me', + 'uri' => [ + 'base' => 'http://myhost.com/api/users/{id}', + 'parameters' => [ + //This value will be resolved by `RequestUriParametersResolver` + '{id}' => '' + ] + ], 'method' => 'POST', 'body' => [ 'scalar' => [ @@ -160,7 +172,7 @@ $request = $requestFactory->fromArray([ 'int' => 23456, 'float' => 18.06 ], - //Built in Faker provider + //Built in Faker provider resolved by `RequestBodyResolver` 'faker' => [ 'name' => '', 'creationDate' => '', @@ -171,6 +183,8 @@ $request = $requestFactory->fromArray([ ]); /** + * This will generate an URL like this: http://myhost.com/api/users/a5098711-b6b2-4acb-96ea-f8baf496c700 + * * This will generate the following body: * * { diff --git a/src/Http/Factory/RequestUriParametersResolver.php b/src/Http/Factory/RequestUriParametersResolver.php new file mode 100644 index 0000000..6748292 --- /dev/null +++ b/src/Http/Factory/RequestUriParametersResolver.php @@ -0,0 +1,27 @@ + $data + */ + public function fromArray(array $data): RequestInterface + { + if (isset($data['uri']) && \is_array($data['uri'])) { + $data['uri']['parameters'] = $this->resolver->resolve($data['uri']['parameters'] ?? []); + } + + return $this->decorated->fromArray($data); + } +} diff --git a/tests/unit/Http/Factory/RequestUriParametersResolverTest.php b/tests/unit/Http/Factory/RequestUriParametersResolverTest.php new file mode 100644 index 0000000..d138a4e --- /dev/null +++ b/tests/unit/Http/Factory/RequestUriParametersResolverTest.php @@ -0,0 +1,62 @@ +createMock(ValueResolverInterface::class); + $decorated = $this->createMock(RequestFactoryInterface::class); + + $factory = new RequestUriParametersResolver($bodyResolver, $decorated); + + static::assertInstanceOf(RequestFactoryInterface::class, $factory); + } + + public function testItResolvesUriParametersWhenThereIsOne(): void + { + $bodyResolver = $this->createMock(ValueResolverInterface::class); + $decorated = $this->createMock(RequestFactoryInterface::class); + + $bodyResolver + ->expects(static::once()) + ->method('resolve') + ->with(['a' => 'b']) + ->willReturn(['c']); + + $decorated + ->expects(static::once()) + ->method('fromArray') + ->with(['uri' => ['parameters' => ['c']]]) + ->willReturn($this->createMock(RequestInterface::class)); + + (new RequestUriParametersResolver($bodyResolver, $decorated)) + ->fromArray(['uri' => ['parameters' => ['a' => 'b']]]); + } + + public function testItDoesntCallResolverOnEmptyParameters(): void + { + $bodyResolver = $this->createMock(ValueResolverInterface::class); + $decorated = $this->createMock(RequestFactoryInterface::class); + + $bodyResolver->expects(static::never())->method('resolve'); + + $decorated + ->expects(static::once()) + ->method('fromArray') + ->with([]) + ->willReturn($this->createMock(RequestInterface::class)); + + (new RequestUriParametersResolver($bodyResolver, $decorated)) + ->fromArray([]); + } +}