Skip to content

Commit 838260a

Browse files
SirRFIrafal
authored and
rafal
committed
[HttpClient] Add real app standalone example on how to use and test HTTP Client
1 parent e7bb5db commit 838260a

File tree

1 file changed

+81
-0
lines changed

1 file changed

+81
-0
lines changed

http_client.rst

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1496,6 +1496,87 @@ Usage example::
14961496
// returns ["Accept: */*", "Authorization: Basic YWxhZGRpbjpvcGVuc2VzYW1l"]
14971497
$mockResponse->getRequestOptions()['headers'];
14981498

1499+
Example
1500+
~~~~~~~
1501+
1502+
The following standalone example demonstrates a way to HTTP client and test it
1503+
in real application::
1504+
1505+
// ExternalArticleService.php
1506+
use Symfony\Contracts\HttpClient\HttpClientInterface;
1507+
1508+
final class ExternalArticleService
1509+
{
1510+
private HttpClientInterface $httpClient;
1511+
1512+
public function __construct(HttpClientInterface $httpClient)
1513+
{
1514+
$this->httpClient = $httpClient;
1515+
}
1516+
1517+
public function createArticle(array $requestData): array
1518+
{
1519+
$requestJson = json_encode($requestData, JSON_THROW_ON_ERROR);
1520+
1521+
$response = $this->httpClient->request('POST', 'api/article', [
1522+
'headers' => [
1523+
'Content-Type: application/json',
1524+
'Accept: application/json',
1525+
],
1526+
'body' => $requestJson,
1527+
]);
1528+
1529+
if (201 !== $response->getStatusCode()) {
1530+
throw new Exception('Response status code is different than expected.');
1531+
}
1532+
1533+
// ... other checks
1534+
1535+
$responseJson = $response->getContent();
1536+
$responseData = json_decode($responseJson, true, 512, JSON_THROW_ON_ERROR);
1537+
1538+
return $responseData;
1539+
}
1540+
}
1541+
1542+
// ExternalArticleServiceTest.php
1543+
use PHPUnit\Framework\TestCase;
1544+
use Symfony\Component\HttpClient\MockHttpClient;
1545+
use Symfony\Component\HttpClient\Response\MockResponse;
1546+
1547+
final class ExternalArticleServiceTest extends TestCase
1548+
{
1549+
public function testSubmitData(): void
1550+
{
1551+
// Arrange
1552+
$requestData = ['title' => 'Testing with Symfony HTTP Client'];
1553+
$expectedRequestData = json_encode($requestData, JSON_THROW_ON_ERROR);
1554+
1555+
$expectedResponseData = ['id' => 12345];
1556+
$mockResponseJson = json_encode($expectedResponseData, JSON_THROW_ON_ERROR);
1557+
$mockResponse = new MockResponse($mockResponseJson, [
1558+
'http_code' => 201,
1559+
'response_headers' => ['Content-Type: application/json'],
1560+
]);
1561+
1562+
$httpClient = new MockHttpClient($mockResponse, 'https://example.com/');
1563+
$service = new ExternalArticleService($httpClient);
1564+
1565+
// Act
1566+
$responseData = $service->createArticle($requestData);
1567+
1568+
// Assert
1569+
self::assertSame('POST', $mockResponse->getRequestMethod());
1570+
self::assertSame('https://example.com/api/article', $mockResponse->getRequestUrl());
1571+
self::assertContains(
1572+
'Content-Type: application/json',
1573+
$mockResponse->getRequestOptions()['headers']
1574+
);
1575+
self::assertSame($expectedRequestData, $mockResponse->getRequestOptions()['body']);
1576+
1577+
self::assertSame($responseData, $expectedResponseData);
1578+
}
1579+
}
14991580

15001581
.. _`cURL PHP extension`: https://www.php.net/curl
15011582
.. _`PSR-17`: https://www.php-fig.org/psr/psr-17/

0 commit comments

Comments
 (0)