John Smith
John Smith

Reputation: 6197

PHPUnit, mock constructor?

I want to mock the constructor like any other methods. I also added a willReturnCallback clause, which does not seem to work. I mean, it all works with methods, but not with the constructor.

$mock = $this->getMock ('MyClass', array(), array(), '', false);
$mock->expects($this->once())->method('__construct')->willReturnCallback(function() { echo 'outputt'; });

so mocking constructor has no effect.

Upvotes: 3

Views: 12041

Answers (1)

Fabian Schmengler
Fabian Schmengler

Reputation: 24551

Adding the relevant comments as community wiki answer because there will never be another answer than "not logically possibly":

The original constructor is called during mock construction (it just calls the baseclass' constructor). If you don't want that, you can also disable it. However, I wonder why you want to mock the constructor? The point is that unless you explicitly call it (which would be a code smell), creating the mock does the construction, so it's too late to expect a constructor call

Besides all limitations of mocking, a controllerconstructor can never return anything, so the mock doesn't mean anything at the moment

Upvotes: 2

Related Questions