client = $this->getMockBuilder(HttpClientInterface::class) ->disableOriginalConstructor() ->getMock(); $response = $this->getMockBuilder(ResponseInterface::class) ->disableOriginalConstructor() ->getMock(); $response->method('getContent')->willReturn('{"a": 1}'); $this->client ->expects($this->once()) ->method('request') ->with("GET", "my_url.org") ->willReturn($response); $this->apiRequestService = new ApiRequestService($this->client); } public function testGetJsonContent() { $this->assertEquals( $this->apiRequestService->getJsonContent('my_url.org'), ['a' => 1] ); } public function testGetContent() { $this->assertEquals( $this->apiRequestService->getContent('my_url.org'), '{"a": 1}' ); } }