PHPackages                             ranierif/api-mocker - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Testing &amp; Quality](/categories/testing)
4. /
5. ranierif/api-mocker

ActiveLibrary[Testing &amp; Quality](/categories/testing)

ranierif/api-mocker
===================

A simple package to mock API responses for testing

v1.0.1(10mo ago)485MITPHPPHP ^8.2CI passing

Since Aug 8Pushed 10mo agoCompare

[ Source](https://github.com/ranierif/api-mocker)[ Packagist](https://packagist.org/packages/ranierif/api-mocker)[ RSS](/packages/ranierif-api-mocker/feed)WikiDiscussions main Synced today

READMEChangelog (4)Dependencies (5)Versions (5)Used By (0)

[![Test](https://github.com/ranierif/api-mocker/actions/workflows/test.yml/badge.svg)](https://github.com/ranierif/api-mocker/actions/workflows/test.yml)[![Latest Stable Version](https://camo.githubusercontent.com/1ac8ac5bac13db9894a1b8d7eb0fdf6391224e57ea49c8fc60127262a17af426/687474703a2f2f706f7365722e707567782e6f72672f72616e69657269662f6170692d6d6f636b65722f76)](https://packagist.org/packages/ranierif/api-mocker)[![Total Downloads](https://camo.githubusercontent.com/c610dbcdfc6c8bb03e878a251c08fdfd75be7476970b7483f920a253c77161a4/687474703a2f2f706f7365722e707567782e6f72672f72616e69657269662f6170692d6d6f636b65722f646f776e6c6f616473)](https://packagist.org/packages/ranierif/api-mocker)[![Latest Unstable Version](https://camo.githubusercontent.com/21f64f59fa5a59473d50cd5cf5c31086980a3a59ecd9439ebd0eb4516d8fec66/687474703a2f2f706f7365722e707567782e6f72672f72616e69657269662f6170692d6d6f636b65722f762f756e737461626c65)](https://packagist.org/packages/ranierif/api-mocker)[![License](https://camo.githubusercontent.com/abca620c61e567d13b778eb96224ee5be45bd5728a468b28f0bb3c09b8c1c94a/687474703a2f2f706f7365722e707567782e6f72672f72616e69657269662f6170692d6d6f636b65722f6c6963656e7365)](https://packagist.org/packages/ranierif/api-mocker)

---

API Mocker
==========

[](#api-mocker)

A simple and flexible package to mock API responses for testing in PHP applications. This package helps you create and manage mock responses for your API integrations, making your tests more reliable and faster.

Introduction
------------

[](#introduction)

API Mocker provides an easy way to mock external API responses in your PHP tests. Instead of making real HTTP requests during testing, you can use predefined JSON responses, making your tests:

- Faster (no actual HTTP requests)
- More reliable (no dependency on external services)
- Deterministic (you control the responses)
- Offline-capable (no internet connection required)

Requirements
------------

[](#requirements)

- PHP 8.2 or higher
- ext-json
- Composer

Installation
------------

[](#installation)

You can install the package via composer:

```
composer require --dev ranierif/api-mocker
```

Usage
-----

[](#usage)

### 1. Creating a New API Mocker

[](#1-creating-a-new-api-mocker)

First, create a new API mocker for your provider using the command:

```
vendor/bin/make-api-mocker YourProviderName
```

For example, to create a Stripe API mocker:

```
vendor/bin/make-api-mocker Stripe
```

This will create:

```
tests/
└── ApiMocker/
    └── Stripe/
        ├── StripeApiMocker.php
        └── json/
```

### 2. Adding Mock Responses

[](#2-adding-mock-responses)

Create JSON files in the `json` directory for your mock responses. The structure should be:

```
tests/ApiMocker/Stripe/json/
├── customers/
│   ├── success.json
│   └── error.json
└── payments/
    ├── success.json
    └── error.json
```

Example JSON response file (`tests/ApiMocker/Stripe/json/customers/success.json`):

```
{
  "id": "cus_123456",
  "object": "customer",
  "email": "test@example.com",
  "name": "Test Customer"
}
```

### 3. Using in Tests

[](#3-using-in-tests)

Here's how to use the API mocker in your tests:

```
