DEV Community

Cover image for How to show var_dumps in phpunit or codeception
Julian
Julian

Posted on

8

How to show var_dumps in phpunit or codeception

if you use phpunit or codeception (which uses phpunit under the hood), you sometimes want to see the output of the vardump()'s of the class you are testing. per default the output of vardump is supressed.

MyClassToTest.php

...
public function doSomething()
{
    ...
    var_dump('my var dump');
    ...
}

...

Enter fullscreen mode Exit fullscreen mode

Test.php

public function test()
{
    $myClassToTest = Stub::make('\App\MyClassToTest', ['name' => 'myname']);

    $this->assertEquals('name', $myClassToTest->doSomething());
}
Enter fullscreen mode Exit fullscreen mode
codecept run unit -vvv
Enter fullscreen mode Exit fullscreen mode

does not show the var_dump calls

if you add a ob_flush(); after your var_dump call, the output is shown

...
public function doSomething()
{
    ...
    var_dump('my var dump');
    ob_flush();
    ...
}

...
Enter fullscreen mode Exit fullscreen mode

alternatively you can use the symfony var dumper component which output is shown without a ob_flush();

...
public function doSomething()
{
    ...
    dump('my var dump');
    ...
}

...
Enter fullscreen mode Exit fullscreen mode

you don't need the -vvv parameter, the output is still shown.

codecept run unit
Enter fullscreen mode Exit fullscreen mode

links:

Sentry image

Hands-on debugging session: instrument, monitor, and fix

Join Lazar for a hands-on session where you’ll build it, break it, debug it, and fix it. You’ll set up Sentry, track errors, use Session Replay and Tracing, and leverage some good ol’ AI to find and fix issues fast.

RSVP here →

Top comments (1)

Collapse
 
ohffs profile image
ohffs

If you don't mind being a bit 'hacky' you can also do :

fwrite(STDERR, 'whatever you want to see');

Which is handy for a quick 'fix' ;-)

AWS Security LIVE!

Tune in for AWS Security LIVE!

Join AWS Security LIVE! for expert insights and actionable tips to protect your organization and keep security teams prepared.

Learn More

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay