-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Co-authored-by: Fabrizio Gargiulo <[email protected]>
- Loading branch information
1 parent
4f12098
commit 01bb9cc
Showing
4 changed files
with
145 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace DoppioGancio\MockedClient\Route; | ||
|
||
use Psr\Http\Message\ResponseInterface; | ||
|
||
class ConsecutiveCallsRouteBuilder extends RouteBuilder | ||
{ | ||
/** @var ResponseInterface[] */ | ||
private array $responses = []; | ||
|
||
public function new(): self | ||
{ | ||
return new self($this->responseFactory, $this->streamFactory); | ||
} | ||
|
||
public function withResponse(ResponseInterface $response): self | ||
{ | ||
$this->responses[] = $response; | ||
|
||
return $this; | ||
} | ||
|
||
public function build(): Route | ||
{ | ||
return $this->buildRoute( | ||
$this->method, | ||
$this->path, | ||
(new ConsecutiveCallsRouteHandler($this->responses))(...), | ||
); | ||
} | ||
} |
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,30 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace DoppioGancio\MockedClient\Route; | ||
|
||
use DoppioGancio\MockedClient\Route\Exception\TooManyConsecutiveCalls; | ||
use Psr\Http\Message\RequestInterface; | ||
use Psr\Http\Message\ResponseInterface; | ||
|
||
use function count; | ||
|
||
class ConsecutiveCallsRouteHandler | ||
{ | ||
private int $currentResponse = 0; | ||
|
||
/** @param ResponseInterface[] $responses */ | ||
public function __construct(private readonly array $responses) | ||
{ | ||
} | ||
|
||
public function __invoke(RequestInterface $request): ResponseInterface | ||
{ | ||
if ($this->currentResponse === count($this->responses)) { | ||
throw new TooManyConsecutiveCalls($request, $this->responses); | ||
} | ||
|
||
return $this->responses[$this->currentResponse++]; | ||
} | ||
} |
27 changes: 27 additions & 0 deletions
27
src/MockedClient/Route/Exception/TooManyConsecutiveCalls.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,27 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace DoppioGancio\MockedClient\Route\Exception; | ||
|
||
use Exception; | ||
use Psr\Http\Message\RequestInterface; | ||
use Psr\Http\Message\ResponseInterface; | ||
use Throwable; | ||
|
||
use function sprintf; | ||
|
||
class TooManyConsecutiveCalls extends Exception | ||
{ | ||
/** @param ResponseInterface[] $responses */ | ||
public function __construct( | ||
RequestInterface $request, | ||
public readonly array $responses, | ||
int $code = 0, | ||
Throwable|null $previous = null, | ||
) { | ||
$message = sprintf('Endpoint "%s" has been called too many times', $request->getUri()->getPath()); | ||
|
||
parent::__construct($message, $code, $previous); | ||
} | ||
} |
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,54 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace DoppioGancio\MockedClient\Tests\Route; | ||
|
||
use DoppioGancio\MockedClient\Route\ConsecutiveCallsRouteBuilder; | ||
use DoppioGancio\MockedClient\Route\Exception\TooManyConsecutiveCalls; | ||
use GuzzleHttp\Psr7\Request; | ||
use Http\Discovery\Psr17FactoryDiscovery; | ||
use PHPUnit\Framework\TestCase; | ||
use Psr\Http\Message\ResponseInterface; | ||
|
||
use function assert; | ||
use function json_decode; | ||
|
||
class ConsecutiveCallsRouteBuilderTest extends TestCase | ||
{ | ||
public function testRoute(): void | ||
{ | ||
$builder = new ConsecutiveCallsRouteBuilder( | ||
Psr17FactoryDiscovery::findResponseFactory(), | ||
Psr17FactoryDiscovery::findStreamFactory(), | ||
); | ||
|
||
$route = $builder->withMethod('GET') | ||
->withPath('/country') | ||
->withStringResponse('{"id":"+43","code":"AU","name":"Austria"}') | ||
->withStringResponse('{"id":"+39","code":"IT","name":"Italy"}') | ||
->build(); | ||
|
||
// Request #1 | ||
$response = $route->getHandler()(new Request('GET', '/country')); | ||
assert($response instanceof ResponseInterface); | ||
|
||
$this->assertEquals(200, $response->getStatusCode()); | ||
|
||
$data = json_decode($response->getBody()->getContents(), true); | ||
$this->assertEquals('Austria', $data['name']); | ||
|
||
// Request #2 | ||
$response = $route->getHandler()(new Request('GET', '/country')); | ||
assert($response instanceof ResponseInterface); | ||
|
||
$this->assertEquals(200, $response->getStatusCode()); | ||
|
||
$data = json_decode($response->getBody()->getContents(), true); | ||
$this->assertEquals('Italy', $data['name']); | ||
|
||
// Request #3 - Called too many times | ||
$this->expectException(TooManyConsecutiveCalls::class); | ||
$route->getHandler()(new Request('GET', '/country')); | ||
} | ||
} |