File tree Expand file tree Collapse file tree 2 files changed +27
-0
lines changed
src/Illuminate/Http/Client Expand file tree Collapse file tree 2 files changed +27
-0
lines changed Original file line number Diff line number Diff line change 5
5
use ArrayAccess ;
6
6
use GuzzleHttp \Psr7 \StreamWrapper ;
7
7
use Illuminate \Support \Collection ;
8
+ use Illuminate \Support \Fluent ;
8
9
use Illuminate \Support \Traits \Macroable ;
9
10
use LogicException ;
10
11
use Stringable ;
@@ -108,6 +109,17 @@ public function collect($key = null)
108
109
return new Collection ($ this ->json ($ key ));
109
110
}
110
111
112
+ /**
113
+ * Get the JSON decoded body of the response as a fluent object.
114
+ *
115
+ * @param string|null $key
116
+ * @return \Illuminate\Support\Fluent
117
+ */
118
+ public function fluent ($ key = null )
119
+ {
120
+ return new Fluent ((array ) $ this ->json ($ key ));
121
+ }
122
+
111
123
/**
112
124
* Get the body of the response as a PHP resource.
113
125
*
Original file line number Diff line number Diff line change @@ -402,6 +402,21 @@ public function testResponseCanBeReturnedAsCollection()
402
402
$ this ->assertEquals (collect (), $ response ->collect ('missing_key ' ));
403
403
}
404
404
405
+ public function testResponseCanBeReturnedAsFluent ()
406
+ {
407
+ $ this ->factory ->fake ([
408
+ '* ' => ['result ' => ['foo ' => 'bar ' ]],
409
+ ]);
410
+
411
+ $ response = $ this ->factory ->get ('http://foo.com/api ' );
412
+
413
+ $ this ->assertInstanceOf (Fluent::class, $ response ->fluent ());
414
+ $ this ->assertEquals (fluent (['result ' => ['foo ' => 'bar ' ]]), $ response ->fluent ());
415
+ $ this ->assertEquals (fluent (['foo ' => 'bar ' ]), $ response ->fluent ('result ' ));
416
+ $ this ->assertEquals (fluent (['bar ' ]), $ response ->fluent ('result.foo ' ));
417
+ $ this ->assertEquals (fluent ([]), $ response ->fluent ('missing_key ' ));
418
+ }
419
+
405
420
public function testSendRequestBodyAsJsonByDefault ()
406
421
{
407
422
$ body = '{"test":"phpunit"} ' ;
You can’t perform that action at this time.
0 commit comments