Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 83e1005

Browse filesBrowse files
committed
[HttpClient] add DecoratorTrait for response
1 parent ccbdc1a commit 83e1005
Copy full SHA for 83e1005

File tree

3 files changed

+158
-0
lines changed
Filter options

3 files changed

+158
-0
lines changed

‎src/Symfony/Component/HttpClient/CHANGELOG.md

Copy file name to clipboardExpand all lines: src/Symfony/Component/HttpClient/CHANGELOG.md
+1Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ CHANGELOG
77
* Add `HttpOptions::setHeader()` to add or replace a single header
88
* Allow mocking `start_time` info in `MockResponse`
99
* Add `MockResponse::fromFile()` and `JsonMockResponse::fromFile()` methods to help using fixtures files
10+
* Add `DecoratorTrait` for responses
1011

1112
7.0
1213
---
+75Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\HttpClient\Response;
13+
14+
use Symfony\Contracts\HttpClient\ResponseInterface;
15+
16+
/**
17+
* @author Cyril Vermande <https://github.com/cyve>
18+
*/
19+
trait DecoratorTrait
20+
{
21+
private ResponseInterface $response;
22+
23+
public function __construct(
24+
ResponseInterface $response,
25+
) {
26+
$this->response = $response;
27+
}
28+
29+
public function getStatusCode(): int
30+
{
31+
return $this->response->getStatusCode();
32+
}
33+
34+
public function getHeaders(bool $throw = true): array
35+
{
36+
return $this->response->getHeaders($throw);
37+
}
38+
39+
public function getContent(bool $throw = true): string
40+
{
41+
return $this->response->getContent($throw);
42+
}
43+
44+
public function toArray(bool $throw = true): array
45+
{
46+
return $this->response->toArray($throw);
47+
}
48+
49+
public function cancel(): void
50+
{
51+
$this->response->cancel();
52+
}
53+
54+
public function getInfo(string $type = null): mixed
55+
{
56+
return $this->response->getInfo($type);
57+
}
58+
59+
/**
60+
* @return resource
61+
*/
62+
public function toStream(bool $throw = true)
63+
{
64+
if ($throw) {
65+
// Ensure headers arrived
66+
$this->response->getHeaders();
67+
}
68+
69+
if ($this->response instanceof StreamableInterface) {
70+
return $this->response->toStream($throw);
71+
}
72+
73+
return StreamWrapper::createResource($this->response);
74+
}
75+
}
+82Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\HttpClient\Tests\Response;
13+
14+
use PHPUnit\Framework\TestCase;
15+
use Symfony\Component\HttpClient\Response\DecoratorTrait;
16+
use Symfony\Component\HttpClient\Response\MockResponse;
17+
18+
/**
19+
* @author Cyril Vermande <https://github.com/cyve>
20+
*/
21+
class DecoratorTraitTest extends TestCase
22+
{
23+
private $subject;
24+
25+
public function setUp(): void
26+
{
27+
$decorated = MockResponse::fromRequest(
28+
'GET',
29+
'https://symfony.com',
30+
[],
31+
new MockResponse(
32+
'{"foo":"bar"}',
33+
[
34+
'http_code' => 200,
35+
'response_headers' => ['Content-Type' => 'application/json'],
36+
],
37+
)
38+
);
39+
$this->subject = $this->getObjectForTrait(DecoratorTrait::class, [$decorated]);
40+
}
41+
42+
public function testShouldReturnDecoratedStatusCode()
43+
{
44+
$this->assertEquals(200, $this->subject->getStatusCode());
45+
}
46+
47+
public function testShouldReturnDecoratedHeaders()
48+
{
49+
$this->assertEquals(['content-type' => ['application/json']], $this->subject->getHeaders());
50+
}
51+
52+
public function testShouldReturnDecoratedContent()
53+
{
54+
$this->assertEquals('{"foo":"bar"}', $this->subject->getContent());
55+
}
56+
57+
public function testShouldReturnDecoratedContentInArray()
58+
{
59+
$this->assertEquals(['foo' => 'bar'], $this->subject->toArray());
60+
}
61+
62+
public function testShouldReturnDecoratedInfo()
63+
{
64+
$info = $this->subject->getInfo();
65+
$this->assertArrayHasKey('http_code', $info);
66+
$this->assertArrayHasKey('response_headers', $info);
67+
$this->assertArrayHasKey('http_method', $info);
68+
$this->assertArrayHasKey('url', $info);
69+
}
70+
71+
public function testShouldReturnDecoratedInfoByType()
72+
{
73+
$this->assertEquals('https://symfony.com', $this->subject->getInfo('url'));
74+
}
75+
76+
public function testShouldReturnDecoratedResponseAsStream()
77+
{
78+
$stream = $this->subject->toStream();
79+
$this->assertTrue(is_resource($stream));
80+
$this->assertEquals('{"foo":"bar"}', stream_get_contents($stream));
81+
}
82+
}

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.