leeshan87 / module-fake-api
Asynchronous Fake Api module for Codeception
Installs: 3 115
Dependents: 0
Suggesters: 0
Security: 0
Stars: 2
Watchers: 2
Forks: 0
Open Issues: 1
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-10-29 19:38:43 UTC
README
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