2012-09-27 27 views
2

我試圖做的是功能測試我的包(一個可重用的)。更多深:當WebTestCase用於功能測試捆綁包時指定配置?

  • 創建給定的URL的請求/my/url
  • 檢查MyParamConverter被調用請求轉換成MyObject
  • 檢查的情況下,該控制器拋出my.event

根據文檔,我應該擴展Symfony\Bundle\FrameworkBundle\Test\WebTestCase並創建一個新的客戶端:

$client = static::createClient(); 
    $crawler = $client->request('GET', '/my/url'); 

這樣做,哪些包被加載?我如何指定一個在環境中使用的配置文件(假設它默認爲test)?

編輯:好的,時間來解釋更好的我的問題。 AcmeMessagingBundle說,我正在擰一個可重複使用的軟件包。現在我想對它進行功能測試。場景是/my/url的電話:

public function testReceiveApiRoute() 
{ 
    $client = $this->createClient(); 

    /** @var $route \Symfony\Component\Routing\Route */ 
    $route = $client->getContainer()->get('router') 
     ->getRouteCollection()->get('acme_messaging_receive'); 

    $this->assertNotNull($route); 
    $this->assertEquals('POST', $route->getRequirement('_method')); 
    $this->assertEquals('acme_messaging.controller.api:receive', 
     $route->getDefault('_controller')); 
} 

/** 
* @depends testReceiveApiRoute 
*/ 
public funcion testReceiveApiWorkflow() 
{ 
    $client = $this->createClient(); 

    // Make a POST request 
    $request = Request::create('/my/route', 'POST', array(
     'a' => 'value' 
    )); 

    // Request is convered in MyObject instance and that my.event is fired 
} 

有了這個測試,app/config_test.yml被加載(說「主配置文件」)。問題是:

不應該測試是「孤立」,意思是不使用主配置文件?如果我的包被另一個空的app/config_test.yml的人測試過會怎麼樣?測試將失敗...

該測試也將失敗,前綴路由。如果使用前綴導入來自AcmeMessagingBundle的routing.xml,則testReceiveApiWorkflow將會失敗!

+0

我無法理解你的問題。 「裝載捆綁」是什麼意思?你的意思是如何指定自定義Web測試客戶端?或者,也許你不確定哪些配置文件被加載?請給我一些澄清 – Cyprian

+0

@Cyprian我不知道哪個配置文件將被加載。我會假設「main」'config_test.yml'(在app/config中),然後加載'config_dev.yml'。我想知道的是:這是一個好習慣嗎?我如何使用'/ Tests/Fixture/config_test.yml'中的配置文件而不是'app/config/config_test.yml'? – gremo

+0

我認爲只要在Bundle \ Resources \ config \ services.yml(或xml)中添加名爲「test.client」的服務就足夠了。該文件由DI \ Extension文件加載,框架將以這種方式處理「test.client」名稱,它將使用您的服務,而不是默認的WebTestClient。但是,要加載其他配置文件,本文可能對您有用:http://symfony.com/doc/2.0/cookbook/bundles/extension.html。問候! – Cyprian

回答

3

使用WebTestCase將使用您自己的AppKernel與測試環境。

您可以添加新的ENV到您的應用程序,並在WebTestCase像這樣使用它:

$client = static::createClient(array('environment' => 'new_env')); 

一個更安全的做法是建立在你的包的測試sanboxed的應用程序。您可以使用JMSCommandBundle爲您生成。 你也可以使用這個技巧來創建sanboxed應用程序:https://github.com/schmittjoh/JMSPaymentCoreBundle/tree/master/Tests/Functional

+0

我已經更新了我的問題。最後的解決方案似乎是我的意思,但我應該進一步調查... – gremo