kirouane / imposter
API mock for PHPUnit
Installs: 11 252
Dependents: 0
Suggesters: 0
Security: 0
Stars: 5
Watchers: 3
Forks: 1
Open Issues: 1
Requires
- php: >=7.0
- ext-json: *
- guzzlehttp/guzzle: >= 6, < 7
- monolog/monolog: >= 1, < 2
- php-di/php-di: ^6.0
- phpunit/phpunit: ^6
- react/http: ^0.8.3
- symfony/console: >= 2, < 5
- symfony/templating: >= 2, < 5
Requires (Dev)
- mockery/mockery: ^1.0@dev
- php-coveralls/php-coveralls: 2.1.x-dev
This package is auto-updated.
Last update: 2024-11-10 18:48:09 UTC
README
Imposter
Imposter is a php library that used to serve http stubs and mocks.
Here is an example to emphasize how is simple to mock an HTTP endpoint with this library in PHPUnit.
namespace Imposter; use PHPUnit\Framework\TestCase; /** * Class ScenarioTest * @package Imposter */ class ReadMeTest extends TestCase { /** * @test * */ public function match() { ImposterFactory::get()->mock(8081) ->withPath('/users/1') ->withMethod('POST') ->returnBody('{"response" :"okay"}') ->once() ->send(); $client = new \GuzzleHttp\Client(); $response = $client->post('http://localhost:8081/users/1')->getBody()->getContents(); self::assertSame($response, '{"response" :"okay"}'); } public function tearDown() { ImposterFactory::get()->close(); } }
Install
composer require kirouane/imposter --dev
Features
Display logs
In case of the HTTP request doesn't match any mock, you can find out the reason here http://localhost:2424/mock/log/html
Below, you can see what the logs page looks like.
PHPUnit Asserter
Imposter Library uses PHPunit asserters to match HTTP requests with the mocks you create.
Example :
namespace Imposter; use PHPUnit\Framework\TestCase; /** * Class ScenarioTest * @package Imposter */ class ReadMeTest extends TestCase { /** * @test */ public function match() { ImposterFactory::get()->mock(8081) ->withPath('/users/1') ->withMethod(new RegularExpression('/POST|PUT/')) ->returnBody('{"response" :"okay"}') ->twice() ->send(); $client = new \GuzzleHttp\Client(); $response = $client->post('http://localhost:8081/users/1')->getBody()->getContents(); self::assertSame($response, '{"response" :"okay"}'); $response = $client->put('http://localhost:8081/users/1')->getBody()->getContents(); self::assertSame($response, '{"response" :"okay"}'); } public function tearDown() { ImposterFactory::get()->close(); } }
Proxies
Not implemented yet