Search code examples
phpsymfonyphpunitfunctional-testing

Symfony 2 Functional test


I encounter an issue which doesn't make much logic to me in one of my functional tests.

The test is quite simple, it just requests for the homepage, and searches for an element with a certain class in it.

The homepage is for a blog that displays a list of articles. And each article can have multiple pages (One-To-Many), each with it's own featured image.

On homepage (in twig) I do $article->getPages()->first()->getImageUrl().

If I use the same fixtures and I access the page from the browser everything is displayed as it should, but when I run the test it throws an exception "Could not access getImageUrl on unknown object", which makes me think it's not pulling the article pages from the DB.

To make this even stranger, if I query myself the db for the articles directly in the test and iterate over the articles they all work ($article->getPages()->first()->getImageUrl()). Only when using the self::$client->request() call it's not working.

I doble checked the fixtures, I even added an explicit innerJoin() on the query builder, and manually checked that the database has the right data.

Moreover, I have other functional tests that load their own fixtures and they all work, except this one.

This is the code of my test below. It fails both when run solely and when executed with the entire suite of tests.

public function testSocialBoxPositionWithFeatured()
{
    self::runCommand('h4cc_alice_fixtures:load:files --drop src/SNN/AdminBundle/Fixtures/Nelmio.yml');

    $crawler = self::$client->request('GET', '/'); //go to homepage

    $classes = $crawler
            ->filter('.postsContainer > .col-md-4')
            ->eq(5)
            ->children()
            ->first()
            ->attr("class");

    $this->assertContains("socialBox", $classes);
}

Thanks in advance,


Solution

  • I found out that clearing the entity manager it makes my test working... so I simply added self::$client->getContainer()->get('doctrine')->getManager()->clear();