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

Commit0a9d383

Browse files
committed
[HttpClient] AddJsonMockResponse::fromFile() andMockResponse::fromFile() shortcuts
1 parent0d9562f commit0a9d383

File tree

8 files changed

+60
-3
lines changed

8 files changed

+60
-3
lines changed

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ CHANGELOG
55
---
66

77
* Allow mocking`start_time` info in`MockResponse`
8+
* Add`MockResponse::fromFile()` and`JsonMockResponse::fromFile()` methods to help using fixtures files
9+
* Add optional argument`bool $isJson = false` to`JsonMockResponse::__construct()` to allow passing JSON data directly
810

911
7.0
1012
---

‎src/Symfony/Component/HttpClient/Response/JsonMockResponse.php‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,18 @@ public function __construct(mixed $body = [], array $info = [])
3030

3131
parent::__construct($json,$info);
3232
}
33+
34+
publicstaticfunctionfromFile(string$path,array$info = []):static
35+
{
36+
if (!is_file($path)) {
37+
thrownewInvalidArgumentException(sprintf('File not found: "%s".',$path));
38+
}
39+
40+
$json =file_get_contents($path);
41+
if (!json_validate($json)) {
42+
thrownew \InvalidArgumentException(sprintf('File "%s" does not contain valid JSON.',$path));
43+
}
44+
45+
returnnewstatic(json_decode($json,true, flags: \JSON_THROW_ON_ERROR),$info);
46+
}
3347
}

‎src/Symfony/Component/HttpClient/Response/MockResponse.php‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,15 @@ public function __construct(string|iterable $body = '', array $info = [])
6464
self::addResponseHeaders($responseHeaders,$this->info,$this->headers);
6565
}
6666

67+
publicstaticfunctionfromFile(string$path,array$info = []):static
68+
{
69+
if (!is_file($path)) {
70+
thrownew \InvalidArgumentException(sprintf('File not found: "%s".',$path));
71+
}
72+
73+
returnnewstatic(file_get_contents($path),$info);
74+
}
75+
6776
/**
6877
* Returns the options used when doing the request.
6978
*/
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
foo ccc
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"foo":"bar"
3+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
foo bar ccc

‎src/Symfony/Component/HttpClient/Tests/Response/JsonMockResponseTest.php‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,4 +85,25 @@ public static function responseHeadersProvider(): array
8585
['application/problem+json', ['x-foo' =>'ccc','content-type' =>'application/problem+json']],
8686
];
8787
}
88+
89+
publicfunctiontestFromFile()
90+
{
91+
$client =newMockHttpClient(JsonMockResponse::fromFile(__DIR__.'/Fixtures/response.json'));
92+
$response =$client->request('GET','https://symfony.com');
93+
94+
$this->assertSame([
95+
'foo' =>'bar',
96+
],$response->toArray());
97+
$this->assertSame('application/json',$response->getHeaders()['content-type'][0]);
98+
}
99+
100+
publicfunctiontestFromFileWithInvalidJson()
101+
{
102+
$path =__DIR__.'/Fixtures/invalid_json.json';
103+
104+
$this->expectException(\InvalidArgumentException::class);
105+
$this->expectExceptionMessage(sprintf('File "%s" does not contain valid JSON.',$path));
106+
107+
JsonMockResponse::fromFile($path);
108+
}
88109
}

‎src/Symfony/Component/HttpClient/Tests/Response/MockResponseTest.php‎

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,9 @@
1515
useSymfony\Component\HttpClient\Exception\InvalidArgumentException;
1616
useSymfony\Component\HttpClient\Exception\JsonException;
1717
useSymfony\Component\HttpClient\Exception\TransportException;
18+
useSymfony\Component\HttpClient\MockHttpClient;
1819
useSymfony\Component\HttpClient\Response\MockResponse;
1920

20-
/**
21-
* Test methods from Symfony\Component\HttpClient\Response\*ResponseTrait.
22-
*/
2321
class MockResponseTestextends TestCase
2422
{
2523
publicfunctiontestTotalTimeShouldBeSimulatedWhenNotProvided()
@@ -133,4 +131,12 @@ public function testMustBeIssuedByMockHttpClient()
133131

134132
(newMockResponse())->getContent();
135133
}
134+
135+
publicfunctiontestFromFile()
136+
{
137+
$client =newMockHttpClient(MockResponse::fromFile(__DIR__.'/Fixtures/response.txt'));
138+
$response =$client->request('GET','https://symfony.com');
139+
140+
$this->assertSame('foo bar ccc',$response->getContent());
141+
}
136142
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp