Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[DV-8391] Added ResponseStream to VerboseErrorHttpClient::stream #7

Merged
merged 3 commits into from
Apr 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions src/HttpClient/Response/VerboseErrorResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,16 @@

namespace Superbrave\VerboseErrorHttpClientBundle\HttpClient\Response;

use Generator;
use Superbrave\VerboseErrorHttpClientBundle\HttpClient\Exception\ClientException;
use Superbrave\VerboseErrorHttpClientBundle\HttpClient\Exception\RedirectionException;
use Superbrave\VerboseErrorHttpClientBundle\HttpClient\Exception\ServerException;
use Symfony\Contracts\HttpClient\Exception\ClientExceptionInterface;
use Symfony\Contracts\HttpClient\Exception\RedirectionExceptionInterface;
use Symfony\Contracts\HttpClient\Exception\ServerExceptionInterface;
use Symfony\Contracts\HttpClient\HttpClientInterface;
use Symfony\Contracts\HttpClient\ResponseInterface;
use Symfony\Contracts\HttpClient\ResponseStreamInterface;

/**
* Wraps a response to be able to decorate the thrown exceptions.
Expand Down Expand Up @@ -76,4 +79,18 @@ public function cancel(): void
{
$this->response->cancel();
}

/**
* @internal
*
* @param iterable<VerboseErrorResponse> $responses
*
* @return Generator<VerboseErrorResponse, ResponseStreamInterface>
*/
public static function stream(HttpClientInterface $client, iterable $responses, ?float $timeout): Generator
{
foreach ($responses as $response) {
yield $response => $client->stream($response->response, $timeout);
}
}
}
12 changes: 10 additions & 2 deletions src/HttpClient/VerboseErrorHttpClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Superbrave\VerboseErrorHttpClientBundle\HttpClient;

use Superbrave\VerboseErrorHttpClientBundle\HttpClient\Response\VerboseErrorResponse;
use Symfony\Component\HttpClient\Response\ResponseStream;
use Symfony\Contracts\HttpClient\HttpClientInterface;
use Symfony\Contracts\HttpClient\ResponseInterface;
use Symfony\Contracts\HttpClient\ResponseStreamInterface;
Expand All @@ -27,9 +28,16 @@ public function request(string $method, string $url, array $options = []): Respo
return new VerboseErrorResponse($response);
}

public function stream($responses, ?float $timeout = null): ResponseStreamInterface
/**
* @param VerboseErrorResponse|iterable $responses
*/
public function stream(ResponseInterface|iterable $responses, ?float $timeout = null): ResponseStreamInterface
{
return $this->client->stream($responses, $timeout);
if ($responses instanceof VerboseErrorResponse) {
$responses = [$responses];
}

return new ResponseStream(VerboseErrorResponse::stream($this->client, $responses, $timeout));
}

public function withOptions(array $options): static
Expand Down
32 changes: 21 additions & 11 deletions tests/VerboseErrorHttpClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use Superbrave\VerboseErrorHttpClientBundle\HttpClient\Exception\ClientException;
use Superbrave\VerboseErrorHttpClientBundle\HttpClient\Exception\RedirectionException;
use Superbrave\VerboseErrorHttpClientBundle\HttpClient\Exception\ServerException;
use Superbrave\VerboseErrorHttpClientBundle\HttpClient\Response\VerboseErrorResponse;
use Superbrave\VerboseErrorHttpClientBundle\HttpClient\VerboseErrorHttpClient;
use Symfony\Component\HttpClient\MockHttpClient;
use Symfony\Component\HttpClient\Response\MockResponse;
Expand Down Expand Up @@ -82,26 +83,35 @@ public function testRequestThrowsRedirectionException(
}

/**
* Tests if VerboseErrorHttpClient::stream only calls the stream method on
* the underlying/decorated HTTP client.
* Tests if {@see VerboseErrorHttpClient::stream()} only calls the stream method on
* the underlying/decorated HTTP client with the underlying response and that it returns a {@see ResponseStream}.
*/
public function testStream(): void
{
// Arrange
$mockResponse = new MockResponse('');

$verboseErrorResponse = new VerboseErrorResponse($mockResponse);

$expectedResponseStream = new ResponseStream(MockResponse::stream([$mockResponse], null));

$httpClientMock = $this->getMockBuilder(HttpClientInterface::class)
->getMock();
$httpClientMock->expects($this->once())
->method('stream')
->with($mockResponse, null)
->willReturn($expectedResponseStream);
$httpClientMock = $this->createMock(HttpClientInterface::class);

$httpClient = new VerboseErrorHttpClient($httpClientMock);
$verboseErrorHttpClient = new VerboseErrorHttpClient($httpClientMock);

$responseStream = $httpClient->stream($mockResponse);
// Act
$responseStream = $verboseErrorHttpClient->stream($verboseErrorResponse);

// Assert
$this->assertEquals($expectedResponseStream, $responseStream);

// Assert (2)
$httpClientMock->expects($this->once())
->method('stream')
->with($mockResponse, null);

$this->assertSame($expectedResponseStream, $responseStream);
// Act (2)
$responseStream->next();
}

public function provideServerExceptionResponses(): array
Expand Down
Loading