You are here

public function QueryTest::testResultsCache in Search API 8

Tests that the results cache works correctly.

File

tests/src/Kernel/System/QueryTest.php, line 189

Class

QueryTest
Tests query functionality.

Namespace

Drupal\Tests\search_api\Kernel\System

Code

public function testResultsCache() {

  /** @var \Drupal\search_api\Query\QueryInterface[] $results */
  $results = [];
  $search_ids = [
    'foo',
    'bar',
  ];
  foreach ($search_ids as $search_id) {
    $results[$search_id] = $this->index
      ->query()
      ->setSearchId($search_id)
      ->execute();
  }
  $results_cache = \Drupal::getContainer()
    ->get('search_api.query_helper');
  foreach ($search_ids as $search_id) {
    $this
      ->assertSame($results[$search_id], $results_cache
      ->getResults($search_id));
  }
  $this
    ->assertNull($results_cache
    ->getResults('foobar'));
  $this
    ->assertSame($results, $results_cache
    ->getAllResults());
  $results_cache
    ->removeResults('foo');
  unset($results['foo']);
  $this
    ->assertSame($results, $results_cache
    ->getAllResults());
}