sebastianbergmann/phpunit · error · InvalidPhptFileException
PHPT file must contain one of the sections --%s--
Error message
PHPT file must contain one of the sections --%s--
What it means
parse() calls ensureExactlyOneSectionOf() twice: once for the code family (FILE, FILEEOF, FILE_EXTERNAL) and once for the expectation family (EXPECT, EXPECT_EXTERNAL, EXPECTF, EXPECTF_EXTERNAL, EXPECTREGEX, EXPECTREGEX_EXTERNAL). When no member of a family is present, InvalidPhptFileException('PHPT file must contain one of the sections --%s--') is thrown with the family list joined as --FILE--, --FILEEOF--, --FILE_EXTERNAL--. Every .phpt needs exactly one code section and exactly one expectation section.
Source
Thrown at src/Runner/Phpt/Parser.php:307
* @param non-empty-list<non-empty-string> $sectionFamily
*
* @throws ConflictingPhptSectionsException
* @throws InvalidPhptFileException
*
* @return non-empty-string
*/
private function ensureExactlyOneSectionOf(array $sections, array $sectionFamily): string
{
$found = [];
foreach ($sectionFamily as $name) {
if (isset($sections[$name])) {
$found[] = $name;
}
}
if ($found === []) {
throw new InvalidPhptFileException(
sprintf(
'PHPT file must contain one of the sections --%s--',
implode('--, --', $sectionFamily),
),
);
}
if (count($found) > 1) {
throw new ConflictingPhptSectionsException($found);
}
return $found[0];
}
/**
* @param array<non-empty-string, string> $sections
* @param non-empty-string $codeSection
*View on GitHub (pinned to f123cdb2a2)
Solutions
- Add the missing code section (--FILE-- is the usual choice) when the complaint lists the FILE family.
- Add the missing expectation section (--EXPECT--, or --EXPECTF--/--EXPECTREGEX-- for fuzzy matching) when the complaint lists the EXPECT family.
- Even when using --SKIPIF-- to skip the test, keep one code and one expectation section in the file.
Example fix
-- before --TEST-- Demo --FILE-- echo 'Hello'; -- after --TEST-- Demo --FILE-- echo 'Hello'; --EXPECT-- Hello
Defensive patterns
Strategy: validation
Validate before calling
function assertPhptHasRequiredFamilies(string $phptFile): void
{
$body = (string) file_get_contents($phptFile);
$code = ['FILE--', 'FILEEOF--', 'FILE_EXTERNAL--'];
$expect = ['EXPECT--', 'EXPECT_EXTERNAL--', 'EXPECTF--', 'EXPECTF_EXTERNAL--', 'EXPECTREGEX--', 'EXPECTREGEX_EXTERNAL--'];
foreach (array_merge($code, $expect) as $needle) {
if (str_contains($body, '--' . $needle)) {
return; // at least one section header found
}
}
throw new RuntimeException($phptFile . ' lacks a code and/or expectation section');
} Prevention
- Scaffold new .phpt files from a template that already contains --TEST--, --FILE-- and --EXPECT--.
- Treat deletion of any of those three headers as a review-blocking change.
When it happens
Trigger: A .phpt file with an expectation section but no --FILE--/--FILEEOF--/--FILE_EXTERNAL--, or with a code section but no --EXPECT*-- section (for example only --TEST-- and --FILE--).
Common situations: Truncating a template when creating a new test; deleting a section while editing; assuming --TEST-- alone is enough because some other PHPT runner tolerates it.
Related errors
- --%s-- is not a valid PHPT section
- --%s-- section occurs more than once
- PHPT file must not contain text before its first section (li
- PHPT file must not contain more than one of the sections --%
- --%s-- section is empty
AI-assisted analysis of sebastianbergmann/phpunit@f123cdb2a2 (2026-08-23).
Data as JSON: /api/errors/c105e467d97c1ecf.
Report an issue: GitHub.