baopham / laravel-api-mocker by baopham

API mocker for Laravel apps
193,002
6
3
Package Data
Maintainer Username: baopham
Maintainer Contact: gbaopham@gmail.com (Bao Pham)
Package Create Date: 2015-11-22
Package Last Update: 2020-10-15
Home Page:
Language: PHP
License: MIT
Last Refreshed: 2024-03-26 03:08:50
Package Statistics
Total Downloads: 193,002
Monthly Downloads: 0
Daily Downloads: 0
Total Stars: 6
Total Watchers: 3
Total Forks: 6
Total Open Issues: 1

api-mocker

Laravel package to mock API endpoints.

If your apps need to integrate with other APIs (Github, Marketo, etc.), you don't want to invoke their API calls during testing. With this package, you can quickly mock the endpoints and it can be especially useful for Behat tests.

Usage:

  1. composer require baopham/api-mocker --dev

  2. Register Service Provider in app/Providers/AppServiceProvider.php:

public function register()
{
    if ($this->app->environment() == 'testing') {
        $this->app->register('BaoPham\ApiMocker\ApiMockerServiceProvider');
    }
}
  1. php artisan vendor:publish

  2. Update the new config file: config/apimocker.php:

<?php

return [
    /*
    |--------------------------------------------------------------------------------------------------
    | Mock on/off
    |--------------------------------------------------------------------------------------------------
    |
    | Determine if the endpoints below should be mocked.
    |
    */

    'should_mock' => env('API_MOCKER_ON', false),

    /*
    |--------------------------------------------------------------------------------------------------
    | Endpoints
    |--------------------------------------------------------------------------------------------------
    |
    | Array of endpoints to be mocked, keyed by the route's path.
    |
    | - fixture: path to the fixture file for the response content, could be JSON or XML.
    |
    | - middleware: an array of middlewares to be applied for this endpoint.
    |
    | - methods: GET, POST, PUT, DELETE. If not defined, it will not check against the method.
    |
    | - code: the response code should be returned. Default to 200.
    |
    | - placeholder: boolean (false by default). Set to `true` if you want to replace the
    |   placeholders in your fixture with the request parameters.
    |
    |     e.g. In your fixture: { "status": "ok", "description": "Folder {{name}} has been updated" }
    |
    |          Request: POST api/v1/folders/123?placeholder_name=BP (prefix with `placeholder_`)
    |
    |          Response will be: { "status": "ok", "description": "Folder BP has been updated" }
    |
    |   This is useful for Behat test for example, when you want to ensure you see the right status message.
    |
    |
    */

    'endpoints' => [

        'api/v1/folders/{id}' => [
            'fixture' => '/path/to/fixture.json',
            'middleware' => ['auth'],
            'methods' => ['POST'],
            'code' => 200,
            'placeholder' => true,
        ],
    ]

];

Requirements:

Laravel 5.1

TODOs:

  • [ ] Tests

License:

MIT

Author:

Bao Pham