class AnnounceTestBase

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

Base class for Announce Kernel tests.

Hierarchy

Expanded class hierarchy of AnnounceTestBase

File

core/modules/announcements_feed/tests/src/Kernel/AnnounceTestBase.php, line 17

Namespace

Drupal\Tests\announcements_feed\Kernel
View source
class AnnounceTestBase extends KernelTestBase {
  
  /**
   * {@inheritdoc}
   */
  protected static $modules = [
    'user',
    'system',
    'announcements_feed',
  ];
  
  /**
   * History of requests/responses.
   *
   * @var array
   */
  protected array $history = [];
  
  /**
   * {@inheritdoc}
   */
  protected function setUp() : void {
    parent::setUp();
    $this->installConfig('system');
    $this->installConfig([
      'user',
    ]);
  }
  
  /**
   * Sets the feed items to be returned for the test.
   *
   * @param mixed[][] $feed_items
   *   The feeds items to test. Every time the http_client makes a request the
   *   next item in this array will be returned. For each feed item 'title' and
   *   'url' are omitted because they do not need to vary between test cases.
   */
  protected function setFeedItems(array $feed_items) : void {
    $responses = [];
    foreach ($feed_items as $feed_item) {
      $feed_item += [
        'title' => 'Drupal security update Test',
        'url' => 'https://www.drupal.org/project/announce',
      ];
      $responses[] = new Response(200, [], json_encode([
        'items' => [
          $feed_item,
        ],
      ]));
    }
    $this->setTestFeedResponses($responses);
  }
  
  /**
   * Sets test feed responses.
   *
   * @param \GuzzleHttp\Psr7\Response[] $responses
   *   The responses for the http_client service to return.
   */
  protected function setTestFeedResponses(array $responses) : void {
    // Create a mock and queue responses.
    $mock = new MockHandler($responses);
    $handler_stack = HandlerStack::create($mock);
    $history = Middleware::history($this->history);
    $handler_stack->push($history);
    // Rebuild the container because the 'system.sa_fetcher' service and other
    // services may already have an instantiated instance of the 'http_client'
    // service without these changes.
    $this->container
      ->get('kernel')
      ->rebuildContainer();
    $this->container = $this->container
      ->get('kernel')
      ->getContainer();
    $this->container
      ->set('http_client', new Client([
      'handler' => $handler_stack,
    ]));
  }

}

Members

Title Sort descending Modifiers Object type Summary Overrides
AnnounceTestBase::$history protected property History of requests/responses.
AnnounceTestBase::$modules protected static property Modules to install. 1
AnnounceTestBase::setFeedItems protected function Sets the feed items to be returned for the test. 2
AnnounceTestBase::setTestFeedResponses protected function Sets test feed responses.
AnnounceTestBase::setUp protected function 2
ExtensionListTestTrait::getModulePath protected function Gets the path for the specified module.
ExtensionListTestTrait::getThemePath protected function Gets the path for the specified theme.
StorageCopyTrait::replaceStorageContents protected static function Copy the configuration from one storage to another and remove stale items.

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