Skip to content

Commit

Permalink
Add cov for removeAllDocuments()
Browse files Browse the repository at this point in the history
  • Loading branch information
Chris Penny committed Aug 1, 2022
1 parent e651ad1 commit 13418a7
Show file tree
Hide file tree
Showing 2 changed files with 123 additions and 4 deletions.
9 changes: 5 additions & 4 deletions src/Service/EnterpriseSearch/EnterpriseSearchService.php
Original file line number Diff line number Diff line change
Expand Up @@ -177,8 +177,6 @@ public function removeDocuments(array $items): array
*/
public function removeAllDocuments(string $indexName): int
{
// TODO NEEDS MANUAL TESTING
// TODO NEEDS UNIT TESTING
$cfg = $this->getConfiguration();
$client = $this->getClient();
$indexName = static::environmentizeIndex($indexName);
Expand All @@ -205,11 +203,14 @@ public function removeAllDocuments(string $indexName): int

// Actually delete the documents
$deletedDocs = $client->appSearch()
->deleteDocuments(new DeleteDocuments($indexName, $idsToRemove));
->deleteDocuments(new DeleteDocuments($indexName, $idsToRemove))
->asArray();

// Keep an accurate running count of the number of documents deleted.
foreach ($deletedDocs as $doc) {
if (is_array($doc) && isset($doc['deleted']) && $doc['deleted'] === true) {
$deleted = $doc['deleted'] ?? false;

if ($deleted) {
$numDeleted++;
}
}
Expand Down
118 changes: 118 additions & 0 deletions tests/Service/EnterpriseSearch/EnterpriseSearchServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1467,6 +1467,124 @@ public function testRemoveDocumentEmpty(): void
$this->assertEquals(0, $this->mock->count());
}

public function testRemoveAllDocuments(): void
{
// Valid headers
$headers = [
'Content-Type' => 'application/json;charset=utf-8',
];
// First response, listing out the documents that are available (which we'll then remove)
$bodyOne = json_encode([
'meta' => [
'page' => [
'current' => 1,
'total_pages' => 1,
'total_results' => 2,
'size' => 3,
],
],
'results' => [
[
'id' => 'doc1',
'record_id' => '1',
],
[
'id' => 'doc2',
'record_id' => '2',
],
[
'id' => 'doc3',
'record_id' => '3',
],
],
]);
// Second response is from our delete request. Adding a mix of deleted true/false. The way our "remove all"
// feature works is that we request a list of all currently available documents, and then request that they
// are removed by their IDs. It is possible that documents are removed through the Elastic Admin UI between
// our two requests. deleted = false does *not* get added to the count of removed documents
$bodyTwo = json_encode([
[
'id' => 'doc1',
'deleted' => true,
],
[
'id' => 'doc2',
'deleted' => false,
],
[
'id' => 'doc3',
'deleted' => true,
],
]);
// Third response, listing out the documents that are available after our delete request. We'll return some
// more items to be deleted (testing that the loop functions)
$bodyThree = json_encode([
'meta' => [
'page' => [
'current' => 1,
// Two pages of results, 3 here, and then we'll have 2 more later
'total_pages' => 2,
// Total of 4 documents, 3 of which are presented now
'total_results' => 5,
'size' => 3,
],
],
'results' => [
[
'id' => 'doc1',
'record_id' => '1',
],
[
'id' => 'doc2',
'record_id' => '2',
],
[
'id' => 'doc3',
'record_id' => '3',
],
],
]);
// Fourth response is from the second delete request
$bodyFour = json_encode([
[
'id' => 'doc4',
'deleted' => true,
],
[
'id' => 'doc5',
'deleted' => true,
],
]);
// Fifth (and final) response is for when we request available documents after our second delete request. We'll
// return no results here, indicating that everything has been deleted
$bodyFive = json_encode([
'meta' => [
'page' => [
'current' => 1,
'total_pages' => 1,
'total_results' => 0,
'size' => 100,
],
],
'results' => [],
]);

// Append our mocks
$this->mock->append(new Response(200, $headers, $bodyOne));
$this->mock->append(new Response(200, $headers, $bodyTwo));
$this->mock->append(new Response(200, $headers, $bodyThree));
$this->mock->append(new Response(200, $headers, $bodyFour));
$this->mock->append(new Response(200, $headers, $bodyFive));

$numRemoved = $this->searchService->removeAllDocuments('content');

// A total of 5 documents were requested to be removed, but only 4 returned deleted = true
$this->assertEqualsCanonicalizing(4, $numRemoved);
// And make sure nothing is left in our Response Stack. This would indicate that every Request we expect to make
// has been made
$this->assertEquals(0, $this->mock->count());
}

protected function setUp(): void
{
parent::setUp();
Expand Down

0 comments on commit 13418a7

Please sign in to comment.