2

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);
    }
Matteo
  • 37,680
  • 11
  • 100
  • 115
bharatesh
  • 952
  • 2
  • 11
  • 23

2 Answers2

3

Your solutions is working but a better approach could be the following:

Reading the doc about symfony2 testing:

More about the request() Method:

The full signature of the request() method is:

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.

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. Regarding to the doc:

'SERVER_PROTOCOL' Name and revision of the information protocol via which the page was requested; i.e. 'HTTP/1.0';

Seems the real value is 'HTTP/1.0' (instead of http://). So double check the class DataHelper.php that generate the error.

EDIT:

You can access to the an HTTP_SERVER variable from the symfony2 request (this in the 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). Probably, in your case, the getScheme method is what you need. As Example:

$protocol = $req->getScheme();

Hope this help

Matteo
  • 37,680
  • 11
  • 100
  • 115
0

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

But a correct solution is open to discussion.

Community
  • 1
  • 1
bharatesh
  • 952
  • 2
  • 11
  • 23
  • 1
    Or you could modify your DataHelper.php class to test if the protocol key exists or not. – COil Sep 06 '16 at 15:56
  • 2
    Or may be reimplement it entirely to not rely on superglobal variables at all. – zerkms Sep 06 '16 at 22:48
  • When getting SERVER_PROTOCOL , why don't you use the request stack service ? – COil Sep 07 '16 at 08:48
  • At all thank you for the comments. At Coil I can modify one class or two, in case if its used every where and it's difficult to modify every where. Secondly its a testing scenario, but in prod or dev modes that Global Parameters are always present. At zerkms re implement just for testing also not good solution. – bharatesh Sep 07 '16 at 09:13