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

[12.x] Add ddBody method to TestResponse for dumping various response payloads #54933

Merged

Conversation

Sammyjo20
Copy link
Contributor

@Sammyjo20 Sammyjo20 commented Mar 7, 2025

This PR implements a more generic ddBody method to the Illuminate/Testing/TestResponse class which is useful for dumping out any HTTP response body.

The ddJson method was added previously and this code will use that if json_validate passes. Otherwise it will dump out the content of the response.

I noticed there weren't any tests for the other methods so I didn't write a test for this, however happy to write tests for this if needed.


Q: Do we need to do anything with streamed content? I noticed the following code in the codebase, perhaps we should use getStreamedContent() when the response is streamed?

if ($this->baseResponse instanceof StreamedResponse ||
    $this->baseResponse instanceof StreamedJsonResponse) {
    // $this->streamedContent();
} else {
    // $this->getContent();
}

{
$content = $this->content();

if (json_validate($content)) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this condition should probably also be added to ddJson, if json_validate is strictly required.

@taylorotwell taylorotwell merged commit fe8c50c into laravel:12.x Mar 7, 2025
39 checks passed
@Sammyjo20
Copy link
Contributor Author

@taylorotwell Sorry for the @ mention directly - but I just had a thought - will this break Laravel for people using PHP 8.2? I forgot to check what the minimum version was and it looks like it's ^8.2.

@Sammyjo20
Copy link
Contributor Author

Perhaps it just needs to change to

if (function_exists('json_validate') && json_validate($content)) {
   $this->ddJson($key);
}

@Sammyjo20
Copy link
Contributor Author

I've created a PR if you choose to merge this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants