A
A
Alexey Verkhovtsev2017-12-13 13:38:06
PHP
Alexey Verkhovtsev, 2017-12-13 13:38:06

What is the difference between getMockBuilder and createMock in PHPUnit?

I understand it this way. CreateMock it copies all methods and properties from the original class, but does not call it. But getMockBuilder we can configure before use, for example, use the disableOriginalConstructor () method (I suspect that the constructor will not be copied). Is this true or not? But if this is the case, then I don’t see the point in getMockBuilder, because the methods are not executed anyway and it doesn’t really matter to us whether there is a constructor or not, or getMockBuilder has advantages that I don’t understand. Thanks in advance

Answer the question

In order to leave comments, you need to log in

1 answer(s)
L
Lexey Felde, 2017-12-18
@Tekill

I will refer to the official documentation :
Take a look at two ways to create identical mock objects:

$mock1 = $this->getMock(MyClass::class, [], [], '', true, false, true);
$mock2 = $this->getMockBuilder(MyClass::class)
    ->disableOriginalClone()
    ->getMock();

Using a mock builder is visually easier in some cases. I always prefer the second way because of the ease of reading.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question