Skip to content

Commit c3e5c0d

Browse files
committed
feat: update README.md
1 parent 67db77b commit c3e5c0d

File tree

3 files changed

+97
-38
lines changed

3 files changed

+97
-38
lines changed

.github/ISSUE_TEMPLATE/config.yml

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,3 @@ contact_links:
66
- name: Request a feature
77
url: https://github.com/lichtner/laravel-mock-api/discussions/new?category=ideas
88
about: Share ideas for new features
9-
- name: Report a security issue
10-
url: https://github.com/lichtner/laravel-mock-api/security/policy
11-
about: Learn how to notify us for sensitive bugs

README.md

Lines changed: 93 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,17 @@
1-
# Easy and smart mocking external APIs to simplify development
1+
# Laravel MockApi
2+
3+
*Easy to use, but the powerful micro library for mocking external API*
24

35
[![Latest Version on Packagist](https://img.shields.io/packagist/v/lichtner/laravel-mock-api.svg?style=flat-square)](https://packagist.org/packages/lichtner/laravel-mock-api)
46
[![GitHub Tests Action Status](https://img.shields.io/github/actions/workflow/status/lichtner/laravel-mock-api/run-tests.yml?branch=main&label=tests&style=flat-square)](https://github.com/lichtner/laravel-mock-api/actions?query=workflow%3Arun-tests+branch%3Amain)
57
[![GitHub Code Style Action Status](https://img.shields.io/github/actions/workflow/status/lichtner/laravel-mock-api/fix-php-code-style-issues.yml?branch=main&label=code%20style&style=flat-square)](https://github.com/lichtner/laravel-mock-api/actions?query=workflow%3A"Fix+PHP+code+style+issues"+branch%3Amain)
68
[![Total Downloads](https://img.shields.io/packagist/dt/lichtner/laravel-mock-api.svg?style=flat-square)](https://packagist.org/packages/lichtner/laravel-mock-api)
79

8-
This is where your description should go. Limit it to a paragraph or two. Consider adding a small example.
9-
10-
## Support us
11-
12-
[<img src="https://github-ads.s3.eu-central-1.amazonaws.com/laravel-mock-api.jpg?t=1" width="419px" />](https://spatie.be/github-ad-click/laravel-mock-api)
10+
## Why?
1311

14-
We invest a lot of resources into creating [best in class open source packages](https://spatie.be/open-source). You can support us by [buying one of our paid products](https://spatie.be/open-source/support-us).
15-
16-
We highly appreciate you sending us a postcard from your hometown, mentioning which of our package(s) you are using. You'll find our address on [our contact page](https://spatie.be/about-us). We publish all received postcards on [our virtual postcard wall](https://spatie.be/open-source/postcards).
12+
Are you using external APIs and web services during development that are also in the development process? Are they unstable, slow, sometimes returning incorrect results, or unexpectedly unavailable? Are they causing you difficulties? Me too! That was the reason why I created MockApi.
1713

14+
MockApi solves these problems for you. It saves all GET requests from your external web services, and when they are unavailable, it returns saved data as if it were a real API. You no longer have to worry about it.
1815
## Installation
1916

2017
You can install the package via composer:
@@ -23,39 +20,110 @@ You can install the package via composer:
2320
composer require lichtner/laravel-mock-api
2421
```
2522

26-
You can publish and run the migrations with:
23+
Now you can publish config and run the migrations with:
2724

2825
```bash
29-
php artisan vendor:publish --tag="laravel-mock-api-migrations"
30-
php artisan migrate
26+
php artisan mock-api:install
3127
```
3228

33-
You can publish the config file with:
29+
## Setup
3430

35-
```bash
36-
php artisan vendor:publish --tag="laravel-mock-api-config"
37-
```
31+
Make simple class which wrap all your `Http::get()` request. Create file e.g.:
3832

39-
This is the contents of the published config file:
33+
`app/HttpMock.php`
4034

4135
```php
42-
return [
43-
];
36+
<?php
37+
38+
namespace App;
39+
40+
use Illuminate\Http\Client\Response;
41+
use Illuminate\Support\Facades\Http;
42+
use Lichtner\MockApi\MockApi;
43+
44+
class HttpMock
45+
{
46+
public static function get($url): Response
47+
{
48+
MockApi::use($url);
49+
50+
$response = Http::get($url);
51+
52+
MockApi::log($url, $response);
53+
54+
return $response;
55+
}
56+
}
4457
```
4558

46-
Optionally, you can publish the views using
59+
Now everywhere in your code where you call:
4760

48-
```bash
49-
php artisan vendor:publish --tag="laravel-mock-api-views"
61+
```php
62+
Http::get($url);
5063
```
5164

52-
## Usage
65+
replace it with:
5366

5467
```php
55-
$mockApi = new Lichtner\MockApi();
56-
echo $mockApi->echoPhrase('Hello, Lichtner!');
68+
HttpMock::get($url);
5769
```
5870

71+
It is done! Now you can start mocking all your external API (and maybe colleagues who are developing them ;-)
72+
73+
## Security
74+
75+
**Don't worry MockApi is used only on `local` environment! It has no effect on other ones!**
76+
77+
## Mocking management
78+
79+
After you did the changes described in [Setup](#setup) all HTTP GET request will be saved in MockApi tables. But they will not be used.
80+
81+
You can manage mocking via environment variables in `.env` file.
82+
83+
### Mock all webservices
84+
85+
When something went wrong and your webservices become unavailable, put in `.env`:
86+
87+
```yaml
88+
MOCK_API=true
89+
```
90+
from that moment all webservices start returning last saved responses.
91+
92+
After your webservices are back, and you want to get real responses just change it back:
93+
94+
```yaml
95+
MOCK_API=false
96+
```
97+
98+
### Mock only some of webservices
99+
100+
In db table `mock_api_url` all rows have in column `use` default value `1`. If you want to mock only 1 or more webservices, set other to `0`.
101+
102+
### Mock data from the past
103+
104+
By default, MockApi returns last saved responses. But maybe all today's responses are messed up because on the testing server of your webservice was installed code with some bug. But you know yesterday result were fine. So use this:
105+
106+
```yaml
107+
MOCK_API_DATETIME_IS_LESS_THAN="YYYY-MM-DD HH:mm:ss"
108+
```
109+
you get results that are less than given datetime.
110+
111+
### Mock data with different status
112+
113+
Normally you don't need to change that. By default, MockApi returns responses with http code less than 300 (200-299). E.g. if you want to return also redirects use this:
114+
115+
```yaml
116+
MOCK_API_STATUS_IS_LESS_THAN=310
117+
```
118+
119+
For more information about configuration check [config/mock-api.php](https://github.com/lichtner/laravel-mock-api/blob/main/config/mock-api.php)
120+
121+
## FAQ
122+
123+
*Why does MockApi manage only GET requests?*
124+
125+
Because simply I don't know how to deal with the others ;-) Imagine app call (e.g. `PUT /users/7`) and now what? Probably you have to find all GET request at least `GET /users/7` and `GET /users` and update them. But information about that user can also be part of other responses e.g. part of the `GET /profile`, or there are many users and users resource is paginated etc. If you have any suggestion how to deal with POST, PUT, PATCH, DELETE requests start [discussion](https://github.com/lichtner/laravel-mock-api/discussions/new?category=ideas).
126+
59127
## Testing
60128

61129
```bash
@@ -66,14 +134,6 @@ composer test
66134

67135
Please see [CHANGELOG](CHANGELOG.md) for more information on what has changed recently.
68136

69-
## Contributing
70-
71-
Please see [CONTRIBUTING](CONTRIBUTING.md) for details.
72-
73-
## Security Vulnerabilities
74-
75-
Please review [our security policy](../../security/policy) on how to report security vulnerabilities.
76-
77137
## Credits
78138

79139
- [Marek Lichtner](https://github.com/lichtner)

composer.json

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
{
22
"name": "lichtner/laravel-mock-api",
3-
"description": "Easy and smart mocking external APIs to simplify development",
3+
"description": "Easy to use, but the powerful micro library for mocking external API",
44
"keywords": [
55
"lichtner",
66
"laravel",
7-
"laravel-mock-api"
7+
"laravel-mock-api",
8+
"mocking",
9+
"api"
810
],
911
"homepage": "https://github.com/lichtner/laravel-mock-api",
1012
"license": "MIT",

0 commit comments

Comments
 (0)