Fix Symfony tests with PHPUnit 10

Manuel Canga - Feb 8 '23 - - Dev Community

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!


. . . . . . .