PHPUnit Mock several different methods
I am new to Unit test and I am experimenting with PHPUnit framework.
I have a function that calls two other functions:
class Dummy{
public function dummyFunction(){
$this->anotherDummyFunction();
.....
$this->yetAnotherDummyFunction();
}
public function anotherDummyFunction(){
.....
}
public function yetAnotherDummyFunction(){
.....
}
}
I want to check that two functions are called when dummyFunction () is called.
Here's the test class:
class TestDummyClass {
public function testDummyFunction(){
$dummyClassMock = $this->getMockBuilder('Dummy')
->setMethods( array( 'anotherDummyFunction','yetAnotherDummyFunction' ) )
->getMock();
$dummyClassMock->expects($this->once())
->method( 'anotherDummyFunction' );
$dummyClassMock->expects($this->once())
->method( 'yetAnotherDummyFunction' );
$dummyClassMock->dummyFunction();
}
}
Now I noticed that if I mock the Dummy class in this way, the result is
The wait error for the method name is anotherDummyFunction when called 1 time (s). The method had to be called 1 time, in fact it was called 0 times.
But if I set the Mock Object this way
$dummyClassMock = $this->getMockBuilder('Dummy')
->setMethods( array( 'anotherDummyFunction' ) )
->getMock();
test pass. Finally, if I set the mock using setMethods (null), the test fails again
It seems that I can pass an array with only one element, the method I want to check if called. But I saw here: https://jtreminio.com/2013/03/unit-testing-tutorial-part-5-mock-methods-and-overriding-constructors/ that setMethods is used to inject the return value of the passed method, so have this would not have an impact on the call itself unless I put dummyFunction in setMethods (in which case the function will return null without calling the other two methods, and thus yes, the test should fail)
Did I do something wrong? I've seen code snippets where there are multiple methods in the setMethods () method ... Why does the test fail if I put these methods in setMethods?
thank
source to share