"Undefined index: localhost" 使用 Symfony2 模块的 Codeception 中的 PHPUnit 异常

"Undefined index: localhost" PHPUnit exception in Codeception with Symfony2 module

我使用 Codeception 及其 Symfony2 模块进行了这个非常简单的验收测试:

public function myTest(AcceptanceTester $I)
{
    $I->wantTo('test something');
    $I->amOnRoute("acme_site_home.es");
    $I->dontSee('hello');
}

当我 运行 它时,我得到一个 Undefined index: localhost PHPUnit_Framework_Exception 由在我的路由操作中调用 $request->server->get("HTTP_HOST") 触发。

我已经尝试过以下方法:

    <phpunit>
        <php>
            <server name='HTTP_HOST' value='http://some.host.com' />
        </php>
    </phpunit>
    $_SERVER['HOST_NAME'] = "some.host.com";
    $_SERVER['HTTP_HOST'] = "some.host.com";

None 这个有效,我总是得到相同的错误信息。

我该如何解决这个问题?


备注

[此处给出的所有内容都已清理(密码、主机名等)并添加了一些换行符和缩进以提高可读性]

为了以防万一,我的验收套件设置如下:

class_name: AcceptanceTester
modules:
    enabled:
        - Asserts
        - Db
        - Symfony2
        - Doctrine2
        - \Tests\Helper\Acceptance

还有我的 Codeception 设置:

namespace: Tests
actor: Tester
paths:
    tests: tests
    log: tests/_output
    data: tests/_data
    support: tests/_support
    envs: tests/_envs
settings:
    bootstrap: _bootstrap.php
    colors: true
    memory_limit: 1024M
extensions:
    enabled: [ Codeception\Extension\RunFailed ]
modules:
    config:
        Db:
            dsn: 'mysql:host=localhost;dbname=acme_test'
            user: 'root'
            password: 'blahblah'
            dump: 'tests/_data/dump.sql'
            populate: false
            cleanup: false
        Doctrine2:
            depends: Symfony2
            cleanup: false
        PhpBrowser:
            url: http://acme.com
        WebDriver:
             browser: firefox
             url: http://acme.com
        REST:
            depends: Symfony2
            url: http://acme.com

这是使用 -vvv 标志 运行ning 测试的结果:

$ bin/codecept run tests/acceptance/ExtranetBundle/Controller/DefaultControllerCest.php -vvv

Codeception PHP Testing Framework v2.1.3
Powered by PHPUnit 4.8.13 by Sebastian Bergmann and contributors.

Tests.acceptance Tests (1) ---------------------------------------------------------------------------------------------------------------------------
Modules: Asserts, Db, Symfony2, Doctrine2, \Tests\Helper\Acceptance
------------------------------------------------------------------------------------------------------------------------------------------------------
Test something (Acceptance\ExtranetBundle\Controller\DefaultControllerCest::myTest)
Scenario:
* I am on route "acme_site_home.es"

[Page] http://localhost/es
[User] anon. []
* I don't see "hello"
FAIL

^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
------------------------------------------------------------------------------------------------------------------------------------------------------


Time: 4.13 seconds, Memory: 65.75Mb

There was 1 failure:

---------
1) Failed to test something in Tests\Acceptance\ExtranetBundle\Controller\DefaultControllerCest::myTest (tests/acceptance/ExtranetBundle/Controller/DefaultControllerCest.php)

Step  I don't see "hello"
Fail  Failed asserting that   /es
-->

Undefined index: localhost (500 Internal Server Error)

[Content too long to display. See complete response in '_output' directory]
--> does not contain "hello".

Scenario Steps:

2. $I->dontSee("hello") at tests/acceptance/ExtranetBundle/Controller/DefaultControllerCest.php:15
1. $I->amOnRoute("acme_site_home.es") at tests/acceptance/ExtranetBundle/Controller/DefaultControllerCest.php:14

