Download the PHP package gromatics/http-fixtures without Composer
On this page you can find all versions of the php package gromatics/http-fixtures. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download gromatics/http-fixtures
More information about gromatics/http-fixtures
Files in gromatics/http-fixtures
Package http-fixtures
Short Description A Laravel package that generates fake JSON responses for HTTP testing using Http::fake(). It simplifies the creation of mock HTTP responses by integrating with FakerPHP to generate realistic test data.
License MIT
Informations about the package http-fixtures
Laravel HTTP Fixtures
This package helps you create mock HTTP responses for Laravel tests. It combines Laravel's Http::fake
functionality with Faker data generation to create realistic test data in a format similar to Laravel factories.
Contents
-
Laravel HTTP Fixtures
- 1. Installation
- 2. What is a HTTP fixture and why do I need it?
-
3. Creating a Fixture
- 3.1 Create a fixture from a real JSON response
- 3.2 Create a fixture using the Artisan command
- 3.3 Create a fixture from a JSON file
- 4. Use a HTTP fixture in tests
- 5. Fixture options
1. Installation
That's all you need to install the package as a development dependency in your Laravel project.
2. What is a HTTP fixture and why do I need it?
An HTTP fixture is a class that mocks the data of a JSON API endpoint. Sometimes you don't want to hit a real API in your tests. APIs can go down, and while your code may work perfectly, a downed API can cause your tests to fail. A common way to
solve this is to save the JSON response and serve it using Http::fake
, like this:
A saved JSON response can contain sensitive data, and it can be cumbersome to filter all of it out before committing the JSON to your repository. The Laravel HTTP Fixture package handles this for you. It can automatically create a fixture from an HTTP request and uses the Faker library to replace sensitive values, similar to how Laravel factories work. A HTTP fixture looks like this:
3. Creating a Fixture
3.1 Create a fixture from a real JSON response
The easiest way to create a fixture is by recording a real HTTP request in your test. You can do this by placing the Http::record() method before calling the service that makes the HTTP requests. After the service has finished, use HttpResponseRecorder::recordedToHttpFixture() to save the responses as fixtures.
A service can make multiple requests, and for each unique request, a new fixture will be created. If the same request is made more than once, it won’t duplicate the fixture.
For example:
This will create a StackexchangeSearchFixture.php file in /tests/Fixtures, which might look like this:
3.2 Create a fixture using the Artisan command
You can also create a fixture using an Artisan command. Just run the command below and follow the on-screen instructions:
3.3 Create a fixture from a JSON file
You can also create a fixture from a saved JSON file. For example, if you’ve saved a Stripe API response in your storage directory, you can do the following:
If your Stripe response looks like this;
Then the command will generate a fixture class similar to this:
4. Use a HTTP fixture in tests
You can use your HTTP fixture in your tests like this:
You can override specific keys when initializing the fixture:
This will return a JSON response similar to:
You can also use dot notation to update nested values:
This will produce a response where the first item's name is set to "John Doe".
5. Fixture options
The output of the fixture can be in JSON, XML, array and a Laravel collection.
XML
If you want to return XML instead of JSON, you can use the toXML()
method and pass the root element name as a parameter.
For example:
This will return na XML response similar to: