views:

31

answers:

1

I'm working on a test in phpunit and I'm running into an issue. I have a public function on my class that I am trying to test. Depending on the parameters passed in to the method, a protected function also in my test class will be called one or two times. I currently have a test in place to check that the return data is correct, but I would also like to make sure the protected method is being called the correct number of times.

I know that a mock object will allow me to count the number of times a function is called, but it will also override the value returned by the protected function. I tried using a mock object with no "will" section, but it would just return null, not the actual value for the protected method.

ExampleClass

public function do_stuff($runTwice){
$results = do_cool_stuff();
  if($runTwice){
    $results = 2 * do_cool_stuff();
  }
  return $results;
}

protected function do_cool_stuff()
{
  return 2;
} 

In my test, I want to check whether do_cool_stuff() was called once or twice, but I still want the return values of both functions to be the same so I can test those as well in my unit test.

tl;dr I want to count the number of times a protected method in my test object is called (like you can do with a mock object) but I still want all the methods in my test method to return their normal values (not like a mock object).

+1  A: 

Try setting a global variable prior to utilizing the class.

$IAmDeclaredOutsideOfTheFucntion;

then use it to store the count and simply check it after your functions and classes have been called.

Michael Eakins