Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update code for properly work with mongodb 3.6-4.0 #175

Open
wants to merge 5 commits into
base: 1.0
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions docker/php/init.sh
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ then
if [[ ${PHP_VERSION:0:2} == "5." ]];
then
pecl install xdebug-2.5.5;
echo "mongo.native_long=0" >> /usr/local/etc/php/conf.d/mongo.ini
echo "mongo.long_as_object=1" >> /usr/local/etc/php/conf.d/mongo.ini
else
pecl install xdebug;
fi
Expand Down
18 changes: 18 additions & 0 deletions src/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,12 @@ public function delete()
{
$status = $this->getMongoCollection()->drop();

// Drop function in mongodb >= 3.6 with unacknowledged write concern return empty result
if ($status === array()) {
$writeConcern = $this->getWriteConcern();
$status = ($writeConcern['w'] === 0 ? array('ok' => 1):array('ok' => 0, 'errmsg' => 'unknown error'));
}

if ($status['ok'] != 1) {
// check if collection exists
if ('ns not found' !== $status['errmsg']) {
Expand Down Expand Up @@ -1038,6 +1044,18 @@ public function aggregate(
} else {
throw new FeatureNotSupportedException('Aggregate cursor supported from driver version 1.5');
}
} else {
if (
empty($options['explain']) &&
version_compare(\MongoClient::VERSION, '1.5.0', '>=') &&
version_compare($this->database->getClient()->getDbVersion(), '2.6', '>=')
) {
try {
return iterator_to_array($this->getMongoCollection()->aggregateCursor($pipeline, $options), false);
} catch (\Exception $e) {
throw new Exception('Aggregate error: ' . $e->getMessage());
}
}
}

// prepare command
Expand Down
13 changes: 8 additions & 5 deletions src/Cursor.php
Original file line number Diff line number Diff line change
Expand Up @@ -765,17 +765,20 @@ public function findAndRemove()
*/
public function findAndUpdate(Operator $operator, $upsert = false, $returnUpdated = true)
{
$options = array(
'new' => $returnUpdated,
'upsert' => $upsert,
);
if ($this->sort) {
$options['sort'] = $this->sort;
}
$mongoDocument = $this->collection
->getMongoCollection()
->findAndModify(
$this->expression->toArray(),
$operator ? $operator->toArray() : null,
$this->projection,
array(
'new' => $returnUpdated,
'sort' => $this->sort,
'upsert' => $upsert,
)
$options
);

if (empty($mongoDocument)) {
Expand Down
58 changes: 44 additions & 14 deletions tests/AggregatePipelineTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -361,21 +361,51 @@ public function testAggregate_ServerSideError()
))
->getMock();

$mongoDatabaseMock
->expects($this->once())
->method('command')
->will($this->returnValue(array(
'ok' => (double) 0,
'errmsg' => 'some_error',
'code' => 1785342,
)));

$database = new Database($this->collection->getDatabase()->getClient(), $mongoDatabaseMock);
$database
->getCollection('phpmongo_test_collection')
->aggregate(array(
array('$match' => array('field' => 'value'))
));

$pipeline = array(
array('$match' => array('field' => 'value'))
);

if (
version_compare(\MongoClient::VERSION, '1.5.0', '>=') &&
version_compare($database->getClient()->getDbVersion(), '2.6', '>=')
) {
$mongoCollectionMock = $this->getMockBuilder('\MongoCollection')
->setMethods(['aggregateCursor'])
->setConstructorArgs([$mongoDatabaseMock, 'phpmongo_test_collection'])
->getMock()
;
$mongoCollectionMock
->method('aggregateCursor')
->willThrowException(new \Exception('some_error'))
;

$collectionMock = $this->getMockBuilder('Sokil\Mongo\Collection')
->setMethods(['getMongoCollection'])
->setConstructorArgs([$database, 'phpmongo_test_collection'])
->getMock()
;
$collectionMock
->method('getMongoCollection')
->willReturn($mongoCollectionMock)
;

$collectionMock->aggregate($pipeline);
} else {
$mongoDatabaseMock
->expects($this->once())
->method('command')
->willReturn(array(
'ok' => (double)0,
'errmsg' => 'some_error',
'code' => 1785342,
));

$database
->getCollection('phpmongo_test_collection')
->aggregate($pipeline);
}
}

public function testAggregate_ExplainOption()
Expand Down
54 changes: 54 additions & 0 deletions tests/CollectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -533,6 +533,60 @@ public function testDeleteCollection_ExceptionOnCollectionDeleteError()
$collection->delete();
}

public function testDeleteCollection_WithUnacknowledgedWriteConcernAndEmptyResultStatus()
{
$this->collectionMock = $this
->getMockBuilder('\MongoCollection')
->setMethods(array('drop','getWriteConcern'))
->setConstructorArgs(array(
$this->database->getMongoDB(),
'phpmongo_test_collection',
))
->getMock();

$this->collectionMock
->expects($this->once())
->method('drop')
->willReturn(array());

$this->collectionMock
->expects($this->once())
->method('getWriteConcern')
->willReturn(array('w' => 0));

$collection = new Collection($this->database, $this->collectionMock);
$collection->delete();
}

/**
* @expectedException \Sokil\Mongo\Exception
* @expectedExceptionMessage Error deleting collection phpmongo_test_collection: unknown error
*/
public function testDeleteCollection_WithAcknowledgedWriteConcernAndEmptyResultStatus()
{
$this->collectionMock = $this
->getMockBuilder('\MongoCollection')
->setMethods(array('drop','getWriteConcern'))
->setConstructorArgs(array(
$this->database->getMongoDB(),
'phpmongo_test_collection',
))
->getMock();

$this->collectionMock
->expects($this->once())
->method('drop')
->willReturn(array());

$this->collectionMock
->expects($this->once())
->method('getWriteConcern')
->willReturn(array('w' => 1));

$collection = new Collection($this->database, $this->collectionMock);
$collection->delete();
}

public function testDeleteDocuments_ExpressionAsArray()
{
// add
Expand Down
2 changes: 1 addition & 1 deletion tests/DocumentValidationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -778,7 +778,7 @@ public function testIsValid_FieldEmail()

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

// additional MX check on valid email
Expand Down