At the moment, I am writing acceptance test cases for Symfony2 application. 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. Any clues or is there any better ways to write the test cases.
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);
}