Skip to content

Commit 3c4e101

Browse files
committed
PHPLIB-1122: Support Document for MapReduce $out parameter
Also adds tests for $out deprecations (re: PHPLIB-480) using various document types.
1 parent 9a8f40a commit 3c4e101

File tree

2 files changed

+29
-1
lines changed

2 files changed

+29
-1
lines changed

src/Operation/MapReduce.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
use function is_object;
4141
use function is_string;
4242
use function MongoDB\create_field_path_type_map;
43+
use function MongoDB\document_to_array;
4344
use function MongoDB\is_mapreduce_output_inline;
4445
use function trigger_error;
4546

@@ -315,7 +316,7 @@ private function checkOutDeprecations($out): void
315316
return;
316317
}
317318

318-
$out = (array) $out;
319+
$out = document_to_array($out);
319320

320321
if (isset($out['nonAtomic']) && ! $out['nonAtomic']) {
321322
@trigger_error('Specifying false for "out.nonAtomic" is deprecated.', E_USER_DEPRECATED);

tests/Operation/MapReduceTest.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@
22

33
namespace MongoDB\Tests\Operation;
44

5+
use MongoDB\BSON\Document;
56
use MongoDB\BSON\Javascript;
67
use MongoDB\BSON\ObjectId;
78
use MongoDB\Exception\InvalidArgumentException;
9+
use MongoDB\Model\BSONDocument;
810
use MongoDB\Operation\MapReduce;
911
use stdClass;
1012

@@ -25,6 +27,31 @@ public function provideInvalidOutValues()
2527
return $this->wrapValuesForDataProvider([123, 3.14, true]);
2628
}
2729

30+
/** @dataProvider provideDeprecatedOutValues */
31+
public function testConstructorOutArgumentDeprecations($out): void
32+
{
33+
$map = new Javascript('function() { emit(this.x, this.y); }');
34+
$reduce = new Javascript('function(key, values) { return Array.sum(values); }');
35+
36+
$this->assertDeprecated(function () use ($map, $reduce, $out): void {
37+
new MapReduce($this->getDatabaseName(), $this->getCollectionName(), $map, $reduce, $out);
38+
});
39+
}
40+
41+
public function provideDeprecatedOutValues(): array
42+
{
43+
return [
44+
'nonAtomic:array' => [['nonAtomic' => false]],
45+
'nonAtomic:object' => [(object) ['nonAtomic' => false]],
46+
'nonAtomic:Serializable' => [new BSONDocument(['nonAtomic' => false])],
47+
'nonAtomic:Document' => [Document::fromPHP(['nonAtomic' => false])],
48+
'sharded:array' => [['sharded' => false]],
49+
'sharded:object' => [(object) ['sharded' => false]],
50+
'sharded:Serializable' => [new BSONDocument(['sharded' => false])],
51+
'sharded:Document' => [Document::fromPHP(['sharded' => false])],
52+
];
53+
}
54+
2855
/** @dataProvider provideInvalidConstructorOptions */
2956
public function testConstructorOptionTypeChecks(array $options): void
3057
{

0 commit comments

Comments
 (0)