bharatesh
bharatesh

Reputation: 1022

Symfony2 acceptance test not working

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);
    }

Upvotes: 2

Views: 327

Answers (2)

Matteo
Matteo

Reputation: 39380

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

Upvotes: 3

bharatesh
bharatesh

Reputation: 1022

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.

Upvotes: 0

Related Questions