symfony2 phpunit-功能测试错误(host_with_path)


symfony2 phpunit - functional testing error (host_with_path)

所以多亏了Matteo(symfony2中的phpunit-没有执行任何测试),我现在可以测试我的功能测试了。

现在我在运行phpunit -c app:时出现以下错误

 You must change the main Request object in the front controller (app.php)
 in order to use the `host_with_path` strategy.

所以我确实在app.php中更改了它,从:

$request = RequestFactory::createFromGlobals('host_with_path');

至:

$request = Request::createFromGlobals();

我还将我的swiftmailer捆绑包从版本2.3更新到了5.4.0。不幸的是,这并没有修复我的错误。

这是我的/app/config_test.yml

swiftmailer:
disable_delivery: true

我是不是遗漏了什么?

我似乎在网上找不到这个错误。有人知道我应该如何修复这个错误吗?

经过一番搜索,我发现app.php不是问题所在。这是DefaultControllerTest.php。可以通过从DefaultControllerTest中删除以下行来修复错误:

        $crawler = $client->request('GET', '/hello/Fabien');
    $this->assertTrue($crawler->filter('html:contains("Hello Fabien")')->count() > 0);

由于最近的发展,我们的开发团队决定停止使用索纳塔。作为副作用,这个错误得到了修复。所以我不会有解决这个问题的办法

这里的问题是,Client对象既没有使用app.php也没有使用app_dev.php.

客户端在内部创建请求。所以这不是你需要的请求。

我能看到的唯一解决方案是重写方法Symfony'Bundle'FrameworkBundle'Test'WebTestCase::createClient以返回您自己的客户端。这个客户端负责创建实际的请求对象。以下是当前行为。

namespace Symfony'Component'HttpKernel;
use Symfony'Component'BrowserKit'Client as BaseClient;
class Client extends BaseClient
{
  ...
  /**
     * Converts the BrowserKit request to a HttpKernel request.
     *
     * @param DomRequest $request A DomRequest instance
     *
     * @return Request A Request instance
     */
    protected function filterRequest(DomRequest $request)
    {
        $httpRequest = Request::create($request->getUri(), $request->getMethod(), $request->getParameters(), $request->getCookies(), $request->getFiles(), $request->getServer(), $request->getContent());
        foreach ($this->filterFiles($httpRequest->files->all()) as $key => $value) {
            $httpRequest->files->set($key, $value);
        }
        return $httpRequest;
    }
  ...
}

您必须重写方法filterRequest才能返回所需类型的请求。