2015-05-19 41 views
1

我正在創建一些功能測試來測試我的控制器。我目前有2個功能。 1用於登錄,1用於實體生命週期。PHPUnit當前節點列表爲空

都應該正常運行(我猜)。然而,我收到以下錯誤:

The current node list is empty

我試圖從測試類中刪除所有我沒有結果的代碼。我也嘗試添加一個空的方法,看看它是否也發生在那裏。是的,它的確如此。該測試也崩潰。

所以我搜索了一個解決方案。我嘗試了一些東西,比如:

$client = static::createClient(array(), array('HTTP_HOST' => 'symfony.dev'));

var_dump($client->getResponse()->getContent());(其獲得的頁面,測試應該是)

Change the header

$response = $this->render('CMSBundle:Front:test.html.twig',); 
$response->headers->set('Content-Type', 'text/html');  

return $response; 

和其他一些東西。他們都沒有工作。我似乎無法弄清楚問題所在。

有沒有人對這個問題有任何建議?

我的測試代碼:

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

    $crawler = $client->request('GET', '/admin/login'); 
    $this->assertEquals(200, $client->getResponse()->getStatusCode(), "Unexpected HTTP status code for GET /admin/login"); 

    // Fill in the form and submit it 
    $form = $crawler->selectButton('Inloggen')->form(array(
     '_username' => 'erik', 
     '_password' => 'erik' 
    )); 
    $client->submit($form); 
} 

提前感謝!

回答

3

經過一番搜索後,我在stackoverflow找到了答案。

symfony2履帶,默認情況下,猜測服務器的位置是「本地主機」。但我的不在那裏。所以我不得不告訴爬蟲在哪裏看。

這是我不得不添加'HTTP_HOST' => 'my.server.location'

添加該代碼的代碼,使代碼看起來是這樣的:

$this->client = static::createClient( 
    array(), 
    array(
     'HTTP_HOST' => 'my.server.location', //dependent on server   
    )); 
$this->client->followRedirects(true); 

所以現在當我得到的URL $crawler = $client->request('GET', '/admin/login');,履帶將獲得以下網址:http://my.server.location/admin/login

希望這有助於任何人!

和信貸以Matt的答案