/Volumes/Utilisateurs/marc/Sites/acme.com/vendor/phpunit/phpunit/src/Framework/Constraint/Not.php:100
/Volumes/Utilisateurs/marc/Sites/acme.com/vendor/phpunit/phpunit/src/Framework/Assert.php:2255
/Volumes/Utilisateurs/marc/Sites/acme.com/vendor/codeception/codeception/src/Codeception/Util/Shared/Asserts.php:280
/Volumes/Utilisateurs/marc/Sites/acme.com/vendor/codeception/codeception/src/Codeception/Lib/InnerBrowser.php:1263
/Volumes/Utilisateurs/marc/Sites/acme.com/vendor/codeception/codeception/src/Codeception/Lib/InnerBrowser.php:284
/Volumes/Utilisateurs/marc/Sites/acme.com/vendor/codeception/codeception/src/Codeception/Step.php:193
/Volumes/Utilisateurs/marc/Sites/acme.com/vendor/codeception/codeception/src/Codeception/TestCase/Shared/Actor.php:99
/Volumes/Utilisateurs/marc/Sites/acme.com/vendor/codeception/codeception/src/Codeception/Scenario.php:101
/Volumes/Utilisateurs/marc/Sites/acme.com/tests/_support/_generated/AcceptanceTesterActions.php:831
/Volumes/Utilisateurs/marc/Sites/acme.com/tests/acceptance/ExtranetBundle/Controller/DefaultControllerCest.php:15
/Volumes/Utilisateurs/marc/Sites/acme.com/vendor/codeception/codeception/src/Codeception/Lib/Di.php:112
/Volumes/Utilisateurs/marc/Sites/acme.com/vendor/codeception/codeception/src/Codeception/TestCase/Cest.php:127
/Volumes/Utilisateurs/marc/Sites/acme.com/vendor/codeception/codeception/src/Codeception/TestCase/Cest.php:136
/Volumes/Utilisateurs/marc/Sites/acme.com/vendor/codeception/codeception/src/Codeception/TestCase/Cest.php:66
/Volumes/Utilisateurs/marc/Sites/acme.com/vendor/phpunit/phpunit/src/Framework/TestCase.php:909
/Volumes/Utilisateurs/marc/Sites/acme.com/vendor/phpunit/phpunit/src/Framework/TestCase.php:768
/Volumes/Utilisateurs/marc/Sites/acme.com/vendor/phpunit/phpunit/src/Framework/TestResult.php:612
/Volumes/Utilisateurs/marc/Sites/acme.com/vendor/phpunit/phpunit/src/Framework/TestCase.php:724
/Volumes/Utilisateurs/marc/Sites/acme.com/vendor/phpunit/phpunit/src/Framework/TestSuite.php:747
/Volumes/Utilisateurs/marc/Sites/acme.com/vendor/codeception/codeception/src/Codeception/PHPUnit/Runner.php:89
/Volumes/Utilisateurs/marc/Sites/acme.com/vendor/codeception/codeception/src/Codeception/SuiteManager.php:153
/Volumes/Utilisateurs/marc/Sites/acme.com/vendor/codeception/codeception/src/Codeception/Codecept.php:203
/Volumes/Utilisateurs/marc/Sites/acme.com/vendor/codeception/codeception/src/Codeception/Codecept.php:172
/Volumes/Utilisateurs/marc/Sites/acme.com/vendor/codeception/codeception/src/Codeception/Command/Run.php:184
/Volumes/Utilisateurs/marc/Sites/acme.com/vendor/symfony/symfony/src/Symfony/Component/Console/Command/Command.php:259
/Volumes/Utilisateurs/marc/Sites/acme.com/vendor/symfony/symfony/src/Symfony/Component/Console/Application.php:878
/Volumes/Utilisateurs/marc/Sites/acme.com/vendor/symfony/symfony/src/Symfony/Component/Console/Application.php:195
/Volumes/Utilisateurs/marc/Sites/acme.com/vendor/symfony/symfony/src/Symfony/Component/Console/Application.php:126
/Volumes/Utilisateurs/marc/Sites/acme.com/vendor/codeception/codeception/codecept:28


FAILURES!
Tests: 1, Assertions: 1, Failures: 1.

这是 Symfony 在 _output 目录中给出的错误堆栈跟踪:

PHPUnit_Framework_Exception: Undefined index: localhost
at n/a
in /Volumes/Utilisateurs/marc/Sites/acme.com/vendor/codeception/codeception/src/Codeception/Subscriber/ErrorHandler.php line 48

