From 95457b9ca96a118da578264df5d0d28316830228 Mon Sep 17 00:00:00 2001 From: drishu Date: Tue, 8 Jun 2021 10:46:51 +0000 Subject: [PATCH] OEL-167: Add mocked responses. --- .../EuropaSearchServerResponse.php | 71 ++++++++++++++++--- tests/src/Kernel/BackendTest.php | 35 ++++++--- 2 files changed, 86 insertions(+), 20 deletions(-) diff --git a/tests/modules/oe_search_test/src/Plugin/ServiceMock/EuropaSearchServerResponse.php b/tests/modules/oe_search_test/src/Plugin/ServiceMock/EuropaSearchServerResponse.php index edc8626..87be4a2 100644 --- a/tests/modules/oe_search_test/src/Plugin/ServiceMock/EuropaSearchServerResponse.php +++ b/tests/modules/oe_search_test/src/Plugin/ServiceMock/EuropaSearchServerResponse.php @@ -25,7 +25,8 @@ 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'; } @@ -33,24 +34,76 @@ public function applies(RequestInterface $request, array $options): bool { * {@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', + ])); + } + } diff --git a/tests/src/Kernel/BackendTest.php b/tests/src/Kernel/BackendTest.php index b12aa8f..c29dab9 100644 --- a/tests/src/Kernel/BackendTest.php +++ b/tests/src/Kernel/BackendTest.php @@ -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); @@ -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');