public function testSumArray() { $array = [1, 2, 3]; $mock = $this->getMockBuilder('stdClass') ->setMethods(['array_sum']) ->getMock(); $mock->expects($this->once()) ->method('array_sum') ->willReturnCallback(function ($array) { return array_sum($array); }); $result = $mock->array_sum($array); $this->assertEquals(6, $result); }
public function testGetUserEmail() { $userId = 1; $email = 'test@example.com'; $dbMock = $this->getMockBuilder('stdClass') ->setMethods(['query']) ->getMock(); $dbMock->expects($this->once()) ->method('query') ->willReturnCallback(function ($query) use ($userId, $email) { if (strpos($query, 'SELECT email') !== false && strpos($query, "WHERE id=$userId") !== false) { return [$email]; } return []; }); $user = new User($dbMock); $result = $user->getEmail($userId); $this->assertEquals($email, $result); }This code utilizes the PHPUnit_Framework_TestCase returnCallback method in conjunction with the PHPUnit testing framework.