From 6a2a03231de79e26caadb70075e58bf01d5cf7bb Mon Sep 17 00:00:00 2001 From: Sammyjo20 <29132017+Sammyjo20@users.noreply.github.com> Date: Mon, 18 Mar 2024 23:01:14 +0000 Subject: [PATCH 01/23] Feature | Debugger Helper Method --- composer.json | 5 ++++- src/Helpers/Debug.php | 31 +++++++++++++++++++++++++++++ src/helpers.php | 45 +++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 80 insertions(+), 1 deletion(-) create mode 100644 src/Helpers/Debug.php create mode 100644 src/helpers.php diff --git a/composer.json b/composer.json index 01b82d21..d20b3393 100644 --- a/composer.json +++ b/composer.json @@ -49,7 +49,10 @@ "autoload": { "psr-4": { "Saloon\\": "src/" - } + }, + "files": [ + "src/helpers.php" + ] }, "autoload-dev": { "psr-4": { diff --git a/src/Helpers/Debug.php b/src/Helpers/Debug.php new file mode 100644 index 00000000..a93f7c7b --- /dev/null +++ b/src/Helpers/Debug.php @@ -0,0 +1,31 @@ + $psrRequest->getMethod(), + 'uri' => (string)$psrRequest->getUri(), + 'headers' => $psrRequest->getHeaders(), + 'body' => (string)$psrRequest->getBody(), + ]); + + if ($die === true) { + exit(1); + } + } + + public static function dump(PendingRequest $request, RequestInterface $psrRequest): void + { + static::dd($request, $psrRequest, false); + } +} diff --git a/src/helpers.php b/src/helpers.php new file mode 100644 index 00000000..e5a88197 --- /dev/null +++ b/src/helpers.php @@ -0,0 +1,45 @@ +debugRequest(function (PendingRequest $pendingRequest, MessageInterface $psrRequest) use ($die) { + $headers = []; + + foreach ($psrRequest->getHeaders() as $headerName => $value) { + $headers[$headerName] = implode(';', $value); + } + + VarDumper::dump([ + 'request' => $pendingRequest->getRequest()::class, + 'method' => $psrRequest->getMethod(), + 'uri' => (string)$psrRequest->getUri(), + 'headers' => $headers, + 'body' => (string)$psrRequest->getBody(), + ]); + + if ($die === true) { + exit(1); + } + }); + + return $debuggable; +} From 4a1137743bbcc83c93b9a6ffa66abf32a9da4d6a Mon Sep 17 00:00:00 2001 From: Sammyjo20 <29132017+Sammyjo20@users.noreply.github.com> Date: Mon, 18 Mar 2024 23:06:49 +0000 Subject: [PATCH 02/23] Cleanup --- src/Helpers/Debug.php | 31 ------------------------------- 1 file changed, 31 deletions(-) delete mode 100644 src/Helpers/Debug.php diff --git a/src/Helpers/Debug.php b/src/Helpers/Debug.php deleted file mode 100644 index a93f7c7b..00000000 --- a/src/Helpers/Debug.php +++ /dev/null @@ -1,31 +0,0 @@ - $psrRequest->getMethod(), - 'uri' => (string)$psrRequest->getUri(), - 'headers' => $psrRequest->getHeaders(), - 'body' => (string)$psrRequest->getBody(), - ]); - - if ($die === true) { - exit(1); - } - } - - public static function dump(PendingRequest $request, RequestInterface $psrRequest): void - { - static::dd($request, $psrRequest, false); - } -} From 2d43de039870bc72f56092ca89a6e0529df781ed Mon Sep 17 00:00:00 2001 From: Sammyjo20 <29132017+Sammyjo20@users.noreply.github.com> Date: Mon, 18 Mar 2024 23:21:27 +0000 Subject: [PATCH 03/23] Rename to debug --- src/helpers.php | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/src/helpers.php b/src/helpers.php index e5a88197..8a42fcf5 100644 --- a/src/helpers.php +++ b/src/helpers.php @@ -11,15 +11,19 @@ use Symfony\Component\VarDumper\VarDumper; /** - * Debug Request + * Debug a request being sent * - * Wrap this around a request class to debug the request being sent. + * Wrap this around a connector or request before sending to debug the + * raw request data being sent to the HTTP sender. Note that anything + * added by the sender will not be reflected here. + * + * Example usage: debug($connector)->send($request) * * @template TDebuggable of \Saloon\Http\Request|\Saloon\Http\Connector * @param TDebuggable $debuggable * @return TDebuggable */ -function debugger(Request|Connector $debuggable, bool $die = true): Request|Connector +function debug(Request|Connector $debuggable, bool $die = true): Request|Connector { $debuggable->debugRequest(function (PendingRequest $pendingRequest, MessageInterface $psrRequest) use ($die) { $headers = []; From cd137425f05476e32e74c3e96ed9e637bb0fd06d Mon Sep 17 00:00:00 2001 From: Sammyjo20 <29132017+Sammyjo20@users.noreply.github.com> Date: Mon, 18 Mar 2024 23:38:49 +0000 Subject: [PATCH 04/23] Styling --- src/helpers.php | 4 ++++ tests/Feature/RequestTest.php | 3 ++- 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/src/helpers.php b/src/helpers.php index 8a42fcf5..afb29a6f 100644 --- a/src/helpers.php +++ b/src/helpers.php @@ -32,6 +32,8 @@ function debug(Request|Connector $debuggable, bool $die = true): Request|Connect $headers[$headerName] = implode(';', $value); } + VarDumper::dump('', '"" 🤠 Saloon Request 🤠'); + VarDumper::dump([ 'request' => $pendingRequest->getRequest()::class, 'method' => $psrRequest->getMethod(), @@ -40,6 +42,8 @@ function debug(Request|Connector $debuggable, bool $die = true): Request|Connect 'body' => (string)$psrRequest->getBody(), ]); + VarDumper::dump('', '""---------------------'); + if ($die === true) { exit(1); } diff --git a/tests/Feature/RequestTest.php b/tests/Feature/RequestTest.php index 9b1ee9d8..ef1fffcf 100644 --- a/tests/Feature/RequestTest.php +++ b/tests/Feature/RequestTest.php @@ -9,10 +9,11 @@ use Saloon\Tests\Fixtures\Requests\ErrorRequest; use Saloon\Tests\Fixtures\Connectors\TestConnector; use Saloon\Tests\Fixtures\Requests\HasConnectorUserRequest; +use function Saloon\debug; test('a request can be made successfully', function () { $connector = new TestConnector(); - $response = $connector->send(new UserRequest); + $response = debug($connector)->send(new UserRequest); $data = $response->json(); From 6bbc6fe43dbd8ff812425daff655b42748d0b9aa Mon Sep 17 00:00:00 2001 From: Sammyjo20 Date: Mon, 18 Mar 2024 23:39:12 +0000 Subject: [PATCH 05/23] =?UTF-8?q?=F0=9F=AA=84=20Code=20Style=20Fixes?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- tests/Feature/RequestTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/Feature/RequestTest.php b/tests/Feature/RequestTest.php index ef1fffcf..f5244062 100644 --- a/tests/Feature/RequestTest.php +++ b/tests/Feature/RequestTest.php @@ -3,13 +3,13 @@ declare(strict_types=1); use Saloon\Http\Response; +use function Saloon\debug; use Saloon\Http\PendingRequest; use Saloon\Http\Senders\GuzzleSender; use Saloon\Tests\Fixtures\Requests\UserRequest; use Saloon\Tests\Fixtures\Requests\ErrorRequest; use Saloon\Tests\Fixtures\Connectors\TestConnector; use Saloon\Tests\Fixtures\Requests\HasConnectorUserRequest; -use function Saloon\debug; test('a request can be made successfully', function () { $connector = new TestConnector(); From d1a307f60dd584ff56382c17ad14466a022badac Mon Sep 17 00:00:00 2001 From: Sammyjo20 <29132017+Sammyjo20@users.noreply.github.com> Date: Mon, 18 Mar 2024 23:43:22 +0000 Subject: [PATCH 06/23] Better styling --- src/helpers.php | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/src/helpers.php b/src/helpers.php index afb29a6f..717ab48a 100644 --- a/src/helpers.php +++ b/src/helpers.php @@ -32,17 +32,13 @@ function debug(Request|Connector $debuggable, bool $die = true): Request|Connect $headers[$headerName] = implode(';', $value); } - VarDumper::dump('', '"" 🤠 Saloon Request 🤠'); - VarDumper::dump([ 'request' => $pendingRequest->getRequest()::class, 'method' => $psrRequest->getMethod(), 'uri' => (string)$psrRequest->getUri(), 'headers' => $headers, 'body' => (string)$psrRequest->getBody(), - ]); - - VarDumper::dump('', '""---------------------'); + ], '🤠 Saloon Request ->'); if ($die === true) { exit(1); From ea654bae937b2c04cc8fd1543ba276f18f4a9c88 Mon Sep 17 00:00:00 2001 From: Sammyjo20 <29132017+Sammyjo20@users.noreply.github.com> Date: Tue, 19 Mar 2024 00:09:27 +0000 Subject: [PATCH 07/23] Added dd() and dump() to trait --- src/Traits/HasDebugging.php | 27 +++++++++++++++++++++++++++ src/helpers.php | 2 ++ tests/Feature/RequestTest.php | 2 +- 3 files changed, 30 insertions(+), 1 deletion(-) diff --git a/src/Traits/HasDebugging.php b/src/Traits/HasDebugging.php index fd7f8196..1f93da7d 100644 --- a/src/Traits/HasDebugging.php +++ b/src/Traits/HasDebugging.php @@ -4,9 +4,12 @@ namespace Saloon\Traits; +use Saloon\Http\Connector; +use Saloon\Http\Request; use Saloon\Http\Response; use Saloon\Enums\PipeOrder; use Saloon\Http\PendingRequest; +use function Saloon\debug; trait HasDebugging { @@ -45,4 +48,28 @@ public function debugResponse(callable $onResponse): static return $this; } + + /** + * Dump a request before being sent and terminate the application + * + * Note that anything added by the sender will not be reflected here. + * + * Requires symfony/var-dumper + */ + public function dd(): static + { + return debug($this, die: true); + } + + /** + * Dump a request before being sent + * + * Note that anything added by the sender will not be reflected here. + * + * Requires symfony/var-dumper + */ + public function dump(): static + { + return debug($this, die: false); + } } diff --git a/src/helpers.php b/src/helpers.php index 717ab48a..892c6202 100644 --- a/src/helpers.php +++ b/src/helpers.php @@ -19,6 +19,8 @@ * * Example usage: debug($connector)->send($request) * + * Requires symfony/var-dumper + * * @template TDebuggable of \Saloon\Http\Request|\Saloon\Http\Connector * @param TDebuggable $debuggable * @return TDebuggable diff --git a/tests/Feature/RequestTest.php b/tests/Feature/RequestTest.php index f5244062..208ee0b9 100644 --- a/tests/Feature/RequestTest.php +++ b/tests/Feature/RequestTest.php @@ -13,7 +13,7 @@ test('a request can be made successfully', function () { $connector = new TestConnector(); - $response = debug($connector)->send(new UserRequest); + $response = $connector->dump()->send(new UserRequest); $data = $response->json(); From c28c5e182b4854cad974101008e1eba1448b7212 Mon Sep 17 00:00:00 2001 From: Sammyjo20 Date: Tue, 19 Mar 2024 00:09:53 +0000 Subject: [PATCH 08/23] =?UTF-8?q?=F0=9F=AA=84=20Code=20Style=20Fixes?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Traits/HasDebugging.php | 3 +-- tests/Feature/RequestTest.php | 1 - 2 files changed, 1 insertion(+), 3 deletions(-) diff --git a/src/Traits/HasDebugging.php b/src/Traits/HasDebugging.php index 1f93da7d..ab94c3f0 100644 --- a/src/Traits/HasDebugging.php +++ b/src/Traits/HasDebugging.php @@ -4,12 +4,11 @@ namespace Saloon\Traits; -use Saloon\Http\Connector; use Saloon\Http\Request; use Saloon\Http\Response; +use function Saloon\debug; use Saloon\Enums\PipeOrder; use Saloon\Http\PendingRequest; -use function Saloon\debug; trait HasDebugging { diff --git a/tests/Feature/RequestTest.php b/tests/Feature/RequestTest.php index 208ee0b9..15a2bf07 100644 --- a/tests/Feature/RequestTest.php +++ b/tests/Feature/RequestTest.php @@ -3,7 +3,6 @@ declare(strict_types=1); use Saloon\Http\Response; -use function Saloon\debug; use Saloon\Http\PendingRequest; use Saloon\Http\Senders\GuzzleSender; use Saloon\Tests\Fixtures\Requests\UserRequest; From 6782c7f3d5c6c773da1447fb5263092314ef0928 Mon Sep 17 00:00:00 2001 From: Sammyjo20 <29132017+Sammyjo20@users.noreply.github.com> Date: Tue, 19 Mar 2024 00:59:38 +0000 Subject: [PATCH 09/23] Default implementations for debugRequest and debugResponse --- src/Traits/HasDebugging.php | 16 ++++++-- src/helpers.php | 70 ++++++++++++++++++++++++----------- tests/Feature/RequestTest.php | 2 +- 3 files changed, 62 insertions(+), 26 deletions(-) diff --git a/src/Traits/HasDebugging.php b/src/Traits/HasDebugging.php index ab94c3f0..8c7467a8 100644 --- a/src/Traits/HasDebugging.php +++ b/src/Traits/HasDebugging.php @@ -15,11 +15,15 @@ trait HasDebugging /** * Register a request debugger * - * @param callable(\Saloon\Http\PendingRequest, \Psr\Http\Message\RequestInterface): void $onRequest + * @param callable(\Saloon\Http\PendingRequest, \Psr\Http\Message\RequestInterface): void|null $onRequest * @return $this */ - public function debugRequest(callable $onRequest): static + public function debugRequest(?callable $onRequest = null): static { + if (is_null($onRequest)) { + return debug($this, response: false, die: false); + } + $this->middleware()->onRequest( callable: static function (PendingRequest $pendingRequest) use ($onRequest): void { $onRequest($pendingRequest, $pendingRequest->createPsrRequest()); @@ -33,11 +37,15 @@ public function debugRequest(callable $onRequest): static /** * Register a response debugger * - * @param callable(\Saloon\Http\Response, \Psr\Http\Message\ResponseInterface): void $onResponse + * @param callable(\Saloon\Http\Response, \Psr\Http\Message\ResponseInterface): void|null $onResponse * @return $this */ - public function debugResponse(callable $onResponse): static + public function debugResponse(?callable $onResponse = null): static { + if (is_null($onResponse)) { + return debug($this, request: false, response: true, die: false); + } + $this->middleware()->onResponse( callable: static function (Response $response) use ($onResponse): void { $onResponse($response, $response->getPsrResponse()); diff --git a/src/helpers.php b/src/helpers.php index 892c6202..9ba0a170 100644 --- a/src/helpers.php +++ b/src/helpers.php @@ -4,10 +4,12 @@ namespace Saloon; +use Psr\Http\Message\RequestInterface; +use Psr\Http\Message\ResponseInterface; use Saloon\Http\Request; use Saloon\Http\Connector; use Saloon\Http\PendingRequest; -use Psr\Http\Message\MessageInterface; +use Saloon\Http\Response; use Symfony\Component\VarDumper\VarDumper; /** @@ -25,27 +27,53 @@ * @param TDebuggable $debuggable * @return TDebuggable */ -function debug(Request|Connector $debuggable, bool $die = true): Request|Connector +function debug(Request|Connector $debuggable, bool $request = true, bool $response = true, bool $die = true): Request|Connector { - $debuggable->debugRequest(function (PendingRequest $pendingRequest, MessageInterface $psrRequest) use ($die) { - $headers = []; - - foreach ($psrRequest->getHeaders() as $headerName => $value) { - $headers[$headerName] = implode(';', $value); - } - - VarDumper::dump([ - 'request' => $pendingRequest->getRequest()::class, - 'method' => $psrRequest->getMethod(), - 'uri' => (string)$psrRequest->getUri(), - 'headers' => $headers, - 'body' => (string)$psrRequest->getBody(), - ], '🤠 Saloon Request ->'); - - if ($die === true) { - exit(1); - } - }); + if ($request === true) { + $debuggable->debugRequest(function (PendingRequest $pendingRequest, RequestInterface $psrRequest) use ($response, $die) { + $headers = []; + + foreach ($psrRequest->getHeaders() as $headerName => $value) { + $headers[$headerName] = implode(';', $value); + } + + $label = end(explode('\\', $pendingRequest->getRequest()::class)); + + VarDumper::dump([ + 'request' => $pendingRequest->getRequest()::class, + 'method' => $psrRequest->getMethod(), + 'uri' => (string)$psrRequest->getUri(), + 'headers' => $headers, + 'body' => (string)$psrRequest->getBody(), + ], 'Saloon Request (' . $label . ') ->'); + + if ($response === false && $die === true) { + exit(1); + } + }); + } + + if ($response === true) { + $debuggable->debugResponse(function (Response $response, ResponseInterface $psrResponse) use ($die) { + $headers = []; + + foreach ($psrResponse->getHeaders() as $headerName => $value) { + $headers[$headerName] = implode(';', $value); + } + + $label = end(explode('\\', $response->getRequest()::class)); + + VarDumper::dump([ + 'status' => $response->status(), + 'headers' => $headers, + 'body' => $response->body(), + ], 'Saloon Response (' . $label . ') ->'); + + if ($die === true) { + exit(1); + } + }); + } return $debuggable; } diff --git a/tests/Feature/RequestTest.php b/tests/Feature/RequestTest.php index 15a2bf07..9b1ee9d8 100644 --- a/tests/Feature/RequestTest.php +++ b/tests/Feature/RequestTest.php @@ -12,7 +12,7 @@ test('a request can be made successfully', function () { $connector = new TestConnector(); - $response = $connector->dump()->send(new UserRequest); + $response = $connector->send(new UserRequest); $data = $response->json(); From 8e659120cc11b77d739d1d713788977bef57b95a Mon Sep 17 00:00:00 2001 From: Sammyjo20 Date: Tue, 19 Mar 2024 01:00:12 +0000 Subject: [PATCH 10/23] =?UTF-8?q?=F0=9F=AA=84=20Code=20Style=20Fixes?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/helpers.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/helpers.php b/src/helpers.php index 9ba0a170..b77bf468 100644 --- a/src/helpers.php +++ b/src/helpers.php @@ -4,12 +4,12 @@ namespace Saloon; -use Psr\Http\Message\RequestInterface; -use Psr\Http\Message\ResponseInterface; use Saloon\Http\Request; +use Saloon\Http\Response; use Saloon\Http\Connector; use Saloon\Http\PendingRequest; -use Saloon\Http\Response; +use Psr\Http\Message\RequestInterface; +use Psr\Http\Message\ResponseInterface; use Symfony\Component\VarDumper\VarDumper; /** From 7255efccd180b5c90aa0c178d55cb14a765eb616 Mon Sep 17 00:00:00 2001 From: Sammyjo20 <29132017+Sammyjo20@users.noreply.github.com> Date: Tue, 19 Mar 2024 01:05:17 +0000 Subject: [PATCH 11/23] New debug method --- src/Traits/HasDebugging.php | 24 ++++++++---------------- 1 file changed, 8 insertions(+), 16 deletions(-) diff --git a/src/Traits/HasDebugging.php b/src/Traits/HasDebugging.php index 8c7467a8..179f28a1 100644 --- a/src/Traits/HasDebugging.php +++ b/src/Traits/HasDebugging.php @@ -15,6 +15,8 @@ trait HasDebugging /** * Register a request debugger * + * Leave blank for a default debugger (requires symfony/var-dump) + * * @param callable(\Saloon\Http\PendingRequest, \Psr\Http\Message\RequestInterface): void|null $onRequest * @return $this */ @@ -37,13 +39,15 @@ public function debugRequest(?callable $onRequest = null): static /** * Register a response debugger * + * Leave blank for a default debugger (requires symfony/var-dump) + * * @param callable(\Saloon\Http\Response, \Psr\Http\Message\ResponseInterface): void|null $onResponse * @return $this */ public function debugResponse(?callable $onResponse = null): static { if (is_null($onResponse)) { - return debug($this, request: false, response: true, die: false); + return debug($this, request: false, die: false); } $this->middleware()->onResponse( @@ -57,26 +61,14 @@ public function debugResponse(?callable $onResponse = null): static } /** - * Dump a request before being sent and terminate the application - * - * Note that anything added by the sender will not be reflected here. - * - * Requires symfony/var-dumper - */ - public function dd(): static - { - return debug($this, die: true); - } - - /** - * Dump a request before being sent + * Dump a request before it is sent and the response. * * Note that anything added by the sender will not be reflected here. * * Requires symfony/var-dumper */ - public function dump(): static + public function debug(bool $die = false): static { - return debug($this, die: false); + return debug($this, die: $die); } } From 7253b20fc0f84c88a419f1ab8f18aa4f116fac83 Mon Sep 17 00:00:00 2001 From: Sammyjo20 <29132017+Sammyjo20@users.noreply.github.com> Date: Tue, 19 Mar 2024 01:10:56 +0000 Subject: [PATCH 12/23] Tidy up throws on response --- src/Http/Response.php | 7 ------- 1 file changed, 7 deletions(-) diff --git a/src/Http/Response.php b/src/Http/Response.php index 3d66eb3d..2d84ae3a 100644 --- a/src/Http/Response.php +++ b/src/Http/Response.php @@ -195,7 +195,6 @@ public function getSenderException(): ?Throwable * * @param array-key|null $key * @return ($key is null ? array : mixed) - * @throws \JsonException */ public function json(string|int|null $key = null, mixed $default = null): mixed { @@ -217,7 +216,6 @@ public function json(string|int|null $key = null, mixed $default = null): mixed * * @param array-key|null $key * @return ($key is null ? array : mixed) - * @throws \JsonException */ public function array(int|string|null $key = null, mixed $default = null): mixed { @@ -226,8 +224,6 @@ public function array(int|string|null $key = null, mixed $default = null): mixed /** * Get the JSON decoded body of the response as an object. - * - * @throws \JsonException */ public function object(): object { @@ -273,7 +269,6 @@ public function xmlReader(): XmlReader * * @param array-key|null $key * @return \Illuminate\Support\Collection - * @throws \JsonException */ public function collect(string|int|null $key = null): Collection { @@ -309,8 +304,6 @@ public function dto(): mixed /** * Convert the response into a DTO or throw a LogicException if the response failed - * - * @throws LogicException */ public function dtoOrFail(): mixed { From 6ff3ec6872507b11840f267a26421eca36075f27 Mon Sep 17 00:00:00 2001 From: Sammyjo20 <29132017+Sammyjo20@users.noreply.github.com> Date: Tue, 19 Mar 2024 01:15:41 +0000 Subject: [PATCH 13/23] Added die --- src/Traits/HasDebugging.php | 20 ++++++++++++++------ src/helpers.php | 1 + 2 files changed, 15 insertions(+), 6 deletions(-) diff --git a/src/Traits/HasDebugging.php b/src/Traits/HasDebugging.php index 179f28a1..88e46f1a 100644 --- a/src/Traits/HasDebugging.php +++ b/src/Traits/HasDebugging.php @@ -20,15 +20,19 @@ trait HasDebugging * @param callable(\Saloon\Http\PendingRequest, \Psr\Http\Message\RequestInterface): void|null $onRequest * @return $this */ - public function debugRequest(?callable $onRequest = null): static + public function debugRequest(?callable $onRequest = null, bool $die = false): static { if (is_null($onRequest)) { - return debug($this, response: false, die: false); + return debug($this, response: false, die: $die); } $this->middleware()->onRequest( - callable: static function (PendingRequest $pendingRequest) use ($onRequest): void { + callable: static function (PendingRequest $pendingRequest) use ($onRequest, $die): void { $onRequest($pendingRequest, $pendingRequest->createPsrRequest()); + + if ($die) { + exit(1); + } }, order: PipeOrder::LAST ); @@ -44,15 +48,19 @@ public function debugRequest(?callable $onRequest = null): static * @param callable(\Saloon\Http\Response, \Psr\Http\Message\ResponseInterface): void|null $onResponse * @return $this */ - public function debugResponse(?callable $onResponse = null): static + public function debugResponse(?callable $onResponse = null, bool $die = false): static { if (is_null($onResponse)) { - return debug($this, request: false, die: false); + return debug($this, request: false, die: $die); } $this->middleware()->onResponse( - callable: static function (Response $response) use ($onResponse): void { + callable: static function (Response $response) use ($onResponse, $die): void { $onResponse($response, $response->getPsrResponse()); + + if ($die) { + exit(1); + } }, order: PipeOrder::FIRST ); diff --git a/src/helpers.php b/src/helpers.php index b77bf468..0e563579 100644 --- a/src/helpers.php +++ b/src/helpers.php @@ -40,6 +40,7 @@ function debug(Request|Connector $debuggable, bool $request = true, bool $respon $label = end(explode('\\', $pendingRequest->getRequest()::class)); VarDumper::dump([ + 'connector' => $pendingRequest->getConnector()::class, 'request' => $pendingRequest->getRequest()::class, 'method' => $psrRequest->getMethod(), 'uri' => (string)$psrRequest->getUri(), From 314c4b37989993f053b421dcfc4550446395be75 Mon Sep 17 00:00:00 2001 From: Sammyjo20 <29132017+Sammyjo20@users.noreply.github.com> Date: Tue, 19 Mar 2024 01:21:04 +0000 Subject: [PATCH 14/23] Unique reference --- src/helpers.php | 8 ++++++-- tests/Feature/PoolTest.php | 2 ++ 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/src/helpers.php b/src/helpers.php index 0e563579..2b557079 100644 --- a/src/helpers.php +++ b/src/helpers.php @@ -37,7 +37,8 @@ function debug(Request|Connector $debuggable, bool $request = true, bool $respon $headers[$headerName] = implode(';', $value); } - $label = end(explode('\\', $pendingRequest->getRequest()::class)); + $debugIndex = random_int(0, 9999); + $label = end(explode('\\', $pendingRequest->getRequest()::class)) . ':' . $debugIndex; VarDumper::dump([ 'connector' => $pendingRequest->getConnector()::class, @@ -51,6 +52,8 @@ function debug(Request|Connector $debuggable, bool $request = true, bool $respon if ($response === false && $die === true) { exit(1); } + + $pendingRequest->config()->add('saloonDebugIndex', $debugIndex); }); } @@ -62,7 +65,8 @@ function debug(Request|Connector $debuggable, bool $request = true, bool $respon $headers[$headerName] = implode(';', $value); } - $label = end(explode('\\', $response->getRequest()::class)); + $debugIndex = $response->getPendingRequest()->config()->get('saloonDebugIndex'); + $label = end(explode('\\', $response->getRequest()::class)) . ':' . $debugIndex; VarDumper::dump([ 'status' => $response->status(), diff --git a/tests/Feature/PoolTest.php b/tests/Feature/PoolTest.php index 47a6efe0..263d21f1 100644 --- a/tests/Feature/PoolTest.php +++ b/tests/Feature/PoolTest.php @@ -20,6 +20,8 @@ $successCount = 0; $errorCount = 0; + $connector->debug(); + $pool = $connector->pool([ new UserRequest, new UserRequest, From 96ff118f4f5cf0c12f8dc8f4548d4c9626c2bd6f Mon Sep 17 00:00:00 2001 From: Sammyjo20 <29132017+Sammyjo20@users.noreply.github.com> Date: Tue, 19 Mar 2024 18:02:57 +0000 Subject: [PATCH 15/23] Removed helper, added comments --- composer.json | 5 +-- src/Traits/HasDebugging.php | 71 ++++++++++++++++++++++++++----- src/helpers.php | 84 ------------------------------------- tests/Feature/PoolTest.php | 2 - 4 files changed, 61 insertions(+), 101 deletions(-) delete mode 100644 src/helpers.php diff --git a/composer.json b/composer.json index d20b3393..01b82d21 100644 --- a/composer.json +++ b/composer.json @@ -49,10 +49,7 @@ "autoload": { "psr-4": { "Saloon\\": "src/" - }, - "files": [ - "src/helpers.php" - ] + } }, "autoload-dev": { "psr-4": { diff --git a/src/Traits/HasDebugging.php b/src/Traits/HasDebugging.php index 88e46f1a..3ddc7834 100644 --- a/src/Traits/HasDebugging.php +++ b/src/Traits/HasDebugging.php @@ -4,9 +4,10 @@ namespace Saloon\Traits; -use Saloon\Http\Request; +use Psr\Http\Message\RequestInterface; +use Psr\Http\Message\ResponseInterface; use Saloon\Http\Response; -use function Saloon\debug; +use Symfony\Component\VarDumper\VarDumper; use Saloon\Enums\PipeOrder; use Saloon\Http\PendingRequest; @@ -22,9 +23,32 @@ trait HasDebugging */ public function debugRequest(?callable $onRequest = null, bool $die = false): static { - if (is_null($onRequest)) { - return debug($this, response: false, die: $die); - } + // When the user has not specified a callable to debug with, we will use this default + // debugging driver. This will use symfony/var-dumper to display a nice output to + // the user's screen of the request. + + $onRequest ??= static function (PendingRequest $pendingRequest, RequestInterface $psrRequest) { + $headers = []; + + foreach ($psrRequest->getHeaders() as $headerName => $value) { + $headers[$headerName] = implode(';', $value); + } + + $className = explode('\\', $pendingRequest->getRequest()::class); + $label = end($className); + + VarDumper::dump([ + 'connector' => $pendingRequest->getConnector()::class, + 'request' => $pendingRequest->getRequest()::class, + 'method' => $psrRequest->getMethod(), + 'uri' => (string)$psrRequest->getUri(), + 'headers' => $headers, + 'body' => (string)$psrRequest->getBody(), + ], 'Saloon Request (' . $label . ') ->'); + }; + + // Register the middleware - we will use PipeOrder::FIRST to ensure that the response + // is shown before it is modified by the user's middleware. $this->middleware()->onRequest( callable: static function (PendingRequest $pendingRequest) use ($onRequest, $die): void { @@ -50,9 +74,29 @@ public function debugRequest(?callable $onRequest = null, bool $die = false): st */ public function debugResponse(?callable $onResponse = null, bool $die = false): static { - if (is_null($onResponse)) { - return debug($this, request: false, die: $die); - } + // When the user has not specified a callable to debug with, we will use this default + // debugging driver. This will use symfony/var-dumper to display a nice output to + // the user's screen of the response. + + $onResponse ??= static function (Response $response, ResponseInterface $psrResponse) { + $headers = []; + + foreach ($psrResponse->getHeaders() as $headerName => $value) { + $headers[$headerName] = implode(';', $value); + } + + $className = explode('\\', $response->getRequest()::class); + $label = end($className); + + VarDumper::dump([ + 'status' => $response->status(), + 'headers' => $headers, + 'body' => $response->body(), + ], 'Saloon Response (' . $label . ') ->'); + }; + + // Register the middleware - we will use PipeOrder::FIRST to ensure that the response + // is shown before it is modified by the user's middleware. $this->middleware()->onResponse( callable: static function (Response $response) use ($onResponse, $die): void { @@ -69,14 +113,19 @@ public function debugResponse(?callable $onResponse = null, bool $die = false): } /** - * Dump a request before it is sent and the response. + * Dump a pretty output of the request and response. + * + * This is useful if you would like to see the request right before it is sent + * to inspect the body and URI to ensure it is correct. You can also inspect + * the raw response as it comes back. * - * Note that anything added by the sender will not be reflected here. + * Note that any changes made to the PSR request by the sender will not be + * reflected by this output. * * Requires symfony/var-dumper */ public function debug(bool $die = false): static { - return debug($this, die: $die); + return $this->debugRequest()->debugResponse(die: $die); } } diff --git a/src/helpers.php b/src/helpers.php deleted file mode 100644 index 2b557079..00000000 --- a/src/helpers.php +++ /dev/null @@ -1,84 +0,0 @@ -send($request) - * - * Requires symfony/var-dumper - * - * @template TDebuggable of \Saloon\Http\Request|\Saloon\Http\Connector - * @param TDebuggable $debuggable - * @return TDebuggable - */ -function debug(Request|Connector $debuggable, bool $request = true, bool $response = true, bool $die = true): Request|Connector -{ - if ($request === true) { - $debuggable->debugRequest(function (PendingRequest $pendingRequest, RequestInterface $psrRequest) use ($response, $die) { - $headers = []; - - foreach ($psrRequest->getHeaders() as $headerName => $value) { - $headers[$headerName] = implode(';', $value); - } - - $debugIndex = random_int(0, 9999); - $label = end(explode('\\', $pendingRequest->getRequest()::class)) . ':' . $debugIndex; - - VarDumper::dump([ - 'connector' => $pendingRequest->getConnector()::class, - 'request' => $pendingRequest->getRequest()::class, - 'method' => $psrRequest->getMethod(), - 'uri' => (string)$psrRequest->getUri(), - 'headers' => $headers, - 'body' => (string)$psrRequest->getBody(), - ], 'Saloon Request (' . $label . ') ->'); - - if ($response === false && $die === true) { - exit(1); - } - - $pendingRequest->config()->add('saloonDebugIndex', $debugIndex); - }); - } - - if ($response === true) { - $debuggable->debugResponse(function (Response $response, ResponseInterface $psrResponse) use ($die) { - $headers = []; - - foreach ($psrResponse->getHeaders() as $headerName => $value) { - $headers[$headerName] = implode(';', $value); - } - - $debugIndex = $response->getPendingRequest()->config()->get('saloonDebugIndex'); - $label = end(explode('\\', $response->getRequest()::class)) . ':' . $debugIndex; - - VarDumper::dump([ - 'status' => $response->status(), - 'headers' => $headers, - 'body' => $response->body(), - ], 'Saloon Response (' . $label . ') ->'); - - if ($die === true) { - exit(1); - } - }); - } - - return $debuggable; -} diff --git a/tests/Feature/PoolTest.php b/tests/Feature/PoolTest.php index 263d21f1..47a6efe0 100644 --- a/tests/Feature/PoolTest.php +++ b/tests/Feature/PoolTest.php @@ -20,8 +20,6 @@ $successCount = 0; $errorCount = 0; - $connector->debug(); - $pool = $connector->pool([ new UserRequest, new UserRequest, From 1701703e4cbd74d28210e6122fc8eaa2c80afb15 Mon Sep 17 00:00:00 2001 From: Sammyjo20 <29132017+Sammyjo20@users.noreply.github.com> Date: Tue, 19 Mar 2024 18:03:05 +0000 Subject: [PATCH 16/23] Code style --- src/Traits/HasDebugging.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Traits/HasDebugging.php b/src/Traits/HasDebugging.php index 3ddc7834..29aee730 100644 --- a/src/Traits/HasDebugging.php +++ b/src/Traits/HasDebugging.php @@ -4,12 +4,12 @@ namespace Saloon\Traits; -use Psr\Http\Message\RequestInterface; -use Psr\Http\Message\ResponseInterface; use Saloon\Http\Response; -use Symfony\Component\VarDumper\VarDumper; use Saloon\Enums\PipeOrder; use Saloon\Http\PendingRequest; +use Psr\Http\Message\RequestInterface; +use Psr\Http\Message\ResponseInterface; +use Symfony\Component\VarDumper\VarDumper; trait HasDebugging { From 2da8ea4b80a6dfe7742b22f8aa50f539adbc5c0e Mon Sep 17 00:00:00 2001 From: Sammyjo20 <29132017+Sammyjo20@users.noreply.github.com> Date: Wed, 20 Mar 2024 20:08:19 +0000 Subject: [PATCH 17/23] Added tests --- src/Helpers/Helpers.php | 27 ++++++++ src/Traits/HasDebugging.php | 5 +- tests/Feature/DebugTest.php | 128 ++++++++++++++++++++++++++++++++++++ tests/Pest.php | 18 +++++ 4 files changed, 176 insertions(+), 2 deletions(-) diff --git a/src/Helpers/Helpers.php b/src/Helpers/Helpers.php index af3b73ff..f466ef76 100644 --- a/src/Helpers/Helpers.php +++ b/src/Helpers/Helpers.php @@ -15,6 +15,11 @@ */ final class Helpers { + /** + * Application "Die" handler. + */ + protected static ?Closure $dieHandler = null; + /** * Returns all traits used by a class, its parent classes and trait of their traits. * @@ -94,4 +99,26 @@ public static function bootPlugin(PendingRequest $pendingRequest, Connector|Requ $resource->{$bootMethodName}($pendingRequest); } + + /** + * Kill the application + * + * This is a method as it can be easily mocked during tests + */ + public static function die(): void + { + $handler = self::$dieHandler ?? static fn () => exit(1); + + $handler(); + } + + /** + * Set the "die" handler + * + * Used for testing/mocking purposes + */ + public static function setDieHandler(?Closure $handler): void + { + self::$dieHandler = $handler; + } } diff --git a/src/Traits/HasDebugging.php b/src/Traits/HasDebugging.php index 29aee730..1f64c650 100644 --- a/src/Traits/HasDebugging.php +++ b/src/Traits/HasDebugging.php @@ -6,6 +6,7 @@ use Saloon\Http\Response; use Saloon\Enums\PipeOrder; +use Saloon\Helpers\Helpers; use Saloon\Http\PendingRequest; use Psr\Http\Message\RequestInterface; use Psr\Http\Message\ResponseInterface; @@ -55,7 +56,7 @@ public function debugRequest(?callable $onRequest = null, bool $die = false): st $onRequest($pendingRequest, $pendingRequest->createPsrRequest()); if ($die) { - exit(1); + Helpers::die(); } }, order: PipeOrder::LAST @@ -103,7 +104,7 @@ public function debugResponse(?callable $onResponse = null, bool $die = false): $onResponse($response, $response->getPsrResponse()); if ($die) { - exit(1); + Helpers::die(); } }, order: PipeOrder::FIRST diff --git a/tests/Feature/DebugTest.php b/tests/Feature/DebugTest.php index 2dc9b4ac..60275c2e 100644 --- a/tests/Feature/DebugTest.php +++ b/tests/Feature/DebugTest.php @@ -3,11 +3,13 @@ declare(strict_types=1); use Saloon\Http\Response; +use Saloon\Helpers\Helpers; use Saloon\Http\PendingRequest; use Saloon\Http\Faking\MockClient; use Saloon\Http\Faking\MockResponse; use Psr\Http\Message\RequestInterface; use Psr\Http\Message\ResponseInterface; +use Symfony\Component\VarDumper\VarDumper; use Saloon\Tests\Fixtures\Requests\UserRequest; use Saloon\Tests\Fixtures\Connectors\TestConnector; use Saloon\Tests\Fixtures\Requests\AlwaysThrowRequest; @@ -133,3 +135,129 @@ expect($middlewareCount)->toBe(2); } }); + +test('the default debugRequest driver will dump an output using symfony var-dumper', function () { + $output = fopen('php://memory', 'rwb+'); + + VarDumper::setHandler(getCustomVarDump($output)); + + $connector = new TestConnector; + + $connector->withMockClient(new MockClient([ + new MockResponse(['name' => 'Sam'], 500), + ])); + + $connector->debugRequest()->send(new UserRequest); + + VarDumper::setHandler(null); + + rewind($output); + + $output = stream_get_contents($output); + + expect($output)->toEqual(<< array:6 [ + "connector" => "Saloon\Tests\Fixtures\Connectors\TestConnector" + "request" => "Saloon\Tests\Fixtures\Requests\UserRequest" + "method" => "GET" + "uri" => "https://tests.saloon.dev/api/user" + "headers" => array:2 [ + "Host" => "tests.saloon.dev" + "Accept" => "application/json" + ] + "body" => "" + ]\n + EOT); +}); + +test('the default debugResponse driver will dump an output using symfony var-dumper', function () { + $output = fopen('php://memory', 'rwb+'); + + VarDumper::setHandler(getCustomVarDump($output)); + + $connector = new TestConnector; + + $connector->withMockClient(new MockClient([ + new MockResponse(['name' => 'Sam'], 500), + ])); + + $connector->debugResponse()->send(new UserRequest); + + VarDumper::setHandler(null); + + rewind($output); + + $output = stream_get_contents($output); + + expect($output)->toEqual(<< array:3 [ + "status" => 500 + "headers" => [] + "body" => "{"name":"Sam"}" + ]\n + EOT); +}); + +test('the debug method will output both request and response at the same time', function () { + $output = fopen('php://memory', 'rwb+'); + + VarDumper::setHandler(getCustomVarDump($output)); + + $connector = new TestConnector; + + $connector->withMockClient(new MockClient([ + new MockResponse(['name' => 'Sam'], 500), + ])); + + $connector->debug()->send(new UserRequest); + + VarDumper::setHandler(null); + + rewind($output); + + $output = stream_get_contents($output); + + expect($output)->toEqual(<< array:6 [ + "connector" => "Saloon\Tests\Fixtures\Connectors\TestConnector" + "request" => "Saloon\Tests\Fixtures\Requests\UserRequest" + "method" => "GET" + "uri" => "https://tests.saloon.dev/api/user" + "headers" => array:2 [ + "Host" => "tests.saloon.dev" + "Accept" => "application/json" + ] + "body" => "" + ] + Saloon Response (UserRequest) -> array:3 [ + "status" => 500 + "headers" => [] + "body" => "{"name":"Sam"}" + ]\n + EOT); +}); + +test('the debug method can kill the application', function () { + $killed = false; + + $output = fopen('php://memory', 'rwb+'); + + VarDumper::setHandler(getCustomVarDump($output)); + + Helpers::setDieHandler(static function () use (&$killed) { + $killed = true; + }); + + $connector = new TestConnector; + + $connector->withMockClient(new MockClient([ + new MockResponse(['name' => 'Sam'], 500), + ])); + + $connector->debug(die: true)->send(new UserRequest); + + VarDumper::setHandler(null); + Helpers::setDieHandler(null); + + expect($killed)->toBeTrue(); +}); diff --git a/tests/Pest.php b/tests/Pest.php index b01cd93a..881234a8 100644 --- a/tests/Pest.php +++ b/tests/Pest.php @@ -26,6 +26,8 @@ | */ +use Symfony\Component\VarDumper\Cloner\VarCloner; +use Symfony\Component\VarDumper\Dumper\CliDumper; use Saloon\Tests\Fixtures\Connectors\TestConnector; /* @@ -48,3 +50,19 @@ function connector(): TestConnector { return new TestConnector; } + +function getCustomVarDump(mixed $output): Closure +{ + return static function ($var, ?string $label = null) use ($output) { + $dumper = new CliDumper; + $cloner = new VarCloner; + + $var = $cloner->cloneVar($var); + + if (null !== $label) { + $var = $var->withContext(['label' => $label]); + } + + $dumper->dump($var, $output); + }; +} From f19f6453e8d1316a5b0199ef6e46b5f46a1930a5 Mon Sep 17 00:00:00 2001 From: Sammyjo20 <29132017+Sammyjo20@users.noreply.github.com> Date: Wed, 20 Mar 2024 22:57:52 +0000 Subject: [PATCH 18/23] Moved to a Debugger class --- src/Helpers/Debugger.php | 79 +++++++++++++++++++++++++++++++++++++ src/Helpers/Helpers.php | 27 ------------- src/Traits/HasDebugging.php | 46 +++------------------ tests/Feature/DebugTest.php | 8 ++-- 4 files changed, 88 insertions(+), 72 deletions(-) create mode 100644 src/Helpers/Debugger.php diff --git a/src/Helpers/Debugger.php b/src/Helpers/Debugger.php new file mode 100644 index 00000000..e1e91d02 --- /dev/null +++ b/src/Helpers/Debugger.php @@ -0,0 +1,79 @@ +getHeaders() as $headerName => $value) { + $headers[$headerName] = implode(';', $value); + } + + $className = explode('\\', $pendingRequest->getRequest()::class); + $label = end($className); + + VarDumper::dump([ + 'connector' => $pendingRequest->getConnector()::class, + 'request' => $pendingRequest->getRequest()::class, + 'method' => $psrRequest->getMethod(), + 'uri' => (string)$psrRequest->getUri(), + 'headers' => $headers, + 'body' => (string)$psrRequest->getBody(), + ], 'Saloon Request (' . $label . ') ->'); + } + + /** + * Debug a response with Symfony Var Dumper + */ + public static function symfonyResponseDebugger(Response $response, ResponseInterface $psrResponse): void + { + $headers = []; + + foreach ($psrResponse->getHeaders() as $headerName => $value) { + $headers[$headerName] = implode(';', $value); + } + + $className = explode('\\', $response->getRequest()::class); + $label = end($className); + + VarDumper::dump([ + 'status' => $response->status(), + 'headers' => $headers, + 'body' => $response->body(), + ], 'Saloon Response (' . $label . ') ->'); + } + + /** + * Kill the application + * + * This is a method as it can be easily mocked during tests + */ + public static function die(): void + { + $handler = self::$dieHandler ?? static fn () => exit(1); + + $handler(); + } +} diff --git a/src/Helpers/Helpers.php b/src/Helpers/Helpers.php index f466ef76..af3b73ff 100644 --- a/src/Helpers/Helpers.php +++ b/src/Helpers/Helpers.php @@ -15,11 +15,6 @@ */ final class Helpers { - /** - * Application "Die" handler. - */ - protected static ?Closure $dieHandler = null; - /** * Returns all traits used by a class, its parent classes and trait of their traits. * @@ -99,26 +94,4 @@ public static function bootPlugin(PendingRequest $pendingRequest, Connector|Requ $resource->{$bootMethodName}($pendingRequest); } - - /** - * Kill the application - * - * This is a method as it can be easily mocked during tests - */ - public static function die(): void - { - $handler = self::$dieHandler ?? static fn () => exit(1); - - $handler(); - } - - /** - * Set the "die" handler - * - * Used for testing/mocking purposes - */ - public static function setDieHandler(?Closure $handler): void - { - self::$dieHandler = $handler; - } } diff --git a/src/Traits/HasDebugging.php b/src/Traits/HasDebugging.php index 1f64c650..daa321c7 100644 --- a/src/Traits/HasDebugging.php +++ b/src/Traits/HasDebugging.php @@ -6,11 +6,8 @@ use Saloon\Http\Response; use Saloon\Enums\PipeOrder; -use Saloon\Helpers\Helpers; +use Saloon\Helpers\Debugger; use Saloon\Http\PendingRequest; -use Psr\Http\Message\RequestInterface; -use Psr\Http\Message\ResponseInterface; -use Symfony\Component\VarDumper\VarDumper; trait HasDebugging { @@ -28,25 +25,7 @@ public function debugRequest(?callable $onRequest = null, bool $die = false): st // debugging driver. This will use symfony/var-dumper to display a nice output to // the user's screen of the request. - $onRequest ??= static function (PendingRequest $pendingRequest, RequestInterface $psrRequest) { - $headers = []; - - foreach ($psrRequest->getHeaders() as $headerName => $value) { - $headers[$headerName] = implode(';', $value); - } - - $className = explode('\\', $pendingRequest->getRequest()::class); - $label = end($className); - - VarDumper::dump([ - 'connector' => $pendingRequest->getConnector()::class, - 'request' => $pendingRequest->getRequest()::class, - 'method' => $psrRequest->getMethod(), - 'uri' => (string)$psrRequest->getUri(), - 'headers' => $headers, - 'body' => (string)$psrRequest->getBody(), - ], 'Saloon Request (' . $label . ') ->'); - }; + $onRequest ??= Debugger::symfonyRequestDebugger(...); // Register the middleware - we will use PipeOrder::FIRST to ensure that the response // is shown before it is modified by the user's middleware. @@ -56,7 +35,7 @@ public function debugRequest(?callable $onRequest = null, bool $die = false): st $onRequest($pendingRequest, $pendingRequest->createPsrRequest()); if ($die) { - Helpers::die(); + Debugger::die(); } }, order: PipeOrder::LAST @@ -79,22 +58,7 @@ public function debugResponse(?callable $onResponse = null, bool $die = false): // debugging driver. This will use symfony/var-dumper to display a nice output to // the user's screen of the response. - $onResponse ??= static function (Response $response, ResponseInterface $psrResponse) { - $headers = []; - - foreach ($psrResponse->getHeaders() as $headerName => $value) { - $headers[$headerName] = implode(';', $value); - } - - $className = explode('\\', $response->getRequest()::class); - $label = end($className); - - VarDumper::dump([ - 'status' => $response->status(), - 'headers' => $headers, - 'body' => $response->body(), - ], 'Saloon Response (' . $label . ') ->'); - }; + $onResponse ??= Debugger::symfonyResponseDebugger(...); // Register the middleware - we will use PipeOrder::FIRST to ensure that the response // is shown before it is modified by the user's middleware. @@ -104,7 +68,7 @@ public function debugResponse(?callable $onResponse = null, bool $die = false): $onResponse($response, $response->getPsrResponse()); if ($die) { - Helpers::die(); + Debugger::die(); } }, order: PipeOrder::FIRST diff --git a/tests/Feature/DebugTest.php b/tests/Feature/DebugTest.php index 60275c2e..5e2a41f6 100644 --- a/tests/Feature/DebugTest.php +++ b/tests/Feature/DebugTest.php @@ -3,7 +3,7 @@ declare(strict_types=1); use Saloon\Http\Response; -use Saloon\Helpers\Helpers; +use Saloon\Helpers\Debugger; use Saloon\Http\PendingRequest; use Saloon\Http\Faking\MockClient; use Saloon\Http\Faking\MockResponse; @@ -244,9 +244,9 @@ VarDumper::setHandler(getCustomVarDump($output)); - Helpers::setDieHandler(static function () use (&$killed) { + Debugger::$dieHandler = static function () use (&$killed) { $killed = true; - }); + }; $connector = new TestConnector; @@ -257,7 +257,7 @@ $connector->debug(die: true)->send(new UserRequest); VarDumper::setHandler(null); - Helpers::setDieHandler(null); + Debugger::$dieHandler = null; expect($killed)->toBeTrue(); }); From d7bd3aebbed94732371160a506d86a43d3f6c892 Mon Sep 17 00:00:00 2001 From: Sammyjo20 <29132017+Sammyjo20@users.noreply.github.com> Date: Wed, 20 Mar 2024 22:59:46 +0000 Subject: [PATCH 19/23] Added suggests --- composer.json | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/composer.json b/composer.json index 01b82d21..6e329c17 100644 --- a/composer.json +++ b/composer.json @@ -35,7 +35,8 @@ "phpstan/phpstan": "^1.9", "saloonphp/xml-wrangler": "^1.1", "spatie/ray": "^1.33", - "symfony/dom-crawler": "^6.0" + "symfony/dom-crawler": "^6.0 || ^7.0", + "symfony/var-dumper": "^6.0 || ^7.0" }, "conflict": { "sammyjo20/saloon": "*" @@ -43,7 +44,8 @@ "suggest": { "illuminate/collections": "Required for the response collect() method.", "symfony/dom-crawler": "Required for the response dom() method.", - "saloonphp/xml-wrangler": "Required for the response xmlReader() method." + "saloonphp/xml-wrangler": "Required for the response xmlReader() method.", + "symfony/var-dumper": "Required for default debugging drivers." }, "minimum-stability": "stable", "autoload": { From 7b22a31a8695a3baeb1851648c120e7181390ec1 Mon Sep 17 00:00:00 2001 From: Sammyjo20 <29132017+Sammyjo20@users.noreply.github.com> Date: Wed, 20 Mar 2024 23:01:38 +0000 Subject: [PATCH 20/23] wip --- tests/Pest.php | 3 +++ 1 file changed, 3 insertions(+) diff --git a/tests/Pest.php b/tests/Pest.php index 881234a8..977dbd48 100644 --- a/tests/Pest.php +++ b/tests/Pest.php @@ -51,6 +51,9 @@ function connector(): TestConnector return new TestConnector; } +/** + * @param resource $output + */ function getCustomVarDump(mixed $output): Closure { return static function ($var, ?string $label = null) use ($output) { From d5e8efdb40a9583774e26f60df0dee7e5edd09a3 Mon Sep 17 00:00:00 2001 From: Sammyjo20 <29132017+Sammyjo20@users.noreply.github.com> Date: Wed, 20 Mar 2024 23:03:58 +0000 Subject: [PATCH 21/23] Minimum version --- composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composer.json b/composer.json index 6e329c17..dff07bc8 100644 --- a/composer.json +++ b/composer.json @@ -36,7 +36,7 @@ "saloonphp/xml-wrangler": "^1.1", "spatie/ray": "^1.33", "symfony/dom-crawler": "^6.0 || ^7.0", - "symfony/var-dumper": "^6.0 || ^7.0" + "symfony/var-dumper": "^6.3 || ^7.0" }, "conflict": { "sammyjo20/saloon": "*" From 72aac79bd21e75e20f3f052fb08696c54c0fb359 Mon Sep 17 00:00:00 2001 From: Sammyjo20 <29132017+Sammyjo20@users.noreply.github.com> Date: Wed, 20 Mar 2024 23:09:20 +0000 Subject: [PATCH 22/23] Change heredoc --- tests/Feature/DebugTest.php | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/tests/Feature/DebugTest.php b/tests/Feature/DebugTest.php index 5e2a41f6..029d44ab 100644 --- a/tests/Feature/DebugTest.php +++ b/tests/Feature/DebugTest.php @@ -155,7 +155,7 @@ $output = stream_get_contents($output); - expect($output)->toEqual(<<toEqual(<< array:6 [ "connector" => "Saloon\Tests\Fixtures\Connectors\TestConnector" "request" => "Saloon\Tests\Fixtures\Requests\UserRequest" @@ -167,7 +167,7 @@ ] "body" => "" ]\n - EOT); + END); }); test('the default debugResponse driver will dump an output using symfony var-dumper', function () { @@ -189,13 +189,13 @@ $output = stream_get_contents($output); - expect($output)->toEqual(<<toEqual(<< array:3 [ "status" => 500 "headers" => [] "body" => "{"name":"Sam"}" ]\n - EOT); + END); }); test('the debug method will output both request and response at the same time', function () { @@ -217,7 +217,7 @@ $output = stream_get_contents($output); - expect($output)->toEqual(<<toEqual(<< array:6 [ "connector" => "Saloon\Tests\Fixtures\Connectors\TestConnector" "request" => "Saloon\Tests\Fixtures\Requests\UserRequest" @@ -234,7 +234,7 @@ "headers" => [] "body" => "{"name":"Sam"}" ]\n - EOT); + END); }); test('the debug method can kill the application', function () { From 34d50f3b764c11f16b9bca62b7f36a635591da59 Mon Sep 17 00:00:00 2001 From: Sammyjo20 <29132017+Sammyjo20@users.noreply.github.com> Date: Wed, 20 Mar 2024 23:11:09 +0000 Subject: [PATCH 23/23] Replace Windows new lines --- tests/Feature/DebugTest.php | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/tests/Feature/DebugTest.php b/tests/Feature/DebugTest.php index 029d44ab..a6fcf706 100644 --- a/tests/Feature/DebugTest.php +++ b/tests/Feature/DebugTest.php @@ -155,7 +155,7 @@ $output = stream_get_contents($output); - expect($output)->toEqual(<< array:6 [ "connector" => "Saloon\Tests\Fixtures\Connectors\TestConnector" "request" => "Saloon\Tests\Fixtures\Requests\UserRequest" @@ -167,7 +167,9 @@ ] "body" => "" ]\n - END); + END; + + expect($output)->toEqual(str_replace("\r\n", "\n", $expected)); }); test('the default debugResponse driver will dump an output using symfony var-dumper', function () { @@ -189,13 +191,15 @@ $output = stream_get_contents($output); - expect($output)->toEqual(<< array:3 [ "status" => 500 "headers" => [] "body" => "{"name":"Sam"}" ]\n - END); + END; + + expect($output)->toEqual(str_replace("\r\n", "\n", $expected)); }); test('the debug method will output both request and response at the same time', function () { @@ -217,7 +221,7 @@ $output = stream_get_contents($output); - expect($output)->toEqual(<< array:6 [ "connector" => "Saloon\Tests\Fixtures\Connectors\TestConnector" "request" => "Saloon\Tests\Fixtures\Requests\UserRequest" @@ -234,7 +238,9 @@ "headers" => [] "body" => "{"name":"Sam"}" ]\n - END); + END; + + expect($output)->toEqual(str_replace("\r\n", "\n", $expected)); }); test('the debug method can kill the application', function () {