Skip to content
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

[9.x] Add ability to supply HTTP client methods with JsonSerializable instances #41055

Merged
merged 2 commits into from
Feb 16, 2022
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
7 changes: 6 additions & 1 deletion src/Illuminate/Http/Client/PendingRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use Illuminate\Support\Str;
use Illuminate\Support\Traits\Conditionable;
use Illuminate\Support\Traits\Macroable;
use JsonSerializable;
use Psr\Http\Message\MessageInterface;
use Symfony\Component\VarDumper\VarDumper;

Expand Down Expand Up @@ -832,7 +833,11 @@ protected function parseRequestData($method, $url, array $options)
$laravelData = is_array($parsedData) ? $parsedData : [];
}

return $laravelData;
if ($laravelData instanceof JsonSerializable) {
$laravelData = $laravelData->jsonSerialize();
}

return is_array($laravelData) ? $laravelData : [];
}

/**
Expand Down
23 changes: 23 additions & 0 deletions tests/Http/HttpClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use Illuminate\Support\Collection;
use Illuminate\Support\Fluent;
use Illuminate\Support\Str;
use JsonSerializable;
use Mockery as m;
use OutOfBoundsException;
use PHPUnit\Framework\AssertionFailedError;
Expand Down Expand Up @@ -198,6 +199,28 @@ public function testCanSendArrayableFormData()
});
}

public function testCanSendJsonSerializableData()
{
$this->factory->fake();

$this->factory->asJson()->post('http://foo.com/form', new class implements JsonSerializable
{
public function jsonSerialize()
{
return [
'name' => 'Taylor',
'title' => 'Laravel Developer',
];
}
});

$this->factory->assertSent(function (Request $request) {
return $request->url() === 'http://foo.com/form' &&
$request->hasHeader('Content-Type', 'application/json') &&
$request['name'] === 'Taylor';
});
}

public function testRecordedCallsAreEmptiedWhenFakeIsCalled()
{
$this->factory->fake([
Expand Down