Skip to content

Commit 85e8502

Browse files
authored
PHPLIB-1548 Inherit typeMap option in Collection::listSearchIndexes() (#1482)
1 parent 985484d commit 85e8502

File tree

4 files changed

+40
-5
lines changed

4 files changed

+40
-5
lines changed

src/Collection.php

+2
Original file line numberDiff line numberDiff line change
@@ -933,6 +933,8 @@ public function listIndexes(array $options = [])
933933
*/
934934
public function listSearchIndexes(array $options = []): Iterator
935935
{
936+
$options = $this->inheritTypeMap($options);
937+
936938
$operation = new ListSearchIndexes($this->databaseName, $this->collectionName, $options);
937939
$server = select_server($this->manager, $options);
938940

tests/Collection/CollectionFunctionalTest.php

+28
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use MongoDB\Collection;
99
use MongoDB\Database;
1010
use MongoDB\Driver\BulkWrite;
11+
use MongoDB\Driver\Exception\CommandException;
1112
use MongoDB\Driver\ReadConcern;
1213
use MongoDB\Driver\ReadPreference;
1314
use MongoDB\Driver\WriteConcern;
@@ -23,6 +24,7 @@
2324
use function array_filter;
2425
use function call_user_func;
2526
use function is_scalar;
27+
use function iterator_to_array;
2628
use function json_encode;
2729
use function str_contains;
2830
use function usort;
@@ -800,6 +802,32 @@ public function testMethodInTransactionWithReadConcernOption($method): void
800802
}
801803
}
802804

805+
public function testListSearchIndexesInheritTypeMap(): void
806+
{
807+
$this->skipIfAtlasSearchIndexIsNotSupported();
808+
809+
$collection = new Collection($this->manager, $this->getDatabaseName(), $this->getCollectionName(), ['typeMap' => ['root' => 'array']]);
810+
811+
// Insert a document to create the collection
812+
$collection->insertOne(['_id' => 1]);
813+
814+
try {
815+
$collection->createSearchIndex(['mappings' => ['dynamic' => false]], ['name' => 'test-search-index']);
816+
} catch (CommandException $e) {
817+
// Ignore duplicate errors in case this test is re-run too quickly
818+
// Index is asynchronously dropped during tearDown, we only need to
819+
// ensure it exists for this test.
820+
if ($e->getCode() !== 68 /* IndexAlreadyExists */) {
821+
throw $e;
822+
}
823+
}
824+
825+
$indexes = $collection->listSearchIndexes();
826+
$indexes = iterator_to_array($indexes);
827+
$this->assertCount(1, $indexes);
828+
$this->assertIsArray($indexes[0]);
829+
}
830+
803831
/**
804832
* Create data fixtures.
805833
*/

tests/FunctionalTestCase.php

+9
Original file line numberDiff line numberDiff line change
@@ -457,6 +457,15 @@ protected function skipIfServerVersion(string $operator, string $version, ?strin
457457
}
458458
}
459459

460+
protected function skipIfAtlasSearchIndexIsNotSupported(): void
461+
{
462+
if (! self::isAtlas()) {
463+
self::markTestSkipped('Search Indexes are only supported on MongoDB Atlas 7.0+');
464+
}
465+
466+
$this->skipIfServerVersion('<', '7.0', 'Search Indexes are only supported on MongoDB Atlas 7.0+');
467+
}
468+
460469
protected function skipIfChangeStreamIsNotSupported(): void
461470
{
462471
if ($this->isStandalone()) {

tests/SpecTests/SearchIndexSpecTest.php

+1-5
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,9 @@ class SearchIndexSpecTest extends FunctionalTestCase
2929

3030
public function setUp(): void
3131
{
32-
if (! self::isAtlas()) {
33-
self::markTestSkipped('Search Indexes are only supported on MongoDB Atlas 7.0+');
34-
}
35-
3632
parent::setUp();
3733

38-
$this->skipIfServerVersion('<', '7.0', 'Search Indexes are only supported on MongoDB Atlas 7.0+');
34+
$this->skipIfAtlasSearchIndexIsNotSupported();
3935
}
4036

4137
/**

0 commit comments

Comments
 (0)