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

Skip to content

[HttpClient] Add JsonMockResponse::fromFile() and MockResponse::fromFile() shortcuts #52970

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/Symfony/Component/HttpClient/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ CHANGELOG
---

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

7.0
---
Expand Down
14 changes: 14 additions & 0 deletions src/Symfony/Component/HttpClient/Response/JsonMockResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,18 @@ public function __construct(mixed $body = [], array $info = [])

parent::__construct($json, $info);
}

public static function fromFile(string $path, array $info = []): static
{
if (!is_file($path)) {
throw new InvalidArgumentException(sprintf('File not found: "%s".', $path));
}

$json = file_get_contents($path);
if (!json_validate($json)) {
throw new \InvalidArgumentException(sprintf('File "%s" does not contain valid JSON.', $path));
}

return new static(json_decode($json, true, flags: \JSON_THROW_ON_ERROR), $info);
}
}
9 changes: 9 additions & 0 deletions src/Symfony/Component/HttpClient/Response/MockResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,15 @@ public function __construct(string|iterable $body = '', array $info = [])
self::addResponseHeaders($responseHeaders, $this->info, $this->headers);
}

public static function fromFile(string $path, array $info = []): static
{
if (!is_file($path)) {
throw new \InvalidArgumentException(sprintf('File not found: "%s".', $path));
}

return new static(file_get_contents($path), $info);
}

/**
* Returns the options used when doing the request.
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
foo ccc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"foo": "bar"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
foo bar ccc
Original file line number Diff line number Diff line change
Expand Up @@ -85,4 +85,25 @@ public static function responseHeadersProvider(): array
['application/problem+json', ['x-foo' => 'ccc', 'content-type' => 'application/problem+json']],
];
}

public function testFromFile()
{
$client = new MockHttpClient(JsonMockResponse::fromFile(__DIR__.'/Fixtures/response.json'));
$response = $client->request('GET', 'https://symfony.com');

$this->assertSame([
'foo' => 'bar',
], $response->toArray());
$this->assertSame('application/json', $response->getHeaders()['content-type'][0]);
}

public function testFromFileWithInvalidJson()
{
$path = __DIR__.'/Fixtures/invalid_json.json';

$this->expectException(\InvalidArgumentException::class);
$this->expectExceptionMessage(sprintf('File "%s" does not contain valid JSON.', $path));

JsonMockResponse::fromFile($path);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,9 @@
use Symfony\Component\HttpClient\Exception\InvalidArgumentException;
use Symfony\Component\HttpClient\Exception\JsonException;
use Symfony\Component\HttpClient\Exception\TransportException;
use Symfony\Component\HttpClient\MockHttpClient;
use Symfony\Component\HttpClient\Response\MockResponse;

/**
* Test methods from Symfony\Component\HttpClient\Response\*ResponseTrait.
*/
class MockResponseTest extends TestCase
{
public function testTotalTimeShouldBeSimulatedWhenNotProvided()
Expand Down Expand Up @@ -133,4 +131,12 @@ public function testMustBeIssuedByMockHttpClient()

(new MockResponse())->getContent();
}

public function testFromFile()
{
$client = new MockHttpClient(MockResponse::fromFile(__DIR__.'/Fixtures/response.txt'));
$response = $client->request('GET', 'https://symfony.com');

$this->assertSame('foo bar ccc', $response->getContent());
}
}