class ExampleClass { public function exampleMethod() { return true; } } class ExampleTestClass extends PHPUnit_Framework_TestCase { public function testExample() { $exampleMock = $this->getMock('ExampleClass'); $exampleMock->expects($this->once())->method('exampleMethod')->will($this->returnValue(false)); $this->assertFalse($exampleMock->exampleMethod()); } }
interface ExampleInterface { public function exampleMethod(); } class ExampleTestClass extends PHPUnit_Framework_TestCase { public function testExample() { $exampleMock = $this->getMock('ExampleInterface'); $exampleMock->expects($this->once())->method('exampleMethod')->will($this->returnValue(false)); $this->assertFalse($exampleMock->exampleMethod()); } }In this example, we are mocking an interface instead of a class. We use the same `getMock` method to create a new instance of the interface, and then use `expects` and `will` to define the behavior of the `exampleMethod`. Both of these examples use the `PHPUnit_Framework_TestCase` class, which is part of the PHPUnit package.