如何在 phpunit 中引用外部数据提供者?

2022-01-25 00:00:00 tdd php symfony composer-php phpunit

我正在尝试使用 PHPUnit 中的通用数据提供程序运行一些测试.

I am trying to run some tests using a common data provider in PHPUnit.

看下面的测试:

    namespace AppBundleTestsController;

    use SymfonyBundleFrameworkBundleTestWebTestCase;
    use AppBundleTestsDataProviderXmlDataProvider;

    class DefaultControllerTest extends WebTestCase
    {
        /**
         * @dataProvider XmlDataProvider::xmlProvider
         * @covers ReceiveController::receiveAction()
         * @param string
         */
        public function testReceive($xml)
        {
            $client = static::createClient([], ['HTTP_HOST' => 'mt.host']);
            $client->request(
                'POST',
                '/receive',
                [],
                [],
                [],
                $xml
            );

            $response = $client->getResponse();
            $this->assertEquals(200, $response->getStatusCode());
        }
    }

现在我想要一个外部数据提供者类:

Now I want an external data provider class:

namespace AppBundleTestsDataProvider;

use SymfonyBundleFrameworkBundleTestWebTestCase;

class XmlDataProvider extends WebTestCase
{
    /**
     * @dataProvider
     */
    public static function xmlProvider()
    {
        return array([
            'xml1' => '<?xml version="1.0" encoding="UTF-8"?><myTestableXml></myTestableXml>'
        ]);
    }
}

但是当我运行 phpunit 时,我得到:

But when I run phpunit I get:

1) 警告 指定的数据提供者AppBundleTestsControllerDefaultControllerTest::testReceive 是无效的.XmlDataProvider 类不存在

1) Warning The data provider specified for AppBundleTestsControllerDefaultControllerTest::testReceive is invalid. Class XmlDataProvider does not exist

2) 警告 类中未找到测试AppBundleTestsDataProviderXmlDataProvider".

2) Warning No tests found in class "AppBundleTestsDataProviderXmlDataProvider".

我该怎么做?

composer.json 自动加载片段供参考:

composer.json autoload snippet for reference:

"autoload": {
    "psr-4": {
        "AppBundle\": "src/AppBundle",
        "Tests\": "tests"
    },
    "classmap": [
        "app/AppKernel.php",
        "app/AppCache.php"
    ]
},
"autoload-dev": {
    "psr-4": {
        "Tests\": "tests/"
    },
    "files": [
        "vendor/symfony/symfony/src/Symfony/Component/VarDumper/Resources/functions/dump.php"
    ]
},

推荐答案

您需要使用完全限定的类名来引用数据提供者:

You need to reference the data provider using the fully-qualified classname:

namespace AppBundleTestsController;

use SymfonyBundleFrameworkBundleTestWebTestCase;

class DefaultControllerTest extends WebTestCase
{
    /**
     * @dataProvider AppBundleTestsDataProviderXmlDataProvider::xmlProvider
     * @covers ReceiveController::receiveAction()
     * @param string $xml
     */
    public function testReceive($xml)
    {
        // ...
    }
}

自动加载

另外,请确保在 composer.json 中调整您的自动加载配置,以便可以自动加载数据提供程序(可能需要根据AppBundleTest"命名空间映射到的目录进行调整):

Autoloading

Also, make sure to adjust your autoload configuration in composer.json, so the data provider can be autoloaded (might need adjustment depending on which directory the ’AppBundleTest` namespace maps to):

{
    "autoload-dev": {
        "psr-4": {
            "AppBundle\Tests\": "tests/"
        }
    }
}

或者,既然您建议您的自动加载配置如下所示:

Alternatively, since you suggest your autoloading configuration looks like this:

{
    "autoload-dev": {
        "psr-4": {
            "Tests\": "tests/"
        }
    }
}

您需要将您的命名空间从 AppBundleTests 调整为 TestsAppBundle.

you need to adjust your namespace for the presented tests from AppBundleTests to TestsAppBundle.

注意与您的问题无关,但就我个人而言,我认为数据提供者不需要扩展 WebTestCase.

Note Unrelated to your question, but personally, I can't see a need for the data provider to extend WebTestCase.

例如,请参阅:

  • https://github.com/refinery29/test-util#example
  • https://github.com/symfony/symfony-demo/blob/master/composer.json#L6-L16

相关文章