phpunit icon indicating copy to clipboard operation
phpunit copied to clipboard

Class "PHPUnit\Metadata\Api\DataProvider" not found

Open crowne opened this issue 3 years ago • 4 comments

Q A
PHPUnit version 10.3.2
PHP version 8.2.7
Installation Method Composer

Summary

Error when running tests PHP Unit Class "PHPUnit\Metadata\Api\DataProvider" not found

Current behavior

composer-docker-image-shell:/app# app/vendor/bin/phpunit test/unit


An error occurred inside PHPUnit.

Message:  Class "PHPUnit\Metadata\Api\DataProvider" not found
Location: /app/app/vendor/phpunit/phpunit/src/Framework/TestBuilder.php:38

#0 /app/app/vendor/phpunit/phpunit/src/Framework/TestSuite.php(477): PHPUnit\Framework\TestBuilder->build(Object(ReflectionClass), 'testLocateTestF...')
#1 /app/app/vendor/phpunit/phpunit/src/Framework/TestSuite.php(125): PHPUnit\Framework\TestSuite->addTestMethod(Object(ReflectionClass), Object(ReflectionMethod))
#2 /app/app/vendor/phpunit/phpunit/src/Framework/TestSuite.php(215): PHPUnit\Framework\TestSuite::fromClassReflector(Object(ReflectionClass))
#3 /app/app/vendor/phpunit/phpunit/src/Framework/TestSuite.php(243): PHPUnit\Framework\TestSuite->addTestSuite(Object(ReflectionClass))
#4 /app/app/vendor/phpunit/phpunit/src/Framework/TestSuite.php(261): PHPUnit\Framework\TestSuite->addTestFile('/app/test/unit/...')
#5 /app/app/vendor/phpunit/phpunit/src/TextUI/Configuration/TestSuiteBuilder.php(83): PHPUnit\Framework\TestSuite->addTestFiles(Array)
#6 /app/app/vendor/phpunit/phpunit/src/TextUI/Configuration/TestSuiteBuilder.php(47): PHPUnit\TextUI\Configuration\TestSuiteBuilder->testSuiteFromPath('/app/test/unit', Array)
#7 /app/app/vendor/phpunit/phpunit/src/TextUI/Application.php(356): PHPUnit\TextUI\Configuration\TestSuiteBuilder->build(Object(PHPUnit\TextUI\Configuration\Configuration)) 
#8 /app/app/vendor/phpunit/phpunit/src/TextUI/Application.php(103): PHPUnit\TextUI\Application->buildTestSuite(Object(PHPUnit\TextUI\Configuration\Configuration))
#9 /app/app/vendor/phpunit/phpunit/phpunit(99): PHPUnit\TextUI\Application->run(Array)
#10 /app/app/vendor/bin/phpunit(122): include('/app/app/vendor...')
#11 {main}

How to reproduce

See this issue on stackoverflow https://stackoverflow.com/questions/76484539/php-unit-class-phpunit-metadata-api-dataprovider-not-found-with-repro-case

Expected behavior

Expect tests to run as they do if the phpunit version is downgraded to 10.0.1

crowne avatar Aug 18 '23 16:08 crowne

It seems to be a namespace error. I did this in my composer.json as a workaround:

"autoload-dev": {
        "psr-4": {
            "PHPUnit\\Metadata\\": "vendor/phpunit/phpunit/src/Metadata",
            "PHPUnit\\Metadata\\Annotation\\Parser\\": "vendor/phpunit/phpunit/src/Metadata/Parser/Annotation"
        }
    }

mihaibalalau avatar Aug 18 '23 20:08 mihaibalalau

PHPUnit uses classmap-based autoloading, you must not mess with that.

sebastianbergmann avatar Aug 19 '23 05:08 sebastianbergmann

Thanks for your replies, I've used the work-around from @mihaibalalau Unfortunately the codebase to which I'm applying this is quite cluttered and adding a classmap-based autoloader increases the runtime of all the composer scripts by more than 60 seconds.

So there's clearly still a lof of refactoring to be done, thanks for your help with step-1 ... adding unit tests

crowne avatar Aug 19 '23 09:08 crowne

It seems to be a namespace error. I did this in my composer.json as a workaround:

"autoload-dev": {
        "psr-4": {
            "PHPUnit\\Metadata\\": "vendor/phpunit/phpunit/src/Metadata",
            "PHPUnit\\Metadata\\Annotation\\Parser\\": "vendor/phpunit/phpunit/src/Metadata/Parser/Annotation"
        }
    }

I can confirm the behaviour. In my vendor/composer/ directory, these classes were indeed missing in the autoload files, despite multiple composer dump-autoload and even deleting the whole vendor folder and reinstalling it 🤯

zippex avatar Sep 04 '23 06:09 zippex