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()); (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!

1 Answer 1

4

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.