at Codeception\Subscriber\ErrorHandler->errorHandler('8', 'Undefined index: localhost', '/Volumes/Utilisateurs/marc/Sites/acme.com/src/Acme/SiteBundle/Controller/DefaultController.php', '160', array(
    'request' => object(Request),
    'locale' => 'es',
    'registrationForm' => object(Form),
    'typeAppFb' => array(
        'acme.com' => array(
            'appId' => '123456789123456',
            'secret' => '123456789abcdef123456789abcdef12'
        ),
        'dev.acme.com' => array(
            'appId' => '123456789123456',
            'secret' => '123456789abcdef123456789abcdef12'
        ),
        'www.prod-acme.com' => array(
            'appId' => '123456789123456',
            'secret' => '123456789abcdef123456789abcdef12'
        )
    )
))
in /Volumes/Utilisateurs/marc/Sites/acme.com/src/Acme/SiteBundle/Controller/DefaultController.php line 160

at Acme\SiteBundle\Controller\DefaultController->homeAction(object(Request))
in  line

at call_user_func_array(array(object(DefaultController), 'homeAction'), array(object(Request)))
in /Volumes/Utilisateurs/marc/Sites/acme.com/app/bootstrap.php.cache line 3109

at Symfony\Component\HttpKernel\HttpKernel->handleRaw(object(Request), '1')
in /Volumes/Utilisateurs/marc/Sites/acme.com/app/bootstrap.php.cache line 3071

at Symfony\Component\HttpKernel\HttpKernel->handle(object(Request), '1', true)
in /Volumes/Utilisateurs/marc/Sites/acme.com/app/bootstrap.php.cache line 3222

at Symfony\Component\HttpKernel\DependencyInjection\ContainerAwareHttpKernel->handle(object(Request), '1', true)
in /Volumes/Utilisateurs/marc/Sites/acme.com/app/bootstrap.php.cache line 2444

at Symfony\Component\HttpKernel\Kernel->handle(object(Request))
in /Volumes/Utilisateurs/marc/Sites/acme.com/vendor/symfony/symfony/src/Symfony/Component/HttpKernel/Client.php line 81

at Symfony\Component\HttpKernel\Client->doRequest(object(Request))
in /Volumes/Utilisateurs/marc/Sites/acme.com/vendor/codeception/codeception/src/Codeception/Lib/Connector/Symfony2.php line 38

at Codeception\Lib\Connector\Symfony2->doRequest(object(Request))
in /Volumes/Utilisateurs/marc/Sites/acme.com/vendor/symfony/symfony/src/Symfony/Component/BrowserKit/Client.php line 327

at Symfony\Component\BrowserKit\Client->request('GET', '/es', array(), array(), array(), null, true)
in /Volumes/Utilisateurs/marc/Sites/acme.com/vendor/codeception/codeception/src/Codeception/Lib/InnerBrowser.php line 130

at Codeception\Lib\InnerBrowser->clientRequest('GET', '/es', array(), array(), array(), null)
in /Volumes/Utilisateurs/marc/Sites/acme.com/vendor/codeception/codeception/src/Codeception/Lib/InnerBrowser.php line 159

at Codeception\Lib\InnerBrowser->_loadPage('GET', '/es')
in /Volumes/Utilisateurs/marc/Sites/acme.com/vendor/codeception/codeception/src/Codeception/Lib/InnerBrowser.php line 203

at Codeception\Lib\InnerBrowser->amOnPage('/es')
in /Volumes/Utilisateurs/marc/Sites/acme.com/vendor/codeception/codeception/src/Codeception/Module/Symfony2.php line 197

at Codeception\Module\Symfony2->amOnRoute('acme_site_home.es')
in  line

at call_user_func_array(array(object(Symfony2), 'amOnRoute'), array('acme_site_home.es'))
in /Volumes/Utilisateurs/marc/Sites/acme.com/vendor/codeception/codeception/src/Codeception/Step.php line 193

at Codeception\Step->run(object(ModuleContainer))
in /Volumes/Utilisateurs/marc/Sites/acme.com/vendor/codeception/codeception/src/Codeception/TestCase/Shared/Actor.php line 99

at Codeception\TestCase\Cest->runStep(object(Condition))
in /Volumes/Utilisateurs/marc/Sites/acme.com/vendor/codeception/codeception/src/Codeception/Scenario.php line 101

at Codeception\Scenario->runStep(object(Condition))
in /Volumes/Utilisateurs/marc/Sites/acme.com/tests/_support/_generated/AcceptanceTesterActions.php line 579

