Skip to content

Instantly share code, notes, and snippets.

@samsonasik
Created June 26, 2015 09:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save samsonasik/3a6ed4f9ebda9881f563 to your computer and use it in GitHub Desktop.
Save samsonasik/3a6ed4f9ebda9881f563 to your computer and use it in GitHub Desktop.
try catch test internal method
class Foo
{
    private function bar($languageId)
    {
        // like http call...
        
        // some heavy task
    }
    
    public function BarCall($languageId)
    {
         if (empty($languageId)) {
              throw new \Exception('must not be empty');
         }
         
         try {
              $this->bar($languageId);
         } catch (\Exception $e) {
              throw new \Exception('cannot connect');
         }
    }
}

Question: how to appropriatery test BarCall() method ?

@SenseException
Copy link

It looks like you can provoke an exception with $languageId. A non empty but invalid one should do the trick. If there are multiple possible exceptions in bar(), then it's going to be hard to test them seperatly, because BarCall() catches them and throws its own exception. So all you can test with the annotation @ExpectedException is the same one for every thrown exception in bar(). I don't know any way to test an embedded previous exception (since PHP 5.3) with phpunit.

How can you tell that BarCall()/bar() worked? What happens after everything was correct?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment