You are here

class AcquiaSearchV3ApiClientTest in Acquia Connector 8

Search v3 Client test.

@coversDefaultClass \Drupal\acquia_search\AcquiaSearchV3ApiClient

@group Acquia search

Hierarchy

Expanded class hierarchy of AcquiaSearchV3ApiClientTest

File

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

Namespace

Drupal\Tests\acquia_search\Unit
View source
class AcquiaSearchV3ApiClientTest extends UnitTestCase {

  /**
   * Search V3 API host.
   *
   * @var string
   */
  protected $searchV3Host;

  /**
   * Search V3 API key.
   *
   * @var string
   */
  protected $searchV3ApiKey;

  /**
   * GuzzleHttp Client.
   *
   * @var \Prophecy\Prophecy\ObjectProphecy
   */
  protected $guzzleClient;

  /**
   * Cache backend.
   *
   * @var \Prophecy\Prophecy\ObjectProphecy
   */
  protected $cacheBackend;

  /**
   * {@inheritdoc}
   */
  public function setUp() {
    parent::setUp();
    $this->searchV3Host = 'https://api.sr-dev.acquia.com';
    $this->searchV3ApiKey = 'XXXXXXXXXXyyyyyyyyyyXXXXXXXXXXyyyyyyyyyy';
    $path = '/index/network_id/get_all?network_id=WXYZ-12345';
    $data = [
      'host' => $this->searchV3Host,
      'headers' => [
        'x-api-key' => $this->searchV3ApiKey,
      ],
    ];
    $uri = $data['host'] . $path;
    $options = [
      'headers' => $data['headers'],
      'body' => Json::encode($data),
    ];
    $json = '[{"name":"WXYZ-12345.dev.drupal8","host":"test.sr-dev.acquia.com"}]';
    $stream = $this
      ->prophesize('Psr\\Http\\Message\\StreamInterface');
    $stream
      ->getSize()
      ->willReturn(1000);
    $stream
      ->read(1000)
      ->willReturn($json);
    $response = $this
      ->prophesize('Psr\\Http\\Message\\ResponseInterface');
    $response
      ->getStatusCode()
      ->willReturn(200);
    $response
      ->getBody()
      ->willReturn($stream);
    $this->guzzleClient = $this
      ->prophesize('\\GuzzleHttp\\Client');
    $this->guzzleClient
      ->get($uri, $options)
      ->willReturn($response);
    $this->cacheBackend = $this
      ->prophesize('\\Drupal\\Core\\Cache\\CacheBackendInterface');
  }

  /**
   * Tests call to search v3 api.
   */
  public function testSearchV3ApiCall() {
    $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());
    $this
      ->assertEquals($expected, $client
      ->getSearchV3Indexes('WXYZ-12345'));
    $this->cacheBackend
      ->set('acquia_search.v3indexes', $expected, time() + 24 * 60 * 60)
      ->shouldHaveBeenCalledTimes(1);
  }

  /**
   * Test to validate cache.
   */
  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);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
AcquiaSearchV3ApiClientTest::$cacheBackend protected property Cache backend.
AcquiaSearchV3ApiClientTest::$guzzleClient protected property GuzzleHttp Client.
AcquiaSearchV3ApiClientTest::$searchV3ApiKey protected property Search V3 API key.
AcquiaSearchV3ApiClientTest::$searchV3Host protected property Search V3 API host.
AcquiaSearchV3ApiClientTest::setUp public function Overrides UnitTestCase::setUp
AcquiaSearchV3ApiClientTest::testSearchV3ApiCache public function Test to validate cache.
AcquiaSearchV3ApiClientTest::testSearchV3ApiCall public function Tests call to search v3 api.
PhpunitCompatibilityTrait::getMock Deprecated public function Returns a mock object for the specified class using the available method.
PhpunitCompatibilityTrait::setExpectedException Deprecated public function Compatibility layer for PHPUnit 6 to support PHPUnit 4 code.
UnitTestCase::$randomGenerator protected property The random generator.
UnitTestCase::$root protected property The app root. 1
UnitTestCase::assertArrayEquals protected function Asserts if two arrays are equal by sorting them first.
UnitTestCase::getBlockMockWithMachineName Deprecated protected function Mocks a block with a block plugin. 1
UnitTestCase::getClassResolverStub protected function Returns a stub class resolver.
UnitTestCase::getConfigFactoryStub public function Returns a stub config factory that behaves according to the passed array.
UnitTestCase::getConfigStorageStub public function Returns a stub config storage that returns the supplied configuration.
UnitTestCase::getContainerWithCacheTagsInvalidator protected function Sets up a container with a cache tags invalidator.
UnitTestCase::getRandomGenerator protected function Gets the random generator for the utility methods.
UnitTestCase::getStringTranslationStub public function Returns a stub translation manager that just returns the passed string.
UnitTestCase::randomMachineName public function Generates a unique random string containing letters and numbers.