PHP code example of jcchavezs / httptest

1. Go to this page and download the library: Download jcchavezs/httptest 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/ */

    

jcchavezs / httptest example snippets




namespace HttpTest\Tests\Integration;

use HttpTest\HttpTestServer;
use PHPUnit_Framework_TestCase;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;

final class TestServerTest extends PHPUnit_Framework_TestCase
{
    const TEST_BODY = 'test_body';
    const TEST_STATUS_CODE = 202;

    public function testHttpSuccess()
    {
        $t = $this;

        $server = HttpTestServer::create(
            function (RequestInterface $request, ResponseInterface &$response) use ($t) {
                /* Assert the HTTP call , CURLOPT_HTTPHEADER, [
            'Content-Type: application/json',
            'Content-Length: ' . strlen(self::TEST_BODY),
        ]);

        if (curl_exec($handle) === true) {
            $statusCode = curl_getinfo($handle, CURLINFO_HTTP_CODE);
            curl_close($handle);

            // Assert client behaviour based on the server response
            $this->assertEquals(self::TEST_STATUS_CODE, $statusCode);
        } else {
            // Stop the server before as `$this->fail(...)` throws an exception
            // In a try/catch block, this should be in the finally block
            $server->stop();
            
            $this->fail(curl_error($handle));
        }
        
        $server->stop();
    }
}