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