1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
<?php
namespace MongoDB\Tests\Collection\CrudSpec;
use MongoDB\Operation\FindOneAndReplace;
/**
* CRUD spec functional tests for findOneAndReplace().
*
* @see https://github.com/mongodb/specifications/tree/master/source/crud/tests
*/
class FindOneAndReplaceFunctionalTest extends FunctionalTestCase
{
public function setUp()
{
parent::setUp();
$this->createFixtures(3);
}
public function testFindOneAndReplaceWhenManyDocumentsMatchReturningDocumentBeforeModification()
{
$filter = ['_id' => ['$gt' => 1]];
$replacement = ['x' => 32];
$options = [
'projection' => ['x' => 1, '_id' => 0],
'sort' => ['x' => 1],
];
$document = $this->collection->findOneAndReplace($filter, $replacement, $options);
$this->assertSameDocument(['x' => 22], $document);
$expected = [
['_id' => 1, 'x' => 11],
['_id' => 2, 'x' => 32],
['_id' => 3, 'x' => 33],
];
$this->assertSameDocuments($expected, $this->collection->find());
}
public function testFindOneAndReplaceWhenManyDocumentsMatchReturningDocumentAfterModification()
{
$filter = ['_id' => ['$gt' => 1]];
$replacement = ['x' => 32];
$options = [
'projection' => ['x' => 1, '_id' => 0],
'sort' => ['x' => 1],
'returnDocument' => FindOneAndReplace::RETURN_DOCUMENT_AFTER,
];
$document = $this->collection->findOneAndReplace($filter, $replacement, $options);
$this->assertSameDocument(['x' => 32], $document);
$expected = [
['_id' => 1, 'x' => 11],
['_id' => 2, 'x' => 32],
['_id' => 3, 'x' => 33],
];
$this->assertSameDocuments($expected, $this->collection->find());
}
public function testFindOneAndReplaceWhenOneDocumentMatchesReturningDocumentBeforeModification()
{
$filter = ['_id' => 2];
$replacement = ['x' => 32];
$options = [
'projection' => ['x' => 1, '_id' => 0],
'sort' => ['x' => 1],
];
$document = $this->collection->findOneAndReplace($filter, $replacement, $options);
$this->assertSameDocument(['x' => 22], $document);
$expected = [
['_id' => 1, 'x' => 11],
['_id' => 2, 'x' => 32],
['_id' => 3, 'x' => 33],
];
$this->assertSameDocuments($expected, $this->collection->find());
}
public function testFindOneAndReplaceWhenOneDocumentMatchesReturningDocumentAfterModification()
{
$filter = ['_id' => 2];
$replacement = ['x' => 32];
$options = [
'projection' => ['x' => 1, '_id' => 0],
'sort' => ['x' => 1],
'returnDocument' => FindOneAndReplace::RETURN_DOCUMENT_AFTER,
];
$document = $this->collection->findOneAndReplace($filter, $replacement, $options);
$this->assertSameDocument(['x' => 32], $document);
$expected = [
['_id' => 1, 'x' => 11],
['_id' => 2, 'x' => 32],
['_id' => 3, 'x' => 33],
];
$this->assertSameDocuments($expected, $this->collection->find());
}
public function testFindOneAndReplaceWhenNoDocumentsMatchReturningDocumentBeforeModification()
{
$filter = ['_id' => 4];
$replacement = ['x' => 44];
$options = [
'projection' => ['x' => 1, '_id' => 0],
'sort' => ['x' => 1],
];
$document = $this->collection->findOneAndReplace($filter, $replacement, $options);
$this->assertNull($document);
$expected = [
['_id' => 1, 'x' => 11],
['_id' => 2, 'x' => 22],
['_id' => 3, 'x' => 33],
];
$this->assertSameDocuments($expected, $this->collection->find());
}
public function testFindOneAndReplaceWithUpsertWhenNoDocumentsMatchReturningDocumentBeforeModification()
{
$filter = ['_id' => 4];
// Server 2.4 and earlier requires any custom ID to also be in the replacement document
$replacement = ['_id' => 4, 'x' => 44];
$options = [
'projection' => ['x' => 1, '_id' => 0],
'sort' => ['x' => 1],
'upsert' => true,
];
$document = $this->collection->findOneAndReplace($filter, $replacement, $options);
$this->assertNull($document);
$expected = [
['_id' => 1, 'x' => 11],
['_id' => 2, 'x' => 22],
['_id' => 3, 'x' => 33],
['_id' => 4, 'x' => 44],
];
$this->assertSameDocuments($expected, $this->collection->find());
}
public function testFindOneAndReplaceWhenNoDocumentsMatchReturningDocumentAfterModification()
{
$filter = ['_id' => 4];
$replacement = ['x' => 44];
$options = [
'projection' => ['x' => 1, '_id' => 0],
'sort' => ['x' => 1],
'returnDocument' => FindOneAndReplace::RETURN_DOCUMENT_AFTER,
];
$document = $this->collection->findOneAndReplace($filter, $replacement, $options);
$this->assertNull($document);
$expected = [
['_id' => 1, 'x' => 11],
['_id' => 2, 'x' => 22],
['_id' => 3, 'x' => 33],
];
$this->assertSameDocuments($expected, $this->collection->find());
}
public function testFindOneAndReplaceWithUpsertWhenNoDocumentsMatchReturningDocumentAfterModification()
{
$filter = ['_id' => 4];
// Server 2.4 and earlier requires any custom ID to also be in the replacement document
$replacement = ['_id' => 4, 'x' => 44];
$options = [
'projection' => ['x' => 1, '_id' => 0],
'sort' => ['x' => 1],
'returnDocument' => FindOneAndReplace::RETURN_DOCUMENT_AFTER,
'upsert' => true,
];
$document = $this->collection->findOneAndReplace($filter, $replacement, $options);
$this->assertSameDocument(['x' => 44], $document);
$expected = [
['_id' => 1, 'x' => 11],
['_id' => 2, 'x' => 22],
['_id' => 3, 'x' => 33],
['_id' => 4, 'x' => 44],
];
$this->assertSameDocuments($expected, $this->collection->find());
}
}