2

所以感谢 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-bundle 从版本 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);

由于最近的事态发展,我们的开发团队决定停止使用 Sonata。作为一个副作用,这个错误得到了修复。所以我不会有这个问题的解决方案。

4

1 回答 1

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以返回您想要的请求类型。

于 2015-04-07T17:27:46.897 回答