定制 leeshan87/module-fake-api 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

leeshan87/module-fake-api

最新稳定版本:0.2.1

Composer 安装命令:

composer require --dev leeshan87/module-fake-api

包简介

Asynchronous Fake Api module for Codeception

README 文档

README

CI status Installs on Packagist

Asynchronous Fake Api module for Codeception.

This Codeception module helps to create an async FakeApi http server. This module requires react/http:^1.0.0 to work. It provides an async http server, which can

  • Respond to http request
  • Proxy request to an external http server
  • Record proxied requests and store them on the disk It can be used in Codeception tests, if you can manage when and how to tick the FakeApi event loop Example Cept usage:
<?php
$I = new ServiceGuy($scenario);
$I->wantTo('Save some api calls for testing');
$I->setUpstreamUrl('https://example.com');
$I->initFakeServer();
$I->assertEmpty($I->grabRecordedRequests());
$I->assertEmpty($I->grabRecordedResponses());
$I->sendRequest('GET', '/');
$I->waitTillNextRequestResolves();
$I->assertNotEmpty($I->grabRecordedRequests());
$I->assertNotEmpty($I->grabRecordedResponses());
// Record all uncovered API call for 30 sec
//$I->recordRequestsForSeconds(30);
//$I->waitTillFakeApiRecordingEnds();
$I->stopFakeApi();
// Save Requests if needed
//$I->saveRecordedInformation(codecept_output_dir(date('Y_m_d_H_i_s') . ".json"));

Example usage out side Codeception

 // find vendor dir if possible
$dir = __DIR__;
$ds = DIRECTORY_SEPARATOR;
for ($i = 0; $i < 3; $i++) {
    $projectRootDir = dirname($dir);
    if (!is_dir("$projectRootDir{$ds}vendor")) {
        $dir = $projectRootDir;
        continue;
    }
    include "$projectRootDir{$ds}vendor{$ds}autoload.php";
    include "$projectRootDir{$ds}vendor{$ds}codeception{$ds}codeception{$ds}autoload.php";
}

use Codeception\Module\FakeApi;
use Codeception\Util\Stub;

$api = new FakeApi(Stub::make(\Codeception\Lib\ModuleContainer::class));
$api->setBindPort(8081);
$api->initFakeServer();
$api->addMessage(200, [], 'hello');
$api->addMessage(200, [], 'hello w');
$api->addMessage(200, [], 'hello wor');
$api->addMessage(200, [], 'hello world');
$api->run();

Install

The recommended way to install this library is through Composer. New to Composer?

This will install the latest supported version:

$ composer require leeshan87/module-fake-api:^0.1

See also the CHANGELOG for details about version upgrades.

This project aims to run on any platform and thus does not require any PHP extensions and supports running on legacy PHP 5.3 through current PHP 8+. It's highly recommended to use PHP 7+ for this project.

Tests

To run the test suite, you first need to clone this repo and then install all dependencies through Composer:

$ composer install

To run the test suite, go to the project root and run:

$ php vendor/bin/codecept run unit

License

MIT

统计信息

  • 总下载量: 4.36k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 2
  • 点击次数: 0
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 2
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2021-08-06