You are here

class MockHttpClientMiddleware in Thunder 8.5

Same name and namespace in other branches
  1. 6.2.x tests/modules/thunder_test_mock_request/src/MockHttpClientMiddleware.php \Drupal\thunder_test_mock_request\MockHttpClientMiddleware
  2. 6.1.x tests/modules/thunder_test_mock_request/src/MockHttpClientMiddleware.php \Drupal\thunder_test_mock_request\MockHttpClientMiddleware

Sets the mocked responses.

Hierarchy

Expanded class hierarchy of MockHttpClientMiddleware

2 files declare their use of MockHttpClientMiddleware
RiddleTest.php in tests/src/FunctionalJavascript/Integration/RiddleTest.php
thunder_test_mock_request.install in tests/modules/thunder_test_mock_request/thunder_test_mock_request.install
Install functions for thunder_test_mock_request.
1 string reference to 'MockHttpClientMiddleware'
thunder_test_mock_request.services.yml in tests/modules/thunder_test_mock_request/thunder_test_mock_request.services.yml
tests/modules/thunder_test_mock_request/thunder_test_mock_request.services.yml
1 service uses MockHttpClientMiddleware
thunder_test_mock_request.http_client.middleware in tests/modules/thunder_test_mock_request/thunder_test_mock_request.services.yml
Drupal\thunder_test_mock_request\MockHttpClientMiddleware

File

tests/modules/thunder_test_mock_request/src/MockHttpClientMiddleware.php, line 14

Namespace

Drupal\thunder_test_mock_request
View source
class MockHttpClientMiddleware {

  /**
   * The request object.
   *
   * @var \Symfony\Component\HttpFoundation\Request
   */
  protected $request;

  /**
   * The state service.
   *
   * @var \Drupal\Core\State\StateInterface
   */
  protected $state;

  /**
   * MockHttpClientMiddleware constructor.
   *
   * @param \Symfony\Component\HttpFoundation\RequestStack $requestStack
   *   The current request stack.
   * @param \Drupal\Core\State\StateInterface $state
   *   The state service.
   */
  public function __construct(RequestStack $requestStack, StateInterface $state) {
    $this->request = $requestStack
      ->getCurrentRequest();
    $this->state = $state;
  }

  /**
   * Add a mocked response.
   *
   * @param string $url
   *   URL of the request.
   * @param string $body
   *   The content body of the response.
   * @param array $headers
   *   The response headers.
   * @param int $status
   *   The response status code.
   */
  public static function addUrlResponse($url, $body, array $headers = [], $status = 200) {
    $items = \Drupal::state()
      ->get(static::class, []);
    $items[$url] = [
      'body' => $body,
      'headers' => $headers,
      'status' => $status,
    ];
    \Drupal::state()
      ->set(static::class, $items);
  }

  /**
   * {@inheritdoc}
   *
   * HTTP middleware that adds the next mocked response.
   */
  public function __invoke() {
    return function ($handler) {
      return function (RequestInterface $request, array $options) use ($handler) {
        $items = $this->state
          ->get(static::class, []);
        $url = (string) $request
          ->getUri();
        if (!empty($items[$url])) {
          $response = new Response($items[$url]['status'], $items[$url]['headers'], $items[$url]['body']);

          // @phpstan-ignore-next-line
          return promise_for($response);
        }
        elseif (strstr($this->request
          ->getHttpHost(), $request
          ->getUri()
          ->getHost()) === FALSE) {
          throw new \Exception(sprintf("No response for %s defined. See MockHttpClientMiddleware::addUrlResponse().", $url));
        }
        return $handler($request, $options);
      };
    };
  }

}

Members

Namesort descending Modifiers Type Description Overrides
MockHttpClientMiddleware::$request protected property The request object.
MockHttpClientMiddleware::$state protected property The state service.
MockHttpClientMiddleware::addUrlResponse public static function Add a mocked response.
MockHttpClientMiddleware::__construct public function MockHttpClientMiddleware constructor.
MockHttpClientMiddleware::__invoke public function HTTP middleware that adds the next mocked response.