PHPUnit test How many times a function is called

I am working on a test in phpunit and I have a problem. I have a public function in my class that I am trying to test. Depending on the parameters passed to the method, the protected function will also be called once or twice in my test class. I currently have a test to verify that the returned data is correct, but I would also like to make sure that the protected method is 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 without a "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;
} 

, do_cool_stuff() , , , unit test.

TL;DR , (, ), , ( ).

+3
2

.

$IAmDeclaredOutsideOfTheFunction;

, .

+1

, . , :

class ExampleClass {
    public function do_stuff($runTwice) {
        $results = $this->do_cool_stuff();
        if ($runTwice) {
            $results = 2 * $this->do_cool_stuff();
        }
        return $results;
    }

    protected function do_cool_stuff() {
        return 2;
    }
}

class TestableExampleClass extends ExampleClass {
    /** Stores how many times the do_cool_stuff method is called */
    protected $callCount;

    function __construct() {
        $this->callCount = 0;
    }

    function getCallCount() {
        return $this->callCount;
    }

    /** Increment the call counter, and then use the base class functionality */
    protected function do_cool_stuff() {
        $this->callCount++;
        return parent::do_cool_stuff();
    }
}


class ExampleClassTest extends PHPUnit_Framework_TestCase {

    public function test_do_stuff() {
        $example = new ExampleClass();
        $this->assertEquals(2, $example->do_stuff(false));
        $this->assertEquals(4, $example->do_stuff(true));
    }

    public function test_do_cool_stuff_is_called_correctly() {
        // Try it out the first way
        $firstExample = new TestableExampleClass();
        $this->assertEquals(0, $firstExample->getCallCount());
        $firstExample->do_stuff(false);
        $this->assertEquals(1, $firstExample->getCallCount());

        // Now test the other code path
        $secondExample = new TestableExampleClass();
        $this->assertEquals(0, $secondExample->getCallCount());
        $secondExample->do_stuff(true);
        $this->assertEquals(2, $secondExample->getCallCount());
    }
}

, , , . . , , ? , , , do_cool_stuff :

class ExampleClass {
    public function do_stuff($runTwice) {
       if ($runTwice) {
          return $this->do_cool_stuff_twice();
       } else {
          return $this->do_cool_stuff_once();
       }
    }
    //...
 }
+4

Source: https://habr.com/ru/post/1766890/


All Articles