Symfony 2股票控制器测试失败,错误为“未定义的索引:HTTP_HOST”


Symfony 2 stock controller tests fail with error `Undefined index: HTTP_HOST`

我想知道是否有人能解决这个错误。我刚刚在Symphony 2项目中设置了PhpUnit 4.3.*和Doctrine Fixtures Bundle,当我运行bin/phpunit -c app/时,我会出现以下错误。这些是Symfony 2代码生成器自动生成的控制器测试,所以我认为它们在任何情况下都应该通过/有效。我有预感phpunit的配置/安装可能有问题,但我不确定。。。

Project'AdminBundle'Tests'Controller'DefaultControllerTest::testIndex
Undefined index: HTTP_HOST

记录在案,我的composer.json是:

{
    "name": "symfony/framework-standard-edition",
    "license": "MIT",
    "type": "project",
    "description": "The '"Symfony Standard Edition'" distribution",
    "autoload": {
        "psr-0": { "": "src/" }
    },
    "require": {
        "php": ">=5.3.3",
        "symfony/symfony": "2.3.*",
        "doctrine/orm": ">=2.2.3,<2.4-dev",
        "doctrine/doctrine-bundle": "1.2.*",
        "twig/extensions": "1.0.*",
        "symfony/assetic-bundle": "2.3.*",
        "symfony/swiftmailer-bundle": "2.3.*",
        "symfony/monolog-bundle": "2.3.*",
        "sensio/distribution-bundle": "2.3.*",
        "sensio/framework-extra-bundle": "2.3.*",
        "sensio/generator-bundle": "2.3.*",
        "incenteev/composer-parameter-handler": "~2.0",
        "dms/dms-filter-bundle": "*",
        "jms/i18n-routing-bundle": "1.1.*",
        "jms/translation-bundle": "1.1.*",
        "vmelnik/doctrine-encrypt-bundle": "dev-master",
        "doctrine/doctrine-fixtures-bundle": "2.2.*"
    },
    "require-dev": {
        "phpunit/phpunit": "4.3.*"
    },
    "scripts": {
        "post-install-cmd": [
            "Incenteev''ParameterHandler''ScriptHandler::buildParameters",
            "Sensio''Bundle''DistributionBundle''Composer''ScriptHandler::buildBootstrap",
            "Sensio''Bundle''DistributionBundle''Composer''ScriptHandler::clearCache",
            "Sensio''Bundle''DistributionBundle''Composer''ScriptHandler::installAssets",
            "Sensio''Bundle''DistributionBundle''Composer''ScriptHandler::installRequirementsFile"
        ],
        "post-update-cmd": [
            "Incenteev''ParameterHandler''ScriptHandler::buildParameters",
            "Sensio''Bundle''DistributionBundle''Composer''ScriptHandler::buildBootstrap",
            "Sensio''Bundle''DistributionBundle''Composer''ScriptHandler::clearCache",
            "Sensio''Bundle''DistributionBundle''Composer''ScriptHandler::installAssets",
            "Sensio''Bundle''DistributionBundle''Composer''ScriptHandler::installRequirementsFile"
        ]
    },
    "config": {
        "bin-dir": "bin"
    },
    "minimum-stability": "stable",
    "extra": {
        "symfony-app-dir": "app",
        "symfony-web-dir": "web",
        "incenteev-parameters": {
            "file": "app/config/parameters.yml"
        },
        "branch-alias": {
            "dev-master": "2.3-dev"
        }
    }
}

经过更多的搜索,我发现我必须向phpunit.xml添加一个配置。为了子孙后代,我将回答我自己的问题。

创建一个phpunit.xml文件并复制phpunit.xml.dist的内容(如果有)。

要解决此问题,请添加

<php>
    <server name='HTTP_HOST' value='http://localhost' />
</php>

到您刚刚创建的配置文件。

有关phpunit配置文件的更多信息:http://www.testically.org/2010/08/24/best-practice-how-to-ship-phpunit-configuration/

这个问题是"PHPUnit error"的重复;未定义索引:HTTP_HOST";