PHP code example of macpaw / extended_mock_http_client
1. Go to this page and download the library: Download macpaw/extended_mock_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/ */
macpaw / extended_mock_http_client example snippets
abstract class AbstractFunctionalTest extends KernelTestCase
{
private ExtendedMockHttpClient $httpClient
protected function setUp(): void
{
/** @var ExtendedMockHttpClient $mockHttpClient */
$this->httpClient = self::getContainer()->get('http_client_service_name');
}
}
class MyTest extends AbstractFunctionalTest
{
/**
* Create simple request using createFixture
* Request with almost empty parameters
* Check response and check called times
*/
public function testSimpleExample1(): void
{
$httpFixture = $this->client->createFixture(
'POST',
'https://test.test/foo?foo=bar',
null,
null,
200,
'ok'
);
$this->client->addFixture($httpFixture);
$response = $this->client->request('POST', 'https://test.test/foo?foo=bar');
self::assertEquals(200, $response->getStatusCode());
self::assertEquals('ok', $response->getContent());
self::assertEquals(1, $httpFixture->getCalledTimes());
}
/**
* Make simple fixture using createFixture
* Request using json
* Check response
*/
public function testSimpleExample2(): void
{
$httpFixture = $this->client->createFixture(
'POST',
'https://test.test/foo?foo=bar',
'{"foo":"bar","baz":123}',
[
'x-header' => 'x-value',
],
200,
'ok'
);
$this->client->addFixture($httpFixture);
$response = $this->client->request('POST', 'https://test.test/foo?foo=bar', [
'json' => [
'foo' => 'bar',
'baz' => 123
],
'headers' => [
'x-header' => 'x-value',
]
]);
self::assertEquals(200, $response->getStatusCode());
self::assertEquals('ok', $response->getContent());
}
}
use ExtendedMockHttpClient\HttpFixture\Request\Comparator\ComparatorInterface;
class CustomComparator implements ComparatorInterface
{
/**
* @var string
*/
private $stringPart1;
/**
* @var string
*/
private $stringPart2;
public static function getName(): string
{
return 'custom';
}
public function __construct(string $stringPart1, string $stringPart2)
{
$this->stringPart1 = $stringPart1;
$this->stringPart2 = $stringPart2;
}
public function __invoke($value): bool
{
return $value === "$this->stringPart1.$this->stringPart2";
}
}
class MyTest extends AbstractFunctionalTest
{
/**
* Make fixture using builder with custom comparator
* Request using string body
* Check response
*/
public function testCustomComparator(): void
{
$builder = $this->client->getHttpFixtureBuilder();
$httpFixture = $builder
->request(
$builder->body(new CustomComparator('foo', 'bar'))
)
->response(200, 'ok')
->build();
$this->client->addFixture($httpFixture);
$response = $this->client->request('GET', 'https://test.test', [
'body' => 'foo.bar'
]);
self::assertEquals(200, $response->getStatusCode());
self::assertEquals('ok', $response->getContent());
}
}
use ExtendedMockHttpClient\Builder\HttpFixtureBuilder as BaseHttpFixtureBuilder;
use ExtendedMockHttpClient\Tests\Fixture\Application\HttpFixture\Request\Comparator\CustomComparator;
class HttpFixtureBuilder extends BaseHttpFixtureBuilder
{
public function custom(string $stringPart1, string $stringPart2): CustomComparator
{
return new CustomComparator($stringPart1, $stringPart2);
}
}
use ExtendedMockHttpClient\Factory\HttpFixtureBuilderFactory as BaseHttpFixtureBuilderFactory;
use ExtendedMockHttpClient\Builder\HttpFixtureBuilder as BaseHttpFixtureBuilder;
use ExtendedMockHttpClient\Tests\Fixture\Application\Builder\HttpFixtureBuilder;
class HttpFixtureBuilderFactory extends BaseHttpFixtureBuilderFactory
{
public function create(): BaseHttpFixtureBuilder
{
return new HttpFixtureBuilder($this->httpFixtureFactory);
}
}
class MyTest extends AbstractFunctionalTest
{
/**
* Make fixture using overwrote builder with custom comparator
* Request using string body
* Check response
*/
public function testBuilderOverwrote(): void
{
/** @var HttpFixtureBuilder $builder */
$builder = $this->client->getHttpFixtureBuilder();
$httpFixture = $builder
->request(
$builder->body($builder->custom('foo', 'bar'))
)
->response(200, 'ok')
->build();
$this->client->addFixture($httpFixture);
$response = $this->client->request('GET', 'https://test.test', [
'body' => 'foo.bar'
]);
self::assertEquals(200, $response->getStatusCode());
self::assertEquals('ok', $response->getContent());
}
}
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.