Skip to content

Commit d84420a

Browse files
committed
Run unified index management tests
1 parent a2bfd13 commit d84420a

File tree

11 files changed

+72
-20
lines changed

11 files changed

+72
-20
lines changed

src/Collection.php

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -386,15 +386,15 @@ public function createIndexes(array $indexes, array $options = [])
386386
*
387387
* @see https://www.mongodb.com/docs/manual/reference/command/createSearchIndexes/
388388
* @see https://mongodb.com/docs/manual/reference/method/db.collection.createSearchIndex/
389-
* @param string $name List of search index specifications
390-
* @param array $definition Atlas Search index definition
391-
* @param array $options Command options
389+
* @param string $name List of search index specifications
390+
* @param array|object $definition Atlas Search index definition
391+
* @param array $options Command options
392392
* @return string The name of the created search index
393393
* @throws UnsupportedException if options are not supported by the selected server
394394
* @throws InvalidArgumentException for parameter/option parsing errors
395395
* @throws DriverRuntimeException for other driver errors (e.g. connection errors)
396396
*/
397-
public function createSearchIndex(string $name, array $definition, array $options): string
397+
public function createSearchIndex(string $name, $definition, array $options = []): string
398398
{
399399
$names = $this->createSearchIndexes([['name' => $name, 'definition' => $definition]], $options);
400400

@@ -1009,6 +1009,10 @@ public function listIndexes(array $options = [])
10091009
*/
10101010
public function listSearchIndexes(string $name, array $aggregationOptions = [], array $listIndexOptions = [])
10111011
{
1012+
if ($name) {
1013+
$listIndexOptions += ['name' => $name];
1014+
}
1015+
10121016
$operation = new ListSearchIndexes($this->databaseName, $this->collectionName, $aggregationOptions, $listIndexOptions);
10131017
$server = select_server($this->manager, $listIndexOptions);
10141018

@@ -1178,16 +1182,16 @@ public function updateOne($filter, $update, array $options = [])
11781182
/**
11791183
* Update a single Atlas Search index in the collection.
11801184
*
1181-
* @param string $name Search index name
1182-
* @param array $definition Atlas Search index definition
1183-
* @param array $options Command options
1185+
* @param string $name Search index name
1186+
* @param array|object $definition Atlas Search index definition
1187+
* @param array $options Command options
11841188
* @throws UnsupportedException if options are not supported by the selected server
11851189
* @throws InvalidArgumentException for parameter parsing errors
11861190
* @throws DriverRuntimeException for other driver errors (e.g. connection errors)
11871191
*/
1188-
public function updateSearchIndex(string $name, array $definition, array $options = []): void
1192+
public function updateSearchIndex(string $name, $definition, array $options = []): void
11891193
{
1190-
$operation = new UpdateSearchIndex($this->databaseName, $this->collectionName, $name, $definition, $options);
1194+
$operation = new UpdateSearchIndex($this->databaseName, $this->collectionName, $name, $definition);
11911195
$server = select_server($this->manager, $options);
11921196

11931197
$operation->execute($server);

src/Operation/ListSearchIndexes.php

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -91,13 +91,14 @@ public function execute(Server $server)
9191
private function executeCommand(Server $server): IndexInfoIteratorIterator
9292
{
9393
$cmd = [
94-
'listSearchIndexes' => $this->collectionName,
94+
'aggregate' => $this->collectionName,
95+
'pipeline' => [
96+
['$listSearchIndexes' => (object) $this->listIndexOptions],
97+
],
9598
];
9699

97-
foreach (['id', 'name'] as $option) {
98-
if (isset($this->listIndexOptions[$option])) {
99-
$cmd[$option] = $this->listIndexOptions[$option];
100-
}
100+
if (! empty($this->aggregationOptions)) {
101+
$cmd['cursor'] = (object) $this->aggregationOptions;
101102
}
102103

103104
try {

src/Operation/UpdateSearchIndex.php

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@
2323
use MongoDB\Exception\InvalidArgumentException;
2424
use MongoDB\Exception\UnsupportedException;
2525

26+
use function is_array;
27+
use function is_object;
28+
2629
/**
2730
* Operation for the createIndexes command.
2831
*
@@ -41,24 +44,28 @@ class UpdateSearchIndex implements Executable
4144
/** @var string */
4245
private $name;
4346

44-
/** @var array */
47+
/** @var array|object */
4548
private $definition;
4649

4750
/**
4851
* Constructs a createSearchIndexes command.
4952
*
50-
* @param string $databaseName Database name
51-
* @param string $collectionName Collection name
52-
* @param string $name Search index name
53-
* @param array $definition Atlas Search index definition
53+
* @param string $databaseName Database name
54+
* @param string $collectionName Collection name
55+
* @param string $name Search index name
56+
* @param array|object $definition Atlas Search index definition
5457
* @throws InvalidArgumentException for parameter parsing errors
5558
*/
56-
public function __construct(string $databaseName, string $collectionName, string $name, array $definition)
59+
public function __construct(string $databaseName, string $collectionName, string $name, $definition)
5760
{
5861
if ($name === '') {
5962
throw new InvalidArgumentException('Index name cannot be empty');
6063
}
6164

65+
if (! is_array($definition) || ! is_object($definition)) {
66+
throw InvalidArgumentException::invalidType('$definition', $definition, 'array or object');
67+
}
68+
6269
$this->databaseName = $databaseName;
6370
$this->collectionName = $collectionName;
6471
$this->name = $name;

tests/UnifiedSpecTests/Operation.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
use function hex2bin;
3737
use function iterator_to_array;
3838
use function key;
39+
use function MongoDB\document_to_array;
3940
use function MongoDB\with_transaction;
4041
use function PHPUnit\Framework\assertArrayHasKey;
4142
use function PHPUnit\Framework\assertContains;
@@ -541,6 +542,29 @@ private function executeForCollection(Collection $collection)
541542
array_diff_key($args, ['to' => 1]),
542543
);
543544

545+
case 'createSearchIndex':
546+
return $collection->createSearchIndex($args['model']->name ?? '', $args['model']->definition);
547+
548+
case 'createSearchIndexes':
549+
return $collection->createSearchIndexes($args['models']);
550+
551+
case 'dropSearchIndex':
552+
assertArrayHasKey('name', $args);
553+
assertIsString($args['name']);
554+
555+
return $collection->dropSearchIndex($args['name']);
556+
557+
case 'updateSearchIndex':
558+
assertArrayHasKey('name', $args);
559+
assertArrayHasKey('definition', $args);
560+
assertIsString($args['name']);
561+
assertInstanceOf(stdClass::class, $args['definition']);
562+
563+
return $collection->updateSearchIndex($args['name'], $args['definition']);
564+
565+
case 'listSearchIndexes':
566+
return $collection->listSearchIndexes($args['name'] ?? '', document_to_array($args['aggregationOptions'] ?? []), document_to_array(($args['listIndexOptions'] ?? [])));
567+
544568
default:
545569
Assert::fail('Unsupported collection operation: ' . $this->name);
546570
}

tests/UnifiedSpecTests/UnifiedSpecTest.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -273,6 +273,17 @@ public function provideFailingTests()
273273
yield from $this->provideTests(__DIR__ . '/valid-fail/*.json');
274274
}
275275

276+
/** @dataProvider provideIndexManagementTests */
277+
public function testIndexManagement(UnifiedTestCase $test): void
278+
{
279+
self::$runner->run($test);
280+
}
281+
282+
public function provideIndexManagementTests()
283+
{
284+
yield from $this->provideTests(__DIR__ . '/index-management/*.json');
285+
}
286+
276287
private function provideTests(string $pattern): Generator
277288
{
278289
foreach (glob($pattern) as $filename) {

tests/UnifiedSpecTests/Util.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,8 @@ final class Util
8888
'createChangeStream' => ['pipeline', 'session', 'fullDocument', 'fullDocumentBeforeChange', 'resumeAfter', 'startAfter', 'startAtOperationTime', 'batchSize', 'collation', 'maxAwaitTimeMS', 'comment', 'showExpandedEvents'],
8989
'createFindCursor' => ['filter', 'session', 'allowDiskUse', 'allowPartialResults', 'batchSize', 'collation', 'comment', 'cursorType', 'hint', 'limit', 'max', 'maxAwaitTimeMS', 'maxScan', 'maxTimeMS', 'min', 'modifiers', 'noCursorTimeout', 'oplogReplay', 'projection', 'returnKey', 'showRecordId', 'skip', 'snapshot', 'sort'],
9090
'createIndex' => ['keys', 'comment', 'commitQuorum', 'maxTimeMS', 'name', 'session', 'unique'],
91+
'createSearchIndex' => ['model'],
92+
'createSearchIndexes' => ['models'],
9193
'dropIndex' => ['name', 'session', 'maxTimeMS', 'comment'],
9294
'count' => ['filter', 'session', 'collation', 'hint', 'limit', 'maxTimeMS', 'skip', 'comment'],
9395
'countDocuments' => ['filter', 'session', 'limit', 'skip', 'collation', 'hint', 'maxTimeMS', 'comment'],
@@ -97,6 +99,7 @@ final class Util
9799
'findOneAndDelete' => ['let', 'filter', 'session', 'projection', 'arrayFilters', 'bypassDocumentValidation', 'collation', 'hint', 'maxTimeMS', 'new', 'sort', 'update', 'upsert', 'comment'],
98100
'distinct' => ['fieldName', 'filter', 'session', 'collation', 'maxTimeMS', 'comment'],
99101
'drop' => ['session', 'comment'],
102+
'dropSearchIndex' => ['name'],
100103
'find' => ['let', 'filter', 'session', 'allowDiskUse', 'allowPartialResults', 'batchSize', 'collation', 'comment', 'cursorType', 'hint', 'limit', 'max', 'maxAwaitTimeMS', 'maxScan', 'maxTimeMS', 'min', 'modifiers', 'noCursorTimeout', 'oplogReplay', 'projection', 'returnKey', 'showRecordId', 'skip', 'snapshot', 'sort'],
101104
'findOne' => ['let', 'filter', 'session', 'allowDiskUse', 'allowPartialResults', 'batchSize', 'collation', 'comment', 'cursorType', 'hint', 'max', 'maxAwaitTimeMS', 'maxScan', 'maxTimeMS', 'min', 'modifiers', 'noCursorTimeout', 'oplogReplay', 'projection', 'returnKey', 'showRecordId', 'skip', 'snapshot', 'sort'],
102105
'findOneAndReplace' => ['let', 'returnDocument', 'filter', 'replacement', 'session', 'projection', 'returnDocument', 'upsert', 'arrayFilters', 'bypassDocumentValidation', 'collation', 'hint', 'maxTimeMS', 'new', 'remove', 'sort', 'comment'],
@@ -105,9 +108,11 @@ final class Util
105108
'findOneAndUpdate' => ['let', 'returnDocument', 'filter', 'update', 'session', 'upsert', 'projection', 'remove', 'arrayFilters', 'bypassDocumentValidation', 'collation', 'hint', 'maxTimeMS', 'sort', 'comment'],
106109
'updateMany' => ['let', 'filter', 'update', 'session', 'upsert', 'arrayFilters', 'bypassDocumentValidation', 'collation', 'hint', 'comment'],
107110
'updateOne' => ['let', 'filter', 'update', 'session', 'upsert', 'arrayFilters', 'bypassDocumentValidation', 'collation', 'hint', 'comment'],
111+
'updateSearchIndex' => ['name', 'definition'],
108112
'insertMany' => ['documents', 'session', 'ordered', 'bypassDocumentValidation', 'comment'],
109113
'insertOne' => ['document', 'session', 'bypassDocumentValidation', 'comment'],
110114
'listIndexes' => ['session', 'maxTimeMS', 'comment'],
115+
'listSearchIndexes' => ['name', 'aggregationOptions'],
111116
'mapReduce' => ['map', 'reduce', 'out', 'session', 'bypassDocumentValidation', 'collation', 'finalize', 'jsMode', 'limit', 'maxTimeMS', 'query', 'scope', 'sort', 'verbose', 'comment'],
112117
],
113118
ChangeStream::class => [

0 commit comments

Comments
 (0)