php
test_error
ai_generated
true
PHPUnit 10.5.20 by Sebastian Bergmann and contributors. Runtime: PHP 8.2.17 Configuration: /var/www/app/phpunit.xml Warning - The data provider specified for Tests\Unit\OrderTest::testDiscount is invalid. Data Provider method Tests\Unit\OrderTest::discountProvider() is not callable or does not return an array of arrays.
ID: php/phpunit-dataprovider-invalid-return
90%Fix Rate
84%Confidence
1Evidence
2024-04-05First Seen
Version Compatibility
| Version | Status | Introduced | Deprecated | Notes |
|---|---|---|---|---|
| PHPUnit 10.5.20 | active | — | — | — |
| PHPUnit 11.0.1 | active | — | — | — |
| PHP 8.2.17 | active | — | — | — |
Root Cause
The @dataProvider annotation references a method that either does not exist, is not static (in PHPUnit 10+), or returns a non-iterable value (e.g., null, string) instead of an array of arrays.
generic中文
@dataProvider 注解引用的方法要么不存在,要么不是静态的(在 PHPUnit 10+ 中),要么返回非可迭代值(例如 null、字符串)而不是数组的数组。
Official Documentation
https://docs.phpunit.de/en/10.5/annotations.html#dataProviderWorkarounds
-
95% success Ensure the data provider method is declared as public static and returns an array of arrays: public static function discountProvider(): array { return [[10, 5], [20, 10]]; }
Ensure the data provider method is declared as public static and returns an array of arrays: public static function discountProvider(): array { return [[10, 5], [20, 10]]; } -
90% success If using PHPUnit 10+, use PHPUnit\Framework\Attributes\DataProvider attribute instead of annotation: #[DataProvider('discountProvider')] public function testDiscount($price, $expected) { ... }
If using PHPUnit 10+, use PHPUnit\Framework\Attributes\DataProvider attribute instead of annotation: #[DataProvider('discountProvider')] public function testDiscount($price, $expected) { ... }
中文步骤
Ensure the data provider method is declared as public static and returns an array of arrays: public static function discountProvider(): array { return [[10, 5], [20, 10]]; }If using PHPUnit 10+, use PHPUnit\Framework\Attributes\DataProvider attribute instead of annotation: #[DataProvider('discountProvider')] public function testDiscount($price, $expected) { ... }
Dead Ends
Common approaches that don't work:
-
80% fail
Making the data provider method non-static and using @dataProvider on an instance method works in PHPUnit 9 but fails in PHPUnit 10+ which requires static data providers.
-
70% fail
Returning a single associative array like ['discount' => 10] instead of an array of arrays [[10], [20]] causes PHPUnit to treat each key as a separate test case, often leading to unexpected failures.