Skip to content

Commit 464de19

Browse files
authored
[12.x] Make the Client Response class tappable (#58115)
* Make the Client Response class tappable. * Small change according to Style CI.
1 parent b094c28 commit 464de19

File tree

2 files changed

+47
-1
lines changed

2 files changed

+47
-1
lines changed

src/Illuminate/Http/Client/Response.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use Illuminate\Support\Collection;
88
use Illuminate\Support\Fluent;
99
use Illuminate\Support\Traits\Macroable;
10+
use Illuminate\Support\Traits\Tappable;
1011
use LogicException;
1112
use Stringable;
1213

@@ -15,7 +16,7 @@
1516
*/
1617
class Response implements ArrayAccess, Stringable
1718
{
18-
use Concerns\DeterminesStatusCode, Macroable {
19+
use Concerns\DeterminesStatusCode, Tappable, Macroable {
1920
__call as macroCall;
2021
}
2122

tests/Http/HttpClientTest.php

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -387,6 +387,51 @@ public function testResponseObjectAsObject()
387387
$this->assertSame('bar', $response->object()->result->foo);
388388
}
389389

390+
public function testResponseObjectIsTappable()
391+
{
392+
$bar = null;
393+
$this->factory->fake([
394+
'*' => ['result' => ['foo' => 'bar']],
395+
]);
396+
397+
$this->factory->get('http://foo.com/api')
398+
->tap(function (Response $response) use (&$bar) {
399+
$bar = $response['result']['foo'];
400+
});
401+
402+
$this->assertSame('bar', $bar);
403+
}
404+
405+
public function testResponseObjectIsMacroable()
406+
{
407+
Response::macro('movieFields', function () {
408+
return $this->collect()
409+
->mapWithKeys(fn ($field, $key) => [strtolower($key) => $field])
410+
->toArray();
411+
});
412+
413+
$response = $this->factory->fake([
414+
'*' => [
415+
'Title' => 'The Godfather',
416+
'Year' => 1972,
417+
'Rated' => 'R',
418+
'Runtime' => '175 min',
419+
'Director' => 'Francis Ford Coppola',
420+
],
421+
]);
422+
423+
$response = $this->factory->get('http://www.omdbapi.com/?apikey=test_api_key&i=test_imdb_id');
424+
425+
$this->assertIsArray($response->movieFields());
426+
$this->assertSame([
427+
'title' => 'The Godfather',
428+
'year' => 1972,
429+
'rated' => 'R',
430+
'runtime' => '175 min',
431+
'director' => 'Francis Ford Coppola',
432+
], $response->movieFields());
433+
}
434+
390435
public function testResponseCanBeReturnedAsResource()
391436
{
392437
$this->factory->fake([

0 commit comments

Comments
 (0)