at Tests\AcceptanceTester->amOnRoute('acme_site_home.es')
in /Volumes/Utilisateurs/marc/Sites/acme.com/tests/acceptance/ExtranetBundle/Controller/DefaultControllerCest.php line 14

at Tests\Acceptance\ExtranetBundle\Controller\DefaultControllerCest->myTest(object(AcceptanceTester))
in  line

at ReflectionMethod->invokeArgs(object(DefaultControllerCest), array(object(AcceptanceTester)))
in /Volumes/Utilisateurs/marc/Sites/acme.com/vendor/codeception/codeception/src/Codeception/Lib/Di.php line 112

at Codeception\Lib\Di->injectDependencies(object(DefaultControllerCest), 'myTest', array(object(AcceptanceTester), object(Scenario)))
in /Volumes/Utilisateurs/marc/Sites/acme.com/vendor/codeception/codeception/src/Codeception/TestCase/Cest.php line 127

at Codeception\TestCase\Cest->invoke('myTest', array(object(AcceptanceTester), object(Scenario)))
in /Volumes/Utilisateurs/marc/Sites/acme.com/vendor/codeception/codeception/src/Codeception/TestCase/Cest.php line 136

at Codeception\TestCase\Cest->executeTestMethod(object(AcceptanceTester))
in /Volumes/Utilisateurs/marc/Sites/acme.com/vendor/codeception/codeception/src/Codeception/TestCase/Cest.php line 66

at Codeception\TestCase\Cest->testCodecept()
in  line

at ReflectionMethod->invokeArgs(object(Cest), array())
in /Volumes/Utilisateurs/marc/Sites/acme.com/vendor/phpunit/phpunit/src/Framework/TestCase.php line 909

at PHPUnit_Framework_TestCase->runTest()
in /Volumes/Utilisateurs/marc/Sites/acme.com/vendor/phpunit/phpunit/src/Framework/TestCase.php line 768

at PHPUnit_Framework_TestCase->runBare()
in /Volumes/Utilisateurs/marc/Sites/acme.com/vendor/phpunit/phpunit/src/Framework/TestResult.php line 612

at PHPUnit_Framework_TestResult->run(object(Cest))
in /Volumes/Utilisateurs/marc/Sites/acme.com/vendor/phpunit/phpunit/src/Framework/TestCase.php line 724

at PHPUnit_Framework_TestCase->run(object(PHPUnit_Framework_TestResult))
in /Volumes/Utilisateurs/marc/Sites/acme.com/vendor/phpunit/phpunit/src/Framework/TestSuite.php line 747

at PHPUnit_Framework_TestSuite->run(object(PHPUnit_Framework_TestResult))
in /Volumes/Utilisateurs/marc/Sites/acme.com/vendor/codeception/codeception/src/Codeception/PHPUnit/Runner.php line 89

at Codeception\PHPUnit\Runner->doEnhancedRun(object(Suite), object(PHPUnit_Framework_TestResult), array(
    'silent' => false,
    'debug' => false,
    'steps' => false,
    'html' => false,
    'xml' => false,
    'json' => false,
    'tap' => false,
    'report' => false,
    'colors' => true,
    'coverage' => false,
    'coverage-xml' => false,
    'coverage-html' => false,
    'coverage-text' => false,
    'groups' => null,
    'excludeGroups' => null,
    'filter' => null,
    'env' => null,
    'fail-fast' => false,
    'verbosity' => '4',
    'interactive' => true,
    'no-rebuild' => false,
    'bootstrap' => '_bootstrap.php',
    'memory_limit' => '1024M',
    'log' => false,
    'strict_xml' => false,
    'verbose' => true
))
in /Volumes/Utilisateurs/marc/Sites/acme.com/vendor/codeception/codeception/src/Codeception/SuiteManager.php line 153

at Codeception\SuiteManager->run(object(Runner), object(PHPUnit_Framework_TestResult), array(
    'silent' => false,
    'debug' => false,
    'steps' => false,
    'html' => false,
    'xml' => false,
    'json' => false,
    'tap' => false,
    'report' => false,
    'colors' => true,
    'coverage' => false,
    'coverage-xml' => false,
    'coverage-html' => false,
    'coverage-text' => false,
    'groups' => null,
    'excludeGroups' => null,
    'filter' => null,
    'env' => null,
    'fail-fast' => false,
    'verbosity' => '4',
    'interactive' => true,
    'no-rebuild' => false,
    'bootstrap' => '_bootstrap.php',
    'memory_limit' => '1024M',
    'log' => false,
    'strict_xml' => false,
    'verbose' => true
))
in /Volumes/Utilisateurs/marc/Sites/acme.com/vendor/codeception/codeception/src/Codeception/Codecept.php line 203

