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

时间:2014-10-15 14:00:24

标签: php symfony phpunit symfony-2.3

我想知道是否有人有这个错误的解决方案。我刚刚在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"
        }
    }
}

1 个答案:

答案 0 :(得分:0)

经过一番搜索后,我发现我必须在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 "undefined index : HTTP_HOST"