The Mock API Bundle is a Symfony bundle designed to help you mock API requests for testing purposes. It allows you to define mock responses for specific requests using YAML configuration files, making it easier to write integration tests without relying on external services. This is especially useful when you need to test third-party API integrations or when you want to simulate different scenarios in your tests.
This will only work in tests, the normal HTTP client behavior will not be affected.
composer require bneumann/mock-api-bundle --dev
config/bundles.php file:return [
// ...
Bneumann\MockApiBundle\BneumannMockApiBundle::class => ['test' => true],
];
bneumann_mock_api.yaml file in your config/packages/test directory:bneumann_mock_api:
mocks_path: tests/mocks
To start using the Mock API Bundle, you need to create one or multiple mock configuration file in the directory specified by the mocks_path configuration option (default is tests/mocks). This file should contain a list of mock responses, each with an URL, method and optional request body for POST and the response data with status code and a body you want to return.
Here's an example of a mock configuration file:
mocks:
- url: https://example.com/api/users
method: GET
response:
status: 200
body:
[
{
"id": 1,
"name": "John Doe"
},
{
"id": 2,
"name": "Jane Doe"
}
]
- url: https://example.com/api/users
method: POST
request:
body:
name: Alice
response:
status: 201
body:
{
"id": 3,
"name": "Alice"
}
You can declare the body or the response and request as JSON or YAML. The example above uses JSON for the response and YAML for the request just for demonstration purposes.
Contributions are welcome! Please feel free to submit a Pull Request or open an Issue to help improve this bundle.
This bundle is open-sourced software licensed under the MIT license.
How can I help you explore Laravel packages today?