我要做的是对我的捆绑包进行功能测试(可重复使用的捆绑包)。更深入:
- 创建对给定 url 的请求
/my/url
- 检查是否
MyParamConverter
被调用并将请求转换为MyObject
- 检查控制器是否抛出
my.event
根据文档,我应该扩展Symfony\Bundle\FrameworkBundle\Test\WebTestCase
并创建一个新客户端:
$client = static::createClient();
$crawler = $client->request('GET', '/my/url');
这样做,加载了哪些捆绑包?如何指定要在环境中使用的配置文件(假设它默认为test
)?
编辑:好的,是时候更好地解释我的问题了。我正在编写一个可重复使用的包,比如 AcmeMessagingBundle。现在我想对其进行功能测试。场景是调用/my/url
:
public function testReceiveApiRoute()
{
$client = $this->createClient();
/** @var $route \Symfony\Component\Routing\Route */
$route = $client->getContainer()->get('router')
->getRouteCollection()->get('acme_messaging_receive');
$this->assertNotNull($route);
$this->assertEquals('POST', $route->getRequirement('_method'));
$this->assertEquals('acme_messaging.controller.api:receive',
$route->getDefault('_controller'));
}
/**
* @depends testReceiveApiRoute
*/
public funcion testReceiveApiWorkflow()
{
$client = $this->createClient();
// Make a POST request
$request = Request::create('/my/route', 'POST', array(
'a' => 'value'
));
// Request is convered in MyObject instance and that my.event is fired
}
通过此测试,app/config_test.yml
已加载(例如“主配置文件”)。问题是:
不应该将测试“隔离”,即不使用主配置文件?如果我的捆绑包被另一个人用空包测试了app/config_test.yml
怎么办?测试会失败...
带有前缀路由的测试也会失败。如果routing.xml
从 AcmeMessagingBundle 导入带有前缀,testReceiveApiWorkflow
将会失败!