Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

carobiscuit's avatar

testing HTTP facade in Laravel7

So after a couple days of fighting to convert a ton of Guzzle Client mock tests to use the new Laravel Facade we've implemented, I am now running into cURL errors when I try to run our tests package.

The structure we're implementing is a base abstract class that wraps the new HTTP facade and outlines the various verbs, GET, POST, etc. Child services then extend this and can just call the verb and pass params. I can't for the life of me find an easy way to test this. Initially I would set up an HTTP::fake() --

Http::fake([ '*' => Http::response( self::RESPONSE, 200 ) ]);

and then call my service method

$this->serviceClass->getData(self::TEST_ID); $this->assertEquals($response, self::TEST_ID);

In this case, the "getData" method may do some things but ends up just calling "httpGet" from the base class. While these appear to work locally they are actually trying to hit the route once they get back to the abstract class. I have to be missing something re: the fake() http instance. Perhaps I need to also create a mock abstract class method?

0 likes
3 replies
bobbybouwmann's avatar

Mmh your code looks correct to me. The only thing I can think of is that you're firing multiple requests and therefore the real request is performed.

In general, it is better to perform the Http::fake with the correct URL, to make sure you're actually calling that URL.

carobiscuit's avatar

Yeah I'm actually doing it with this guy:

Http::fake([ self::ROUTE.'*' => Http::response( self::RESPONSE, 200 ) ]);

I think you're right that it initially uses this fake but since the service invokes the parent's method which also calls HTTP its overriding my fake instance. Is there a smart way to prevent this? I confess to not having tried to test inherited methods before.

bobbybouwmann's avatar

Not sure what else it can be, other than defining all the specific HTTP calls in your test.

Please or to participate in this conversation.