【问题标题】:symfony2 phpunit - functional testing error (host_with_path)symfony2 phpunit - 功能测试错误(host_with_path)
【发布时间】:2015-06-08 14:42:14
【问题描述】:

感谢 Matteo (phpunit in symfony2 - No tests executed),我现在可以测试我的功能测试。

现在运行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。作为一个副作用,这个错误得到了修复。所以我不会有解决这个问题的办法。

【问题讨论】:

    标签: php symfony phpunit functional-testing symfony-2.6


    【解决方案1】:

    这里的问题是,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 才能返回您想要的请求类型。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-06-10
      • 1970-01-01
      • 2013-07-08
      • 1970-01-01
      • 2019-06-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多