sebastianbergmann/phpunit · error · InvalidDataProviderException

The key must not be an empty string

Error message

The key must not be an empty string

What it means

Thrown while iterating provider data when a dataset key is the empty string ''. The key doubles as the dataset's name in test IDs, output, and result caches, and an empty name would make those ambiguous, so PHPUnit rejects it explicitly.

Source

Thrown at src/Metadata/Api/DataProvider.php:191

                    ...$methodsCalled,
                );

                throw InvalidDataProviderException::forException($e, $providerLabel);
            }

            try {
                foreach ($data as $key => $value) {
                    if (!is_int($key) && !is_string($key)) {
                        throw new InvalidDataProviderException(
                            sprintf(
                                'The key must be an integer or a string, %s given',
                                get_debug_type($key),
                            ),
                        );
                    }

                    if ($key === '') {
                        throw new InvalidDataProviderException(
                            'The key must not be an empty string',
                        );
                    }

                    if (!is_array($value)) {
                        throw new InvalidDataProviderException(
                            sprintf(
                                'Data set %s provided by %s is invalid, expected array but got %s',
                                $this->formatKey($key),
                                $providerLabel,
                                get_debug_type($value),
                            ),
                        );
                    }

                    if ($validateArgumentCount && $testMethodNumberOfParameters < count($value)) {
                        $this->triggerWarningForArgumentCount(
                            $testMethod,

View on GitHub (pinned to f123cdb2a2)

Solutions

  1. Give every dataset a meaningful non-empty string key (e.g. '#case ' . $i => $row)
  2. Filter or default blank keys when building the provider: $key === '' ? 'unnamed' : $key
  3. Use array_values($data) to switch to sequential integer keys

Example fix

// before
public static function provideCases(): array
{
    $rows = array_flip(array_column($this->csv(), 0)); // blank first column -> '' key
    ...
}

// after
public static function provideCases(): array
{
    $cases = [];

    foreach (array_column($this->csv(), 0) as $i => $name) {
        $cases['case ' . $i . ': ' . $name] = [$name];
    }

    return $cases;
}
Defensive patterns

Strategy: validation

Validate before calling

// Guard against blank keys from external data
$cases = [];

foreach ($rows as $i => $row) {
    $name = trim((string) ($row['name'] ?? ''));
    $cases[$name !== '' ? $name : 'case ' . $i] = $row;
}

return $cases;

Prevention

When it happens

Trigger: A provider returning ['' => [...]] or an array whose first element was unset ('unset($data[0])' leaving '' keys); generators yielding '' as a key.

Common situations: Providers built from CSV/JSON rows where a column used as the key is blank; array_flip on values containing empty strings; unsetting elements that reindexes to '' through string casts.

Related errors


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