当前节点列表为空

当前节点列表为空

本文介绍了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:

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 当前节点列表为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-24 16:30