at Codeception\Codecept->runSuite(array(
    'class_name' => 'AcceptanceTester',
        'modules' => array(
            'enabled' => array(
                'Asserts',
                'Db',
                'Symfony2',
                'Doctrine2',
                '\Tests\Helper\Acceptance'
            ),
            'config' => array(
                'Db' => array(
                    'dsn' => 'mysql:host=localhost;dbname=acme_test',
                    'user' => 'root',
                    'password' => 'blahblah',
                    'dump' => 'tests/_data/dump.sql',
                    'populate' => false,
                    'cleanup' => false
                ),
                'PhpBrowser' => array(
                    'url' => 'http://acme.com'
                ),
                'WebDriver' => array(
                    'url' => 'http://acme.com',
                    'browser' => 'firefox'
                ),
                'REST' => array(
                    'url' => 'http://acme.com',
                    'depends' => 'Symfony2'
                ),
                'Doctrine2' => array(
                    'depends' => 'Symfony2',
                    'cleanup' => false
                )
            ),
            'depends' => array()
        ),
        'colors' => true,
        'bootstrap' => '_bootstrap.php',
        'memory_limit' => '1024M',
        'log' => false,
        'strict_xml' => false,
        'coverage' => array(),
        'namespace' => 'Tests',
        'groups' => array('failed' => 'tests/_output/failed'),
        'path' => '/Volumes/Utilisateurs/marc/Sites/acme.com/tests/acceptance/',
        'shuffle' => false,
        'error_level' => 'E_ALL & ~E_STRICT & ~E_DEPRECATED'
    ),
    'acceptance',
    'ExtranetBundle/Controller/DefaultControllerCest.php'
)
in /Volumes/Utilisateurs/marc/Sites/acme.com/vendor/codeception/codeception/src/Codeception/Codecept.php line 172

at Codeception\Codecept->run('acceptance', 'ExtranetBundle/Controller/DefaultControllerCest.php')
in /Volumes/Utilisateurs/marc/Sites/acme.com/vendor/codeception/codeception/src/Codeception/Command/Run.php line 184

at Codeception\Command\Run->execute(object(ArgvInput), object(ConsoleOutput))
in /Volumes/Utilisateurs/marc/Sites/acme.com/vendor/symfony/symfony/src/Symfony/Component/Console/Command/Command.php line 259

at Symfony\Component\Console\Command\Command->run(object(ArgvInput), object(ConsoleOutput))
in /Volumes/Utilisateurs/marc/Sites/acme.com/vendor/symfony/symfony/src/Symfony/Component/Console/Application.php line 878

at Symfony\Component\Console\Application->doRunCommand(object(Run), object(ArgvInput), object(ConsoleOutput))
in /Volumes/Utilisateurs/marc/Sites/acme.com/vendor/symfony/symfony/src/Symfony/Component/Console/Application.php line 195

at Symfony\Component\Console\Application->doRun(object(ArgvInput), object(ConsoleOutput))
in /Volumes/Utilisateurs/marc/Sites/acme.com/vendor/symfony/symfony/src/Symfony/Component/Console/Application.php line 126

at Symfony\Component\Console\Application->run()
in /Volumes/Utilisateurs/marc/Sites/acme.com/vendor/codeception/codeception/codecept line 28

这是一个双方面的问题:

  1. 代码没有很好地处理未知域并发出警告,PhpUnit 将警告转换为异常。

  2. 要测试对特定域的请求,您必须向该域发出请求,例如$I->amOnPage('http://acme.com/es');。由于您使用的是 Symfony2 模块,它不会向该站点发出实际的 HTTP 请求,而是在内部处理它。如果您使用 amOnRoute() 方法,请确保路由生成的 url 包含主机名部分。

P.S。我不知道网站包是如何工作的,但我建议至少为该域设置一个基于主机名的路由,否则您的测试可能会停止使用未来版本的 Codeception。