Skip to content

Commit

Permalink
feat: Introduce RequestUriParametersResolver.
Browse files Browse the repository at this point in the history
  • Loading branch information
shulard committed Oct 6, 2022
1 parent f66162b commit 211ea68
Show file tree
Hide file tree
Showing 3 changed files with 114 additions and 11 deletions.
36 changes: 25 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -137,30 +137,42 @@ 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}' => '<userId()>'
]
],
'method' => 'POST',
'body' => [
'scalar' => [
'bool' => true,
'int' => 23456,
'float' => 18.06
],
//Built in Faker provider
//Built in Faker provider resolved by `RequestBodyResolver`
'faker' => [
'name' => '<name()>',
'creationDate' => '<date("Y-m-d")>',
Expand All @@ -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:
*
* {
Expand Down
27 changes: 27 additions & 0 deletions src/Http/Factory/RequestUriParametersResolver.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

namespace CHStudio\Raven\Http\Factory;

use CHStudio\Raven\Http\Factory\Resolver\ValueResolverInterface;
use Psr\Http\Message\RequestInterface;

class RequestUriParametersResolver implements RequestFactoryInterface
{
public function __construct(
private readonly ValueResolverInterface $resolver,
private readonly RequestFactoryInterface $decorated
) {
}

/**
* @param array<string, mixed> $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);
}
}
62 changes: 62 additions & 0 deletions tests/unit/Http/Factory/RequestUriParametersResolverTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?php

declare(strict_types=1);

namespace CHStudio\RavenTest\Http\Factory;

use CHStudio\Raven\Http\Factory\Resolver\ValueResolverInterface;
use CHStudio\Raven\Http\Factory\RequestUriParametersResolver;
use CHStudio\Raven\Http\Factory\RequestFactoryInterface;
use PHPUnit\Framework\TestCase;
use Psr\Http\Message\RequestInterface;

final class RequestUriParametersResolverTest extends TestCase
{
public function testItCanBeBuilt(): void
{
$bodyResolver = $this->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([]);
}
}

0 comments on commit 211ea68

Please sign in to comment.