Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Mock Api Bundle Laravel Package

bneumann/mock-api-bundle

View on GitHub
Deep Wiki
Context7

Mock API Bundle

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.

Features

  • Mock HTTP requests based on URL and request body.
  • Define mock responses in YAML files.
  • Seamless integration with Symfony's HTTP client.
  • Service decoration to ensure usage only in test environments.

Installation

  1. Add the bundle to your Symfony project using Composer:
composer require bneumann/mock-api-bundle --dev
  1. Register the bundle in your config/bundles.php file:
return [
    // ...
    Bneumann\MockApiBundle\BneumannMockApiBundle::class => ['test' => true],
];
  1. (Optional) Configure the bundle by creating a bneumann_mock_api.yaml file in your config/packages/test directory:
bneumann_mock_api:
    mocks_path: tests/mocks

Usage

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.

Contributing

Contributions are welcome! Please feel free to submit a Pull Request or open an Issue to help improve this bundle.

License

This bundle is open-sourced software licensed under the MIT license.

Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
cuci/prototurk-sdk-symfony
clementtalleu/easyadmin-markdown-bundle
codeflextech/permission-manager
karnoweb/livewire-datepicker
sayedenam/sayed-dashboard
milito/query-filter
apiboxsym/user-bundle
apiboxsym/health-check-bundle
jayeshmepani/jpl-moshier-ephemeris-php
elnasnato/laraliveui
labrodev/rest-sdk
sampaui/sampaui
babelqueue/php-sdk
facebook/capi-param-builder-php
babelqueue/symfony
hamzi/corewatch
minionfactory/raw-hydrator
hexters/coinpayment
rjcodes/rjcms
act-training/laravel-permissions-manager