class AnnounceTestHttpClientMiddleware

Same name and namespace in other branches
  1. 11.x core/modules/announcements_feed/tests/modules/announce_feed_test/src/AnnounceTestHttpClientMiddleware.php \Drupal\announce_feed_test\AnnounceTestHttpClientMiddleware

Overrides the requested endpoint when running tests.

Hierarchy

Expanded class hierarchy of AnnounceTestHttpClientMiddleware

3 files declare their use of AnnounceTestHttpClientMiddleware
AccessAnnouncementTest.php in core/modules/announcements_feed/tests/src/FunctionalJavascript/AccessAnnouncementTest.php
AlertsJsonFeedTest.php in core/modules/announcements_feed/tests/src/FunctionalJavascript/AlertsJsonFeedTest.php
AnnounceBlockTest.php in core/modules/announcements_feed/tests/src/FunctionalJavascript/AnnounceBlockTest.php
1 string reference to 'AnnounceTestHttpClientMiddleware'
announce_feed_test.services.yml in core/modules/announcements_feed/tests/modules/announce_feed_test/announce_feed_test.services.yml
core/modules/announcements_feed/tests/modules/announce_feed_test/announce_feed_test.services.yml
1 service uses AnnounceTestHttpClientMiddleware
announce_feed_test.announce_client_middleware in core/modules/announcements_feed/tests/modules/announce_feed_test/announce_feed_test.services.yml
Drupal\announce_feed_test\AnnounceTestHttpClientMiddleware

File

core/modules/announcements_feed/tests/modules/announce_feed_test/src/AnnounceTestHttpClientMiddleware.php, line 14

Namespace

Drupal\announce_feed_test
View source
class AnnounceTestHttpClientMiddleware {
  
  /**
   * HTTP middleware that replaces request endpoint for a test one.
   */
  public function __invoke() : \Closure {
    return function ($handler) {
      return function (RequestInterface $request, array $options) use ($handler) : PromiseInterface {
        $test_end_point = \Drupal::state()->get('announce_test_endpoint');
        if ($test_end_point && str_contains((string) $request->getUri(), '://www.drupal.org/announcements.json')) {
          // 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.
          $request = $request->withUri(new Uri($test_end_point));
        }
        return $handler($request, $options);
      };
    };
  }
  
  /**
   * Sets the test endpoint for the advisories JSON feed.
   *
   * @param string $test_endpoint
   *   The test endpoint.
   */
  public static function setAnnounceTestEndpoint(string $test_endpoint) : void {
    // Convert the endpoint to an absolute URL.
    $test_endpoint = Url::fromUri('base:/' . $test_endpoint)->setAbsolute()
      ->toString();
    \Drupal::state()->set('announce_test_endpoint', $test_endpoint);
    \Drupal::service('keyvalue.expirable')->get('announcements_feed')
      ->delete('announcements');
    Cache::invalidateTags([
      'announcements_feed:feed',
    ]);
  }

}

Members

Title Sort descending Modifiers Object type Summary
AnnounceTestHttpClientMiddleware::setAnnounceTestEndpoint public static function Sets the test endpoint for the advisories JSON feed.
AnnounceTestHttpClientMiddleware::__invoke public function HTTP middleware that replaces request endpoint for a test one.

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.