Movatterモバイル変換


[0]ホーム

URL:


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

Sign up
Appearance settings

Commit83e1005

Browse files
committed
[HttpClient] add DecoratorTrait for response
1 parentccbdc1a commit83e1005

File tree

3 files changed

+158
-0
lines changed

3 files changed

+158
-0
lines changed

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

Lines 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
---
Lines 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+
namespaceSymfony\Component\HttpClient\Response;
13+
14+
useSymfony\Contracts\HttpClient\ResponseInterface;
15+
16+
/**
17+
* @author Cyril Vermande <https://github.com/cyve>
18+
*/
19+
trait DecoratorTrait
20+
{
21+
privateResponseInterface$response;
22+
23+
publicfunction__construct(
24+
ResponseInterface$response,
25+
) {
26+
$this->response =$response;
27+
}
28+
29+
publicfunctiongetStatusCode():int
30+
{
31+
return$this->response->getStatusCode();
32+
}
33+
34+
publicfunctiongetHeaders(bool$throw =true):array
35+
{
36+
return$this->response->getHeaders($throw);
37+
}
38+
39+
publicfunctiongetContent(bool$throw =true):string
40+
{
41+
return$this->response->getContent($throw);
42+
}
43+
44+
publicfunctiontoArray(bool$throw =true):array
45+
{
46+
return$this->response->toArray($throw);
47+
}
48+
49+
publicfunctioncancel():void
50+
{
51+
$this->response->cancel();
52+
}
53+
54+
publicfunctiongetInfo(string$type =null):mixed
55+
{
56+
return$this->response->getInfo($type);
57+
}
58+
59+
/**
60+
* @return resource
61+
*/
62+
publicfunctiontoStream(bool$throw =true)
63+
{
64+
if ($throw) {
65+
// Ensure headers arrived
66+
$this->response->getHeaders();
67+
}
68+
69+
if ($this->responseinstanceof StreamableInterface) {
70+
return$this->response->toStream($throw);
71+
}
72+
73+
return StreamWrapper::createResource($this->response);
74+
}
75+
}
Lines 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+
namespaceSymfony\Component\HttpClient\Tests\Response;
13+
14+
usePHPUnit\Framework\TestCase;
15+
useSymfony\Component\HttpClient\Response\DecoratorTrait;
16+
useSymfony\Component\HttpClient\Response\MockResponse;
17+
18+
/**
19+
* @author Cyril Vermande <https://github.com/cyve>
20+
*/
21+
class DecoratorTraitTestextends TestCase
22+
{
23+
private$subject;
24+
25+
publicfunctionsetUp():void
26+
{
27+
$decorated = MockResponse::fromRequest(
28+
'GET',
29+
'https://symfony.com',
30+
[],
31+
newMockResponse(
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+
publicfunctiontestShouldReturnDecoratedStatusCode()
43+
{
44+
$this->assertEquals(200,$this->subject->getStatusCode());
45+
}
46+
47+
publicfunctiontestShouldReturnDecoratedHeaders()
48+
{
49+
$this->assertEquals(['content-type' => ['application/json']],$this->subject->getHeaders());
50+
}
51+
52+
publicfunctiontestShouldReturnDecoratedContent()
53+
{
54+
$this->assertEquals('{"foo":"bar"}',$this->subject->getContent());
55+
}
56+
57+
publicfunctiontestShouldReturnDecoratedContentInArray()
58+
{
59+
$this->assertEquals(['foo' =>'bar'],$this->subject->toArray());
60+
}
61+
62+
publicfunctiontestShouldReturnDecoratedInfo()
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+
publicfunctiontestShouldReturnDecoratedInfoByType()
72+
{
73+
$this->assertEquals('https://symfony.com',$this->subject->getInfo('url'));
74+
}
75+
76+
publicfunctiontestShouldReturnDecoratedResponseAsStream()
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)

[8]ページ先頭

©2009-2025 Movatter.jp