English | 日本語
A library for HTTP testing.
name | version |
---|---|
PHP | ^8.1 |
PHPUnit | ^10.0 |
composer require --dev sayuprc/http-test-case
Extend the HttpTestCase
class and implement the following methods:
getClient()
getRequestFactory()
getUriFactory()
getStreamFactory()
<?php
use GuzzleHttp\Client;
use GuzzleHttp\Psr7\HttpFactory;
use Psr\Http\Client\ClientInterface;
use Psr\Http\Message\RequestFactoryInterface;
use Psr\Http\Message\StreamFactoryInterface;
use Psr\Http\Message\UriFactoryInterface;
use Sayuprc\HttpTestCase\HttpTestCase;
class SampleTest extends HttpTestCase
{
protected function getClient(): ClientInterface
{
return new Client();
}
protected function getRequestFactory(): RequestFactoryInterface
{
return new HttpFactory();
}
protected function getUriFactory(): UriFactoryInterface
{
return new HttpFactory();
}
protected function getStreamFactory(): StreamFactoryInterface
{
return new HttpFactory();
}
}
<?php
use Sayuprc\HttpTestCase\HttpTestCase;
class SampleTest extends HttpTestCase
{
public function testGet()
{
$response = $this->get('https://example.com');
$response->assertStatusCode(200);
}
}
<?php
use Sayuprc\HttpTestCase\HttpTestCase;
class SampleTest extends HttpTestCase
{
public function testPost()
{
$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.