Can I mock an interface implementation with PHPUnit? Can I mock an interface implementation with PHPUnit? php php

Can I mock an interface implementation with PHPUnit?


Instead of

$http_client = $this->getMockBuilder(Cpm\Http\IClient::class);

use

$http_client = $this->getMock(Cpm\Http\IClient::class);

or

$http_client = $this->getMockBuilder(Cpm\Http\IClient::class)->getMock();

Totally works!


The following works for me:

$myMockObj = $this->createMock(MyInterface::class);


$http_client = $this->getMockBuilder(Cpm\Http\IClient::class)                    ->setMockClassName('SomeClassName')                    ->getMock();

The setMockClassName() can be used to fix this in some circumstances.