You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+93-33Lines changed: 93 additions & 33 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff 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*
2
4
3
5
[](https://packagist.org/packages/lichtner/laravel-mock-api)
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.
17
13
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.
18
15
## Installation
19
16
20
17
You can install the package via composer:
@@ -23,39 +20,110 @@ You can install the package via composer:
23
20
composer require lichtner/laravel-mock-api
24
21
```
25
22
26
-
You can publish and run the migrations with:
23
+
Now you can publish config and run the migrations with:
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:
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
+
59
127
## Testing
60
128
61
129
```bash
@@ -66,14 +134,6 @@ composer test
66
134
67
135
Please see [CHANGELOG](CHANGELOG.md) for more information on what has changed recently.
68
136
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.
0 commit comments