PHPUnit 当前节点列表为空

PHPUnit The current node list is empty

我正在创建一些功能测试来测试我的控制器。我现在有两个功能。 1 个用于登录,1 个用于实体生命周期。

两者都应该 运行 通常(我猜)。但是我收到以下错误:

The current node list is empty

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

所以我在谷歌上搜索了解决方案。我尝试了一些事情,例如:

()

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;

还有其他一些事情。 None 他们成功了。我似乎无法弄清楚问题是什么。

有人对这个问题有什么建议吗?

我的测试代码:

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

提前致谢!

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

默认情况下,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);

所以现在当我获取 url $crawler = $client->request('GET', '/admin/login'); 时,爬虫将获取以下内容 url:http://my.server.location/admin/login.

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

并将答案归功于 Matt