現在、Symfony2 アプリケーションの受け入れテスト ケースを作成しています。以下を行っています。
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());
}
}
ただし、以下のログファイルを調べると失敗する場合があります。
app/logs/test.log
どうやら
[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)"} []
$_SERVER
変数にいくつかの値が欠落しているようです。手がかり、またはテストケースを作成するためのより良い方法はありますか。
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);
}