Mock HTTP requests on the server side in your PHP unit tests.
HTTP Mock for PHP mocks the server side of an HTTP request to allow integration testing with the HTTP side. It uses PHP’s builtin web server to start a second process that handles the mocking. The server allows registering request matcher and responses from the client side.
BIG FAT WARNING: software like this is inherently insecure. Only use in trusted, controlled environments.
class ExampleTest extends PHPUnit_Framework_TestCase
{
use \InterNations\Component\HttpMock\PHPUnit\HttpMockTrait;
public static function setUpBeforeClass()
{
static::setUpHttpMockBeforeClass('8082', 'localhost');
}
public static function tearDownAfterClass()
{
static::tearDownHttpMockAfterClass();
}
public function setUp()
{
$this->setUpHttpMock();
}
public function tearDown()
{
$this->tearDownHttpMock();
}
public function testSimpleRequest()
{
$this->http->mock
->when()
->methodIs('GET')
->pathIs('/foo')
->then()
->body('mocked body')
->end();
$this->http->setUp();
$this->assertSame('mocked body', file_get_contents('http://localhost:8082/foo'));
}
public function testAccessingRecordedRequests()
{
$this->http->mock
->when()
->methodIs('POST')
->pathIs('/foo')
->then()
->body('mocked body')
->end();
$this->http->setUp();
$this->assertSame('mocked body', $this->http->client->post('http://localhost:8082/foo')->send()->getBody(true));
$this->assertSame('POST', $this->http->requests->latest()->getMethod());
$this->assertSame('/foo', $this->http->requests->latest()->getPath());
}
}
POST /_expectation
{
response (required): serialized Symfony response
matcher (optional): serialized list of closures
limiter (optional): serialized closure that limits the validity of the expectation
}
GET /_request/latest
Content-Type: text/plain
RECORDED REQUEST
GET /_request/{{index}}
Content-Type: text/plain
RECORDED REQUEST
GET /_request/shift
Content-Type: text/plain
RECORDED REQUEST
GET /_request/pop
Content-Type: text/plain
RECORDED REQUEST
DELETE /_expectation
DELETE /_request
DELETE /_all
GET /_me