如何在 phpunit 中引用外部数据提供者?
How can I reference external data providers in phpunit?
我正在尝试 运行 使用 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());
}
}
现在我想要一个外部数据提供者class:
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) Warning The data provider specified for
AppBundle\Tests\Controller\DefaultControllerTest::testReceive is
invalid. Class XmlDataProvider does not exist
2) Warning No tests found in class
"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"
]
},
您需要使用完全限定的类名来引用数据提供者:
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
。
示例见:
PHPUnit 提供程序自动加载器
Magic helper to autoload CSV, JSON, PHP, XML and YAML data provider in PHPUnit.
安装
composer require redaxmedia/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/redaxmedia/phpunit-provider-autoloader
示例集成:PHP test autoloads PHP class provider and PHP method provider
我正在尝试 运行 使用 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());
}
}
现在我想要一个外部数据提供者class:
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) Warning The data provider specified for AppBundle\Tests\Controller\DefaultControllerTest::testReceive is invalid. Class XmlDataProvider does not exist
2) Warning No tests found in class "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"
]
},
您需要使用完全限定的类名来引用数据提供者:
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
。
示例见:
PHPUnit 提供程序自动加载器
Magic helper to autoload CSV, JSON, PHP, XML and YAML data provider in PHPUnit.
安装
composer require redaxmedia/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/redaxmedia/phpunit-provider-autoloader
示例集成:PHP test autoloads PHP class provider and PHP method provider