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

SeebeckZachary's avatar

Laravel Dusk and HTTP::mock()

Hey there, so I'm starting to get into testing my Laravel apps, and I've really enjoyed using the mock functions when dealing with external APIs. Now the app that I'm currently dealing with is all external stuff, there's no local DB currently. Given the nature of this app, and how we're wanting to have this run through a GitHub action when making pull requests, I'm not really wanting to have a .env in git with API keys.

Long story short, is there any way to use HTTP::mock() with Laravel Dusk. I've tried a good handful of things, but nothing has seemed to work.

Thanks!

0 likes
6 replies
SeebeckZachary's avatar

Hmm. We have one file where all of the APIs are run through. Would there be a way to have everything in the 'controllers' folder re-route to a different API helper? Or would we have to register each controller manually?

Either way, this looks like a nice solution, and would definitely solve our problem. We could then not have the normal tests use mock either and just use a config to switch from using real APIs and Mocked ones.

bugsysha's avatar

Would there be a way to have everything in the 'controllers' folder re-route to a different API helper? Or would we have to register each controller manually?

Help me understand why would you do something like that?

What I've suggested will cover that for all of the app. No need to change controllers or re-route things.

SeebeckZachary's avatar

I apologize if I'm looking at this the wrong way. So 99% of our controllers will be implementing the API class. We would have one API class that goes and retrieves the desired information from the proper sources, while let's say we have an ApiTest class that would act like the previous class, but instead of using HTTP at all, it would just return the formatted data just like the API class would. Just never hit anything else.

So something like this I guess is what I'm meaning.

$this->app->when(SingleController::class)
          ->needs(App\Helpers\Api::class)
          ->give(function () {
              	if (config('app.is_testing') {
			return App\Helpers\ApiTesting::class;
		} else {
			return App\Helpers\Api::class;
		}
          });

So the way the documentation shows it, I could need to add every controller that uses the API class in. What i'm wondering is if there a way to have every controller in App\Http\Controllers have that needs/give peice. I/E

$this->app->when('App\Http\Controllers')
          ->needs(App\Helpers\Api::class)
          ->give(function () {
              	if (config('app.is_testing') {
			return App\Helpers\ApiTesting::class;
		} else {
			return App\Helpers\Api::class;
		}
          });
SeebeckZachary's avatar

So in theory there would be no need to change our controllers and everything would go along just like it has been. This would just be another Api file that it would look at if the app is in 'testing mode' persay.

bugsysha's avatar

Then just bind it to container without context based on your app.is_testing config. That way you will cover all of your code that is using that specific class.

Please or to participate in this conversation.