You are here

public function TestHttp::mockHttpClient in Migrate Plus 8.4

Same name and namespace in other branches
  1. 8.5 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.

\PHPUnit_Framework_MockObject_MockObject $authenticator: Mocked authenticator plugin.

File

tests/src/Unit/data_fetcher/HttpTest.php, line 194
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, \PHPUnit_Framework_MockObject_MockObject $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,
  ]);
}