我想用Symfony2进行功能测试,检查用户配置文件是否显示以及表单是否正常工作:
命名空间Dbe \ DDBundle \ Tests \ Controller;
使用Symfony \ Bundle \ FrameworkBundle \ Test \ WebTestCase;
类ProfileControllerTest扩展了WebTestCase {
public function testNewsletterOnOff(){
// Create a new client to browse the application
$client = static::createClient();
$client->request('GET', '/', array(), array(), array(
'HTTP_ACCEPT_LANGUAGE' => 'en',
));
$crawler = $client->request('GET', '/en/profile/');
在控制台中执行测试后:phpunit -c app我收到以下错误消息:
There was 1 error:
1) Dbe\DDBundle\Tests\Controller\ProfileControllerTest::testNewsletterOnOff
Undefined index: HTTP_ACCEPT_LANGUAGE
我猜这个错误是因为Controller正在检查浏览器语言。那么如何在symfony的功能测试中伪造HTTP_ACCEPT_LANGUAGE呢? 在此先感谢您的帮助!
答案 0 :(得分:0)
我找到了解决方案,因为网站访问者在构建控制器之前根据我需要定义的浏览器语言进行重定向:
$_SERVER['HTTP_ACCEPT_LANGUAGE'] = 'en';
$client = static::createClient();
$crawler = $client->request('GET', '/en/how');