Skip to content

HttpMockTrait

Mocks the HTTP requests made in kernel tests.

The HTTP client's middleware still runs. The trait's register() makes the HTTP handler stack available to the test. A test class that has its own register() method replaces the trait's, so it must call $this->registerHttpMock($container) itself.


  • Full name: \Drupal\Tests\helper\Traits\HttpMockTrait

Methods

register

{@inheritdoc}

public register(\Drupal\Core\DependencyInjection\ContainerBuilder $container): void

Parameters:

Parameter Type Description
$container \Drupal\Core\DependencyInjection\ContainerBuilder

registerHttpMock

Makes the HTTP handler stack available for mocking.

protected registerHttpMock(\Drupal\Core\DependencyInjection\ContainerBuilder $container): void

Parameters:

Parameter Type Description
$container \Drupal\Core\DependencyInjection\ContainerBuilder
The container being built.

mockHttpResponses

Replies to the next HTTP requests with the given responses.

protected mockHttpResponses((\Psr\Http\Message\ResponseInterface|\Throwable)[] $responses): \GuzzleHttp\Handler\MockHandler

Parameters:

Parameter Type Description
$responses (\Psr\Http\Message\ResponseInterface|\Throwable)[]
The responses, in the order the requests are made.

Return Value:

The mock handler, whose count is the number of responses not used yet.