Symfony:更改请求基础 class 并更新测试环境

Symfony: changing the request base class and updating the test environment

我已按照 accepted answer of this question 中的说明修改了我的应用程序的基本请求 class。它工作得很好,除了在启动我的功能测试时,我收到以下错误:

Controller "My\Bundle\AppBundle\Controller\MyController::searchAction()" requires that you provide a value for the "$request" argument (because there is no default value or because there is a non-optional argument after this one). (500 Internal Server Error)

确实没有使用 app_test.php 控制器。我希望它可以在我的 WebTestCase 基础 class 的 createCient() 函数中完成,但是怎么做呢?

/**
 * Creates a Client.
 *
 * @param array $options An array of options to pass to the createKernel class
 * @param array $server  An array of server parameters
 *
 * @return Client A Client instance
 */
protected static function createClient(array $options = array(), array $server = array())
{
    static::bootKernel($options);

    $client = static::$kernel->getContainer()->get('test.client');
    $client->setServerParameters($server);

    return $client;
} 

[编辑 2017 年 9 月 21 日]:

迁移到 Symfony 3.3 时我必须执行以下操作:

config_test.yml:

parameters:
    test.client.class: My\Bundle\AppBundle\Tests\Client

services:
    test.client:
        class: '%test.client.class%'
        arguments: ['@kernel', '%test.client.parameters%', '@test.client.history', '@test.client.cookiejar']

我认为您必须为 test.client 覆盖 class,默认情况下,它使用 Symfony\Bundle\FrameworkBundle\Client.

类似于:

<parameters>
    <parameter name="test.client.class">My\Bundle\AppBundle\Test\Client</parameter>
</parameters>

首先看一下基地classSymfony\Component\BrowserKit\Client.

如果您查看此 class 的 request 方法,您会发现:

public function request(/* ... */)
{
    // ...
    $this->internalRequest = new Request($uri, $method, $parameters, $files, $this->cookieJar->allValues($uri), $server, $content);

    $this->request = $this->filterRequest($this->internalRequest);
    // ...
}

现在看看 Symfony\Component\HttpKernel\ClientfilterRequest 方法:

protected function filterRequest(DomRequest $request)
{
     $httpRequest = Request::create($request->getUri(), $request->getMethod(), $request->getParameters(), $request->getCookies(), $request->getFiles(), $request->getServer(), $request->getContent());

    // ...
}

这里是真正的魔法发生的地方。如果您需要不同的请求对象,则必须重写此方法。

编辑:

这个$httpRequest然后将被序列化和反序列化。看看Symfony\Component\HttpKernel\Clientclass.

getScript方法就知道了