Skip to content

Commit f4e785e

Browse files
committed
Add unit tests on search index operations
1 parent 49937a6 commit f4e785e

File tree

10 files changed

+183
-15
lines changed

10 files changed

+183
-15
lines changed

psalm-baseline.xml

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -285,6 +285,17 @@
285285
<code>$this-&gt;index['name']</code>
286286
</MixedReturnStatement>
287287
</file>
288+
<file src="src/Model/SearchIndexInput.php">
289+
<DocblockTypeContradiction occurrences="1">
290+
<code>is_string($index['name'])</code>
291+
</DocblockTypeContradiction>
292+
<MixedReturnStatement occurrences="1">
293+
<code>$this-&gt;index['name']</code>
294+
</MixedReturnStatement>
295+
<RedundantConditionGivenDocblockType occurrences="1">
296+
<code>! is_array($index['definition']) &amp;&amp; ! is_object($index['definition'])</code>
297+
</RedundantConditionGivenDocblockType>
298+
</file>
288299
<file src="src/Operation/Aggregate.php">
289300
<MixedArgument occurrences="2">
290301
<code>$this-&gt;options['typeMap']</code>
@@ -437,6 +448,14 @@
437448
<code>isInTransaction</code>
438449
</MixedMethodCall>
439450
</file>
451+
<file src="src/Operation/CreateSearchIndexes.php">
452+
<DocblockTypeContradiction occurrences="1">
453+
<code>is_array($index)</code>
454+
</DocblockTypeContradiction>
455+
<MixedArgumentTypeCoercion occurrences="1">
456+
<code>$index</code>
457+
</MixedArgumentTypeCoercion>
458+
</file>
440459
<file src="src/Operation/DatabaseCommand.php">
441460
<DocblockTypeContradiction occurrences="1">
442461
<code>! is_array($command) &amp;&amp; ! is_object($command)</code>
@@ -639,6 +658,11 @@
639658
<code>$options['session']</code>
640659
</MixedAssignment>
641660
</file>
661+
<file src="src/Operation/ListSearchIndexes.php">
662+
<DocblockTypeContradiction occurrences="1">
663+
<code>! is_array($filter) &amp;&amp; ! is_object($filter)</code>
664+
</DocblockTypeContradiction>
665+
</file>
642666
<file src="src/Operation/MapReduce.php">
643667
<DocblockTypeContradiction occurrences="1">
644668
<code>! is_string($out) &amp;&amp; ! is_array($out) &amp;&amp; ! is_object($out)</code>
@@ -721,6 +745,11 @@
721745
<code>! is_array($update) &amp;&amp; ! is_object($update)</code>
722746
</DocblockTypeContradiction>
723747
</file>
748+
<file src="src/Operation/UpdateSearchIndex.php">
749+
<RedundantConditionGivenDocblockType occurrences="1">
750+
<code>! is_object($definition)</code>
751+
</RedundantConditionGivenDocblockType>
752+
</file>
724753
<file src="src/Operation/Watch.php">
725754
<DocblockTypeContradiction occurrences="1">
726755
<code>isset($resumeToken) &amp;&amp; ! is_array($resumeToken) &amp;&amp; ! is_object($resumeToken)</code>

src/Operation/ListSearchIndexes.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ public function __construct(string $databaseName, string $collectionName, $filte
7070

7171
$filter = document_to_array($filter);
7272

73-
if (isset($filter['name']) && ! is_string($options['name'])) {
73+
if (isset($filter['name']) && ! is_string($filter['name'])) {
7474
throw InvalidArgumentException::invalidType('"name" filter', $filter['name'], 'string');
7575
}
7676

tests/Operation/BulkWriteTest.php

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -92,11 +92,6 @@ public function testDeleteManyCollationOptionTypeCheck($collation): void
9292
]);
9393
}
9494

