sebastianbergmann/phpunit · error · ConflictingPhptSectionsException

PHPT file must not contain more than one of the sections --%

Error message

PHPT file must not contain more than one of the sections --%s--

What it means

The same ensureExactlyOneSectionOf() then checks count($found) > 1 and throws ConflictingPhptSectionsException('PHPT file must not contain more than one of the sections --%s--'). The code family (FILE, FILEEOF, FILE_EXTERNAL) and the expectation family (EXPECT, EXPECT_EXTERNAL, EXPECTF, EXPECTF_EXTERNAL, EXPECTREGEX, EXPECTREGEX_EXTERNAL) are mutually exclusive within their family: exactly one member of each may appear.

Source

Thrown at src/Runner/Phpt/Parser.php:316

        $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
     *
     * @throws InvalidPhptFileException
     */
    private function ensureCodeIsNotEmpty(array $sections, string $codeSection): void
    {
        if (isset($sections['FILE']) && $sections['FILE'] !== '') {
            return;
        }

        if ($codeSection === 'FILE_EXTERNAL') {

View on GitHub (pinned to f123cdb2a2)

Solutions

  1. Keep exactly one member of each family: delete the alternative code or expectation section.
  2. If you want both exact and fuzzy checks, split them into two separate .phpt files.

Example fix

-- before
--FILE--
echo 'Hello';
--FILE_EXTERNAL--
hello.php

-- after
--FILE_EXTERNAL--
hello.php
Defensive patterns

Strategy: validation

Validate before calling

function assertPhptSectionFamiliesExclusive(string $phptFile): void
{
    $body = (string) file_get_contents($phptFile);
    $families = [
        ['FILE--', 'FILEEOF--', 'FILE_EXTERNAL--'],
        ['EXPECT--', 'EXPECT_EXTERNAL--', 'EXPECTF--', 'EXPECTF_EXTERNAL--', 'EXPECTREGEX--', 'EXPECTREGEX_EXTERNAL--'],
    ];

    foreach ($families as $family) {
        $found = array_filter($family, fn (string $s) => str_contains($body, '--' . $s));
        if (count($found) > 1) {
            throw new RuntimeException('Conflicting sections: ' . implode(', ', $found));
        }
    }
}

Prevention

When it happens

Trigger: A .phpt containing both --FILE-- and --FILE_EXTERNAL--, or both --EXPECT-- and --EXPECTF--, or any other pair from the same family.

Common situations: Switching an expectation from --EXPECT-- to --EXPECTF-- in place and forgetting to delete the old block; merging tests; copy-pasting a block that already exists.

Related errors


AI-assisted analysis of sebastianbergmann/phpunit@f123cdb2a2 (2026-08-23). Data as JSON: /api/errors/fd803e783c3f3034. Report an issue: GitHub.