From 60bc9a0c6246e38047aef6715794a7d62eb8e7ed Mon Sep 17 00:00:00 2001 From: Craig Morris Date: Thu, 23 Jan 2025 21:53:52 +1100 Subject: [PATCH 1/3] Add dump and dd methods to Response class for debugging --- src/Illuminate/Http/Client/Response.php | 63 +++++++++++++++++++++++++ tests/Http/HttpClientTest.php | 57 ++++++++++++++++++++++ 2 files changed, 120 insertions(+) diff --git a/src/Illuminate/Http/Client/Response.php b/src/Illuminate/Http/Client/Response.php index 491eecf081fd..8f3ae05755e0 100644 --- a/src/Illuminate/Http/Client/Response.php +++ b/src/Illuminate/Http/Client/Response.php @@ -9,6 +9,7 @@ use Illuminate\Support\Traits\Macroable; use LogicException; use Stringable; +use Symfony\Component\VarDumper\VarDumper; /** * @mixin \Psr\Http\Message\ResponseInterface @@ -302,6 +303,68 @@ public function toException() } } + /** + * Dump the content from the response. + * + * @param string|null $key + * @return $this + */ + public function dump($key = null) + { + $content = $this->body(); + + $json = json_decode($content); + + if (json_last_error() === JSON_ERROR_NONE) { + $content = $json; + } + + if (! is_null($key)) { + dump(data_get($content, $key)); + } else { + dump($content); + } + + return $this; + } + + /** + * Dump the content from the response and end the script. + * + * @param string|null $key + * @return never + */ + public function dd($key = null) + { + $this->dump($key); + + exit(1); + } + + /** + * Dump the headers from the response. + * + * @return $this + */ + public function dumpHeaders() + { + dump($this->headers()); + + return $this; + } + + /** + * Dump the headers from the response and end the script. + * + * @return never + */ + public function ddHeaders() + { + $this->dumpHeaders(); + + exit(1); + } + /** * Throw an exception if a server or client error occurred. * diff --git a/tests/Http/HttpClientTest.php b/tests/Http/HttpClientTest.php index 64745bf49987..21c9718c2da6 100644 --- a/tests/Http/HttpClientTest.php +++ b/tests/Http/HttpClientTest.php @@ -1606,6 +1606,63 @@ public function testCanDump() VarDumper::setHandler(null); } + public function testResponseCanDump() + { + $dumped = []; + + VarDumper::setHandler(function ($value) use (&$dumped) { + $dumped[] = $value; + }); + + $this->factory->fake([ + '200.com' => $this->factory::response('hello', 200), + ]); + + $this->factory->get('http://200.com')->dump(); + + $this->assertSame('hello', $dumped[0]); + + VarDumper::setHandler(null); + } + + public function testResponseCanDumpWithKey() + { + $dumped = []; + + VarDumper::setHandler(function ($value) use (&$dumped) { + $dumped[] = $value; + }); + + $this->factory->fake([ + '200.com' => $this->factory::response(['hello' => 'world'], 200), + ]); + + $this->factory->get('http://200.com')->dump('hello'); + + $this->assertSame('world', $dumped[0]); + + VarDumper::setHandler(null); + } + + public function testResponseCanDumpHeaders() + { + $dumped = []; + + VarDumper::setHandler(function ($value) use (&$dumped) { + $dumped[] = $value; + }); + + $this->factory->fake([ + '200.com' => $this->factory::response('hello', 200, ['hello' => 'world']), + ]); + + $this->factory->get('http://200.com')->dumpHeaders(); + + $this->assertSame(['hello' => ['world']], $dumped[0]); + + VarDumper::setHandler(null); + } + public function testResponseSequenceIsMacroable() { ResponseSequence::macro('customMethod', function () { From 65a61eb86ae5c75b50fe1c6e27746d3131172091 Mon Sep 17 00:00:00 2001 From: Craig Morris Date: Thu, 23 Jan 2025 21:59:55 +1100 Subject: [PATCH 2/3] Remove unused VarDumper import from Response class --- src/Illuminate/Http/Client/Response.php | 1 - 1 file changed, 1 deletion(-) diff --git a/src/Illuminate/Http/Client/Response.php b/src/Illuminate/Http/Client/Response.php index 8f3ae05755e0..a9167467e4b2 100644 --- a/src/Illuminate/Http/Client/Response.php +++ b/src/Illuminate/Http/Client/Response.php @@ -9,7 +9,6 @@ use Illuminate\Support\Traits\Macroable; use LogicException; use Stringable; -use Symfony\Component\VarDumper\VarDumper; /** * @mixin \Psr\Http\Message\ResponseInterface From 5ae7905caf3381c680daea06c1641404da9042c6 Mon Sep 17 00:00:00 2001 From: Taylor Otwell Date: Thu, 23 Jan 2025 08:03:57 -0600 Subject: [PATCH 3/3] Update Response.php --- src/Illuminate/Http/Client/Response.php | 124 ++++++++++++------------ 1 file changed, 62 insertions(+), 62 deletions(-) diff --git a/src/Illuminate/Http/Client/Response.php b/src/Illuminate/Http/Client/Response.php index a9167467e4b2..ac51d9c7ade8 100644 --- a/src/Illuminate/Http/Client/Response.php +++ b/src/Illuminate/Http/Client/Response.php @@ -302,68 +302,6 @@ public function toException() } } - /** - * Dump the content from the response. - * - * @param string|null $key - * @return $this - */ - public function dump($key = null) - { - $content = $this->body(); - - $json = json_decode($content); - - if (json_last_error() === JSON_ERROR_NONE) { - $content = $json; - } - - if (! is_null($key)) { - dump(data_get($content, $key)); - } else { - dump($content); - } - - return $this; - } - - /** - * Dump the content from the response and end the script. - * - * @param string|null $key - * @return never - */ - public function dd($key = null) - { - $this->dump($key); - - exit(1); - } - - /** - * Dump the headers from the response. - * - * @return $this - */ - public function dumpHeaders() - { - dump($this->headers()); - - return $this; - } - - /** - * Dump the headers from the response and end the script. - * - * @return never - */ - public function ddHeaders() - { - $this->dumpHeaders(); - - exit(1); - } - /** * Throw an exception if a server or client error occurred. * @@ -458,6 +396,68 @@ public function throwIfServerError() return $this->serverError() ? $this->throw() : $this; } + /** + * Dump the content from the response. + * + * @param string|null $key + * @return $this + */ + public function dump($key = null) + { + $content = $this->body(); + + $json = json_decode($content); + + if (json_last_error() === JSON_ERROR_NONE) { + $content = $json; + } + + if (! is_null($key)) { + dump(data_get($content, $key)); + } else { + dump($content); + } + + return $this; + } + + /** + * Dump the content from the response and end the script. + * + * @param string|null $key + * @return never + */ + public function dd($key = null) + { + $this->dump($key); + + exit(1); + } + + /** + * Dump the headers from the response. + * + * @return $this + */ + public function dumpHeaders() + { + dump($this->headers()); + + return $this; + } + + /** + * Dump the headers from the response and end the script. + * + * @return never + */ + public function ddHeaders() + { + $this->dumpHeaders(); + + exit(1); + } + /** * Determine if the given offset exists. *