如何找到数据提供者正在向其提供数据的测试?

How to find the test a data provider is providing data to?

在一个 PHPUnit 测试用例中,有两个或多个测试使用相同的 @dataProvider, I want the provider to find out, which test it is providing data to. I have accomplished this using debug_backtrace(),但这感觉不对。 PHPUnit 是否提供了另一种更标准的方法来实现这一点?如果是,怎么办?

<?php

class MyTest extends \PHPUnit_Framework_TestCase {

    /** @dataProvider dataProvider */
    public function testA () {}

    /** @dataProvider dataProvider */
    public function testB () {}

    public function dataProvider () {

        $trace = debug_backtrace(false, 3);
        $caller = $trace[2]['args'][2];

        // $caller === 'testA' or $caller === 'testB'
    }
}

创建调用基础数据提供程序的单个数据提供程序。这样你就知道调用者是什么并且仍然可以有一个入口点。

<?php

class MyTest extends \PHPUnit_Framework_TestCase {

    /** @dataProvider dataProviderA */
    public function testA() {}

    /** @dataProvider dataProviderB */
    public function testB() {}

    public function dataProviderA() 
    { 
        $dataProvider = $this->getProviderData();
        // Caller is A ...
        // Mutate base provider data as necessary...
    }

    public function dataProviderB() 
    { 
        $dataProvider = $this->getProviderData();
        // Caller is B ...
        // Mutate base provider data as necessary...
    }

    public function getProviderData() 
    {
        // ...
    }
}