diff --git a/src/Collection.php b/src/Collection.php index 6eacf2280..a4ff6326f 100644 --- a/src/Collection.php +++ b/src/Collection.php @@ -933,6 +933,8 @@ public function listIndexes(array $options = []) */ public function listSearchIndexes(array $options = []): Iterator { + $options = $this->inheritTypeMap($options); + $operation = new ListSearchIndexes($this->databaseName, $this->collectionName, $options); $server = select_server($this->manager, $options); diff --git a/tests/Collection/CollectionFunctionalTest.php b/tests/Collection/CollectionFunctionalTest.php index a919e38d2..89bb26800 100644 --- a/tests/Collection/CollectionFunctionalTest.php +++ b/tests/Collection/CollectionFunctionalTest.php @@ -8,6 +8,7 @@ use MongoDB\Collection; use MongoDB\Database; use MongoDB\Driver\BulkWrite; +use MongoDB\Driver\Exception\CommandException; use MongoDB\Driver\ReadConcern; use MongoDB\Driver\ReadPreference; use MongoDB\Driver\WriteConcern; @@ -23,6 +24,7 @@ use function array_filter; use function call_user_func; use function is_scalar; +use function iterator_to_array; use function json_encode; use function str_contains; use function usort; @@ -800,6 +802,32 @@ public function testMethodInTransactionWithReadConcernOption($method): void } } + public function testListSearchIndexesInheritTypeMap(): void + { + $this->skipIfAtlasSearchIndexIsNotSupported(); + + $collection = new Collection($this->manager, $this->getDatabaseName(), $this->getCollectionName(), ['typeMap' => ['root' => 'array']]); + + // Insert a document to create the collection + $collection->insertOne(['_id' => 1]); + + try { + $collection->createSearchIndex(['mappings' => ['dynamic' => false]], ['name' => 'test-search-index']); + } catch (CommandException $e) { + // Ignore duplicate errors in case this test is re-run too quickly + // Index is asynchronously dropped during tearDown, we only need to + // ensure it exists for this test. + if ($e->getCode() !== 68 /* IndexAlreadyExists */) { + throw $e; + } + } + + $indexes = $collection->listSearchIndexes(); + $indexes = iterator_to_array($indexes); + $this->assertCount(1, $indexes); + $this->assertIsArray($indexes[0]); + } + /** * Create data fixtures. */ diff --git a/tests/FunctionalTestCase.php b/tests/FunctionalTestCase.php index 07c03e45e..51c34cf3c 100644 --- a/tests/FunctionalTestCase.php +++ b/tests/FunctionalTestCase.php @@ -457,6 +457,15 @@ protected function skipIfServerVersion(string $operator, string $version, ?strin } } + protected function skipIfAtlasSearchIndexIsNotSupported(): void + { + if (! self::isAtlas()) { + self::markTestSkipped('Search Indexes are only supported on MongoDB Atlas 7.0+'); + } + + $this->skipIfServerVersion('<', '7.0', 'Search Indexes are only supported on MongoDB Atlas 7.0+'); + } + protected function skipIfChangeStreamIsNotSupported(): void { if ($this->isStandalone()) { diff --git a/tests/SpecTests/SearchIndexSpecTest.php b/tests/SpecTests/SearchIndexSpecTest.php index 31a8a85a6..4fff82ef7 100644 --- a/tests/SpecTests/SearchIndexSpecTest.php +++ b/tests/SpecTests/SearchIndexSpecTest.php @@ -29,13 +29,9 @@ class SearchIndexSpecTest extends FunctionalTestCase public function setUp(): void { - if (! self::isAtlas()) { - self::markTestSkipped('Search Indexes are only supported on MongoDB Atlas 7.0+'); - } - parent::setUp(); - $this->skipIfServerVersion('<', '7.0', 'Search Indexes are only supported on MongoDB Atlas 7.0+'); + $this->skipIfAtlasSearchIndexIsNotSupported(); } /**