PHP code example of sayuprc / http-test-case

1. Go to this page and download the library: Download sayuprc/http-test-case library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

sayuprc / http-test-case example snippets




use GuzzleHttp\Client;
use GuzzleHttp\Psr7\HttpFactory;
use HttpTest\HttpTestCase;
use Psr\Http\Client\ClientInterface;
use Psr\Http\Message\RequestFactoryInterface;
use Psr\Http\Message\StreamFactoryInterface;
use Psr\Http\Message\UriFactoryInterface;

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();
    }
}



use HttpTest\HttpTestCase;

class SampleTest extends HttpTestCase
{
    public function testGet()
    {
        $response = $this->get('https://example.com');

        $response->assertStatusCode(200);
    }
}



use HttpTest\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);
    }
}