如何将数组作为参数从PHPUnit中的提供程序传递


How to pass an array as an argument from providers in PHPUnit?

我有一个测试方法,它接收一个数组作为参数,并且我想从数据提供程序方法馈送数据?

如何才能做到这一点?

public function dataProvider(){
        return array(array(
                'id'=>1,
                'description'=>'',
        ));
}
/**
 * @dataProvider dataProvider
 */
public function testAddDocument($data){
// data here shall be an array provided by the data provider
// some test data here
}

结果是它传递了"id"键的值。。。etc

我想通过整个阵列

数据提供程序方法必须为传递给测试方法的每组参数返回一个包含一个数组的数组。若要传递数组,请将其和其他参数一起包含。请注意,在您的示例代码中,您将需要另一个封闭数组。

下面是一个返回两组数据的示例,每组数据有两个参数(一个数组和一个字符串)。

public function dataProvider(): array
{
    return
        [// data sets
            [// data set 0
                [// first argument data set 0
                    'id'          => 1,
                    'description' => 'this',
                ],
                'foo',//second argument data set 0
            ],
            [//data set 1
                [//first argument data set 1
                    'id'          => 2,
                    'description' => 'that',
                ],
                'bar',//second argument data set 1       
            ],
        ];
}

重要信息:数据提供程序方法必须是非静态的。PHPUnit实例化测试用例以调用每个数据提供程序方法。

为了清晰起见,我喜欢编写构建变量的提供者,这些变量可以命名什么是什么。清洁编码规则z。

对于这个例子,我将从一个简单的数据提供程序开始,它可以获得2个电话号码和2个号码。最后,我以你的具体例子为例。

首先,我总是将$tests变量命名,这表明我将返回一个"表示测试的项目阵列":

public function successProvider() : array
{
    $tests = [
        [ '+11 111 11 11 111', '+11 111 11 11 111', 1, 2 ],
        [ '+11 111 11 11 111', '+55 555 55 55 555', 2, 3 ],
    ];
    return $tests;
}

如果测试用例变得更加复杂,我会逐一命名它们,并在数组中分配一个密钥:

public function successProvider() : array
{
    $tests = [
        'same phone' => [ '+11 111 11 11 111', '+11 111 11 11 111', 1, 2 ],
        'phone changed' => [ '+11 111 11 11 111', '+55 555 55 55 555', 2, 3 ],
    ];
    return $tests;
}

如果每个测试用例变得更加复杂,我会用单独的变量来命名每个测试用例,如下所示:

public function successProvider() : array
{
    $testSamePhone = [
        '+11 111 11 11 111',
        '+11 111 11 11 111',
        1,
        2,
    ];
    $testPhoneChanged = [
        '+11 111 11 11 111',
        '+55 555 55 55 555',
        2,
        3,
    ];
    $tests = [
        $testSamePhone,
        $testPhoneChanged,
    ];
    return $tests;
}

为了弄清楚每件事是什么,有时我会在每个测试中设置一个密钥。我通常使这些键类似于接收参数的测试中的变量名。看起来像这样:

public function successProvider() : array
{
    $testSamePhone = [
        'possibleNumber' => '+11 111 11 11 111',
        'realNumber' => '+11 111 11 11 111',
        'numberOfLogCalls' => 1,
        'numberOfEvents' => 2,
    ];
    $testPhoneChanged = [
        'possibleNumber' => '+11 111 11 11 111',
        'realNumber' => '+55 555 55 55 555',
        'numberOfLogCalls' => 2,
        'numberOfEvents' => 3,
    ];
    $tests = [
        $testSamePhone,
        $testPhoneChanged,
    ];
    return $tests;
}

现在它变得超级可读,并且不需要";评论";("清洁代码"规则提倡代码自己说话)。

现在,如果输入的电话号码而不是数字是一个数字数组,该怎么办?说而不是"一个CCD_ 2";我们有";一组CCD_ 3〃:

public function successProvider() : array
{
    $testSamePhone = [
        'possibleNumbers' => [ '+11 111 11 11 111', '+3 333 333', '+999 999 9 9 9 9' ],
        'realNumber' => '+11 111 11 11 111',
        'numberOfLogCalls' => 1,
        'numberOfEvents' => 2,
    ];
    $testPhoneChanged = [
        'possibleNumbers' => [ '+11 111 11 11 111', '+3 333 333',  ],
        'realNumber' => '+55 555 55 55 555',
        'numberOfLogCalls' => 2,
        'numberOfEvents' => 3,
    ];
    $tests = [
        $testSamePhone,
        $testPhoneChanged,
    ];
    return $tests;
}

就你而言

我会这样写数据提供者:

public function dataProvider() : array
{
    $testDescriptiveNameOfTheTestCase = [
        'data' => [ 'id' => 1, 'description' => '' ],
    ];
    
    $tests = [
        $testDescriptiveNameOfTheTestCase,
    ];
    
    return $tests;
}

或者,更紧凑:

public function dataProvider() : array
{
    $tests = [
        'Descriptive name of the Test Case' => [
            'data' => [ 'id' => 1, 'description' => '' ],
        ],
    ];
    return $tests;
}