Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PhpStorm with PHPUnit 8.4 gives exception Uncaught PHPUnit\Runner\Exception class ... could not be found

I tried to use PHPUnit v8. However I was not succeeded with PhpStorm. When I run simple test (class method) in PhpStorm I got the following message:

PHP Fatal error:  Uncaught PHPUnit\Runner\Exception: Class 'Mrself\\TreeType\\Tests\\Functional\\BuildingTest' could not be found in '/vagrant/symfony-tree-type/tests/Functional/BuildingTest.php'. in /vagrant/symfony-tree-type/vendor/phpunit/phpunit/src/Runner/StandardTestSuiteLoader.php:65

Yes, I have that class and yes I have psr configured properly:

"autoload": {
        "psr-4": {
            "Mrself\\TreeType\\": "./src/"
        }
    },
    "autoload-dev": {
        "psr-4": {
            "Mrself\\TreeType\\Tests\\": "./tests/"
        }
    }

The proof the I have everything correctly setup is that when I run vendor/bin/phpunit it gives me correct result.

When I run method in PhpStorm I got the following call:

/usr/bin/php /vagrant/symfony-tree-type/vendor/phpunit/phpunit/phpunit --configuration /vagrant/symfony-tree-type/phpunit.xml --filter "/(::testFormCanBeBuild)( .*)?$/" Mrself\\TreeType\\Tests\\Functional\\BuildingTest /vagrant/symfony-tree-type/tests/Functional/BuildingTest.php --teamcity

However if I prepend class namespace with \\ everything works correctly as well. I can not get a clue what's going on. PHPUnit version 7 works as well.

like image 508
FreeLightman Avatar asked Oct 06 '19 13:10

FreeLightman


1 Answers

Same thing happened to me. All of the sudden I started getting the following error:

PHP Fatal error:  Uncaught PHPUnit\Runner\Exception: Class 'Tests\\Feature\\ExampleTest' could not be found 

And after I have read @frank-vue's comment I noticed the same thing and he did: If I run tests on the entire folder it runs normally, but if I run test on a specific class/method I get that error.

I tried earlier version of PHPStorm, downgraded PHP plugin etc... and nothing worked.

In my case, when I checked the stacktrace looks like:

#0 /var/www/vendor/phpunit/phpunit/src/Runner/BaseTestRunner.php(145): PHPUnit\Runner\StandardTestSuiteLoader->load('Tests\\\\Unit\\\\Ex...', '/var/www/tests/...')
#1 /var/www/vendor/phpunit/phpunit/src/Runner/BaseTestRunner.php(105): PHPUnit\Runner\BaseTestRunner->loadSuiteClass('Tests\\\\Unit\\\\Ex...', '/var/www/tests/...')
#2 /var/www/vendor/phpunit/phpunit/src/TextUI/Command.php(177): PHPUnit\Runner\BaseTestRunner->getTest('Tests\\\\Unit\\\\Ex...', '/var/www/tests/...', Array)
#3 /var/www/vendor/phpunit/phpunit/src/TextUI/Command.php(159): PHPUnit\TextUI\Command->run(Array, true)
#4 /var/www/vendor/phpunit/phpunit/phpunit(61): PHPUnit\TextUI\Command::main()
#5 {main}
  thrown in /var/www/vendor/phpunit/phpunit/src/Runner/StandardTestSuiteLoader.php on line 69

Notice Tests\\\\Unit\\\\Ex... instead of Tests\\Unit\\Ex....

So in the end I broke the rule and I've modified vendor file, which should be avoided at any cost, but as a temporary solution it solves my problem.

So I added 1 line to the vendor/phpunit/phpunit/src/Runner/BaseTestRunner.php on line 98 (PHPUnit version 8.4.1), which replaces unnecessary '\'s.

if (empty($suiteClassFile) && \is_dir($suiteClassName) && !\is_file($suiteClassName . '.php')) {
    /** @var string[] $files */
    $files = (new FileIteratorFacade)->getFilesAsArray(
        $suiteClassName,
        $suffixes
    );

    $suite = new TestSuite($suiteClassName);
    $suite->addTestFiles($files);

    return $suite;
}
$suiteClassName = str_replace('\\\\', '\\', $suiteClassName); // THIS IS THE LINE I ADDED
try {
    $testClass = $this->loadSuiteClass(
        $suiteClassName,
        $suiteClassFile
    );
} catch (Exception $e) {
    $this->runFailed($e->getMessage());
    return null;
}
like image 131
Jan Demsar Avatar answered Nov 13 '22 02:11

Jan Demsar