Skip to content

Commit 53b47ad

Browse files
authored
docs: add array on object method (#41866)
1 parent 05dd0e0 commit 53b47ad

File tree

2 files changed

+25
-1
lines changed

2 files changed

+25
-1
lines changed

src/Illuminate/Http/Client/Response.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ public function json($key = null, $default = null)
7171
/**
7272
* Get the JSON decoded body of the response as an object.
7373
*
74-
* @return object
74+
* @return object|array
7575
*/
7676
public function object()
7777
{

tests/Http/HttpClientTest.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,30 @@ public function testResponseBodyCasting()
9393
$this->assertSame('bar', $response->json('result.foo'));
9494
$this->assertSame('default', $response->json('missing_key', 'default'));
9595
$this->assertSame(['foo' => 'bar'], $response['result']);
96+
}
97+
98+
public function testResponseObjectAsArray()
99+
{
100+
$this->factory->fake([
101+
'*' => [['foo' => 'bar'], ['bar' => 'foo']],
102+
]);
103+
104+
$response = $this->factory->get('http://foo.com/api');
105+
106+
$this->assertSame('[{"foo":"bar"},{"bar":"foo"}]', $response->body());
107+
$this->assertSame('[{"foo":"bar"},{"bar":"foo"}]', (string) $response);
108+
$this->assertIsArray($response->object());
109+
$this->assertSame('bar', $response->object()[0]->foo);
110+
}
111+
112+
public function testResponseObjectAsObject()
113+
{
114+
$this->factory->fake([
115+
'*' => ['result' => ['foo' => 'bar']],
116+
]);
117+
118+
$response = $this->factory->get('http://foo.com/api');
119+
96120
$this->assertIsObject($response->object());
97121
$this->assertSame('bar', $response->object()->result->foo);
98122
}

0 commit comments

Comments
 (0)