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
- Give every dataset a meaningful non-empty string key (e.g. '#case ' . $i => $row)
- Filter or default blank keys when building the provider: $key === '' ? 'unnamed' : $key
- 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
- Default blank keys to a generated name like 'case ' . $i
- Validate CSV/JSON-derived keys before using them as dataset names
- Use array_values() when you do not need named datasets
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
- The key must be an integer or a string, %s given
- Data Provider method %s::%s() is not public
- Data Provider method %s::%s() is not static
- Data Provider method %s::%s() expects an argument
- Data Provider method %s::%s() does not return an iterable
AI-assisted analysis of sebastianbergmann/phpunit@f123cdb2a2 (2026-08-23).
Data as JSON: /api/errors/8ed0a05cf0bb4c62.
Report an issue: GitHub.