Skip to content

Commit 3d7d2eb

Browse files
committed
PHPLIB-1144: Add unit tests on Explainable::getCommandDocument
1 parent b3607d9 commit 3d7d2eb

File tree

6 files changed

+215
-0
lines changed

6 files changed

+215
-0
lines changed

tests/Operation/CountTest.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace MongoDB\Tests\Operation;
44

5+
use MongoDB\Driver\ReadConcern;
56
use MongoDB\Exception\InvalidArgumentException;
67
use MongoDB\Operation\Count;
78

@@ -64,4 +65,31 @@ private function getInvalidHintValues()
6465
{
6566
return [123, 3.14, true];
6667
}
68+
69+
public function testExplainableCommandDocument(): void
70+
{
71+
$options = [
72+
'hint' => '_id_',
73+
'limit' => 10,
74+
'skip' => 20,
75+
'readConcern' => new ReadConcern(ReadConcern::LOCAL),
76+
'collation' => ['locale' => 'fr'],
77+
'comment' => 'explain me',
78+
'maxTimeMS' => 100,
79+
];
80+
$operation = new Count($this->getDatabaseName(), $this->getCollectionName(), ['x' => 1], $options);
81+
82+
$expected = [
83+
'count' => $this->getCollectionName(),
84+
'query' => (object) ['x' => 1],
85+
'collation' => (object) ['locale' => 'fr'],
86+
'hint' => '_id_',
87+
'comment' => 'explain me',
88+
'limit' => 10,
89+
'skip' => 20,
90+
'maxTimeMS' => 100,
91+
'readConcern' => new ReadConcern(ReadConcern::LOCAL),
92+
];
93+
$this->assertEquals($expected, $operation->getCommandDocument());
94+
}
6795
}

tests/Operation/DeleteTest.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,4 +65,30 @@ public function provideInvalidConstructorOptions()
6565

6666
return $options;
6767
}
68+
69+
public function testExplainableCommandDocument(): void
70+
{
71+
$options = [
72+
'collation' => ['locale' => 'fr'],
73+
'hint' => '_id_',
74+
// Ignored options
75+
'let' => ['a' => 1],
76+
'ordered' => true,
77+
'comment' => 'explain me',
78+
];
79+
$operation = new Delete($this->getDatabaseName(), $this->getCollectionName(), ['x' => 1], 0, $options);
80+
81+
$expected = [
82+
'delete' => $this->getCollectionName(),
83+
'deletes' => [
84+
[
85+
'q' => ['x' => 1],
86+
'limit' => 0,
87+
'collation' => (object) ['locale' => 'fr'],
88+
'hint' => '_id_',
89+
],
90+
],
91+
];
92+
$this->assertEquals($expected, $operation->getCommandDocument());
93+
}
6894
}

tests/Operation/DistinctTest.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace MongoDB\Tests\Operation;
44

5+
use MongoDB\Driver\ReadConcern;
6+
use MongoDB\Driver\ReadPreference;
57
use MongoDB\Exception\InvalidArgumentException;
68
use MongoDB\Operation\Distinct;
79

@@ -51,4 +53,28 @@ public function provideInvalidConstructorOptions()
5153

5254
return $options;
5355
}
56+
57+
public function testExplainableCommandDocument(): void
58+
{
59+
$options = [
60+
'collation' => ['locale' => 'fr'],
61+
'maxTimeMS' => 100,
62+
'readConcern' => new ReadConcern(ReadConcern::LOCAL),
63+
'readPreference' => new ReadPreference(ReadPreference::SECONDARY_PREFERRED),
64+
'typeMap' => ['root' => 'array'],
65+
'comment' => 'explain me',
66+
];
67+
$operation = new Distinct($this->getDatabaseName(), $this->getCollectionName(), 'f', ['x' => 1], $options);
68+
69+
$expected = [
70+
'distinct' => $this->getCollectionName(),
71+
'key' => 'f',
72+
'query' => (object) ['x' => 1],
73+
'collation' => (object) ['locale' => 'fr'],
74+
'comment' => 'explain me',
75+
'maxTimeMS' => 100,
76+
'readConcern' => new ReadConcern(ReadConcern::LOCAL),
77+
];
78+
$this->assertEquals($expected, $operation->getCommandDocument());
79+
}
5480
}

tests/Operation/FindAndModifyTest.php

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace MongoDB\Tests\Operation;
44

5+
use MongoDB\Driver\WriteConcern;
56
use MongoDB\Exception\InvalidArgumentException;
67
use MongoDB\Operation\FindAndModify;
78

@@ -83,4 +84,45 @@ public function testConstructorUpdateAndRemoveOptionsAreMutuallyExclusive(): voi
8384
$this->expectExceptionMessage('The "remove" option must be true or an "update" document must be specified, but not both');
8485
new FindAndModify($this->getDatabaseName(), $this->getCollectionName(), ['remove' => true, 'update' => []]);
8586
}
87+
88+
public function testExplainableCommandDocument(): void
89+
{
90+
$options = [
91+
'arrayFilters' => [['x' => 1]],
92+
'bypassDocumentValidation' => true,
93+
'collation' => ['locale' => 'fr'],
94+
'comment' => 'explain me',
95+
'fields' => ['_id' => 0],
96+
'hint' => '_id_',
97+
'maxTimeMS' => 100,
98+
'new' => true,
99+
'query' => ['y' => 2],
100+
'remove' => false,
101+
'sort' => ['x' => 1],
102+
'typeMap' => ['root' => 'array'],
103+
'update' => ['$set' => ['x' => 2]],
104+
'upsert' => true,
105+
'let' => ['a' => 3],
106+
'writeConcern' => new WriteConcern(WriteConcern::MAJORITY),
107+
];
108+
$operation = new FindAndModify($this->getDatabaseName(), $this->getCollectionName(), $options);
109+
110+
$expected = [
111+
'findAndModify' => $this->getCollectionName(),
112+
'new' => true,
113+
'upsert' => true,
114+
'collation' => (object) ['locale' => 'fr'],
115+
'fields' => (object) ['_id' => 0],
116+
'let' => (object) ['a' => 3],
117+
'query' => (object) ['y' => 2],
118+
'sort' => (object) ['x' => 1],
119+
'update' => (object) ['$set' => ['x' => 2]],
120+
'arrayFilters' => [['x' => 1]],
121+
'bypassDocumentValidation' => true,
122+
'comment' => 'explain me',
123+
'hint' => '_id_',
124+
'maxTimeMS' => 100,
125+
];
126+
$this->assertEquals($expected, $operation->getCommandDocument());
127+
}
86128
}

