Download the PHP package lichtner/laravel-mock-api without Composer
On this page you can find all versions of the php package lichtner/laravel-mock-api. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download lichtner/laravel-mock-api
More information about lichtner/laravel-mock-api
Files in lichtner/laravel-mock-api
Package laravel-mock-api
Short Description Easy to use, but the powerful micro library for mocking external API
License MIT
Homepage https://github.com/lichtner/laravel-mock-api
Informations about the package laravel-mock-api
Laravel MockApi
Laravel MockAPI is a powerful yet lightweight library designed for mocking external APIs and web services.
Why?
Are you using external APIs and web services during development that are also undergoing development? Are they unstable, slow, occasionally returning incorrect results, or unexpectedly unavailable? Are they causing you headaches? Me too! That was the reason why I created MockApi.
After installation and setup, MockApi will save all requests from your external web services in the background, and when they are unavailable, return them just like real APIs.
Installation
Now you can publish config file and run the migrations with:
Setup
Mocking GET request
Make a simple class that wrap all your Http::get()
requests e.g.:
app/HttpMock.php
Then everywhere in your code replace:
with:
It is done! Now you can start mocking all your external APIs GET requests (and maybe colleagues who are developing them ;-)
Mocking POST, PUT, PATCH, DELETE requests
You can also mock mutation requests too, but it is not necessary if you don't need to.
E.g. for mocking POST requests add this in app/HttpMock.php
Then everywhere in your code replace:
with:
Mocking other HTTP methods are very similar. Check in example application file /app/HttpMock.php
Example application
You can check full usage of MockApi in example application
Security
By default, MockApi works only on the local
environment! It does not affect the other ones!
Usage
After you did the changes described in Setup, all HTTP requests will be saved in MockApi tables. But they won't be used.
Mock all resources
For returning mocked data add this in the .env
file:
You can set MOCK_API=true
immediately after installation. From that moment all external resources will return the last saved successful responses.
If you try to request resource which has not been saved yet, first a real request is made and saved in mock api tables, and then returned.
After your web services are back, you can change it to:
Mock management
You can manage your mocks in tables mock_api_url
and mock_api_url_history
.
Mock only some resources
By default, is in table set mock_api_url.mock = 1
. It means resource is mocked. If you want to mock only some of them, set the others to 0
.
Mock data from the past
By default, MockApi returns the last saved successful responses (status < 300). If some of the resources returns status 200 with some data, but they are incorrect, and yesterday's were fine, set in table mock_api_url.mock_before
datetime for all incorrect resources.
Mock error requests
Maybe you want to improve how your app deals with external API errors. You can mock error responses too. Check table mock_api_url_history
if there is saved such a response from the past. If not, add desired error response for the resource e.g.:
After setting in table mock_api_url.mock_status = 404
for that resource you will get this 404 response.
Mock mutation requests
Mutation requests like POST, PUT, PATCH, DELETE don't put anything in mock_api_url_history.data
field. Without any changes they returns same data, you send them. E.g. for:
response is:
Especially for POST request your real API probably add id
field. To simulate this behaviour you can update field data
for specific row:
Then same requests response will be:
You can add anything in mutation responses (e.g. uuid, etc.). These fields will be merged recursively with your json POST data.
Mock two requests with same url and method
In table mock_api_url
is set unique key for (method, url) so you are not able to mock two request with same method and url which is expected behavior. But for specific situation you want to. Maybe you want to mock two different articles with different titles with resource POST /articles
. To do this you can create special class for that purpose.
As you can see you can modify $url
parameter only for MockApi::init()
and MockApi::log()
functions, but not for real request Http::post()
. So two articles with different titles will be saved.
Config
For more information about configuration check config/mock-api.php
Testing
Changelog
For changelog check releases.
Credits
License
The MIT License (MIT). Please see License File for more information.
All versions of laravel-mock-api with dependencies
spatie/laravel-package-tools Version ^1.14.0
illuminate/contracts Version ^10.0