Skip to content

Commit

Permalink
OEL-167: Add mocked responses.
Browse files Browse the repository at this point in the history
  • Loading branch information
drishu authored and claudiu-cristea committed Jun 8, 2021
1 parent 0b721bd commit 95457b9
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,32 +25,85 @@ class EuropaSearchServerResponse extends PluginBase implements ServiceMockPlugin
* {@inheritdoc}
*/
public function applies(RequestInterface $request, array $options): bool {
$this->collectCalledMethods(__FUNCTION__);
$this->collectCalledMethods($request->getUri()->getPath(), __FUNCTION__);
// @todo implement collectRequests().
return $request->getUri()->getHost() === 'example.com';
}

/**
* {@inheritdoc}
*/
public function getResponse(RequestInterface $request, array $options): ResponseInterface {
$this->collectCalledMethods(__FUNCTION__);
return new Response(200, [], 'Mocking example.com response');
$path = $request->getUri()->getPath();
$this->collectCalledMethods($path, __FUNCTION__);

switch ($path) {
case '/token':
$response = $this->getTokenResponse();
break;

case '/ingest/text':
$response = $this->getIngestTextResponse();
break;

default:
$response = new Response(200, [], 'Mocking example.com response');
break;
}

return $response;
}

/**
* Counts how many times each method of this class were called.
*
* @param string $path
* The request path.
* @param string $method
* The method being called.
*/
protected function collectCalledMethods(string $method): void {
protected function collectCalledMethods(string $path, string $method): void {
$state = \Drupal::state();
$calls = $state->get('oe_search_test.service_mock_calls', [
'applies' => 0,
'getResponse' => 0,
]);
$calls[$method]++;
$calls = $state->get('oe_search_test.service_mock_calls', []);

if (!isset($calls[$path])) {
$calls[$path] = [
'applies' => 0,
'getResponse' => 0,
];
}

$calls[$path][$method]++;
$state->set('oe_search_test.service_mock_calls', $calls);
}

/**
* Get mocked token response.
*
* @return \Psr\Http\Message\ResponseInterface
* The mocked response.
*/
protected function getTokenResponse(): ResponseInterface {
return new Response(200, [], json_encode([
'access_token' => 'JWT_TOKEN',
'scope' => 'APPLICATION_SCOPE',
'token_type' => 'Bearer',
'expires_in' => 3600,
]));
}

/**
* Get mocked ingest text response.
*
* @return \Psr\Http\Message\ResponseInterface
* The mocked response.
*/
protected function getIngestTextResponse(): ResponseInterface {
return new Response(200, [], json_encode([
'apiVersion' => '2.67',
'reference' => 'foo',
'trackingId' => 'bar',
]));
}

}
35 changes: 24 additions & 11 deletions tests/src/Kernel/BackendTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ public function setUp(): void {
public function testIndexItems(): void {
$field_helper = $this->container->get('search_api.fields_helper');
$datasource_manager = $this->container->get('plugin.manager.search_api.datasource');
/** @var \Drupal\oe_search\Plugin\search_api\backend\SearchApiEuropaSearchBackend $backend */
$backend = Server::load($this->serverId)->getBackend();
$index = Index::load($this->indexId);

Expand All @@ -116,29 +117,41 @@ public function testIndexItems(): void {
// indexed by default.
// @see \Drupal\oe_search\Plugin\search_api\backend\SearchApiEuropaSearchBackend::getDocuments()
$backend->indexItems($index, $items);
$this->assertServiceMockCalls(0, 0);
$this->assertServiceMockCalls('/ingest/text', 0, 0);

// Enable ingestion of 'entity_test_mulrev_changed' entities.
// @see \Drupal\oe_search_test\EventSubscriber\OeSearchTestSubscriber::indexEntityTestMulRevChanged()
$this->container->get('state')->set('oe_search_test.enable_document_alter', TRUE);
$backend->indexItems($index, $items);
$this->assertServiceMockCalls(1, 1);

// print_r($items['entity:entity_test_mulrev_changed/1:en']->getOriginalObject()->toArray());
$this->assertServiceMockCalls('/ingest/text', 5, 5);
// @todo fetch collected requests and compare to $items.
}

/**
* Asserts that the service mock methods are called.
*
* @param string $path
* The request path.
* @param int $applies_calls
* Received requests count.
* @param int $get_response_calls
* Count of replies from mocked server.
*
* @throws \Exception
*/
protected function assertServiceMockCalls(int $applies_calls, int $get_response_calls): void {
protected function assertServiceMockCalls(string $path, int $applies_calls, int $get_response_calls): void {
$state = $this->container->get('state');
$calls = $state->get('oe_search_test.service_mock_calls', [
'applies' => 0,
'getResponse' => 0,
]);
$calls = $state->get('oe_search_test.service_mock_calls', []);

if (!isset($calls[$path])) {
$calls[$path] = [
'applies' => 0,
'getResponse' => 0,
];
}

$this->assertSame($applies_calls, $calls['applies']);
$this->assertSame($get_response_calls, $calls['getResponse']);
$this->assertSame($applies_calls, $calls[$path]['applies']);
$this->assertSame($get_response_calls, $calls[$path]['getResponse']);

// Leave the place clean for future assertions.
$state->delete('oe_search_test.service_mock_calls');
Expand Down

0 comments on commit 95457b9

Please sign in to comment.