[英]Symfony2 acceptance test not working
At the moment, I am writing acceptance test cases for Symfony2 application. 目前,我正在为Symfony2应用程序编写验收测试用例。 I am doing following.
我正在关注。
namespace my\Bundle\ProjectBundle\Tests\Controller;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
class DefaultControllerTest extends WebTestCase
{
public function testIndex()
{
$client = static::createClient();
$client->request('GET', '/');
$this->assertEquals(200, $client->getResponse()->getStatusCode());
}
}
But it cases is failing on looking into following log file. 但是它的情况是无法查看以下日志文件。
app/logs/test.log
It appears that 看起来
[2016-09-06 12:56:58] request.CRITICAL: Uncaught PHP Exception PHPUnit_Framework_Error_Notice: "Undefined index: SERVER_PROTOCOL" at /var/www/src/my/Bundle/projectBundle/Helper/DataHelper.php line 139 {"exception":"[object] (PHPUnit_Framework_Error_Notice(code: 8): Undefined index: SERVER_PROTOCOL at /var/www/src/my/Bundle/projectBundle/Helper/DataHelper.php:139)"} []
It appears that $_SERVER
variable is missing some values in it. 看来
$_SERVER
变量缺少一些值。 Any clues or is there any better ways to write the test cases. 有任何线索或有更好的方法编写测试用例。
DataHelper.php DataHelper.php
public function getCanonicalUrl()
{
$router = $this->container->get('router');
$req = $this->container->get('request');
$route = $req->get('_route');
if (!$route) {
return 'n/a';
}
$url = $router->generate($route, $req->get('_route_params'));
$protocol = stripos($_SERVER['SERVER_PROTOCOL'], 'https') === true ? 'https://' : 'http://';
return $protocol . ($this->getHostname() . $url);
}
Your solutions is working but a better approach could be the following: 您的解决方案正在运行,但是可以采用以下更好的方法:
Reading the doc about symfony2 testing : 阅读有关symfony2测试的文档 :
More about the request() Method: 有关request()方法的更多信息:
The full signature of the request() method is:
request()方法的完整签名为:
request( $method, $uri, array $parameters = array(), array $files = array(), array $server = array(), $content = null, $changeHistory = true )
The server array is the raw values that you'd expect to normally find in the PHP
$_SERVER
superglobal.服务器数组是您通常希望在PHP
$_SERVER
超全局变量中找到的原始值。
So probably a more cleaner approach could be: 因此,可能更干净的方法是:
$client->request(
'GET',
'/',
array(),
array(),
array(
'SERVER_PROTOCOL' => 'http://',
)
);
One problem could be about the value you are setting in the SERVER_PROTOCOL
variable. 一个问题可能与您在
SERVER_PROTOCOL
变量中设置的值有关。 Regarding to the doc : 关于文档 :
'SERVER_PROTOCOL' Name and revision of the information protocol via which the page was requested;
'SERVER_PROTOCOL'请求页面的信息协议的名称和修订版; ie 'HTTP/1.0';
即“ HTTP / 1.0”;
Seems the real value is 'HTTP/1.0'
(instead of http://
). 似乎实际值是
'HTTP/1.0'
(而不是http://
)。 So double check the class DataHelper.php
that generate the error. 因此,请仔细检查生成错误的
DataHelper.php
类。
EDIT: 编辑:
You can access to the an HTTP_SERVER variable from the symfony2 request (this in the doc ) 您可以从symfony2请求中访问HTTP_SERVER变量(在doc中 )
// retrieve $_SERVER variables
$request->server->get('HTTP_SERVER');
You can also call the request's method: getScheme and isSecure in order to obtain this info (Check the source code of the Request class for example). 您还可以调用请求的方法: getScheme和isSecure以获得此信息(例如,检查Request类的源代码)。 Probably, in your case, the getScheme method is what you need.
在您的情况下,可能需要使用getScheme方法。 As Example:
例如:
$protocol = $req->getScheme();
Hope this help 希望这个帮助
Anyway I found a more or less workaround for it as of now. 无论如何,到目前为止,我发现了一个或多或少的解决方法。 As of shown below anyway its a test case.
如下所示,无论如何它都是一个测试用例。
namespace Chip\Bundle\PraxistippsBundle\Tests\Controller;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
class DefaultControllerTest extends WebTestCase
{
public function testIndex()
{
$_SERVER['SERVER_PROTOCOL'] = 'http://';
$client = static::createClient();
$client->request('GET', '/');
$this->assertEquals(200, $client->getResponse()->getStatusCode());
}
}
I tried following as well. 我也尝试跟随。
Stackoverflow One more Answer Stackoverflow还有一个答案
But a correct solution is open to discussion. 但是,正确的解决方案尚待讨论。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.