tests/Operation/FindTest.php

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace MongoDB\Tests\Operation;
44

5+
use MongoDB\Driver\ReadConcern;
6+
use MongoDB\Driver\ReadPreference;
57
use MongoDB\Exception\InvalidArgumentException;
68
use MongoDB\Operation\Find;
79

@@ -154,4 +156,61 @@ public function provideInvalidConstructorCursorTypeOptions()
154156
{
155157
return $this->wrapValuesForDataProvider([-1, 0, 4]);
156158
}
159+
160+
public function testExplainableCommandDocument(): void
161+
{
162+
// all options except deprecated "snapshot" and "maxScan"
163+
$options = [
164+
'allowDiskUse' => true,
165+
'allowPartialResults' => true,
166+
'batchSize' => 123,
167+
'collation' => ['locale' => 'fr'],
168+
'comment' => 'explain me',
169+
'cursorType' => Find::NON_TAILABLE,
170+
'hint' => '_id_',
171+
'limit' => 15,
172+
'max' => ['x' => 100],
173+
'maxAwaitTimeMS' => 500,
174+
'maxTimeMS' => 100,
175+
'min' => ['x' => 10],
176+
'modifiers' => ['foo' => 'bar'],
177+
'noCursorTimeout' => true,
178+
'oplogReplay' => true,
179+
'projection' => ['_id' => 0],
180+
'readConcern' => new ReadConcern(ReadConcern::LOCAL),
181+
'readPreference' => new ReadPreference(ReadPreference::SECONDARY_PREFERRED),
182+
'returnKey' => true,
183+
'showRecordId' => true,
184+
'skip' => 5,
185+
'sort' => ['x' => 1],
186+
'let' => ['y' => 2],
187+
'typeMap' => ['root' => 'array'],
188+
];
189+
$operation = new Find($this->getDatabaseName(), $this->getCollectionName(), ['x' => 1], $options);
190+
191+
$expected = [
192+
'find' => $this->getCollectionName(),
193+
'filter' => (object) ['x' => 1],
194+
'allowDiskUse' => true,
195+
'allowPartialResults' => true,
196+
'batchSize' => 123,
197+
'comment' => 'explain me',
198+
'hint' => '_id_',
199+
'limit' => 15,
200+
'maxTimeMS' => 100,
201+
'noCursorTimeout' => true,
202+
'oplogReplay' => true,
203+
'projection' => ['_id' => 0],
204+
'readConcern' => new ReadConcern(ReadConcern::LOCAL),
205+
'returnKey' => true,
206+
'showRecordId' => true,
207+
'skip' => 5,
208+
'sort' => ['x' => 1],
209+
'collation' => (object) ['locale' => 'fr'],
210+
'let' => (object) ['y' => 2],
211+
'max' => (object) ['x' => 100],
212+
'min' => (object) ['x' => 10],
213+
];
214+
$this->assertEquals($expected, $operation->getCommandDocument());
215+
}
157216
}

tests/Operation/UpdateTest.php

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace MongoDB\Tests\Operation;
44

5+
use MongoDB\Driver\WriteConcern;
56
use MongoDB\Exception\InvalidArgumentException;
67
use MongoDB\Operation\Update;
78

@@ -75,4 +76,37 @@ public function testConstructorMultiOptionProhibitsReplacementDocumentOrEmptyPip
7576
$this->expectExceptionMessage('"multi" option cannot be true unless $update has update operator(s) or non-empty pipeline');
7677
new Update($this->getDatabaseName(), $this->getCollectionName(), ['x' => 1], $update, ['multi' => true]);
7778
}
79+
80+
public function testExplainableCommandDocument(): void
81+
{
82+
$options = [
83+
'arrayFilters' => [['x' => 1]],
84+
'bypassDocumentValidation' => true,
85+
'collation' => ['locale' => 'fr'],
86+
'comment' => 'explain me',
87+
'hint' => '_id_',
88+
'multi' => true,
89+
'upsert' => true,
90+
'let' => ['a' => 3],
91+
'writeConcern' => new WriteConcern(WriteConcern::MAJORITY),
92+
];
93+
$operation = new Update($this->getDatabaseName(), $this->getCollectionName(), ['x' => 1], ['$set' => ['x' => 2]], $options);
94+
95+
$expected = [
96+
'update' => $this->getCollectionName(),
97+
'bypassDocumentValidation' => true,
98+
'updates' => [
99+
[
100+
'q' => ['x' => 1],
101+
'u' => ['$set' => ['x' => 2]],
102+
'multi' => true,
103+
'upsert' => true,
104+
'arrayFilters' => [['x' => 1]],
105+
'hint' => '_id_',
106+
'collation' => (object) ['locale' => 'fr'],
107+
],
108+
],
109+
];
110+
$this->assertEquals($expected, $operation->getCommandDocument());
111+
}
78112
}

0 commit comments

Comments
 (0)