What is the difference between createMock and getMockBuilder in phpUnit?

For the love of my life, I can't understand the difference between createMock($type) and getMockBuilder($type)

I am looking through the original documentation, and there is only one insert that I did not understand.

... you can use the getMockBuilder ($ type) method to set up a test dual generation using a free interface.

If you can provide me with an example, I would be grateful. Thank you

+5
source share
2 answers

From the manual https://phpunit.de/manual/current/en/test-doubles.html

The createMock ($ type) and getMockBuilder ($ type) methods provided by PHPUnit can be used in the test to automatically create an object that can act as a test counterpart for the specified source type (interface or class name). This test double object can be used in every context where the expected or required object of the original type is expected.

The createMock ($ type) method immediately returns a test double object for the specified type (interface or class). The creation of this double test is performed using the default settings (__construct () and the __clone () methods of the source class are not executed, and the arguments passed to the test double method will not be cloned.

If these defaults are not what you need, you can use the getMockBuilder ($ type) method to configure the test dual-generation using a free interface.

They already have many responses to stack overflows, which speaks of freely distributed interfaces.

+5
source

createMock ($ type) uses getMockBuilder() internally:

 protected function createMock($originalClassName) { return $this->getMockBuilder($originalClassName) ->disableOriginalConstructor() ->disableOriginalClone() ->disableArgumentCloning() ->disallowMockingUnknownTypes() ->getMock(); } 

Thus, the createMock() method will return to you a layout constructed using the general default settings.

But with getMockBuilder ($ type) you can create a layout with your own requirements.

+10
source

All Articles