PHPUnit当前节点列表为空 [英] PHPUnit The current node list is empty

查看:121
本文介绍了PHPUnit当前节点列表为空的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am creating some functional tests to test my controller. I have 2 functions at the moment. 1 for loggin in and 1 for the entity life cycle.

两者都应该正常运行(我想).但是我收到以下错误:

both should run normally (I guess). Yet I am getting the following error:

当前节点列表为空

The current node list is empty

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

I tried removing all my code from the test class with no result. I also tried adding an empty method to see if it also happens there. And yes it does. That test also crashes.

所以我用谷歌搜索了一个解决方案.我尝试了几件事,例如:

So I googled for a solution. I tried a few things, like:

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

var_dump($ client-> getResponse() -> getContent()); (获取测试应在的页面)

var_dump($client->getResponse()->getContent()); (which gets the page where the test should be)

更改标题:

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

return $response;

还有其他一些事情.他们都没有工作.我似乎无法弄清楚问题出在哪里.

And some other things. None of them worked. I cannot seem to figure out what the problem is.

有人对此问题有任何建议吗?

Does anyone has any suggestions on this problem?

我的测试代码:

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

提前谢谢!

推荐答案

经过一番搜索,我在默认情况下,symfony2搜寻器猜测服务器的位置在"localhost".但是我的不在那里.所以我不得不告诉爬行者在哪里看.

The symfony2 crawler, by default, guesses that the location of server is at "localhost". Yet mine is not there. So I had to tell the crawler where to look.

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

This is the code I had to add 'HTTP_HOST' => 'my.server.location'

添加此代码可使代码看起来像这样:

Adding this code makes the code look like this:

$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.

So now when I get the url $crawler = $client->request('GET', '/admin/login');, The crawler will get the following url: http://my.server.location/admin/login.

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

Hope this helps anyone!

并记入 Matt 作为答案

And credits to Matt for the answer

这篇关于PHPUnit当前节点列表为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