symfonyfunctional-testingliipfunctionaltestbundle

Functional test fails although content exists


In the functional test below, the assertion that the text 'Glenshire' exists fails even though the output of echo $client->getResponse()->getContent(); includes

<li>Glenshire...

The first two assertions are true. There is no redirect.

The test

class SearchTest extends WebTestCase
{

    public function setUp()
    {
        $classes = array(
            'Vol\VolBundle\DataFixtures\ORM\LoadFocusSkillData',
            'Vol\VolBundle\DataFixtures\ORM\LoadOpportunity',
        );
        $this->loadFixtures($classes);
    }

    public function testSearch()
    {
        $client = static::createClient();
        $crawler = $client->request('GET', '/search');
        $this->assertTrue($crawler->filter('html:contains("Focus")')->count() > 0);
        $this->assertTrue($crawler->filter('html:contains("Skill")')->count() > 0);
        $form = $crawler->selectButton('match_search[Submit]')->form();
        $form['match_search[focuses][0]'] = 1;
        $client->submit($form);
        echo $client->getResponse()->getContent();
        $this->assertTrue($crawler->filter('li:contains("Glenshire")')->count() > 0, 'Glenshire not found');
    }

The fixture (using LiipFunctionalTestBundle)

    public function load(ObjectManager $manager)
    {
        $manager->clear();
        $org = new Organization();
        $org->setOrganization('Glenshire Marmot Fund');
        $foc1 = $manager->getRepository("VolVolBundle:Focus")->find(1);
        $foc3 = $manager->getRepository("VolVolBundle:Focus")->find(3);
        $foc4 = $manager->getRepository("VolVolBundle:Focus")->find(4);
        $org->addFocus($foc1);
        $org->addFocus($foc3);
        $org->addFocus($foc4);
        $opp = new Opportunity();
        $opp->setName('Feeder');
        $opp->setDescription("Beyond recourse");
        $opp->setExpireDate(date_add(new \DateTime(), new \DateInterval('P1Y')));
        $opp->setOrganization($org);

        $manager->persist($opp);
        $manager->flush();
   }

Solution

  • Curiously, replacing the line $this->assertTrue($crawler->filter('li:contains("Glenshire")')->count() > 0, 'Glenshire not found');

    with

    $this->assertRegExp(
            '/Glenshire/', $client->getResponse()->getContent(), 'Glenshire not found'
    );
    

    provides a successful test!