2015-06-16 22 views
1

我修改了我的應用程序的基本請求類,如accepted answer of this question中所述。它工作得非常好,我的啓動功能測試時除外,我得到以下錯誤:Symfony:更改請求基類和更新測試環境

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基類的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; 
} 
  • 的Symfony 2.6.9被使用。

[編輯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'] 
+0

如果您自己重新定義服務,請不要忘記添加'shared:false'。查看[默認配置](https://github.com/symfony/symfony/blob/master/src/Symfony/Bundle/FrameworkBundle/Resources/config/test.xml#L14)以獲得[每次新實例]( https://symfony.com/doc/current/service_container/shared.html) – Mick

回答

3

我認爲你必須重寫test.client的類別默認情況下使用Symfony\Bundle\FrameworkBundle\Client

喜歡的東西:

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

首先,來看看基類Symfony\Component\BrowserKit\Client

如果你看一下這個類的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\Client類的getScript方法。

+0

感謝提示@ awons!我將修改我的問題以添加關於我所做的事情的詳細信息。現在一切都按預期工作,我的控制器中沒有更多的「快捷」功能。 – COil

+0

對我來說,在那裏添加配置文件** test.client.class ** - 它的配置文件services.xxx(在我的例子中是services.yml)是有點混淆的。它的**參數**屬性就在文件的開頭。 –