Skip to content

Commit 1714264

Browse files
committed
Add tests on FindOneAnd*::getCommandDocument
1 parent 6a69605 commit 1714264

File tree

3 files changed

+106
-0
lines changed

3 files changed

+106
-0
lines changed

tests/Operation/FindOneAndDeleteTest.php

Lines changed: 31 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\FindOneAndDelete;
78

@@ -31,4 +32,34 @@ public function provideInvalidConstructorOptions()
3132

3233
return $options;
3334
}
35+
36+
public function testExplainableCommandDocument(): void
37+
{
38+
$options = [
39+
'collation' => ['locale' => 'fr'],
40+
'comment' => 'explain me',
41+
'hint' => '_id_',
42+
'maxTimeMS' => 100,
43+
'projection' => ['_id' => 0],
44+
'sort' => ['x' => 1],
45+
'let' => ['a' => 3],
46+
'typeMap' => ['root' => 'array'],
47+
'writeConcern' => new WriteConcern(WriteConcern::MAJORITY),
48+
];
49+
$operation = new FindOneAndDelete($this->getDatabaseName(), $this->getCollectionName(), ['y' => 2], $options);
50+
51+
$expected = [
52+
'findAndModify' => $this->getCollectionName(),
53+
'collation' => (object) ['locale' => 'fr'],
54+
'fields' => (object) ['_id' => 0],
55+
'let' => (object) ['a' => 3],
56+
'query' => (object) ['y' => 2],
57+
'sort' => (object) ['x' => 1],
58+
'comment' => 'explain me',
59+
'hint' => '_id_',
60+
'maxTimeMS' => 100,
61+
'remove' => true,
62+
];
63+
$this->assertEquals($expected, $operation->getCommandDocument());
64+
}
3465
}

tests/Operation/FindOneAndReplaceTest.php

Lines changed: 36 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\FindOneAndReplace;
78

@@ -82,4 +83,39 @@ public function provideInvalidConstructorReturnDocumentOptions()
8283
{
8384
return $this->wrapValuesForDataProvider([-1, 0, 3]);
8485
}
86+
87+
public function testExplainableCommandDocument(): void
88+
{
89+
$options = [
90+
'bypassDocumentValidation' => true,
91+
'collation' => ['locale' => 'fr'],
92+
'comment' => 'explain me',
93+
'fields' => ['_id' => 0],
94+
'hint' => '_id_',
95+
'maxTimeMS' => 100,
96+
'projection' => ['_id' => 0],
97+
'returnDocument' => FindOneAndReplace::RETURN_DOCUMENT_AFTER,
98+
'sort' => ['x' => 1],
99+
'typeMap' => ['root' => 'array'],
100+
'let' => ['a' => 3],
101+
'writeConcern' => new WriteConcern(WriteConcern::MAJORITY),
102+
];
103+
$operation = new FindOneAndReplace($this->getDatabaseName(), $this->getCollectionName(), ['y' => 2], ['y' => 3], $options);
104+
105+
$expected = [
106+
'findAndModify' => $this->getCollectionName(),
107+
'new' => true,
108+
'collation' => (object) ['locale' => 'fr'],
109+
'fields' => (object) ['_id' => 0],
110+
'let' => (object) ['a' => 3],
111+
'query' => (object) ['y' => 2],
112+
'sort' => (object) ['x' => 1],
113+
'update' => (object) ['y' => 3],
114+
'bypassDocumentValidation' => true,
115+
'comment' => 'explain me',
116+
'hint' => '_id_',
117+
'maxTimeMS' => 100,
118+
];
119+
$this->assertEquals($expected, $operation->getCommandDocument());
120+
}
85121
}

tests/Operation/FindOneAndUpdateTest.php

Lines changed: 39 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\FindOneAndUpdate;
78

@@ -65,4 +66,42 @@ public function provideInvalidConstructorReturnDocumentOptions()
6566
{
6667
return $this->wrapValuesForDataProvider([-1, 0, 3]);
6768
}
69+
70+
public function testExplainableCommandDocument(): void
71+
{
72+
$options = [
73+
'arrayFilters' => [['x' => 1]],
74+
'bypassDocumentValidation' => true,
75+
'collation' => ['locale' => 'fr'],
76+
'comment' => 'explain me',
77+
'hint' => '_id_',
78+
'maxTimeMS' => 100,
79+
'projection' => ['_id' => 0],
80+
'returnDocument' => FindOneAndUpdate::RETURN_DOCUMENT_AFTER,
81+
'sort' => ['x' => 1],
82+
'typeMap' => ['root' => 'array'],
83+
'upsert' => true,
84+
'let' => ['a' => 3],
85+
'writeConcern' => new WriteConcern(WriteConcern::MAJORITY),
86+
];
87+
$operation = new FindOneAndUpdate($this->getDatabaseName(), $this->getCollectionName(), ['y' => 2], ['$set' => ['x' => 2]], $options);
88+
89+
$expected = [
90+
'findAndModify' => $this->getCollectionName(),
91+
'new' => true,
92+
'upsert' => true,
93+
'collation' => (object) ['locale' => 'fr'],
94+
'fields' => (object) ['_id' => 0],
95+
'let' => (object) ['a' => 3],
96+
'query' => (object) ['y' => 2],
97+
'sort' => (object) ['x' => 1],
98+
'update' => (object) ['$set' => ['x' => 2]],
99+
'arrayFilters' => [['x' => 1]],
100+
'bypassDocumentValidation' => true,
101+
'comment' => 'explain me',
102+
'hint' => '_id_',
103+
'maxTimeMS' => 100,
104+
];
105+
$this->assertEquals($expected, $operation->getCommandDocument());
106+
}
68107
}

0 commit comments

Comments
 (0)