简体   繁体   中英

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,

我发现清除实体管理器可以使我的测试正常工作...所以我只添加了self :: $ client-> getContainer()-> get('doctrine')-> getManager()-> clear();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM