baopham/api-mocker

Laravel应用的API模拟器

0.3.0 2018-07-02 11:58 UTC

This package is auto-updated.

Last update: 2024-09-22 01:52:20 UTC


README

Laravel包,用于模拟API端点。

如果你的应用需要与其他API(如GitHub、Marketo等)集成,你不想在测试期间调用它们的API调用。使用此包,你可以快速模拟端点,它对于Behat测试特别有用。

用法

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

  2. app/Providers/AppServiceProvider.php 中注册服务提供者

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

  2. 更新新的配置文件: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,
        ],
    ]

];

要求

Laravel 5.1

待办事项

  • 测试

许可证

MIT

作者

包伯·范