English | 日本語
A library for HTTP testing.
| name | version |
|---|---|
| PHP | ^8.3 |
| PHPUnit | ^12.0 |
composer require --dev sayuprc/http-test-case
Extend the HttpTestCase class and implement the following methods:
getClient()getRequestFactory()getUriFactory()getStreamFactory()
<?phpuseGuzzleHttp\Client;
useGuzzleHttp\Psr7\HttpFactory;
useHttpTest\HttpTestCase;
usePsr\Http\Client\ClientInterface;
usePsr\Http\Message\RequestFactoryInterface;
usePsr\Http\Message\StreamFactoryInterface;
usePsr\Http\Message\UriFactoryInterface;
class SampleTest extends HttpTestCase
{
protectedfunctiongetClient(): ClientInterface
{
returnnewClient();
}
protectedfunctiongetRequestFactory(): RequestFactoryInterface
{
returnnewHttpFactory();
}
protectedfunctiongetUriFactory(): UriFactoryInterface
{
returnnewHttpFactory();
}
protectedfunctiongetStreamFactory(): StreamFactoryInterface
{
returnnewHttpFactory();
}
}<?phpuseHttpTest\HttpTestCase;
class SampleTest extends HttpTestCase
{
publicfunctiontestGet()
{
$response = $this->get('https://example.com');
$response->assertStatusCode(200);
}
}<?phpuseHttpTest\HttpTestCase;
class SampleTest extends HttpTestCase
{
publicfunctiontestPost()
{
$response = $this->post(
'https://example.com',
[
'headers' => [
'Content-Type' => 'application/json',
],
'json' => [
'key' => 'value',
],
]
);
$response->assertStatusCode(200);
}
}Please refer to the here for the methods and assertions of HttpTestCase.