8

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.

2
  • 1
    I see the issue too. Cannot run a single class or a single method, but test whole directory still run Commented Oct 11, 2019 at 1:41
  • 1
    I have same issue for a few weeks. When downgrading PHPUnit to 8.3 it's working fine again. Reported this here: youtrack.jetbrains.com/issue/WI-49333 Commented Oct 22, 2019 at 16:58

1 Answer 1

7

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;
}
Sign up to request clarification or add additional context in comments.

2 Comments

Yea, it seems to fix the issue. Think you should make a PR to solve that
I have same issue for a few weeks. When downgrading PHPUnit to 8.3 it's working fine again. Reported this here: youtrack.jetbrains.com/issue/WI-49333

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.