Skip to content

Commit 3e9c9c4

Browse files
committed
[HttpClient] Add JsonMockResponse::fromFile() and MockResponse::fromFile() shortcuts
1 parent 04be415 commit 3e9c9c4

File tree

8 files changed

+59
-3
lines changed

8 files changed

+59
-3
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ CHANGELOG
55
---
66

77
* Allow mocking `start_time` info in `MockResponse`
8+
* Add `MockResponse::fromFile()` and `JsonMockResponse::fromFile()` methods to help using fixtures files
89

910
7.0
1011
---

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+
public static function fromFile(string $path, array $info = []): static
35+
{
36+
if (!is_file($path)) {
37+
throw new InvalidArgumentException(sprintf('File not found: "%s".', $path));
38+
}
39+
40+
$json = file_get_contents($path);
41+
if (!json_validate($json)) {
42+
throw new \InvalidArgumentException(sprintf('File "%s" does not contain valid JSON.', $path));
43+
}
44+
45+
return new static(json_decode($json, true, flags: \JSON_THROW_ON_ERROR), $info);
46+
}
3347
}

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
*/
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
foo ccc

Tests/Response/Fixtures/response.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"foo": "bar"
3+
}

Tests/Response/Fixtures/response.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
foo bar ccc

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
}

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)