Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a better dump() helper for responses #616

Merged
merged 4 commits into from
Jan 15, 2025
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,16 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## Unreleased

### Added

- Added a better `dump()` method to the response object when testing HTTP
requests that will dump the request/response to the console.

### Changed

- Disable `spatie/once`'s cache if found during unit testing.
- Ensure that the `QUERY_STRING` server variable is set when testing HTTP
requests.

## v1.3.3 - 2025-01-10

Expand Down
39 changes: 21 additions & 18 deletions src/mantle/testing/class-pending-testable-request.php
Original file line number Diff line number Diff line change
Expand Up @@ -278,21 +278,21 @@ public function call( string $method, mixed $uri, array $parameters = [], array

$this->test_case->call_before_callbacks();

// Setup the current request object.
$request = new Request(
$_GET,
$_POST,
[],
$_COOKIE,
$_FILES,
$_SERVER,
$content
);

// Attempt to run the query through the Mantle router.
if ( isset( $this->test_case->app['router'] ) ) {
$kernel = new HttpKernel( $this->test_case->app, $this->test_case->app['router'] );

// Setup the current request object.
$request = new Request(
$_GET,
$_POST,
[],
$_COOKIE,
$_FILES,
$_SERVER,
$content
);

// Mirror the logic from Request::createFromGlobals().
if (
str_starts_with( (string) $request->headers->get( 'CONTENT_TYPE', '' ), 'application/x-www-form-urlencoded' )
Expand Down Expand Up @@ -330,6 +330,7 @@ public function call( string $method, mixed $uri, array $parameters = [], array
} catch ( \Exception $e ) {
// If an exception occurs, make sure the output buffer is closed before the exception continues to the caller.
ob_end_clean();

throw $e;
}

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

$response->set_app( $this->test_case->app );
$response
->set_app( $this->test_case->app )
->set_request( $request );

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

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

if ( isset( $_SERVER['CONTENT_TYPE'] ) ) {
unset( $_SERVER['CONTENT_TYPE'] );
}

if ( isset( $_SERVER['REMOTE_ADDR'] ) ) {
unset( $_SERVER['REMOTE_ADDR'] );
foreach ( [ 'CONTENT_TYPE', 'QUERY_STRING', 'REMOTE_ADDR' ] as $header ) {
if ( isset( $_SERVER[ $header ] ) ) {
unset( $_SERVER[ $header ] );
}
}

Expand Down Expand Up @@ -468,6 +469,8 @@ protected function set_server_state( $method, $url, $server, $data, array $cooki
$req = $url;
}

$_SERVER['QUERY_STRING'] = $parts['query'] ?? '';

$_SERVER['REQUEST_URI'] = $req;

$_POST = $data;
Expand Down
135 changes: 28 additions & 107 deletions src/mantle/testing/class-test-response.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

use Exception;
use Mantle\Contracts\Application;
use Mantle\Http\Request;
use Mantle\Http\Response;
use Mantle\Support\Traits\Macroable;
use PHPUnit\Framework\Assert as PHPUnit;
Expand All @@ -18,6 +19,7 @@
*/
class Test_Response {
use Concerns\Element_Assertions;
use Concerns\Response_Dumper;
use Concerns\Response_Snapshot_Testing;
use Macroable;

Expand All @@ -28,6 +30,8 @@ class Test_Response {

/**
* Response headers.
*
* @var array<string, string|array<string>>
*/
public array $headers;

Expand All @@ -46,6 +50,11 @@ class Test_Response {
*/
protected Assertable_Json_String $decoded_json;

/**
* Request that generated the response.
*/
protected Request $request;

/**
* Create a new test response instance.
*
Expand All @@ -69,14 +78,31 @@ public function __construct(
* Set the container instance.
*
* @param Application $app Application instance.
* @return static
*/
public function set_app( Application $app ) {
public function set_app( Application $app ): static {
$this->app = $app;

return $this;
}

/**
* Set the request that generated the response.
*
* @param Request $request Request instance.
*/
public function set_request( Request $request ): static {
$this->request = $request;

return $this;
}

/**
* Get the request that generated the response.
*/
public function get_request(): ?Request {
return $this->request ?? null;
}

/**
* Create a response from a base response instance.
*
Expand Down Expand Up @@ -802,109 +828,4 @@ public function decoded_json(): Assertable_Json_String {
public function json( ?string $key = null ) {
return $this->decoded_json()->json( $key );
}

/**
* Dump the contents of the response to the screen.
*/
public function dump(): static {
$content = $this->get_content();

// If the content is not JSON, dump it as is.
if ( 'application/json' !== $this->get_header( 'Content-Type' ) ) {
dump( $content );

return $this;
}

$json = json_decode( $content );

if ( json_last_error() === JSON_ERROR_NONE ) {
$content = $json;
}

dump( $content );

return $this;
}

/**
* Dump the headers of the response to the screen.
*/
public function dump_headers(): static {
dump( $this->headers );

return $this;
}

/**
* Camel-case alias to dump_headers().
*/
public function dumpHeaders(): static {
return $this->dump_headers();
}

/**
* Dump the JSON, optionally by path, to the screen.
*
* @param string|null $path
*/
public function dump_json( ?string $path = null ): static {
dump( $this->json( $path ) );

return $this;
}

/**
* Camel-case alias to dump_json().
*
* @param string|null $path
*/
public function dumpJson( ?string $path = null ): static {
return $this->dump_json( $path );
}

/**
* Dump the content from the response and end the script.
*/
public function dd(): void {
$this->dump();

exit( 1 );
}

/**
* Dump the headers from the response and end the script.
*/
public function dd_headers(): void {
$this->dump_headers();

exit( 1 );
}

/**
* Camel-case alias to dd_headers().
*/
public function ddHeaders(): void {
$this->dd_headers();
}

/**
* Dump the JSON from the response and end the script.
*
* @param string|null $path
*/
public function dd_json( ?string $path = null ): void {
$this->dump_json( $path );

exit( 1 );
}

/**
* Camel-case alias to dd_json().
*
* @param string|null $path
*/
public function ddJson( ?string $path = null ): void {
$this->dd_json( $path );
}
}
Loading
Loading