clientService = $this->createMock(IClientService::class); $this->client = $this->createMock(IClient::class); $this->clientService->expects($this->any()) ->method('newClient') ->willReturn($this->client); $this->client->expects($this->any()) ->method('get') ->willReturnCallback(function ($url) { if (!isset($this->expectedGetRequests[$url])) { throw new \Exception('unexpected request: ' . $url); } $result = $this->expectedGetRequests[$url]; if ($result instanceof \Exception) { throw $result; } else { $response = $this->createMock(IResponse::class); $response->expects($this->any()) ->method('getBody') ->willReturn($result); return $response; } }); $this->client->expects($this->any()) ->method('post') ->willReturnCallback(function ($url) { if (!isset($this->expectedPostRequests[$url])) { throw new \Exception('unexpected request: ' . $url); } $result = $this->expectedPostRequests[$url]; if ($result instanceof \Exception) { throw $result; } else { $response = $this->createMock(IResponse::class); $response->expects($this->any()) ->method('getBody') ->willReturn($result); return $response; } }); } /** * @param string $url * @param string|\Exception $result */ protected function expectGetRequest($url, $result) { $this->expectedGetRequests[$url] = $result; } /** * @param string $url * @param string|\Exception $result */ protected function expectPostRequest($url, $result) { $this->expectedPostRequests[$url] = $result; } /** * @return IClientService|\PHPUnit\Framework\MockObject\MockObject */ protected function getClientService() { return $this->clientService; } }