{"record":{"id":"68d444ce90762874","repo":"sebastianbergmann/phpunit","slug":"class-s-cannot-be-found-in-s","errorCode":null,"errorMessage":"Class %s cannot be found in %s","messagePattern":"Class (.+?) cannot be found in (.+?)","errorType":"exception","errorClass":"ClassCannotBeFoundException","httpStatus":null,"severity":"error","filePath":"src/Runner/TestSuiteLoader.php","lineNumber":51,"sourceCode":"     */\n    private static ?int $numberOfDeclaredClasses = null;\n\n    /**\n     * @var array<non-empty-string, list<class-string>>\n     */\n    private static array $fileToClassesMap = [];\n\n    /**\n     * @throws Exception\n     *\n     * @return ReflectionClass<TestCase>\n     */\n    public function load(string $suiteClassFile): ReflectionClass\n    {\n        $resolved = realpath($suiteClassFile);\n\n        if ($resolved === false) {\n            throw new ClassCannotBeFoundException($suiteClassFile, $suiteClassFile);\n        }\n\n        $suiteClassFile = $resolved;\n        $suiteClassName = $this->classNameFromFileName($suiteClassFile);\n        $loadedClasses  = $this->loadSuiteClassFile($suiteClassFile);\n\n        foreach ($loadedClasses as $className) {\n            /** @noinspection PhpUnhandledExceptionInspection */\n            $class = new ReflectionClass($className);\n\n            if ($class->isAnonymous()) {\n                continue;\n            }\n\n            if ($class->getFileName() !== $suiteClassFile) {\n                continue;\n            }\n","sourceCodeStart":33,"sourceCodeEnd":69,"githubUrl":"https://github.com/sebastianbergmann/phpunit/blob/f123cdb2a2d49f15025794166cfed8bda8627dd2/src/Runner/TestSuiteLoader.php#L33-L69","documentation":"TestSuiteLoader::load() (used when you pass a single test file to phpunit, e.g. 'phpunit tests/ExampleTest.php') starts with realpath($suiteClassFile); when realpath() returns false the file does not exist (or is outside open_basedir), and ClassCannotBeFoundException('Class %s cannot be found in %s') is thrown with the path for both placeholders.","triggerScenarios":"Calling phpunit with a path to a file that does not exist: typo in the filename, wrong working directory, deleted/moved file, or a path blocked by open_basedir so realpath() fails even though the file exists.","commonSituations":"Typos in CLI arguments or CI scripts; running phpunit from a different directory than assumed; stale IDE run configurations after files were renamed; open_basedir restrictions in hardened environments.","solutions":["Check the file path argument for typos and run phpunit from the directory the path is relative to (or pass an absolute path).","Confirm the file still exists: ls -l tests/ExampleTest.php.","If the file exists but realpath() still fails, check open_basedir / symlink targets."],"exampleFix":"# before\nphpunit tests/ExmapleTest.php   # typo\n\n# after\nphpunit tests/ExampleTest.php","handlingStrategy":"validation","validationCode":"$file = 'tests/ExampleTest.php';\n\nif (!is_file($file)) {\n    throw new InvalidArgumentException(\"Test file does not exist: {$file}\");\n}\n\n$loader = new \\PHPUnit\\Runner\\TestSuiteLoader();\n$class  = $loader->load($file);","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Pass absolute paths (or resolve against the project root) when invoking phpunit programmatically or in CI scripts.","Regenerate IDE run configurations after renaming test files.","Fail fast with is_file() checks in wrapper scripts that accept file arguments."],"tags":["phpunit","cli","file-not-found","test-loader","typo"],"backgroundTag":"test-class-not-found","analyzedSha":"f123cdb2a2d49f15025794166cfed8bda8627dd2","analyzedAt":"2026-08-23T01:20:58.058Z","schemaVersion":2},"datasetVersion":"2026-08-23T08:06:27.607Z"}