2

現在、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);
    }
4

2 に答える 2

0

とにかく、今のところ多かれ少なかれ回避策を見つけました。以下に示すように、とにかくテストケースです。

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

私もフォローしてみました。

もう1つの回答

しかし、正しい解決策は議論の余地があります。

于 2016-09-06T14:48:40.497 に答える