Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit e3e831f

Browse files
committed
[HttpClient] Add JsonMockResponse::fromFile() and MockResponse::fromFile() shortcuts
1 parent c715b55 commit e3e831f

File tree

5 files changed

+69
-10
lines changed

5 files changed

+69
-10
lines changed

src/Symfony/Component/HttpClient/CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
CHANGELOG
22
=========
33

4+
7.1
5+
---
6+
7+
* Add `MockResponse::fromFile()` and `JsonMockResponse::fromFile()` methods to help using fixtures files
8+
* Add optional argument `bool $json = false` to `JsonMockResponse::__construct()` to allow passing JSON data directly
9+
410
7.0
511
---
612

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

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,18 +16,35 @@
1616
class JsonMockResponse extends MockResponse
1717
{
1818
/**
19-
* @param mixed $body Any value that `json_encode()` can serialize
19+
* @param mixed $data Any value that `json_encode()` can serialize
20+
* @param bool $json Whether the data is already a JSON string
2021
*/
21-
public function __construct(mixed $body = [], array $info = [])
22+
public function __construct(mixed $data = [], array $info = [], bool $json = false)
2223
{
23-
try {
24-
$json = json_encode($body, \JSON_THROW_ON_ERROR);
25-
} catch (\JsonException $e) {
26-
throw new InvalidArgumentException('JSON encoding failed: '.$e->getMessage(), $e->getCode(), $e);
24+
if (!$json) {
25+
try {
26+
$data = json_encode($data, \JSON_THROW_ON_ERROR);
27+
} catch (\JsonException $e) {
28+
throw new InvalidArgumentException('JSON encoding failed: '.$e->getMessage(), $e->getCode(), $e);
29+
}
2730
}
2831

2932
$info['response_headers']['content-type'] ??= 'application/json';
3033

31-
parent::__construct($json, $info);
34+
parent::__construct($data, $info);
35+
}
36+
37+
public static function fromFile(string $path, array $info = []): static
38+
{
39+
if (!is_file($path)) {
40+
throw new \InvalidArgumentException(sprintf('File not found: "%s".', $path));
41+
}
42+
43+
$json = file_get_contents($path);
44+
if (!json_validate($json)) {
45+
throw new \InvalidArgumentException(sprintf('File "%s" does not contain valid JSON.', $path));
46+
}
47+
48+
return new static($json, $info, true);
3249
}
3350
}

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+
public static function fromFile(string $path, array $info = []): static
68+
{
69+
if (!is_file($path)) {
70+
throw new \InvalidArgumentException(sprintf('File not found: "%s".', $path));
71+
}
72+
73+
return new static(file_get_contents($path), $info);
74+
}
75+
6776
/**
6877
* Returns the options used when doing the request.
6978
*/

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+
public function testFromFile()
90+
{
91+
$client = new MockHttpClient(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+
public function testFromFileWithInvalidJson()
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
use Symfony\Component\HttpClient\Exception\InvalidArgumentException;
1616
use Symfony\Component\HttpClient\Exception\JsonException;
1717
use Symfony\Component\HttpClient\Exception\TransportException;
18+
use Symfony\Component\HttpClient\MockHttpClient;
1819
use Symfony\Component\HttpClient\Response\MockResponse;
1920

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

134132
(new MockResponse())->getContent();
135133
}
134+
135+
public function testFromFile()
136+
{
137+
$client = new MockHttpClient(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)