class AcquiaSearchV3ApiClientTest in Acquia Connector 8
Search v3 Client test.
@coversDefaultClass \Drupal\acquia_search\AcquiaSearchV3ApiClient
@group Acquia search
Hierarchy
- class \Drupal\Tests\UnitTestCase extends \PHPUnit\Framework\TestCase uses PhpunitCompatibilityTrait
- class \Drupal\Tests\acquia_search\Unit\AcquiaSearchV3ApiClientTest
Expanded class hierarchy of AcquiaSearchV3ApiClientTest
File
- acquia_search/
tests/ src/ Unit/ AcquiaSearchV3ApiClientTest.php, line 16
Namespace
Drupal\Tests\acquia_search\UnitView 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
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
AcquiaSearchV3ApiClientTest:: |
protected | property | Cache backend. | |
AcquiaSearchV3ApiClientTest:: |
protected | property | GuzzleHttp Client. | |
AcquiaSearchV3ApiClientTest:: |
protected | property | Search V3 API key. | |
AcquiaSearchV3ApiClientTest:: |
protected | property | Search V3 API host. | |
AcquiaSearchV3ApiClientTest:: |
public | function |
Overrides UnitTestCase:: |
|
AcquiaSearchV3ApiClientTest:: |
public | function | Test to validate cache. | |
AcquiaSearchV3ApiClientTest:: |
public | function | Tests call to search v3 api. | |
PhpunitCompatibilityTrait:: |
public | function | Returns a mock object for the specified class using the available method. | |
PhpunitCompatibilityTrait:: |
public | function | Compatibility layer for PHPUnit 6 to support PHPUnit 4 code. | |
UnitTestCase:: |
protected | property | The random generator. | |
UnitTestCase:: |
protected | property | The app root. | 1 |
UnitTestCase:: |
protected | function | Asserts if two arrays are equal by sorting them first. | |
UnitTestCase:: |
protected | function | Mocks a block with a block plugin. | 1 |
UnitTestCase:: |
protected | function | Returns a stub class resolver. | |
UnitTestCase:: |
public | function | Returns a stub config factory that behaves according to the passed array. | |
UnitTestCase:: |
public | function | Returns a stub config storage that returns the supplied configuration. | |
UnitTestCase:: |
protected | function | Sets up a container with a cache tags invalidator. | |
UnitTestCase:: |
protected | function | Gets the random generator for the utility methods. | |
UnitTestCase:: |
public | function | Returns a stub translation manager that just returns the passed string. | |
UnitTestCase:: |
public | function | Generates a unique random string containing letters and numbers. |