{"record":{"id":"14d75e09e86aaaa2","repo":"sebastianbergmann/phpunit","slug":"could-not-load-s-s-for-phpt-file","errorCode":null,"errorMessage":"Could not load --%s-- %s for PHPT file","messagePattern":"Could not load --(.+?)-- (.+?) for PHPT file","errorType":"exception","errorClass":"PhptExternalFileCannotBeLoadedException","httpStatus":null,"severity":"error","filePath":"src/Runner/Phpt/Parser.php","lineNumber":262,"sourceCode":"     */\n    private function parseExternal(string $phptFile, array &$sections): void\n    {\n        $allowSections = [\n            'FILE',\n            'EXPECT',\n            'EXPECTF',\n            'EXPECTREGEX',\n        ];\n\n        $testDirectory = dirname($phptFile) . DIRECTORY_SEPARATOR;\n\n        foreach ($allowSections as $section) {\n            if (isset($sections[$section . '_EXTERNAL'])) {\n                $externalFilename = trim($sections[$section . '_EXTERNAL']);\n\n                if (!is_file($testDirectory . $externalFilename) ||\n                    !is_readable($testDirectory . $externalFilename)) {\n                    throw new PhptExternalFileCannotBeLoadedException(\n                        $section,\n                        $testDirectory . $externalFilename,\n                    );\n                }\n\n                $externalPath = $testDirectory . $externalFilename;\n                $contents     = file_get_contents($externalPath);\n\n                assert($contents !== false);\n\n                $sections[$section] = $contents;\n\n                if ($section === 'FILE') {\n                    $resolvedPath = realpath($externalPath);\n\n                    assert(is_string($resolvedPath));\n                    assert($resolvedPath !== '');\n","sourceCodeStart":244,"sourceCodeEnd":280,"githubUrl":"https://github.com/sebastianbergmann/phpunit/blob/f123cdb2a2d49f15025794166cfed8bda8627dd2/src/Runner/Phpt/Parser.php#L244-L280","documentation":"parseExternal() resolves the --FILE_EXTERNAL--, --EXPECT_EXTERNAL--, --EXPECTF_EXTERNAL-- and --EXPECTREGEX_EXTERNAL-- values: the referenced file must exist in the same directory as the .phpt file (path is testDirectory . filename, so references are relative to the test, never to the CWD) and be readable; otherwise PhptExternalFileCannotBeLoadedException('Could not load --%s-- %s for PHPT file') is thrown. Both is_file() and is_readable() must pass after trim()ing the value.","triggerScenarios":"An external section references a file that does not exist next to the .phpt file (typo, wrong case, missing from the distribution) or exists but is not readable by the PHP process; a stray trailing character in the section value makes the trimmed filename wrong.","commonSituations":"Packaging PHPT suites where external fixture files were excluded from the dist/zipball; case-sensitivity differences between macOS and Linux filesystems; checkouts or containers where file permissions deny read access.","solutions":["Check that the referenced file exists in the same directory as the .phpt file and that its name (including case) matches exactly.","Ship or restore the missing external fixture in your test distribution.","Fix permissions so the PHP process can read the file (is_readable must pass, not just file_exists)."],"exampleFix":"-- before\n--FILE_EXTERNAL--\nexample_code.php.txt   % actual file is example_code.php\n\n-- after\n--FILE_EXTERNAL--\nexample_code.php","handlingStrategy":"validation","validationCode":"function assertPhptExternalFilesExist(string $phptFile): void\n{\n    $dir = dirname($phptFile) . DIRECTORY_SEPARATOR;\n\n    foreach (file($phptFile) as $line) {\n        if (preg_match('/^--(FILE|EXPECT|EXPECTF|EXPECTREGEX)_EXTERNAL--/', $line, $m) === 0) {\n            continue;\n        }\n        // value = next line(s); simplified: assume single-line value follows\n    }\n\n    $sections = (new \\PHPUnit\\Runner\\Phpt\\Parser())->parse($phptFile); // not reached if invalid\n}\n\n// simpler targeted check before running a suite:\nforeach (glob('tests/*.phpt') as $phpt) {\n    $dir = dirname($phpt) . DIRECTORY_SEPARATOR;\n    $body = file_get_contents($phpt);\n    foreach (['FILE', 'EXPECT', 'EXPECTF', 'EXPECTREGEX'] as $s) {\n        if (preg_match('/--' . $s . '_EXTERNAL--\\R\\s*(\\S+)/', $body, $m) === 1\n            && (!is_file($dir . $m[1]) || !is_readable($dir . $m[1]))) {\n            throw new RuntimeException(\"Missing external fixture: {$dir}{$m[1]}\");\n        }\n    }\n}","typeGuard":null,"tryCatchPattern":"try {\n    (new \\PHPUnit\\Runner\\Phpt\\Parser())->parse($phptFile);\n} catch (\\PHPUnit\\Runner\\Phpt\\PhptExternalFileCannotBeLoadedException $e) {\n    // message names the section and expected path; restore the fixture and retry\n}","preventionTips":["Keep external fixture files co-located with the .phpt file and reference them by bare filename only.","Add *.php fixture files next to *.phpt to your distribution/CI artifact list so they are not filtered out.","Watch for case mismatches when developing on macOS/Windows and running on Linux."],"tags":["phpunit","phpt","external-file","fixtures","file-permissions"],"backgroundTag":"external-file-not-found","analyzedSha":"f123cdb2a2d49f15025794166cfed8bda8627dd2","analyzedAt":"2026-08-23T01:20:58.058Z","schemaVersion":2},"datasetVersion":"2026-08-23T08:06:27.607Z"}