You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

PHPUnit Data Provider报错:ArgumentCountError参数不足问题排查

问题原因及解决方法

核心问题

你遇到的ArgumentCountError是因为PHPUnit 11没有识别到你的DataProvider注解,导致测试方法未收到数据提供者传递的参数。

PHPUnit 10及以上版本中,数据提供者的注解已从PHPUnit\Framework\TestCase迁移到PHPUnit\Framework\Attributes命名空间下,你的代码没有导入该注解,导致注解失效。

修复步骤

  1. 导入DataProvider属性
    在测试类的use语句中添加:
use PHPUnit\Framework\Attributes\DataProvider;
  1. 确认其他符合要求的配置(你的代码已满足)
  • 数据提供者方法apiCountryNamesProvider必须是public static类型
  • 数据提供者返回的二维数组中,每个子数组的元素数量需与测试方法的参数数量一致(你的代码中两者都是2个,符合要求)

修改后的完整代码片段

<?php declare(strict_types=1);

use PHPUnit\Framework\TestCase;
use PHPUnit\Framework\Attributes\DataProvider; // 新增这行

final class TaskRunnerTest extends TestCase
{
    ...
    
    #[DataProvider('apiCountryNamesProvider')]
    public function testGetCountryIdByName(string $apiCountryName, string $dbCountryName): void 
    {
        // Configure the stub.
        $this->dbStub->expects($this->once())
            ->method('getCountryIdByName')
            ->with($this->equalTo($dbCountryName));
        
        // test 
        $this->taskRunner->getCountryIdByName($apiCountryName);
    }

    ...

    public static function apiCountryNamesProvider(): array
    {
        return [
            ["Saudi-Arabia", "Saudi Arabia"],
        ];
    }

}

额外检查

如果修复后仍有问题,可以确认:

  • PHP版本是否符合PHPUnit 11的要求(PHPUnit 11需要PHP 8.2及以上)
  • 数据提供者方法的名称是否与注解中指定的完全一致(大小写敏感)

内容的提问来源于stack exchange,提问作者Martyn

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.28 19:42:12