【发布时间】:2018-02-02 14:47:49
【问题描述】:
我正在尝试使用 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 自动加载 sn -p 供参考:
"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