Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/Service/BifrostService.php
Original file line number Diff line number Diff line change
Expand Up @@ -533,15 +533,15 @@ private function getContentMapForDocuments(string $indexSuffix, array $documents
$indexes = $this->getConfiguration()->getIndexConfigurationsForDocument($document);

if (!$indexes) {
Injector::inst()->get(LoggerInterface::class)->warn(
Injector::inst()->get(LoggerInterface::class)->warning(
sprintf('No valid indexes found for document %s, skipping...', $document->getIdentifier())
);

continue;
}

if (!in_array($indexSuffix, array_keys($indexes), true)) {
Injector::inst()->get(LoggerInterface::class)->warn(
Injector::inst()->get(LoggerInterface::class)->warning(
sprintf(
'%s is not a valid index for document %s, skipping...',
$indexSuffix,
Expand Down
43 changes: 36 additions & 7 deletions tests/Service/BifrostServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use SilverStripe\Forager\Extensions\SearchServiceExtension;
use SilverStripe\Forager\Service\DocumentBuilder;
use SilverStripe\Forager\Service\IndexConfiguration;
use SilverStripe\Forager\Service\IndexData;
use SilverStripe\ForagerBifrost\Service\BifrostService;
use SilverStripe\ForagerBifrost\Service\ClientFactory;
use SilverStripe\ForagerBifrost\Tests\Fake\DataObjectFake;
Expand Down Expand Up @@ -255,8 +256,14 @@ public function testGetContentMapForDocuments(): void
$reflectionMethod = new ReflectionMethod(BifrostService::class, 'getContentMapForDocuments');
$reflectionMethod->setAccessible(true);

// Invoke our method which will trigger 2 API calls, and we're expecting the second API call to trigger an error
$this->assertEquals($expectedMap, $reflectionMethod->invoke($this->searchService, 'content', $documents));
$indexData = $this->searchService->getConfiguration()->getIndexDataForSuffix('content');
$indexData->withIndexContext(
function (IndexData $index) use ($expectedMap, $reflectionMethod, $documents): void {
// Invoke our method which will trigger 2 API calls, and we're expecting the second API call to trigger an error
$this->assertEquals($expectedMap, $reflectionMethod->invoke($this->searchService, 'content', $documents));

}
);
}

public function testConfigureNewField(): void
Expand Down Expand Up @@ -700,8 +707,14 @@ public function testAddDocuments(): void
'doc-123',
'321',
];
$resultIds = [];

$resultIds = $this->searchService->addDocuments('content', $documents);
$indexData = $this->searchService->getConfiguration()->getIndexDataForSuffix('content');
$indexData->withIndexContext(
function (IndexData $index) use (&$resultIds, $documents): void {
$resultIds = $this->searchService->addDocuments('content', $documents);
}
);

$this->assertEqualsCanonicalizing($expectedIds, $resultIds);
// And make sure nothing is left in our Response Stack. This would indicate that every Request we expect to make
Expand Down Expand Up @@ -738,7 +751,12 @@ public function testAddDocument(): void
// Append this mock response to our stack
$this->mock->append(new Response(200, $headers, $body));

$resultId = $this->searchService->addDocument('content', $document);
$indexData = $this->searchService->getConfiguration()->getIndexDataForSuffix('content');
$indexData->withIndexContext(
function (IndexData $index) use (&$resultId, $document): void {
$resultId = $this->searchService->addDocument('content', $document);
}
);

$this->assertEquals('doc-123', $resultId);
// And make sure nothing is left in our Response Stack. This would indicate that every Request we expect to make
Expand All @@ -761,8 +779,13 @@ public function testAddDocumentEmpty(): void
// Append this mock response to our stack
$this->mock->append(new Response(200, $headers, $body));

// Kinda just checking that the array_shift correctly returns null if no results were presented from Bifrost
$resultId = $this->searchService->addDocument('content', $document);
$indexData = $this->searchService->getConfiguration()->getIndexDataForSuffix('content');
$indexData->withIndexContext(
function (IndexData $index) use (&$resultId, $document): void {
// Kinda just checking that the array_shift correctly returns null if no results were presented from Bifrost
$resultId = $this->searchService->addDocument('content', $document);
}
);

$this->assertNull($resultId);
// And make sure nothing is left in our Response Stack. This would indicate that every Request we expect to make
Expand Down Expand Up @@ -817,8 +840,14 @@ public function testRemoveDocuments(): void
sprintf('silverstripe_searchservice_tests_fake_dataobjectfake_%s', $documentThree->ID),
'123',
];
$resultIds = [];

$resultIds = $this->searchService->addDocuments('content', $documents);
$indexData = $this->searchService->getConfiguration()->getIndexDataForSuffix('content');
$indexData->withIndexContext(
function (IndexData $index) use (&$resultIds, $documents): void {
$resultIds = $this->searchService->addDocuments('content', $documents);
}
);

$this->assertEqualsCanonicalizing($expectedIds, $resultIds);
// And make sure nothing is left in our Response Stack. This would indicate that every Request we expect to make
Expand Down