You are here

public function AcquiaSearchV3ApiClientTest::testSearchV3ApiCache in Acquia Connector 8

Test to validate cache.

File

acquia_search/tests/src/Unit/AcquiaSearchV3ApiClientTest.php, line 102

Class

AcquiaSearchV3ApiClientTest
Search v3 Client test.

Namespace

Drupal\Tests\acquia_search\Unit

Code

public function testSearchV3ApiCache() {
  $expected = [
    [
      'balancer' => 'test.sr-dev.acquia.com',
      'core_id' => 'WXYZ-12345.dev.drupal8',
      'version' => 'v3',
    ],
  ];
  $client = new AcquiaSearchV3ApiClient($this->searchV3Host, $this->searchV3ApiKey, $this->guzzleClient
    ->reveal(), $this->cacheBackend
    ->reveal());
  $fresh_cache = (object) [
    'data' => $expected,
    'expire' => time() + 24 * 60 * 60,
  ];
  $this->cacheBackend
    ->get('acquia_search.v3indexes')
    ->willReturn($fresh_cache);
  $client
    ->getSearchV3Indexes('WXYZ-12345');

  // New cache should not have been set when there is already a valid cache.
  $this->cacheBackend
    ->set('acquia_search.v3indexes', $expected, time() + 24 * 60 * 60)
    ->shouldHaveBeenCalledTimes(0);
  $expired_cache = (object) [
    'data' => $expected,
    'expire' => 0,
  ];
  $this->cacheBackend
    ->get('acquia_search.v3indexes')
    ->willReturn($expired_cache);
  $client
    ->getSearchV3Indexes('WXYZ-12345');

  // When the current cache value is expired, it should have set a new one.
  $this->cacheBackend
    ->set('acquia_search.v3indexes', $expected, time() + 24 * 60 * 60)
    ->shouldHaveBeenCalledTimes(1);
}