You are here

class AdvisoriesTestHttpClient in Drupal 9

Provides a decorator service for the 'http_client' service for testing.

Hierarchy

Expanded class hierarchy of AdvisoriesTestHttpClient

1 file declares its use of AdvisoriesTestHttpClient
SecurityAdvisoryTest.php in core/modules/system/tests/src/Functional/SecurityAdvisories/SecurityAdvisoryTest.php
1 string reference to 'AdvisoriesTestHttpClient'
advisory_feed_test.services.yml in core/modules/system/tests/modules/advisory_feed_test/advisory_feed_test.services.yml
core/modules/system/tests/modules/advisory_feed_test/advisory_feed_test.services.yml
1 service uses AdvisoriesTestHttpClient
http_client.advisory_feed_test in core/modules/system/tests/modules/advisory_feed_test/advisory_feed_test.services.yml
Drupal\advisory_feed_test\AdvisoriesTestHttpClient

File

core/modules/system/tests/modules/advisory_feed_test/src/AdvisoriesTestHttpClient.php, line 11

Namespace

Drupal\advisory_feed_test
View source
class AdvisoriesTestHttpClient extends Client {

  /**
   * The decorated http_client service.
   *
   * @var \GuzzleHttp\Client
   */
  protected $innerClient;

  /**
   * Constructs an AdvisoriesTestHttpClient object.
   *
   * @param \GuzzleHttp\Client $client
   *   The decorated http_client service.
   */
  public function __construct(Client $client) {
    $this->innerClient = $client;
  }

  /**
   * {@inheritdoc}
   */
  public function get($uri, array $options = []) : ResponseInterface {
    $test_end_point = \Drupal::state()
      ->get('advisories_test_endpoint');
    if ($test_end_point && strpos($uri, '://updates.drupal.org/psa.json') !== FALSE) {

      // Only override $uri if it matches the advisories JSON feed to avoid
      // changing any other uses of the 'http_client' service during tests with
      // this module installed.
      $uri = $test_end_point;
    }
    return $this->innerClient
      ->get($uri, $options);
  }

  /**
   * Sets the test endpoint for the advisories JSON feed.
   *
   * @param string $test_endpoint
   *   The test endpoint.
   * @param bool $delete_stored_response
   *   Whether to delete stored feed response.
   */
  public static function setTestEndpoint(string $test_endpoint, bool $delete_stored_response = FALSE) : void {
    \Drupal::state()
      ->set('advisories_test_endpoint', $test_endpoint);
    if ($delete_stored_response) {
      \Drupal::service('system.sa_fetcher')
        ->deleteStoredResponse();
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
AdvisoriesTestHttpClient::$innerClient protected property The decorated http_client service.
AdvisoriesTestHttpClient::get public function
AdvisoriesTestHttpClient::setTestEndpoint public static function Sets the test endpoint for the advisories JSON feed.
AdvisoriesTestHttpClient::__construct public function Constructs an AdvisoriesTestHttpClient object.