PHPUnit当前节点列表为空

时间:2015-05-19 14:34:03

标签: php symfony testing phpunit

我正在创建一些功能测试来测试我的控制器。我目前有2个功能。 1表示登录,1表示实体生命周期。

两者都应该正常运行(我猜)。然而,我收到以下错误:

  

当前节点列表为空

我尝试从测试类中删除所有代码但没有结果。我还尝试添加一个空方法,看看它是否也发生在那里。是的,确实如此。那个测试也崩溃了。

所以我用Google搜索解决方案。我试了几件事,比如:

$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);
}

提前致谢!

1 个答案:

答案 0 :(得分:3)

经过一番搜索,我在stackoverflow找到了答案。

默认情况下,symfony2抓取工具会猜测服务器的位置位于" localhost"。然而我的不在那里。所以我不得不告诉爬虫在哪里看。

这是我必须添加'HTTP_HOST' => 'my.server.location'

的代码

添加此代码会使代码如下所示:

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

现在,当我收到网址$crawler = $client->request('GET', '/admin/login');时,抓取工具会获得以下网址:http://my.server.location/admin/login

希望这对任何人都有帮助!

并向Matt求助