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
<?php
namespace MongoDB\Tests\Operation;
use MongoDB\BSON\Javascript;
use MongoDB\Driver\BulkWrite;
use MongoDB\Operation\CreateCollection;
use MongoDB\Operation\DropCollection;
use MongoDB\Operation\Find;
use MongoDB\Operation\MapReduce;
use MongoDB\Tests\CommandObserver;
use stdClass;
class MapReduceFunctionalTest extends FunctionalTestCase
{
public function testDefaultReadConcernIsOmitted()
{
$operation = new CreateCollection($this->getDatabaseName(), $this->getCollectionName());
$operation->execute($this->getPrimaryServer());
(new CommandObserver)->observe(
function() {
$operation = new MapReduce(
$this->getDatabaseName(),
$this->getCollectionName(),
new Javascript('function() { emit(this.x, this.y); }'),
new Javascript('function(key, values) { return Array.sum(values); }'),
['inline' => 1],
['readConcern' => $this->createDefaultReadConcern()]
);
$operation->execute($this->getPrimaryServer());
},
function(stdClass $command) {
$this->assertObjectNotHasAttribute('readConcern', $command);
}
);
}
public function testDefaultWriteConcernIsOmitted()
{
$operation = new CreateCollection($this->getDatabaseName(), $this->getCollectionName());
$operation->execute($this->getPrimaryServer());
(new CommandObserver)->observe(
function() {
$operation = new MapReduce(
$this->getDatabaseName(),
$this->getCollectionName(),
new Javascript('function() { emit(this.x, this.y); }'),
new Javascript('function(key, values) { return Array.sum(values); }'),
$this->getCollectionName() . '.output',
['writeConcern' => $this->createDefaultWriteConcern()]
);
$operation->execute($this->getPrimaryServer());
},
function(stdClass $command) {
$this->assertObjectNotHasAttribute('writeConcern', $command);
}
);
$operation = new DropCollection($this->getDatabaseName(), $this->getCollectionName() . '.output');
$operation->execute($this->getPrimaryServer());
}
public function testResult()
{
$this->createFixtures(3);
$map = new Javascript('function() { emit(this.x, this.y); }');
$reduce = new Javascript('function(key, values) { return Array.sum(values); }');
$out = ['inline' => 1];
$operation = new MapReduce($this->getDatabaseName(), $this->getCollectionName(), $map, $reduce, $out);
$result = $operation->execute($this->getPrimaryServer());
$this->assertInstanceOf('MongoDB\MapReduceResult', $result);
$this->assertGreaterThanOrEqual(0, $result->getExecutionTimeMS());
$this->assertNotEmpty($result->getCounts());
$this->assertNotEmpty($result->getTiming());
}
public function testResultDoesNotIncludeTimingWithoutVerboseOption()
{
$this->createFixtures(3);
$map = new Javascript('function() { emit(this.x, this.y); }');
$reduce = new Javascript('function(key, values) { return Array.sum(values); }');
$out = ['inline' => 1];
$operation = new MapReduce($this->getDatabaseName(), $this->getCollectionName(), $map, $reduce, $out, ['verbose' => false]);
$result = $operation->execute($this->getPrimaryServer());
$this->assertInstanceOf('MongoDB\MapReduceResult', $result);
$this->assertGreaterThanOrEqual(0, $result->getExecutionTimeMS());
$this->assertNotEmpty($result->getCounts());
$this->assertEmpty($result->getTiming());
}
/**
* @dataProvider provideTypeMapOptionsAndExpectedDocuments
*/
public function testTypeMapOptionWithInlineResults(array $typeMap = null, array $expectedDocuments)
{
$this->createFixtures(3);
$map = new Javascript('function() { emit(this.x, this.y); }');
$reduce = new Javascript('function(key, values) { return Array.sum(values); }');
$out = ['inline' => 1];
$operation = new MapReduce($this->getDatabaseName(), $this->getCollectionName(), $map, $reduce, $out, ['typeMap' => $typeMap]);
$results = iterator_to_array($operation->execute($this->getPrimaryServer()));
$this->assertEquals($expectedDocuments, $results);
}
public function provideTypeMapOptionsAndExpectedDocuments()
{
return [
[
null,
[
(object) ['_id' => 1, 'value' => 3],
(object) ['_id' => 2, 'value' => 6],
(object) ['_id' => 3, 'value' => 9],
],
],
[
['root' => 'array'],
[
['_id' => 1, 'value' => 3],
['_id' => 2, 'value' => 6],
['_id' => 3, 'value' => 9],
],
],
[
['root' => 'object'],
[
(object) ['_id' => 1, 'value' => 3],
(object) ['_id' => 2, 'value' => 6],
(object) ['_id' => 3, 'value' => 9],
],
],
];
}
/**
* @dataProvider provideTypeMapOptionsAndExpectedDocuments
*/
public function testTypeMapOptionWithOutputCollection(array $typeMap = null, array $expectedDocuments)
{
$this->createFixtures(3);
$map = new Javascript('function() { emit(this.x, this.y); }');
$reduce = new Javascript('function(key, values) { return Array.sum(values); }');
$out = $this->getCollectionName() . '.output';
$operation = new MapReduce($this->getDatabaseName(), $this->getCollectionName(), $map, $reduce, $out, ['typeMap' => $typeMap]);
$results = iterator_to_array($operation->execute($this->getPrimaryServer()));
$this->assertEquals($expectedDocuments, $results);
$operation = new Find($this->getDatabaseName(), $out, [], ['typeMap' => $typeMap]);
$cursor = $operation->execute($this->getPrimaryServer());
$this->assertEquals($expectedDocuments, iterator_to_array($cursor));
$operation = new DropCollection($this->getDatabaseName(), $out);
$operation->execute($this->getPrimaryServer());
}
/**
* Create data fixtures.
*
* @param integer $n
*/
private function createFixtures($n)
{
$bulkWrite = new BulkWrite(['ordered' => true]);
for ($i = 1; $i <= $n; $i++) {
$bulkWrite->insert(['x' => $i, 'y' => $i]);
$bulkWrite->insert(['x' => $i, 'y' => $i * 2]);
}
$result = $this->manager->executeBulkWrite($this->getNamespace(), $bulkWrite);
$this->assertEquals($n * 2, $result->getInsertedCount());
}
}