DEV Community

Manuel Canga
Manuel Canga

Posted on • Updated on

Fix Symfony tests with PHPUnit 10

Maybe, In your Symfony project, you tried to run something as:

$ php bin/phpunit
Enter fullscreen mode Exit fullscreen mode

and you had as result this ugly message (or similar):

PHP Fatal error: Uncaught Error: Class "PHPUnit\TextUI \Command" not found in /var/www/html/yourproject/bin/phpunit:12
Stack trace:
#0 {main}
thrown in /var/www/html/yourproject/bin/phpunit on line 12

Don't worry. Edit your bin/phpunit file and rewrite it with the following content:

#!/usr/bin/env php
<?php

if (!ini_get('date.timezone')) {
    ini_set('date.timezone', 'UTC');
}


if (is_file(dirname(__DIR__).'/vendor/phpunit/phpunit/phpunit')) {
    define('PHPUNIT_COMPOSER_INSTALL', dirname(__DIR__).'/vendor/autoload.php');
    require PHPUNIT_COMPOSER_INSTALL;

exit((new \PHPUnit\TextUI\Application())->run($GLOBALS['argv']));
} else {
    if (!is_file(dirname(__DIR__).'/vendor/symfony/phpunit-bridge/bin/simple-phpunit.php')) {
        echo "Unable to find the `simple-phpunit.php` script in `vendor/symfony/phpunit-bridge/bin/`.\n";
        exit(1);
    }

    require dirname(__DIR__).'/vendor/symfony/phpunit-bridge/bin/simple-phpunit.php';
}
Enter fullscreen mode Exit fullscreen mode

Happy codding!


Latest comments (8)

Collapse
 
melroy89 profile image
Melroy van den Berg

Also don't forget to upgrade the xml.dist file:

vendor/bin/phpunit --migrate-configuration

Collapse
 
melroy89 profile image
Melroy van den Berg

What should we do with.. this.. This it's gone after the new xml.dist file, because it seems that listeners is not a valid parent element anymore in v10:

<listeners>
    <listener class="Symfony\Bridge\PhpUnit\SymfonyTestsListener" />
</listeners>
Enter fullscreen mode Exit fullscreen mode
Collapse
 
houssem-guemer profile image
Guemer Houssem

The updated symfony bridge takes care of that

Collapse
 
bmykyta profile image
Mykyta B

Thanks 💙

Collapse
 
simonjamain profile image
simonjamain

Thank you for code, it helps, but there is one important thing missing that got us quite confused :

The command line exit code was always 0 no matter what, which break the backward compatibility (and out CI/CD). As a matter of fact, the return code is return by the run method so you just have to wrap your line with and exit() :

You should IMHO make the Following change to your provided code :

exit((new \PHPUnit\TextUI\Application())->run($GLOBALS['argv']));

Thank you again !

Collapse
 
manuelcanga profile image
Manuel Canga

You're right. Thanks you for fixing my code!

Collapse
 
romaixn profile image
Romain

Thanks 🙏🏻

Collapse
 
manuelcanga profile image
Manuel Canga

To you for reading it!