Summer Sale! All accounts are 50% off this week.

BobBb's avatar
Level 1

PHPUnit lists tests vertically

How can I configure PHPUnit to revert back to making tests horizontal? It seems to have each individual test on a per line basis, whereas before the tests were just on one row and then spills over to the next when there was more than the line width.

Example Tests View: https://imgur.com/a/ZqafVi1

0 likes
9 replies
bobbybouwmann's avatar

You probably have a printerClass set up in your phpunit.xml file. If you remove it there it goes back to normal.

For example:

<phpunit printerClass="Sempro\PHPUnitPrettyPrinter\PrettyPrinter"
         backupGlobals="false"
         backupStaticAttributes="false"
         bootstrap="vendor/autoload.php"
    ...

Remove the printerClass and everything is good again!

bobbybouwmann's avatar

Looking again at your picture... It seems something is either hitting the enter key or you echo out a new line somewhere that causes this!

BobBb's avatar
Level 1

This is my entire phpunit.xml file; I don't believe there is a prettyprinter class going on over here.

<?xml version="1.0" encoding="UTF-8"?>
<phpunit backupGlobals="false"
         backupStaticAttributes="false"
         bootstrap="vendor/autoload.php"
         colors="true"
         convertErrorsToExceptions="true"
         convertNoticesToExceptions="true"
         convertWarningsToExceptions="true"
         processIsolation="false"
         stopOnFailure="false">
    <testsuites>
        <testsuite name="Feature">
            <directory suffix="Test.php">./tests/Feature</directory>
        </testsuite>

        <testsuite name="Unit">
            <directory suffix="Test.php">./tests/Unit</directory>
        </testsuite>
    </testsuites>
    <filter>
        <whitelist processUncoveredFilesFromWhitelist="true">
            <directory suffix=".php">./app</directory>
        </whitelist>
    </filter>
    <php>
        <env name="APP_ENV" value="testing"/>
        <env name="DB_CONNECTION" value="sqlite"/>
        <env name="DB_DATABASE" value=":memory:"/>
        <env name="CACHE_DRIVER" value="array"/>
        <env name="SESSION_DRIVER" value="array"/>
        <env name="QUEUE_DRIVER" value="sync"/>
        <env name="SCOUT_DRIVER" value="null"/>
        <env name="APP_URL" value="http://localhost"/>
        <env name="CASHIER_ENV" value="testing"/>
    </php>
</phpunit>
Sinnbeck's avatar

Do you have dump(), echo or print in any of your tests?

BobBb's avatar
Level 1

I've been able to narrow it down to this function in TestCase.php, but I'm not sure exactly why it's making the tests print a new line after each.

    protected function disableExceptionHandling()
    {
        $this->oldExceptionHandler = $this->app->make(ExceptionHandler::class);
        $this->app->instance(ExceptionHandler::class, new class extends Handler
        {
            public function __construct()
            { }

            public function report(\Exception $e)
            { }

            public function render($request, \Exception $e)
            {
                throw $e;
            }
        });
    }
Talinon's avatar

@bobbb It could be coming from almost anywhere. Tests still boot the framework, so it might not be related to your TestCase files. It could be coming from a service provider, a migration file, or even another package (although doubtful)

It will take a fine eye for detail, but one way to test would be to just run php artisan

Is there an extra line between the command prompt and the first line printed to the console from artisan?

For example, it should look something like:

$ php artisan
Laravel Framework 5.8.10

But if it looks like:

$ php artisan

Laravel Framework 5.8.10

Then you'll need to hunt it down elsewhere.

2 likes
BobBb's avatar
Level 1

I found a workaround a while ago by modifying my TestCase.php file.

I put ob_start() in my setUp() function, and ob_end_clean() in my tearDown() function.

This seems to do the trick for now, as I'm unable to locate exactly where the new line character is coming from.

sgilberg's avatar

I know this post is from years ago, but I came across it after noticing a similar thing following an upgrade to Laravel 11.x, and @talinon's tip helped me track it down. In my case, it was a stray extra line before the <?php tag in my bootstrap/providers.php file that I created as part of the upgrade. Getting rid of the extra line fixed the problem. Sharing in case it helps anyone else.

Please or to participate in this conversation.