PHP code example of dobrosite / phpunit-psr-http-client

1. Go to this page and download the library: Download dobrosite/phpunit-psr-http-client 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/ */

    

dobrosite / phpunit-psr-http-client example snippets


$this->getHttpClient()
    ->expectRequest('POST', 'https://example.com/api/v1/users')
    ->headers([
        'accept' => 'application/vnd.api+json',
        'content-type' => 'application/vnd.api+json',
    ])
    ->body([
        'data' => [
            'type' => 'users',
            'attributes' => [
                'email' => '[email protected]',
            ],
        ],
    ])
    ->willReturn(
        [
            'errors' => [
                [
                    'code' => 'EmailAlreadyRegistered',
                    'title' => 'Адрес электронной почты уже заргистрирован',
                ],
            ],
        ],
        422,
        [
            'content-type' => 'application/vnd.api+json',
        ]
    );

use DobroSite\PHPUnit\PSR18\Symfony\TestHttpClientTrait;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;

final class SomeTest extends WebTestCase
{
    use TestHttpClientTrait;

    public function testSomething(): void
    {
        // Подготовка.
        
        $this->getHttpClient()
            ->expectRequest('GET', 'https://some.service/some/resource')
            ->willReturn([
                'data' => [/* Имитация ответа сторонней службы. */],
            ]);
        
        // Действие.
        
        $client = static::createClient();
        $crawler = $client->request('GET', '/api/foo');

        // Проверки.

        $this->assertResponseIsSuccessful();
    }
}

use Psr\Http\Client\ClientInterface;

class Foo 
{
    public function __construct(
        private readonly ClientInterface $httpClient,
    ) {
    }
    
    public function doSomething(): Something
    {
        // …
        $response = $this->httpClient->sendRequest($request);
        // …
    }
}

$this->getHttpClient()->expectRequest('GET', 'https://some.service/some/resource')

$this->getHttpClient()->expectRequest(new IsAnything(), new StringStartsWith('https://example.com/'))
// или
$this->getHttpClient()->expectRequest(self::anything(), self::stringStartsWith('https://example.com/'))

$this->getHttpClient()->expectRequest(/* … */)->headers([
    'Accept' => self::stringContains('application/json'),
    'content-type' => 'application/json',
]);

$this->getHttpClient()->expectRequest(/* … */)->body('{"foo":"bar"}');
$this->getHttpClient()->expectRequest(/* … */)->body(['foo' => 'bar']);
$this->getHttpClient()->expectRequest(/* … */)->body(self::stringContains('bar'));

$this->getHttpClient()->expectRequest(/* … */)->willReturn(
    body: '{"foo":"bar"}',
    statusCode: 200,
);
$this->getHttpClient()->expectRequest(/* … */)->willReturn(
    body: null,
    statusCode: 204,
    headers: ['ETag' => 'viChieyiupaidahng6eiv3bohRohcohb']
);

$this->getHttpClient()->expectRequest(/* … */)->willThrowException(new SomeException(/* … */));