Search code examples
symfonyphpunitfunctional-testing

Symfony2: functional test of record edit fails


I'm on the steep part of the learning curve for functional tests. On a simple form that changes an agency's status (Yes->No, No->Yes), the record edit fails. The test code is:

$crawler = $client->request('GET', '/agency/manage');
//read agency table
$nodeValues = $crawler->filter('td')->each(function ($node, $i) {
    return $node->nodeValue;
});

//$initial = number of Active=Yes agencies
$initial = 0;
foreach ($nodeValues as $node) {
    if ($node == 'Yes') {
        $initial ++;
    }
}
//edit agency to Active=No
$crawler = $client->request('GET', '/agency/1/edit');
$form = $crawler->selectButton('Edit')->form();
$form['agency[active]'] = 'No';
$crawler = $client->submit($form);

$crawler = $client->request('GET', '/agency/manage');

$nodeValues = $crawler->filter('td')->each(function ($node, $i) {
    return $node->nodeValue;
});

//$final = number of Active=Yes agencies
$final = 0;
foreach ($nodeValues as $node) {
    if ($node == 'Yes') {
        $final ++;
    }
}

$this->assertTrue($initial > $final);

For my test case, both initial and final values are 2. I've built a test that properly adds an agency so I know I'm not totally off track. (I also imagine there are easier ways to count the number of times Yes appears in a table.)

Thanks.


Solution

  • The answer: fix the routing of the edit action! Test now is passed.