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.

FAQ

After the download, you have to make one include require_once('vendor/autoload.php');. After that you have to import the classes with use statements.

Example:
If you use only one package a project is not needed. But if you use more then one package, without a project it is not possible to import the classes with use statements.

In general, it is recommended to use always a project to download your libraries. In an application normally there is more than one library needed.
Some PHP packages are not free to download and because of that hosted in private repositories. In this case some credentials are needed to access such packages. Please use the auth.json textarea to insert credentials, if a package is coming from a private repository. You can look here for more information.

  • Some hosting areas are not accessible by a terminal or SSH. Then it is not possible to use Composer.
  • To use Composer is sometimes complicated. Especially for beginners.
  • Composer needs much resources. Sometimes they are not available on a simple webspace.
  • If you are using private repositories you don't need to share your credentials. You can set up everything on our site and then you provide a simple download link to your team member.
  • Simplify your Composer build process. Use our own command line tool to download the vendor folder as binary. This makes your build process faster and you don't need to expose your credentials for private repositories.
Please rate this library. Is it a good library?

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.

Latest Version on Packagist GitHub Tests Action Status GitHub Code Style Action Status Total Downloads

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

PHP Build Version
Package Version
Requires php Version ^8.1
spatie/laravel-package-tools Version ^1.14.0
illuminate/contracts Version ^10.0
Composer command for our command line client (download client) This client runs in each environment. You don't need a specific PHP version etc. The first 20 API calls are free. Standard composer command

The package lichtner/laravel-mock-api contains the following files

Loading the files please wait ....