Download the PHP package ranierif/api-mocker without Composer
On this page you can find all versions of the php package ranierif/api-mocker. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Informations about the package 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
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
- PHP 8.2 or higher
- ext-json
- Composer
Installation
You can install the package via composer:
Usage
1. Creating a New API Mocker
First, create a new API mocker for your provider using the command:
For example, to create a Stripe API mocker:
This will create:
2. Adding Mock Responses
Create JSON files in the json directory for your mock responses. The structure should be:
Example JSON response file (tests/ApiMocker/Stripe/json/customers/success.json):
3. Using in Tests
Here's how to use the API mocker in your tests:
4. Creating Custom Responses
You can extend the base ApiMocker class to add custom methods for your specific needs:
Testing Different Scenarios
You can create different JSON files for various scenarios:
- Success responses
- Error responses
- Validation failures
- Special cases
Simply create a new JSON file with an appropriate name and use it in your tests:
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
License
The MIT License (MIT). Please see License File for more information.
All versions of api-mocker with dependencies
ext-json Version *