DennisEilander's avatar

Unit test shows response body in console

Hi,

I'm trying to write a test to check if a non-existing urlKey is redirecting to the global redirect url. But when I run the phpunit in my console, the output contains the redirect body of the $response

For example:

mywebsite.com/non-existing-url must redirect to a global domain, which is set in the config file

This is my test:

/** @test */
public function it_will_redirect_to_global_redirect_url()
{
    $urlKey = 'non-existing-url';

    $response = $this->get(route('website', $urlKey));
    $response->assertRedirect(config('app.redirect_url'));
}

And this is my output

$ phpunit
PHPUnit 7.5.20 by Sebastian Bergmann and contributors.

.<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8" />
        <meta http-equiv="refresh" content="0;url='https://www.redirected-domain.com/'" />

        <title>Redirecting to https://www.redirected-domain.com/</title>
    </head>
    <body>
        Redirecting to <a href="https://www.redirected-domain.com/">https://www.redirected-domain.com/</a>.
    </body>
</html>.                                                                  2 / 2 (100%)

Time: 619 ms, Memory: 26.00 MB

OK (2 tests, 3 assertions)

Is there anything wrong in the test, or is it something else I have to set? Despite the tests are passing, it is very annoying seeing this in the console.

0 likes
4 replies
tykus's avatar

Did you leave a dump somewhere in the underlying code?

If you can run the dump-server while executing the test in a separate console, you should see the file and line number that is causing that output.

DennisEilander's avatar

@tykus thanks for your reply.

I tried to test again with the dump-server enabled, but no result. It prints nothing. I did a global search for dd() or dump() but nothing found..

tykus's avatar
tykus
Best Answer
Level 104

Ok, how are you performing the redirect?

DennisEilander's avatar

@tykus I found it, not sure why this way of redirecting is used, but this causes the problem.

// This will output the response body
return Redirect::to(config('app.redirect_url'), 302)->send();
// This is working as expected without any output
return redirect()->away(config('app.redirect_url'));

Thanks for your suggestions and quick response

1 like

Please or to participate in this conversation.