https://github.com/kirouane/imposter
Mock HTTP requests with php
https://github.com/kirouane/imposter
api http mock php phpunit stub
Last synced: 14 days ago
JSON representation
Mock HTTP requests with php
- Host: GitHub
- URL: https://github.com/kirouane/imposter
- Owner: Kirouane
- Created: 2018-09-18T11:24:18.000Z (about 7 years ago)
- Default Branch: master
- Last Pushed: 2020-03-22T22:41:32.000Z (over 5 years ago)
- Last Synced: 2025-10-12T17:14:18.757Z (14 days ago)
- Topics: api, http, mock, php, phpunit, stub
- Language: PHP
- Homepage:
- Size: 234 KB
- Stars: 5
- Watchers: 2
- Forks: 1
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
[](https://travis-ci.com/Kirouane/imposter.svg?branch=master)
[](https://coveralls.io/github/Kirouane/imposter?branch=master)
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.
```php
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](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 :
```php
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