Skip to content

Commit 31b0c4e

Browse files
committed
Add unit tests on search index operations
1 parent 94c8d93 commit 31b0c4e

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
@@ -291,6 +291,17 @@
291291
<code>$this-&gt;index['name']</code>
292292
</MixedReturnStatement>
293293
</file>
294+
<file src="src/Model/SearchIndexInput.php">
295+
<DocblockTypeContradiction occurrences="1">
296+
<code>is_string($index['name'])</code>
297+
</DocblockTypeContradiction>
298+
<MixedReturnStatement occurrences="1">
299+
<code>$this-&gt;index['name']</code>
300+
</MixedReturnStatement>
301+
<RedundantConditionGivenDocblockType occurrences="1">
302+
<code>! is_array($index['definition']) &amp;&amp; ! is_object($index['definition'])</code>
303+
</RedundantConditionGivenDocblockType>
304+
</file>
294305
<file src="src/Operation/Aggregate.php">
295306
<MixedArgument occurrences="2">
296307
<code>$this-&gt;options['typeMap']</code>
@@ -451,6 +462,14 @@
451462
<code>isInTransaction</code>
452463
</MixedMethodCall>
453464
</file>
465+
<file src="src/Operation/CreateSearchIndexes.php">
466+
<DocblockTypeContradiction occurrences="1">
467+
<code>is_array($index)</code>
468+
</DocblockTypeContradiction>
469+
<MixedArgumentTypeCoercion occurrences="1">
470+
<code>$index</code>
471+
</MixedArgumentTypeCoercion>
472+
</file>
454473
<file src="src/Operation/DatabaseCommand.php">
455474
<DocblockTypeContradiction occurrences="1">
456475
<code>! is_array($command) &amp;&amp; ! is_object($command)</code>
@@ -653,6 +672,11 @@
653672
<code>$options['session']</code>
654673
</MixedAssignment>
655674
</file>
675+
<file src="src/Operation/ListSearchIndexes.php">
676+
<DocblockTypeContradiction occurrences="1">
677+
<code>! is_array($filter) &amp;&amp; ! is_object($filter)</code>
678+
</DocblockTypeContradiction>
679+
</file>
656680
<file src="src/Operation/MapReduce.php">
657681
<DocblockTypeContradiction occurrences="1">
658682
<code>! is_string($out) &amp;&amp; ! is_array($out) &amp;&amp; ! is_object($out)</code>
@@ -735,6 +759,11 @@
735759
<code>! is_array($update) &amp;&amp; ! is_object($update)</code>
736760
</DocblockTypeContradiction>
737761
</file>
762+
<file src="src/Operation/UpdateSearchIndex.php">
763+
<RedundantConditionGivenDocblockType occurrences="1">
764+
<code>! is_object($definition)</code>
765+
</RedundantConditionGivenDocblockType>
766+
</file>
738767
<file src="src/Operation/Watch.php">
739768
<DocblockTypeContradiction occurrences="1">
740769
<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)