Skip to content

Commit 68d4908

Browse files
committed
Merge pull request #723
2 parents 546f928 + ea75303 commit 68d4908

File tree

7 files changed

+78
-11
lines changed

7 files changed

+78
-11
lines changed

.travis.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,12 @@ jobs:
9191
env:
9292
- SERVER_VERSION=4.0.12
9393

94+
# Test upcoming server version
95+
- stage: Test
96+
php: "7.3"
97+
env:
98+
- SERVER_VERSION=4.3.3
99+
94100
# Test other server configurations
95101
- stage: Test
96102
php: "7.3"

src/MapReduceResult.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,8 @@ class MapReduceResult implements IteratorAggregate
5555
public function __construct(callable $getIterator, stdClass $result)
5656
{
5757
$this->getIterator = $getIterator;
58-
$this->executionTimeMS = (integer) $result->timeMillis;
59-
$this->counts = (array) $result->counts;
58+
$this->executionTimeMS = isset($result->timeMillis) ? (integer) $result->timeMillis : 0;
59+
$this->counts = isset($result->counts) ? (array) $result->counts : [];
6060
$this->timing = isset($result->timing) ? (array) $result->timing : [];
6161
}
6262

src/Model/CollectionInfoCommandIterator.php

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
namespace MongoDB\Model;
1919

2020
use IteratorIterator;
21+
use Traversable;
2122

2223
/**
2324
* CollectionInfoIterator for listCollections command results.
@@ -32,6 +33,19 @@
3233
*/
3334
class CollectionInfoCommandIterator extends IteratorIterator implements CollectionInfoIterator
3435
{
36+
/** @var string|null */
37+
private $databaseName;
38+
39+
/**
40+
* @param string|null $databaseName
41+
*/
42+
public function __construct(Traversable $iterator, $databaseName = null)
43+
{
44+
parent::__construct($iterator);
45+
46+
$this->databaseName = $databaseName;
47+
}
48+
3549
/**
3650
* Return the current element as a CollectionInfo instance.
3751
*
@@ -41,6 +55,12 @@ class CollectionInfoCommandIterator extends IteratorIterator implements Collecti
4155
*/
4256
public function current()
4357
{
44-
return new CollectionInfo(parent::current());
58+
$info = parent::current();
59+
60+
if ($this->databaseName !== null && isset($info['idIndex']) && ! isset($info['idIndex']['ns'])) {
61+
$info['idIndex']['ns'] = $this->databaseName . '.' . $info['name'];
62+
}
63+
64+
return new CollectionInfo($info);
4565
}
4666
}

src/Operation/ListCollections.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,6 @@ private function executeCommand(Server $server)
136136
$cursor = $server->executeReadCommand($this->databaseName, new Command($cmd), $this->createOptions());
137137
$cursor->setTypeMap(['root' => 'array', 'document' => 'array']);
138138

139-
return new CollectionInfoCommandIterator(new CachingIterator($cursor));
139+
return new CollectionInfoCommandIterator(new CachingIterator($cursor), $this->databaseName);
140140
}
141141
}

tests/Collection/CollectionFunctionalTest.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -396,8 +396,10 @@ public function testMapReduce()
396396

397397
$this->assertSameDocuments($expected, $result);
398398

399-
$this->assertGreaterThanOrEqual(0, $result->getExecutionTimeMS());
400-
$this->assertNotEmpty($result->getCounts());
399+
if (version_compare($this->getServerVersion(), '4.3.0', '<')) {
400+
$this->assertGreaterThanOrEqual(0, $result->getExecutionTimeMS());
401+
$this->assertNotEmpty($result->getCounts());
402+
}
401403
}
402404

403405
public function collectionMethodClosures()

tests/Operation/MapReduceFunctionalTest.php

Lines changed: 33 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@
99
use MongoDB\Operation\Find;
1010
use MongoDB\Operation\MapReduce;
1111
use MongoDB\Tests\CommandObserver;
12+
use function is_object;
1213
use function iterator_to_array;
14+
use function usort;
1315
use function version_compare;
1416

1517
class MapReduceFunctionalTest extends FunctionalTestCase
@@ -92,12 +94,19 @@ public function testResult()
9294
$result = $operation->execute($this->getPrimaryServer());
9395

9496
$this->assertInstanceOf(MapReduceResult::class, $result);
95-
$this->assertGreaterThanOrEqual(0, $result->getExecutionTimeMS());
96-
$this->assertNotEmpty($result->getCounts());
97+
98+
if (version_compare($this->getServerVersion(), '4.3.0', '<')) {
99+
$this->assertGreaterThanOrEqual(0, $result->getExecutionTimeMS());
100+
$this->assertNotEmpty($result->getCounts());
101+
}
97102
}
98103

