Skip to content

Commit ddf50b0

Browse files
MCubsTinyXXX
authored andcommitted
Update code for mongodb 3.6-4.0 work properly
1 parent 96d72b9 commit ddf50b0

File tree

3 files changed

+62
-15
lines changed

3 files changed

+62
-15
lines changed

src/Collection.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,11 @@ public function delete()
232232
{
233233
$status = $this->getMongoCollection()->drop();
234234

235+
if ($status === array()) {
236+
$writeConcern = $this->getWriteConcern();
237+
$status['ok'] = ($writeConcern['w'] === 0 ? 1:0);
238+
}
239+
235240
if ($status['ok'] != 1) {
236241
// check if collection exists
237242
if ('ns not found' !== $status['errmsg']) {
@@ -1038,6 +1043,18 @@ public function aggregate(
10381043
} else {
10391044
throw new FeatureNotSupportedException('Aggregate cursor supported from driver version 1.5');
10401045
}
1046+
} else {
1047+
if (
1048+
empty($options['explain']) &&
1049+
version_compare(\MongoClient::VERSION, '1.5.0', '>=') &&
1050+
version_compare($this->database->getClient()->getDbVersion(), '2.6', '>=')
1051+
) {
1052+
try {
1053+
return iterator_to_array($this->getMongoCollection()->aggregateCursor($pipeline, $options), false);
1054+
} catch (\Exception $e) {
1055+
throw new Exception('Aggregate error: ' . $e->getMessage());
1056+
}
1057+
}
10411058
}
10421059

10431060
// prepare command

tests/AggregatePipelineTest.php

Lines changed: 44 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -361,21 +361,51 @@ public function testAggregate_ServerSideError()
361361
))
362362
->getMock();
363363

364-
$mongoDatabaseMock
365-
->expects($this->once())
366-
->method('command')
367-
->will($this->returnValue(array(
368-
'ok' => (double) 0,
369-
'errmsg' => 'some_error',
370-
'code' => 1785342,
371-
)));
372-
373364
$database = new Database($this->collection->getDatabase()->getClient(), $mongoDatabaseMock);
374-
$database
375-
->getCollection('phpmongo_test_collection')
376-
->aggregate(array(
377-
array('$match' => array('field' => 'value'))
378-
));
365+
366+
$pipeline = array(
367+
array('$match' => array('field' => 'value'))
368+
);
369+
370+
if (
371+
version_compare(\MongoClient::VERSION, '1.5.0', '>=') &&
372+
version_compare($database->getClient()->getDbVersion(), '2.6', '>=')
373+
) {
374+
$mongoCollectionMock = $this->getMockBuilder('\MongoCollection')
375+
->setMethods(['aggregateCursor'])
376+
->setConstructorArgs([$mongoDatabaseMock, 'phpmongo_test_collection'])
377+
->getMock()
378+
;
379+
$mongoCollectionMock
380+
->method('aggregateCursor')
381+
->willThrowException(new \Exception('some_error'))
382+
;
383+
384+
$collectionMock = $this->getMockBuilder('Sokil\Mongo\Collection')
385+
->setMethods(['getMongoCollection'])
386+
->setConstructorArgs([$database, 'phpmongo_test_collection'])
387+
->getMock()
388+
;
389+
$collectionMock
390+
->method('getMongoCollection')
391+
->willReturn($mongoCollectionMock)
392+
;
393+
394+
$collectionMock->aggregate($pipeline);
395+
} else {
396+
$mongoDatabaseMock
397+
->expects($this->once())
398+
->method('command')
399+
->willReturn(array(
400+
'ok' => (double)0,
401+
'errmsg' => 'some_error',
402+
'code' => 1785342,
403+
));
404+
405+
$database
406+
->getCollection('phpmongo_test_collection')
407+
->aggregate($pipeline);
408+
}
379409
}
380410

381411
public function testAggregate_ExplainOption()

tests/DocumentValidationTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -778,7 +778,7 @@ public function testIsValid_FieldEmail()
778778

779779
try {
780780
// additional MX check on wrong email
781-
$document->set('some-field-name-mx', 'user@example.com');
781+
$document->set('some-field-name-mx', 'user@example');
782782
$this->assertFalse($document->isValid());
783783

784784
// additional MX check on valid email

0 commit comments

Comments
 (0)