2015-05-19 40 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的答案