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

问题描述 投票:0回答:2

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

参见下面的测试:

    namespace AppBundle\Tests\Controller;

    use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
    use AppBundle\Tests\DataProvider\XmlDataProvider;

    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());
        }
    }

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

namespace AppBundle\Tests\DataProvider;

use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;

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

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

1) 警告指定的数据提供者 AppBundle\Tests\Controller\DefaultControllerTest::testReceive 是 无效的。 XmlDataProvider 类不存在

2) 警告课堂上未发现任何测试 “AppBundle\Tests\DataProvider\XmlDataProvider”。

我该怎么做?

更新

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

"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"
    ]
},
php symfony phpunit composer-php tdd
2个回答
10
投票

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

namespace AppBundle\Tests\Controller;

use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;

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

自动加载

此外,请确保在

composer.json
中调整自动加载配置,以便可以自动加载数据提供程序(可能需要根据“AppBundle\Test”命名空间映射到哪个目录进行调整):

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

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

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

您需要将所提供的测试的命名空间从

AppBundle\Tests
调整为
Tests\AppBundle

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

WebTestCase

示例请参见:


2
投票

PHPUnit 提供商自动加载器

在 PHPUnit 中自动加载 CSV、JSON、PHP、XML 和 YAML 数据提供程序的神奇助手。

安装

composer require henryruhs/phpunit-provider-autoloader

用法

为您的测试套件创建 TestCaseAbstract:

<?php
namespace ExampleProject\Tests;

use PHPUnitProviderAutoloader;

/**
 * TestCaseAbstract
 *
 * @since 2.0.0
 *
 * @package ExampleProject
 * @category Tests
 */

abstract class TestCaseAbstract extends PHPUnitProviderAutoloader\TestCaseAbstract
{
    /**
     * directory of the provider
     *
     * @var string
     */

    protected $_providerDirectory = 'tests' . DIRECTORY_SEPARATOR . 'provider';
        
    /**
     * namespace of the testing suite
     *
     * @var string
     */

    protected $_testNamespace = __NAMESPACE__;
}

从 TestCaseAbstract 扩展以自动加载 ExampleTest{_testMethod}.{csv|json|php|xml|yml} 文件:

<?php
namespace ExampleProject\Tests;

/**
 * ExampleTest
 *
 * @since 2.0.0
 *
 * @package ExampleProject
 * @category Tests
 */

class ExampleTest extends TestCaseAbstract
{
    /**
     * testMethod
     *
     * @since 2.0.0
     *
     * @param string $expect
     *
     * @dataProvider providerAutoloader
     */

    public function testMethod(string $expect = null)
    {
        $this->assertEquals($expect, 'test');
    }
}

了解更多

相关存储库:https://github.com/henryruhs/phpunit-provider-autoloader

集成示例: PHP 测试 自动加载 PHP 类提供程序PHP 方法提供程序

© www.soinside.com 2019 - 2024. All rights reserved.