leeshan87 / module-fake-api
Codeception的异步模拟API模块
0.2.1
2024-06-29 18:55 UTC
Requires
- php: >=5.6
- codeception/codeception: ^4.0
- codeception/module-asserts: ^1.0.0
- leeshan87/reactphp-multi-loop: ~0.1
- react/event-loop: ~1.5.0
- react/http: ~1.10.0
- ringcentral/psr7: ~1.3.0
Requires (Dev)
- overtrue/phplint: ~2.4.1
This package is auto-updated.
Last update: 2024-09-29 19:25:43 UTC
README
Codeception的异步模拟API模块。
此Codeception模块帮助创建一个异步的模拟API http服务器。此模块需要react/http:^1.0.0才能工作。它提供了一个异步http服务器,可以
- 响应HTTP请求
- 将请求代理到外部HTTP服务器
- 记录代理请求并将它们存储在磁盘上。如果可以管理FakeApi事件循环的何时以及如何触发,则可以在Codeception测试中使用它。示例Cept用法
<?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"));
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();
安装
安装此库的推荐方法是通过Composer。您是Composer的新手?不熟悉Composer?
这将安装最新支持的版本
$ composer require leeshan87/module-fake-api:^0.1
有关版本升级的详细信息,请参阅变更日志。
此项目旨在在任何平台上运行,因此不需要任何PHP扩展,并支持从PHP 5.3到当前PHP 8+的运行。强烈建议使用PHP 7+。
测试
要运行测试套件,您首先需要克隆此仓库,然后通过Composer安装所有依赖项。
$ composer install
要运行测试套件,请转到项目根目录并运行
$ php vendor/bin/codecept run unit
许可协议
MIT