Skip to content

Commit 3db21c5

Browse files
authored
Add a better dump() helper for responses (#616)
* Add a better dump() helper for responses * CHANGELOG about QUERY_STRING
1 parent 7e24725 commit 3db21c5

File tree

5 files changed

+315
-128
lines changed

5 files changed

+315
-128
lines changed

CHANGELOG.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,22 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
77

88
## Unreleased
99

10-
Minimum PHP version is now 8.2. The framework supports 8.2 - 8.4.
10+
📢 Minimum PHP version is now 8.2. The framework supports 8.2 - 8.4.
1111

1212
### Added
1313

1414
- Add a `without_local_object_cache()` method to prevent the `object-cache.php` drop-in from being loaded locally.
15+
- Added a better `dump()` method to the response object when testing HTTP
16+
requests that will dump the request/response to the console.
1517

1618
### Changed
1719

1820
- Removed support for PHP 8.1. The minimum PHP version is now 8.2.
1921
- For projects that require PHPUnit 9, the `phpunit/phpunit` version is now set to `^9.6.22`.
2022
- Upgraded to Symfony 7.0 packages.
2123
- Disable `spatie/once`'s cache if found during unit testing.
24+
- Ensure that the `QUERY_STRING` server variable is set when testing HTTP
25+
requests.
2226

2327
### Fixed
2428

src/mantle/testing/class-pending-testable-request.php

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -278,21 +278,21 @@ public function call( string $method, mixed $uri, array $parameters = [], array
278278

279279
$this->test_case->call_before_callbacks();
280280

281+
// Setup the current request object.
282+
$request = new Request(
283+
$_GET,
284+
$_POST,
285+
[],
286+
$_COOKIE,
287+
$_FILES,
288+
$_SERVER,
289+
$content
290+
);
291+
281292
// Attempt to run the query through the Mantle router.
282293
if ( isset( $this->test_case->app['router'] ) ) {
283294
$kernel = new HttpKernel( $this->test_case->app, $this->test_case->app['router'] );
284295

285-
// Setup the current request object.
286-
$request = new Request(
287-
$_GET,
288-
$_POST,
289-
[],
290-
$_COOKIE,
291-
$_FILES,
292-
$_SERVER,
293-
$content
294-
);
295-
296296
// Mirror the logic from Request::createFromGlobals().
297297
if (
298298
str_starts_with( (string) $request->headers->get( 'CONTENT_TYPE', '' ), 'application/x-www-form-urlencoded' )
@@ -330,6 +330,7 @@ public function call( string $method, mixed $uri, array $parameters = [], array
330330
} catch ( \Exception $e ) {
331331
// If an exception occurs, make sure the output buffer is closed before the exception continues to the caller.
332332
ob_end_clean();
333+
333334
throw $e;
334335
}
335336

@@ -364,7 +365,9 @@ public function call( string $method, mixed $uri, array $parameters = [], array
364365
);
365366
}
366367

367-
$response->set_app( $this->test_case->app );
368+
$response
369+
->set_app( $this->test_case->app )
370+
->set_request( $request );
368371

369372
$this->test_case->call_after_callbacks( $response );
370373

@@ -421,13 +424,11 @@ protected function reset_request_state(): void {
421424
if ( str_starts_with( $key, 'HTTP_' ) && 'HTTP_HOST' !== $key ) {
422425
unset( $_SERVER[ $key ] );
423426
}
427+
}
424428

425-
if ( isset( $_SERVER['CONTENT_TYPE'] ) ) {
426-
unset( $_SERVER['CONTENT_TYPE'] );
427-
}
428-
429-
if ( isset( $_SERVER['REMOTE_ADDR'] ) ) {
430-
unset( $_SERVER['REMOTE_ADDR'] );
429+
foreach ( [ 'CONTENT_TYPE', 'QUERY_STRING', 'REMOTE_ADDR' ] as $header ) {
430+
if ( isset( $_SERVER[ $header ] ) ) {
431+
unset( $_SERVER[ $header ] );
431432
}
432433
}
433434

@@ -468,6 +469,8 @@ protected function set_server_state( $method, $url, $server, $data, array $cooki
468469
$req = $url;
469470
}
470471

472+
$_SERVER['QUERY_STRING'] = $parts['query'] ?? '';
473+
471474
$_SERVER['REQUEST_URI'] = $req;
472475

473476
$_POST = $data;

src/mantle/testing/class-test-response.php

Lines changed: 28 additions & 107 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
use Exception;
1111
use Mantle\Contracts\Application;
12+
use Mantle\Http\Request;
1213
use Mantle\Http\Response;
1314
use Mantle\Support\Traits\Macroable;
1415
use PHPUnit\Framework\Assert as PHPUnit;
@@ -18,6 +19,7 @@
1819
*/
1920
class Test_Response {
2021
use Concerns\Element_Assertions;
22+
use Concerns\Response_Dumper;
2123
use Concerns\Response_Snapshot_Testing;
2224
use Macroable;
2325

@@ -28,6 +30,8 @@ class Test_Response {
2830

2931
/**
3032
* Response headers.
33+
*
34+
* @var array<string, string|array<string>>
3135
*/
3236
public array $headers;
3337

@@ -46,6 +50,11 @@ class Test_Response {
4650
*/
4751
protected Assertable_Json_String $decoded_json;
4852

53+
/**
54+
* Request that generated the response.
55+
*/
56+
protected Request $request;
57+
4958
/**
5059
* Create a new test response instance.
5160
*
@@ -69,14 +78,31 @@ public function __construct(
6978
* Set the container instance.
7079
*
7180
* @param Application $app Application instance.
72-
* @return static
7381
*/
74-
public function set_app( Application $app ) {
82+
public function set_app( Application $app ): static {
7583
$this->app = $app;
7684

7785
return $this;
7886
}
7987

88+
/**
89+
* Set the request that generated the response.
90+
*
91+
* @param Request $request Request instance.
92+
*/
93+
public function set_request( Request $request ): static {
94+
$this->request = $request;
95+
96+
return $this;
97+
}
98+
99+
/**
100+
* Get the request that generated the response.
101+
*/
102+
public function get_request(): ?Request {
103+
return $this->request ?? null;
104+
}
105+
80106
/**
81107
* Create a response from a base response instance.
82108
*
@@ -802,109 +828,4 @@ public function decoded_json(): Assertable_Json_String {
802828
public function json( ?string $key = null ) {
803829
return $this->decoded_json()->json( $key );
804830
}
805-
806-
/**
807-
* Dump the contents of the response to the screen.
808-
*/
809-
public function dump(): static {
810-
$content = $this->get_content();
811-
812-
// If the content is not JSON, dump it as is.
813-
if ( 'application/json' !== $this->get_header( 'Content-Type' ) ) {
814-
dump( $content );
815-
816-
return $this;
817-
}
818-
819-
$json = json_decode( $content );
820-
821-
if ( json_last_error() === JSON_ERROR_NONE ) {
822-
$content = $json;
823-
}
824-
825-
dump( $content );
826-
827-
return $this;
828-
}
829-
830-
/**
831-
* Dump the headers of the response to the screen.
832-
*/
833-
public function dump_headers(): static {
834-
dump( $this->headers );
835-
836-
return $this;
837-
}
838-
839-
/**
840-
* Camel-case alias to dump_headers().
841-
*/
842-
public function dumpHeaders(): static {
843-
return $this->dump_headers();
844-
}
845-
846-
/**
847-
* Dump the JSON, optionally by path, to the screen.
848-
*
849-
* @param string|null $path
850-
*/
851-
public function dump_json( ?string $path = null ): static {
852-
dump( $this->json( $path ) );
853-
854-
return $this;
855-
}
856-
857-
/**
858-
* Camel-case alias to dump_json().
859-
*
860-
* @param string|null $path
861-
*/
862-
public function dumpJson( ?string $path = null ): static {
863-
return $this->dump_json( $path );
864-
}
865-
866-
/**
867-
* Dump the content from the response and end the script.
868-
*/
869-
public function dd(): void {
870-
$this->dump();
871-
872-
exit( 1 );
873-
}
874-
875-
/**
876-
* Dump the headers from the response and end the script.
877-
*/
878-
public function dd_headers(): void {
879-
$this->dump_headers();
880-
881-
exit( 1 );
882-
}
883-
884-
/**
885-
* Camel-case alias to dd_headers().
886-
*/
887-
public function ddHeaders(): void {
888-
$this->dd_headers();
889-
}
890-
891-
/**
892-
* Dump the JSON from the response and end the script.
893-
*
894-
* @param string|null $path
895-
*/
896-
public function dd_json( ?string $path = null ): void {
897-
$this->dump_json( $path );
898-
899-
exit( 1 );
900-
}
901-
902-
/**
903-
* Camel-case alias to dd_json().
904-
*
905-
* @param string|null $path
906-
*/
907-
public function ddJson( ?string $path = null ): void {
908-
$this->dd_json( $path );
909-
}
910831
}

0 commit comments

Comments
 (0)