|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace MongoDB\Tests\Collection; |
| 4 | + |
| 5 | +use MongoDB\Driver\Cursor; |
| 6 | +use MongoDB\Tests\FunctionalTestCase; |
| 7 | + |
| 8 | +use function bin2hex; |
| 9 | +use function count; |
| 10 | +use function current; |
| 11 | +use function getenv; |
| 12 | +use function random_bytes; |
| 13 | +use function sleep; |
| 14 | + |
| 15 | +/** |
| 16 | + * Functional tests for the Atlas Search index management. |
| 17 | + */ |
| 18 | +class SearchIndexFunctionalTest extends FunctionalTestCase |
| 19 | +{ |
| 20 | + public function testIndexLifecycle(): void |
| 21 | + { |
| 22 | + $atlasUri = getenv('MONGODB_ATLAS_URI'); |
| 23 | + |
| 24 | + if (! $atlasUri) { |
| 25 | + $this->markTestSkipped('MONGODB_ATLAS_URI environment variable is not set'); |
| 26 | + } |
| 27 | + |
| 28 | + $this->manager = static::createTestManager($atlasUri); |
| 29 | + $collection = $this->createCollection($this->getDatabaseName(), $this->getCollectionName()); |
| 30 | + |
| 31 | + $name = 'search_index_' . bin2hex(random_bytes(5)); |
| 32 | + |
| 33 | + // Create a search index |
| 34 | + $result = $collection->createSearchIndex($name, [ |
| 35 | + 'mappings' => ['dynamic' => true], |
| 36 | + ]); |
| 37 | + $this->assertSame($name, $result); |
| 38 | + |
| 39 | + // Wait for the index to be ready |
| 40 | + $count = 0; |
| 41 | + do { |
| 42 | + sleep(1); |
| 43 | + $result = $collection->listSearchIndexes($name); |
| 44 | + $this->assertInstanceOf(Cursor::class, $result); |
| 45 | + $index = current($result->toArray()); |
| 46 | + $this->assertObjectHasAttribute('queryable', $index); |
| 47 | + |
| 48 | + if ($count++ > 100) { |
| 49 | + $this->fail('Search index did not become queryable'); |
| 50 | + } |
| 51 | + } while (! $index->queryable); |
| 52 | + |
| 53 | + // Update the search index |
| 54 | + $collection->updateSearchIndex($name, [ |
| 55 | + 'mappings' => ['dynamic' => false], |
| 56 | + ]); |
| 57 | + |
| 58 | + // Delete the search index |
| 59 | + $collection->dropSearchIndex($name); |
| 60 | + |
| 61 | + $count = 0; |
| 62 | + do { |
| 63 | + sleep(1); |
| 64 | + $result = $collection->listSearchIndexes($name); |
| 65 | + $this->assertInstanceOf(Cursor::class, $result); |
| 66 | + |
| 67 | + if ($count++ > 100) { |
| 68 | + $this->fail('Search index was not deleted'); |
| 69 | + } |
| 70 | + } while (count($result->toArray()) > 0); |
| 71 | + } |
| 72 | +} |
0 commit comments