twigphp/Twig · error · InvalidArgumentException

Test " " is not valid.

Error message

Test "%s" is not valid.

What it means

IntegrationTestCase::assembleTests() parses fixture files using --TEST--, --CONDITION--, --TEMPLATE(--TEMPLATE VARIATION--)--, --DATA--/--CONFIG--/--EXPECT-- section markers. A file whose contents do not match the expected overall pattern (the initial --TEST--/--TEMPLATE-- regex) produces this InvalidArgumentException naming the offending file relative to the fixtures directory.

Solutions

  1. Open the file named in the message and fix/complete the required sections: --TEST--, --TEMPLATE-- (or --DATA--/--EXPECT-- pairs).
  2. Check marker spelling and casing exactly (--EXPECT--, --DATA--, --CONFIG--, --TEMPLATE VARIATION--).
  3. Remove non-fixture files from the fixtures directory or move them elsewhere.
  4. Validate the fixture against an existing working one as a template.

Example fix

// before (broken fixture)
Name of test
{{ 1 + 1 }}

// after (valid fixture)
--TEST--
Name of test
--TEMPLATE--
{{ 1 + 1 }}
--EXPECT--
2
Defensive patterns

Strategy: validation

Validate before calling

$contents = file_get_contents($fixtureFile);
if (!preg_match('/^--TEST--/m', $contents) || !str_contains($contents, '--TEMPLATE--') && !str_contains($contents, '--DATA--')) {
    throw new \Exception("Invalid fixture: $fixtureFile");
}

Try / catch

try { $tests = $this->getTests(); } catch (\InvalidArgumentException $e) { if (str_contains($e->getMessage(), 'is not valid')) { fwrite(STDERR, $e->getMessage()); } throw $e; }

Prevention

When it happens

Trigger: A fixture file in getFixturesDirectory() missing required sections (no --TEST-- or --TEMPLATE--), misspelled markers (e.g. --TEHST--, --EXCEPT--), markers with wrong casing, or a file that only matches neither the template nor data/config variant regex.

Common situations: Hand-written fixtures with typos in section names; truncated fixture files; copying fixtures from other test suites with slightly different marker conventions; adding binary or unrelated files into the fixtures directory.

Understand the failure class

Background: "Invalid ... format", "must be in format X", "does not look like a ..." — invalid argument format errors across CLI tools and libraries — this error's family across 17 libraries.

Related errors


AI-assisted analysis of twigphp/Twig@a414c3a491 (2026-09-13). Data as JSON: /api/errors/32fdbc714ea3c90a. Report an issue: GitHub.

Appendix: source

Thrown at src/Test/IntegrationTestCase.php:206

            $test = file_get_contents($file->getRealpath());

            if (preg_match('/--TEST--\s*(.*?)\s*(?:--CONDITION--\s*(.*))?\s*(?:--DEPRECATION--\s*(.*?))?\s*((?:--TEMPLATE(?:\(.*?\))?--(?:.*?))+)\s*(?:--DATA--\s*(.*))?\s*--EXCEPTION--\s*(.*)/sx', $test, $match)) {
                $message = $match[1];
                $condition = $match[2];
                $deprecation = $match[3];
                $templates = self::parseTemplates($match[4]);
                $exception = $match[6];
                $outputs = [[null, $match[5], null, '']];
            } elseif (preg_match('/--TEST--\s*(.*?)\s*(?:--CONDITION--\s*(.*))?\s*(?:--DEPRECATION--\s*(.*?))?\s*((?:--TEMPLATE(?:\(.*?\))?--(?:.*?))+)--DATA--.*?--EXPECT--.*/s', $test, $match)) {
                $message = $match[1];
                $condition = $match[2];
                $deprecation = $match[3];
                $templates = self::parseTemplates($match[4]);
                $exception = false;
                preg_match_all('/--DATA--(.*?)(?:--CONFIG--(.*?))?--EXPECT--(.*?)(?=\-\-DATA\-\-|$)/s', $test, $outputs, \PREG_SET_ORDER);
            } else {
                throw new \InvalidArgumentException(\sprintf('Test "%s" is not valid.', str_replace($fixturesDir.'/', '', $file)));
            }

            $tests[str_replace($fixturesDir.'/', '', $file)] = [str_replace($fixturesDir.'/', '', $file), $message, $condition, $templates, $exception, $outputs, $deprecation];
        }

        if ($legacyTests && !$tests) {
            // add a dummy test to avoid a PHPUnit message
            return [['not', '-', '', [], '', []]];
        }

        return $tests;
    }

    /**
     * @final since Twig 3.13
     *
     * @return iterable
     */

View on GitHub (pinned to a414c3a491)