数据提供者在phpunit中发布

时间:2017-11-16 10:05:49

标签: php phpunit

这是我的数据提供者功能:

public function addDataProvider() {
    return [
        [
            [
                'currency' => 'eur',
                'amount' => 800,
            ],
            [
                'currency' => '12312k',
                'amount' => 1201,
            ],
            [
                'currency' => 'DKK',
                'amount' => 1200.01
            ],
            [
                'currency' => 'SEK  ',
                'amount' => 1200.01
            ]
        ],
    ];
}

这是我的测试功能:

/**
 * Test case
 * @covers AmountPerCustomer::checkAmounts()
 * @dataProvider addDataProvider
 */
public function testCheckAmountUsingCurrency($query)
{
        ....
        try {
            $ordersPerCustomer->checkAmounts($query['amount'], $query['currency'], $dateInterval);
            $this->fail("Expected Exception has not been raised.");
        }catch (\Exception $error) {
            $this->assertEquals($error->getMessage(), "Total order amount event given parameters exceed sum {$query['amount']} since {$dateInterval->format('H:i d-m-Y')} from source {$source}");
        }
}

我得到了这个成功消息:OK (1 test, 1 assertion),我的问题为什么我只有一个断言。我的提供程序中有多个数组值。我究竟做错了什么 ?

谢谢!

2 个答案:

答案 0 :(得分:1)

作为从数据提供程序返回带有测试方法参数的数组数组的替代方法,您也可以使用生成器和yield参数数组:

public function addDataProvider() : \Generator
{
    $queries = [
        [
            'currency' => 'eur',
            'amount' => 800,
        ],
        [
            'currency' => 'dkk',
            'amount' => 1201,
        ],
        [
            'currency' => 'GBP',
            'amount' => 1201,
        ],
        [
            'currency' => 'sek',
            'amount' => 1200.01,
        ],
    ];

    foreach ($queries as $query) {
        yield [
            $query,
        ];
    }
}

然后可能更容易看到发生了什么,并且需要更少的数组嵌套。

注意

在测试中使用expectException()expectExceptionMessage()可能更有意义:

public function testCheckAmountUsingCurrency($query)
{
    // ...

    $this->expectException(\Exception::class);
    $this->expectExceptionMessage(sprintf(
        'Total order amount event given parameters exceed sum %s since %s from source %s',
        $query['amount'],
        $dateInterval->format('H:i d-m-Y'),
        $source          
    ));

    $ordersPerCustomer->checkAmounts(
        $query['amount'], 
        $query['currency'], 
        $dateInterval
    );
}

供参考,见

答案 1 :(得分:0)

正如@localheinz在评论中所建议的那样,这是一个深度的阵列问题。这就是我的数组应该是这样的:

public function addDataProvider() {
    return [
        [
            [
                'currency' => 'eur',
                'amount' => 800,
            ]
        ],
        [
            [
                'currency' => 'dkk',
                'amount' => 1201,
            ],
        ],
        [
            [
                'currency' => 'GBP',
                'amount' => 1201,
            ],
        ],
        [
            [
                'currency' => 'sek',
                'amount' => 1200.01,
            ],
        ],
    ];
}