Skip to content

Commit 7cbe4b8

Browse files
committed
Run unified index management tests
1 parent a42baa8 commit 7cbe4b8

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
@@ -399,15 +399,15 @@ public function createIndexes(array $indexes, array $options = [])
399399
*
400400
* @see https://www.mongodb.com/docs/manual/reference/command/createSearchIndexes/
401401
* @see https://mongodb.com/docs/manual/reference/method/db.collection.createSearchIndex/
402-
* @param string $name List of search index specifications
403-
* @param array $definition Atlas Search index definition
404-
* @param array $options Command options
402+
* @param string $name List of search index specifications
403+
* @param array|object $definition Atlas Search index definition
404+
* @param array $options Command options
405405
* @return string The name of the created search index
406406
* @throws UnsupportedException if options are not supported by the selected server
407407
* @throws InvalidArgumentException for parameter/option parsing errors
408408
* @throws DriverRuntimeException for other driver errors (e.g. connection errors)
409409
*/
410-
public function createSearchIndex(string $name, array $definition, array $options): string
410+
public function createSearchIndex(string $name, $definition, array $options = []): string
411411
{
412412
$names = $this->createSearchIndexes([['name' => $name, 'definition' => $definition]], $options);
413413

@@ -1022,6 +1022,10 @@ public function listIndexes(array $options = [])
10221022
*/
10231023
public function listSearchIndexes(string $name, array $aggregationOptions = [], array $listIndexOptions = [])
10241024
{
1025+
if ($name) {
1026+
$listIndexOptions += ['name' => $name];
1027+
}
1028+
10251029
$operation = new ListSearchIndexes($this->databaseName, $this->collectionName, $aggregationOptions, $listIndexOptions);
10261030
$server = select_server($this->manager, $listIndexOptions);
10271031

@@ -1191,16 +1195,16 @@ public function updateOne($filter, $update, array $options = [])
11911195
/**
11921196
* Update a single Atlas Search index in the collection.
11931197
*
1194-
* @param string $name Search index name
1195-
* @param array $definition Atlas Search index definition
1196-
* @param array $options Command options
1198+
* @param string $name Search index name
1199+
* @param array|object $definition Atlas Search index definition
1200+
* @param array $options Command options
11971201
* @throws UnsupportedException if options are not supported by the selected server
11981202
* @throws InvalidArgumentException for parameter parsing errors
11991203
* @throws DriverRuntimeException for other driver errors (e.g. connection errors)
12001204
*/
1201-
public function updateSearchIndex(string $name, array $definition, array $options = []): void
1205+
public function updateSearchIndex(string $name, $definition, array $options = []): void
12021206
{
1203-
$operation = new UpdateSearchIndex($this->databaseName, $this->collectionName, $name, $definition, $options);
1207+
$operation = new UpdateSearchIndex($this->databaseName, $this->collectionName, $name, $definition);
12041208
$server = select_server($this->manager, $options);
12051209

12061210
$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;
@@ -557,6 +558,29 @@ function (IndexInfo $info) {
557558
array_diff_key($args, ['to' => 1])
558559
);
559560

561+
case 'createSearchIndex':
562+
return $collection->createSearchIndex($args['model']->name ?? '', $args['model']->definition);
563+
564+
case 'createSearchIndexes':
565+
return $collection->createSearchIndexes($args['models']);
566+
567+
case 'dropSearchIndex':
568+
assertArrayHasKey('name', $args);
569+
assertIsString($args['name']);
570+
571+
return $collection->dropSearchIndex($args['name']);
572+
573+
case 'updateSearchIndex':
574+
assertArrayHasKey('name', $args);
575+
assertArrayHasKey('definition', $args);
576+
assertIsString($args['name']);
577+
assertInstanceOf(stdClass::class, $args['definition']);
578+
579+
return $collection->updateSearchIndex($args['name'], $args['definition']);
580+
581+
case 'listSearchIndexes':
582+
return $collection->listSearchIndexes($args['name'] ?? '', document_to_array($args['aggregationOptions'] ?? []), document_to_array(($args['listIndexOptions'] ?? [])));
583+
560584
default:
561585
Assert::fail('Unsupported collection operation: ' . $this->name);
562586
}

tests/UnifiedSpecTests/UnifiedSpecTest.php

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

278+
/** @dataProvider provideIndexManagementTests */
279+
public function testIndexManagement(UnifiedTestCase $test): void
280+
{
281+
self::$runner->run($test);
282+
}
283+
284+
public function provideIndexManagementTests()
285+
{
286+
yield from $this->provideTests(__DIR__ . '/index-management/*.json');
287+
}
288+
278289
private function provideTests(string $pattern): Generator
279290
{
280291
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)