leet
leet

Reputation: 507

Symfony server ignores catch block in unittest

At the moment I program some test with phpunit for my symfony project.

I want to test if the server handles a special problem correctly. The server trys to add a mail to a group. But the group does not exist. So the ldap_mod_add throws an exception. In the code I catch the exception and create the group so that the mail can be added to it.

If I try the code without phpunit it works fine. But with phpunit the server ignores the catch block.

Heres the function

//Add
try
{
    ldap_mod_add($this->ldapCon,"mail=$forward,$ldaptree",$forward_info);
}
catch (ContextErrorException $e)
{
    //Check if the forward exist
    $forwardGroup = $this->getForwardForMail($forward);
    if($forwardGroup == [])
    {
        $this->logger->addAlert("Forward $forward does not exist");
        $this->session->getFlashBag()->add("notice","We added the group $forward, because it did not exist.");
        //Create forward and add the first mail
        $this->createForward($forward,$mail);
        return;
    }
    throw new AllreadyInGroupException("User already in forward $forward");
}

I disabled the convertWarningsToExceptions option so that the test will go on even if php gives a warning because of the not existing group.

So why does the server ignores the catch block with phpunit?

Thank you for your help

Upvotes: 1

Views: 114

Answers (1)

Shira
Shira

Reputation: 6570

Check the actual class of the exception being thrown.

Symfony throws ContextErrorException only if its ErrorHandler is active, which might not be the case in unit tests run by PHPUnit.

Try catching \ErrorException instead.

I'd keep convertWarningsToExceptions enabled for consistent behavior. Without it your forwarding logic won't work in tests (the catch block will not get run if only a plain PHP warning is emitted).

Upvotes: 1

Related Questions