$mock = $this->getMockBuilder(MyClass::class) ->getMock(); $mock->expects($this->any()) ->method('_call') ->with('myMethod', ['arg1', 'arg2']) ->will($this->returnValue('mocked response')); $this->assertEquals('mocked response', $mock->myMethod('arg1', 'arg2'));
use PHPUnit\Framework\TestCase; use PHPUnit\Framework\MockObject\MockObject; class MockObjectTest extends TestCase { public function testCallMethod() { $mock = $this->createMock(MyClass::class); $mock->expects($this->any()) ->method('_call') ->with('myMethod', $this->logicalAnd( $this->greaterThan(10), $this->lessThan(20) )) ->will($this->returnValue('mocked response')); $this->assertEquals('mocked response', $mock->myMethod(15)); $this->assertNotEquals('mocked response', $mock->myMethod(25)); } }In this example, we use PHPUnit's built-in mocking functionality to create a mock object of the MyClass class. We define the mock object's behavior for the myMethod method using the _call method. Whenever myMethod is called with an argument between 10 and 20, the mock will return the string 'mocked response'. We then use PHPUnit's built-in assertion functionality to ensure that calling myMethod on the mock object with a value of 15 returns the expected response, and that calling myMethod with a value of 25 does not return the mocked response. Package Library: PHPUnit_Framework_MockObject