You are here

public function TestHttp::mockHttpClient in Migrate Plus 8.5

Same name and namespace in other branches
  1. 8.4 tests/src/Unit/data_fetcher/HttpTest.php \Drupal\Tests\migrate_plus\Unit\data_fetcher\TestHttp::mockHttpClient()

Mock the HttpClient, so we can control the request/response(s) etc.

Parameters

array $responses: An array of responses (arrays), with each consisting of properties, ordered: response code, content-type and response body.

object $authenticator: Mocked authenticator plugin.

File

tests/src/Unit/data_fetcher/HttpTest.php, line 196
PHPUnit tests for the Migrate Plus Http 'data fetcher' plugin.

Class

TestHttp
Test class to mock an HTTP request.

Namespace

Drupal\Tests\migrate_plus\Unit\data_fetcher

Code

public function mockHttpClient(array $responses, object $authenticator = NULL) {

  // Set mocked authentication plugin to be used for the request auth plugin.
  $this->authenticator = $authenticator;
  $handler_responses = [];
  foreach ($responses as $response) {
    $handler_responses[] = new Response($response[0], [
      'Content-Type' => $response[1],
    ], $response[2]);
  }
  $mock = new MockHandler($handler_responses);
  $handler = HandlerStack::create($mock);
  $this->httpClient = new Client([
    'handler' => $handler,
  ]);
}