Skip to content

Commit 392ac5d

Browse files
committed
PHPLIB-1419 Encode Agg builder objects in Collection methods
1 parent fe5e143 commit 392ac5d

File tree

2 files changed

+137
-1
lines changed

2 files changed

+137
-1
lines changed

src/Collection.php

+18-1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
use Iterator;
2222
use MongoDB\BSON\JavascriptInterface;
2323
use MongoDB\Builder\BuilderEncoder;
24+
use MongoDB\Builder\Pipeline;
2425
use MongoDB\Codec\DocumentCodec;
2526
use MongoDB\Codec\Encoder;
2627
use MongoDB\Driver\CursorInterface;
@@ -216,8 +217,9 @@ public function __toString()
216217
* @throws InvalidArgumentException for parameter/option parsing errors
217218
* @throws DriverRuntimeException for other driver errors (e.g. connection errors)
218219
*/
219-
public function aggregate(array $pipeline, array $options = [])
220+
public function aggregate(array|Pipeline $pipeline, array $options = [])
220221
{
222+
$pipeline = $this->builderEncoder->encodeIfSupported($pipeline);
221223
$hasWriteStage = is_last_pipeline_operator_write($pipeline);
222224

223225
$options = $this->inheritReadPreference($options);
@@ -257,6 +259,7 @@ public function aggregate(array $pipeline, array $options = [])
257259
*/
258260
public function bulkWrite(array $operations, array $options = [])
259261
{
262+
// @todo encode each operation in $operations
260263
$options = $this->inheritWriteOptions($options);
261264
$options = $this->inheritCodec($options);
262265

@@ -281,6 +284,7 @@ public function bulkWrite(array $operations, array $options = [])
281284
*/
282285
public function count(array|object $filter = [], array $options = [])
283286
{
287+
$filter = $this->builderEncoder->encodeIfSupported($filter);
284288
$options = $this->inheritReadOptions($options);
285289

286290
$operation = new Count($this->databaseName, $this->collectionName, $filter, $options);
@@ -302,6 +306,7 @@ public function count(array|object $filter = [], array $options = [])
302306
*/
303307
public function countDocuments(array|object $filter = [], array $options = [])
304308
{
309+
$filter = $this->builderEncoder->encodeIfSupported($filter);
305310
$options = $this->inheritReadOptions($options);
306311

307312
$operation = new CountDocuments($this->databaseName, $this->collectionName, $filter, $options);
@@ -439,6 +444,7 @@ public function createSearchIndexes(array $indexes, array $options = []): array
439444
*/
440445
public function deleteMany(array|object $filter, array $options = [])
441446
{
447+
$filter = $this->builderEncoder->encodeIfSupported($filter);
442448
$options = $this->inheritWriteOptions($options);
443449

444450
$operation = new DeleteMany($this->databaseName, $this->collectionName, $filter, $options);
@@ -460,6 +466,7 @@ public function deleteMany(array|object $filter, array $options = [])
460466
*/
461467
public function deleteOne(array|object $filter, array $options = [])
462468
{
469+
$filter = $this->builderEncoder->encodeIfSupported($filter);
463470
$options = $this->inheritWriteOptions($options);
464471

465472
$operation = new DeleteOne($this->databaseName, $this->collectionName, $filter, $options);
@@ -482,6 +489,7 @@ public function deleteOne(array|object $filter, array $options = [])
482489
*/
483490
public function distinct(string $fieldName, array|object $filter = [], array $options = [])
484491
{
492+
$filter = $this->builderEncoder->encodeIfSupported($filter);
485493
$options = $this->inheritReadOptions($options);
486494
$options = $this->inheritTypeMap($options);
487495

@@ -640,6 +648,7 @@ public function explain(Explainable $explainable, array $options = [])
640648
*/
641649
public function find(array|object $filter = [], array $options = [])
642650
{
651+
$filter = $this->builderEncoder->encodeIfSupported($filter);
643652
$options = $this->inheritReadOptions($options);
644653
$options = $this->inheritCodecOrTypeMap($options);
645654

@@ -662,6 +671,7 @@ public function find(array|object $filter = [], array $options = [])
662671
*/
663672
public function findOne(array|object $filter = [], array $options = [])
664673
{
674+
$filter = $this->builderEncoder->encodeIfSupported($filter);
665675
$options = $this->inheritReadOptions($options);
666676
$options = $this->inheritCodecOrTypeMap($options);
667677

@@ -687,6 +697,7 @@ public function findOne(array|object $filter = [], array $options = [])
687697
*/
688698
public function findOneAndDelete(array|object $filter, array $options = [])
689699
{
700+
$filter = $this->builderEncoder->encodeIfSupported($filter);
690701
$options = $this->inheritWriteOptions($options);
691702
$options = $this->inheritCodecOrTypeMap($options);
692703

@@ -717,6 +728,7 @@ public function findOneAndDelete(array|object $filter, array $options = [])
717728
*/
718729
public function findOneAndReplace(array|object $filter, array|object $replacement, array $options = [])
719730
{
731+
$filter = $this->builderEncoder->encodeIfSupported($filter);
720732
$options = $this->inheritWriteOptions($options);
721733
$options = $this->inheritCodecOrTypeMap($options);
722734

@@ -747,6 +759,7 @@ public function findOneAndReplace(array|object $filter, array|object $replacemen
747759
*/
748760
public function findOneAndUpdate(array|object $filter, array|object $update, array $options = [])
749761
{
762+
$filter = $this->builderEncoder->encodeIfSupported($filter);
750763
$options = $this->inheritWriteOptions($options);
751764
$options = $this->inheritCodecOrTypeMap($options);
752765

@@ -995,6 +1008,7 @@ public function rename(string $toCollectionName, ?string $toDatabaseName = null,
9951008
*/
9961009
public function replaceOne(array|object $filter, array|object $replacement, array $options = [])
9971010
{
1011+
$filter = $this->builderEncoder->encodeIfSupported($filter);
9981012
$options = $this->inheritWriteOptions($options);
9991013
$options = $this->inheritCodec($options);
10001014

@@ -1018,6 +1032,7 @@ public function replaceOne(array|object $filter, array|object $replacement, arra
10181032
*/
10191033
public function updateMany(array|object $filter, array|object $update, array $options = [])
10201034
{
1035+
$filter = $this->builderEncoder->encodeIfSupported($filter);
10211036
$options = $this->inheritWriteOptions($options);
10221037

10231038
$operation = new UpdateMany($this->databaseName, $this->collectionName, $filter, $update, $options);
@@ -1040,6 +1055,7 @@ public function updateMany(array|object $filter, array|object $update, array $op
10401055
*/
10411056
public function updateOne(array|object $filter, array|object $update, array $options = [])
10421057
{
1058+
$filter = $this->builderEncoder->encodeIfSupported($filter);
10431059
$options = $this->inheritWriteOptions($options);
10441060

10451061
$operation = new UpdateOne($this->databaseName, $this->collectionName, $filter, $update, $options);
@@ -1077,6 +1093,7 @@ public function updateSearchIndex(string $name, array|object $definition, array
10771093
*/
10781094
public function watch(array $pipeline = [], array $options = [])
10791095
{
1096+
$pipeline = $this->builderEncoder->encodeIfSupported($pipeline);
10801097
$options = $this->inheritReadOptions($options);
10811098
$options = $this->inheritCodecOrTypeMap($options);
10821099

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
<?php
2+
3+
namespace MongoDB\Tests\Collection;
4+
5+
use MongoDB\Builder\Expression;
6+
use MongoDB\Builder\Pipeline;
7+
use MongoDB\Builder\Query;
8+
use MongoDB\Builder\Stage;
9+
10+
class BuilderCollectionFunctionalTest extends FunctionalTestCase
11+
{
12+
public function testAggregate(): void
13+
{
14+
$this->collection->insertMany([['x' => 10], ['x' => 2], ['x' => 5]]);
15+
$pipeline = new Pipeline(
16+
Stage::bucketAuto(
17+
groupBy: Expression::intFieldPath('x'),
18+
buckets: 2,
19+
),
20+
);
21+
$results = $this->collection->aggregate($pipeline)->toArray();
22+
23+
$this->assertCount(2, $results);
24+
}
25+
26+
public function testBulkWrite(): void
27+
{
28+
$this->markTestIncomplete(__FUNCTION__);
29+
}
30+
31+
public function testCountDocuments(): void
32+
{
33+
$this->markTestIncomplete(__FUNCTION__);
34+
}
35+
36+
public function testDeleteMany(): void
37+
{
38+
$this->markTestIncomplete(__FUNCTION__);
39+
}
40+
41+
public function testDeleteOne(): void
42+
{
43+
$this->markTestIncomplete(__FUNCTION__);
44+
}
45+
46+
public function testDistinct(): void
47+
{
48+
$this->markTestIncomplete(__FUNCTION__);
49+
}
50+
51+
public function testFind(): void
52+
{
53+
$this->collection->insertMany([['x' => 1], ['x' => 2]]);
54+
55+
$results = $this->collection->find(Query::query(x: Query::gt(1)))->toArray();
56+
$this->assertCount(1, $results);
57+
$this->assertEquals(2, $results[0]->x);
58+
}
59+
60+
public function testFindOne(): void
61+
{
62+
$this->markTestIncomplete(__FUNCTION__);
63+
}
64+
65+
public function testFindOneAndDelete(): void
66+
{
67+
$this->markTestIncomplete(__FUNCTION__);
68+
}
69+
70+
public function testFindAndReplace(): void
71+
{
72+
$this->markTestIncomplete(__FUNCTION__);
73+
}
74+
75+
public function testFindAndUpdate(): void
76+
{
77+
$this->markTestIncomplete(__FUNCTION__);
78+
}
79+
80+
public function testReplaceOne(): void
81+
{
82+
$this->markTestIncomplete(__FUNCTION__);
83+
}
84+
85+
public function testUpdateOne(): void
86+
{
87+
$this->collection->insertOne(['x' => 1]);
88+
89+
$result = $this->collection->updateOne(
90+
Query::query(x: Query::eq(1)),
91+
// @todo Use Builder when update operators are supported by PHPLIB-1507
92+
['$set' => ['x' => 2]],
93+
);
94+
$this->assertEquals(1, $result->getModifiedCount());
95+
96+
$result = $this->collection->findOne();
97+
$this->assertEquals(2, $result->x);
98+
}
99+
100+
public function testUpdateWithPipeline(): void
101+
{
102+
$this->markTestIncomplete(__FUNCTION__);
103+
}
104+
105+
public function testUpdateMany(): void
106+
{
107+
$this->markTestIncomplete(__FUNCTION__);
108+
}
109+
110+
public function testUpdateManyWithPipeline(): void
111+
{
112+
$this->markTestIncomplete(__FUNCTION__);
113+
}
114+
115+
public function testWatch(): void
116+
{
117+
$this->markTestIncomplete(__FUNCTION__);
118+
}
119+
}

0 commit comments

Comments
 (0)