95-
public function provideInvalidDocumentValues()
96-
{
97-
return $this->wrapValuesForDataProvider($this->getInvalidDocumentValues());
98-
}
99-
10095
public function testDeleteOneFilterArgumentMissing(): void
10196
{
10297
$this->expectException(InvalidArgumentException::class);

tests/Operation/CreateIndexesTest.php

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -110,9 +110,4 @@ public function testConstructorRequiresIndexSpecificationNameToBeString($name):
110110
$this->expectExceptionMessage('Expected "name" option to have type "string"');
111111
new CreateIndexes($this->getDatabaseName(), $this->getCollectionName(), [['key' => ['x' => 1], 'name' => $name]]);
112112
}
113-
114-
public function provideInvalidStringValues()
115-
{
116-
return $this->wrapValuesForDataProvider($this->getInvalidStringValues());
117-
}
118113
}
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
<?php
2+
3+
namespace MongoDB\Tests\Operation;
4+
5+
use MongoDB\Exception\InvalidArgumentException;
6+
use MongoDB\Operation\CreateSearchIndexes;
7+
8+
class CreateSearchIndexesTest extends TestCase
9+
{
10+
public function testConstructorIndexesArgumentMustBeAList(): void
11+
{
12+
$this->expectException(InvalidArgumentException::class);
13+
$this->expectExceptionMessage('$indexes is not a list');
14+
new CreateSearchIndexes($this->getDatabaseName(), $this->getCollectionName(), [1 => ['name' => 'index name', 'definition' => []]]);
15+
}
16+
17+
public function testConstructorRequiresAtLeastOneIndex(): void
18+
{
19+
$this->expectException(InvalidArgumentException::class);
20+
$this->expectExceptionMessage('$indexes is empty');
21+
new CreateSearchIndexes($this->getDatabaseName(), $this->getCollectionName(), []);
22+
}
23+
24+
/** @dataProvider provideInvalidIndexSpecificationTypes */
25+
public function testConstructorRequiresIndexSpecificationsToBeAnArray($index): void
26+
{
27+
$this->expectException(InvalidArgumentException::class);
28+
$this->expectExceptionMessage('Expected $index[0] to have type "array"');
29+
new CreateSearchIndexes($this->getDatabaseName(), $this->getCollectionName(), [$index]);
30+
}
31+
32+
public function provideInvalidSearchIndexTypes()
33+
{
34+
return $this->wrapValuesForDataProvider($this->getInvalidArrayValues());
35+
}
36+
37+
public function testConstructorRequiresSearchIndexName(): void
38+
{
39+
$this->expectException(InvalidArgumentException::class);
40+
$this->expectExceptionMessage('Required "name" string is missing from index specification');
41+
new CreateSearchIndexes($this->getDatabaseName(), $this->getCollectionName(), [[]]);
42+
}
43+
44+
/** @dataProvider provideInvalidStringValues */
45+
public function testConstructorRequiresSearchIndexNameToBeAString($name): void
46+
{
47+
$this->expectException(InvalidArgumentException::class);
48+
$this->expectExceptionMessage('Expected "name" option to have type "string"');
49+
new CreateSearchIndexes($this->getDatabaseName(), $this->getCollectionName(), [['name' => $name]]);
50+
}
51+
52+
public function testConstructorRequiresSearchIndexDefinitionToBeDefined(): void
53+
{
54+
$this->expectException(InvalidArgumentException::class);
55+
$this->expectExceptionMessage('Required "definition" document is missing from search index specification');
56+
new CreateSearchIndexes($this->getDatabaseName(), $this->getCollectionName(), [['name' => 'index name']]);
57+
}
58+
59+
/** @dataProvider provideInvalidDocumentValues */
60+
public function testConstructorRequiresSearchIndexDefinitionToBeAnArray($definition): void
61+
{
62+
$this->expectException(InvalidArgumentException::class);
63+
$this->expectExceptionMessage('Expected "definition" option to have type "array or object"');
64+
new CreateSearchIndexes($this->getDatabaseName(), $this->getCollectionName(), [['name' => 'index name', 'definition' => $definition]]);
65+
}
66+
67+
public function provideInvalidIndexSpecificationTypes()
68+
{
69+
return $this->wrapValuesForDataProvider($this->getInvalidArrayValues());
70+
}
71+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
namespace MongoDB\Tests\Operation;
4+
5+
use MongoDB\Exception\InvalidArgumentException;
6+
use MongoDB\Operation\DropSearchIndex;
7+
8+
class DropSearchIndexTest extends TestCase
9+
{
10+
public function testConstructorRequiresANotEmptyIndexName(): void
11+
{
12+
$this->expectException(InvalidArgumentException::class);
13+
new DropSearchIndex($this->getDatabaseName(), $this->getCollectionName(), '');
14+
}
15+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
3+
namespace MongoDB\Tests\Operation;
4+
5+
use MongoDB\Exception\InvalidArgumentException;
6+
use MongoDB\Operation\ListSearchIndexes;
7+
8+
class ListSearchIndexesTest extends TestCase
9+
{
10+
/** @dataProvider provideInvalidStringValues */
11+
public function testConstructorExpectNameFilterToBeAString($name): void
12+
{
13+
$this->expectException(InvalidArgumentException::class);
14+
$this->expectExceptionMessage('Expected "name" filter to have type "string"');
15+
new ListSearchIndexes($this->getDatabaseName(), $this->getCollectionName(), ['name' => $name]);
16+
}
17+
18+
/** @dataProvider provideInvalidConstructorOptions */
19+
public function testConstructorOptionTypeChecks(array $options): void
20+
{
21+
$this->expectException(InvalidArgumentException::class);
22+
new ListSearchIndexes($this->getDatabaseName(), $this->getCollectionName(), [], $options);
23+
}
24+
25+
public function provideInvalidConstructorOptions(): array
26+
{
27+
$options = [];
28+
29+
foreach ($this->getInvalidIntegerValues() as $value) {
30+
$options[][] = ['batchSize' => $value];
31+
}
32+
33+
return $options;
34+
}
35+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
namespace MongoDB\Tests\Operation;
4+
5+
use MongoDB\Exception\InvalidArgumentException;
6+
use MongoDB\Operation\UpdateSearchIndex;
7+
8+
class UpdateSearchIndexTest extends TestCase
9+
{
10+
public function testConstructorRequiresANotEmptyIndexName(): void
11+
{
12+
$this->expectException(InvalidArgumentException::class);
13+
new UpdateSearchIndex($this->getDatabaseName(), $this->getCollectionName(), '', []);
14+
}
15+
16+
/** @dataProvider provideInvalidDocumentValues */
17+
public function testConstructorRequiresSearchIndexDefinitionToBeADocument($definition): void
18+
{
19+
$this->expectException(InvalidArgumentException::class);
20+
$this->expectExceptionMessage('Expected $definition to have type "array or object"');
21+
new UpdateSearchIndex($this->getDatabaseName(), $this->getCollectionName(), 'index name', $definition);
22+
}
23+
}

tests/TestCase.php

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -122,21 +122,26 @@ public function dataDescription(): string
122122
return is_string($dataName) ? $dataName : '';
123123
}
124124

125-
public function provideInvalidArrayValues()
125+
public function provideInvalidArrayValues(): array
126126
{
127127
return $this->wrapValuesForDataProvider($this->getInvalidArrayValues());
128128
}
129129

130-
public function provideInvalidDocumentValues()
130+
public function provideInvalidDocumentValues(): array
131131
{
132132
return $this->wrapValuesForDataProvider($this->getInvalidDocumentValues());
133133
}
134134

135-
public function provideInvalidIntegerValues()
135+
public function provideInvalidIntegerValues(): array
136136
{
137137
return $this->wrapValuesForDataProvider($this->getInvalidIntegerValues());
138138
}
139139

140+
public function provideInvalidStringValues(): array
141+
{
142+
return $this->wrapValuesForDataProvider($this->getInvalidStringValues());
143+
}
144+
140145
protected function assertDeprecated(callable $execution): void
141146
{
142147
$errors = [];

tests/UnifiedSpecTests/Operation.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -579,7 +579,7 @@ function (IndexInfo $info) {
579579
return $collection->updateSearchIndex($args['name'], $args['definition']);
580580

581581
case 'listSearchIndexes':
582-
return $collection->listSearchIndexes($args['name'] ?? '', document_to_array($args['aggregationOptions'] ?? []), document_to_array(($args['listIndexOptions'] ?? [])));
582+
return $collection->listSearchIndexes($args['name'] ?? '', document_to_array($args['aggregationOptions'] ?? []));
583583

584584
default:
585585
Assert::fail('Unsupported collection operation: ' . $this->name);

0 commit comments

Comments
 (0)