-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Introduce RequestUriParametersResolver.
- Loading branch information
Showing
3 changed files
with
114 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
62
tests/unit/Http/Factory/RequestUriParametersResolverTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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([]); | ||
} | ||
} |