Laravel Pro Tips

Laravel Pro Tips

Share this post

Laravel Pro Tips
Laravel Pro Tips
Laravel Testing: How to Test External API Calls

Laravel Testing: How to Test External API Calls

One frequent question in Laravel testing is, "How do I test interactions with external APIs?" In this guide, we'll look at three methods for doing just that.

Laravel Pro Tips's avatar
Laravel Pro Tips
Oct 27, 2023
∙ Paid

Share this post

Laravel Pro Tips
Laravel Pro Tips
Laravel Testing: How to Test External API Calls
Share

black and silver laptop computer
Photo by Fahim Muntashir on Unsplash

This article summarizes a section from the course: "Advanced Laravel Testing."

We'll cover:

  1. Simulating HTTP requests

  2. Mimicking Classes

  3. Working with External Test Environments

Imagine you have a feature that fetches data based on IP addresses using Laravel's HTTP Client. Here's an example:

private static function fetchIPDetails(string $ip): array
{
    $apiKey = config('location.ipdata.key', '');
    $apiEndpoint = "https://api.ipdata.co/{$ip}?api-key=".$apiKey;

    return Http::get($apiEndpoint)->throw()->json();
}

This post is for paid subscribers

Already a paid subscriber? Sign in
© 2025 Laravel Pro Tips
Privacy ∙ Terms ∙ Collection notice
Start writingGet the app
Substack is the home for great culture

Share