99104
public function testResultIncludesTimingWithVerboseOption()
100105
{
106+
if (version_compare($this->getServerVersion(), '4.3.0', '>=')) {
107+
$this->markTestSkipped('mapReduce statistics are no longer exposed');
108+
}
109+
101110
$this->createFixtures(3);
102111

103112
$map = new Javascript('function() { emit(this.x, this.y); }');
@@ -115,6 +124,10 @@ public function testResultIncludesTimingWithVerboseOption()
115124

116125
public function testResultDoesNotIncludeTimingWithoutVerboseOption()
117126
{
127+
if (version_compare($this->getServerVersion(), '4.3.0', '>=')) {
128+
$this->markTestSkipped('mapReduce statistics are no longer exposed');
129+
}
130+
118131
$this->createFixtures(3);
119132

120133
$map = new Javascript('function() { emit(this.x, this.y); }');
@@ -226,7 +239,7 @@ public function testTypeMapOptionWithInlineResults(array $typeMap = null, array
226239
$operation = new MapReduce($this->getDatabaseName(), $this->getCollectionName(), $map, $reduce, $out, ['typeMap' => $typeMap]);
227240
$results = iterator_to_array($operation->execute($this->getPrimaryServer()));
228241

229-
$this->assertEquals($expectedDocuments, $results);
242+
$this->assertEquals($this->sortResults($expectedDocuments), $this->sortResults($results));
230243
}
231244

232245
public function provideTypeMapOptionsAndExpectedDocuments()
@@ -273,12 +286,12 @@ public function testTypeMapOptionWithOutputCollection(array $typeMap = null, arr
273286
$operation = new MapReduce($this->getDatabaseName(), $this->getCollectionName(), $map, $reduce, $out, ['typeMap' => $typeMap]);
274287
$results = iterator_to_array($operation->execute($this->getPrimaryServer()));
275288

276-
$this->assertEquals($expectedDocuments, $results);
289+
$this->assertEquals($this->sortResults($expectedDocuments), $this->sortResults($results));
277290

278291
$operation = new Find($this->getDatabaseName(), $out, [], ['typeMap' => $typeMap]);
279292
$cursor = $operation->execute($this->getPrimaryServer());
280293

281-
$this->assertEquals($expectedDocuments, iterator_to_array($cursor));
294+
$this->assertEquals($this->sortResults($expectedDocuments), $this->sortResults(iterator_to_array($cursor)));
282295

283296
$operation = new DropCollection($this->getDatabaseName(), $out);
284297
$operation->execute($this->getPrimaryServer());
@@ -302,4 +315,19 @@ private function createFixtures($n)
302315

303316
$this->assertEquals($n * 2, $result->getInsertedCount());
304317
}
318+
319+
private function sortResults(array $results) : array
320+
{
321+
$sortFunction = static function ($resultA, $resultB) : int {
322+
$idA = is_object($resultA) ? $resultA->_id : $resultA['_id'];
323+
$idB = is_object($resultB) ? $resultB->_id : $resultB['_id'];
324+
325+
return $idA <=> $idB;
326+
};
327+
328+
$sortedResults = $results;
329+
usort($sortedResults, $sortFunction);
330+
331+
return $sortedResults;
332+
}
305333
}

tests/SpecTests/CrudSpecTest.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,13 @@
1414
*/
1515
class CrudSpecTest extends FunctionalTestCase
1616
{
17+
/** @var array */
18+
private static $incompleteTests = [
19+
'find-allowdiskuse: Find does not send allowDiskuse when value is not specified' => 'PHPLIB-500',
20+
'find-allowdiskuse: Find sends allowDiskuse false when false is specified' => 'PHPLIB-500',
21+
'find-allowdiskuse: Find sends allowDiskUse true when true is specified' => 'PHPLIB-500',
22+
];
23+
1724
/**
1825
* Assert that the expected and actual command documents match.
1926
*
@@ -37,6 +44,10 @@ public static function assertCommandMatches(stdClass $expected, stdClass $actual
3744
*/
3845
public function testCrud(stdClass $test, array $runOn = null, array $data, $databaseName = null, $collectionName = null)
3946
{
47+
if (isset(self::$incompleteTests[$this->dataDescription()])) {
48+
$this->markTestIncomplete(self::$incompleteTests[$this->dataDescription()]);
49+
}
50+
4051
if (isset($runOn)) {
4152
$this->checkServerRequirements($runOn);
4253
}

0 commit comments

Comments
 (0)