PHPUnit The current node list is empty(PHPUnit 当前节点列表为空)
问题描述
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()); (which gets the page where the test should be)
Change the header:
$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?
My test code:
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);
}
Thanks in advance!
After some searching I found the answer here on stackoverflow.
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.
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);
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!
And credits to Matt for the answer
这篇关于PHPUnit 当前节点列表为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:PHPUnit 当前节点列表为空


基础教程推荐
- 通过 PHP SoapClient 请求发送原始 XML 2021-01-01
- Libpuzzle 索引数百万张图片? 2022-01-01
- 超薄框架REST服务两次获得输出 2022-01-01
- 在 PHP 中强制下载文件 - 在 Joomla 框架内 2022-01-01
- 在多维数组中查找最大值 2021-01-01
- WooCommerce 中选定产品类别的自定义产品价格后缀 2021-01-01
- mysqli_insert_id 是否有可能在高流量应用程序中返回 2021-01-01
- XAMPP 服务器不加载 CSS 文件 2022-01-01
- 在 Woocommerce 中根据运输方式和付款方式添加费用 2021-01-01
- 如何在 PHP 中的请求之间持久化对象 2022-01-01