search_api_meilisearch-1.0.x-dev/tests/src/Kernel/Backend/ReadOnlyTests.php
tests/src/Kernel/Backend/ReadOnlyTests.php
<?php
namespace Drupal\Tests\search_api_meilisearch\Kernel\Backend;
use Drupal\search_api_meilisearch\Api\MeilisearchApiService;
use PHPUnit\Framework\Attributes\Group;
/**
* Tests the read-only backend.
*
* @group search_api_meilisearch
*/
#[Group('search_api_meilisearch')]
class ReadOnlyTests extends BackendKernelTestBase {
protected const TEST_INDEX = 'kernel_backend_read_only_test_index';
/**
* Test that normal index will call removeIndex method.
*/
public function testNormalIndex(): void {
$this->mockMeiliService(1);
$this->prepareTestIndex(self::TEST_INDEX);
// Make sure that index is not read-only.
$this->assertFalse($this->index->isReadOnly());
$backend = $this->index->getServerInstance()->getBackend();
$backend->removeIndex($this->index);
}
/**
* Test that read-only index will not call removeIndex method.
*/
public function testReadOnlyIndex(): void {
$this->mockMeiliService(0);
$this->prepareTestIndex(self::TEST_INDEX, TRUE);
// Make sure that index is read-only.
$this->assertTrue($this->index->isReadOnly());
$backend = $this->index->getServerInstance()->getBackend();
$backend->removeIndex($this->index);
}
/**
* Mocks the MeilisearchApiService. And sets the expectations.
*
* @param int $numOfRemoveIndexCalls
* The number of times the removeIndex method will be called.
*/
protected function mockMeiliService(int $numOfRemoveIndexCalls): void {
$mockMeilisearchApiService = $this->getMockBuilder(MeilisearchApiService::class)
->disableOriginalConstructor()
->getMock();
$mockMeilisearchApiService->expects($this->any())
->method('createIndex')
->willReturn(['taskUid' => 1]);
$mockMeilisearchApiService->expects($this->any())
->method('addDocuments')
->willReturn(['taskUid' => 2]);
$mockMeilisearchApiService->expects($this->any())
->method('waitForUpdate')
->willReturn(['status' => 'success']);
$mockMeilisearchApiService->expects($this->exactly($numOfRemoveIndexCalls))
->method('removeIndex')
->willReturn(TRUE);
$this->container->set('search_api_meilisearch.api', $mockMeilisearchApiService);
}
/**
* {@inheritdoc}
*/
protected function getTestIndexes(): array {
return [self::TEST_INDEX];
}
}
