Browse Source

Stabilize a bunch of tests with ext-mongo

...and break them when running against adapter, which will be fixed later.
Andreas Braun 10 years ago
parent
commit
26ba61c4ea

+ 2 - 9
lib/Mongo/MongoCollection.php

@@ -263,7 +263,7 @@ class MongoCollection
     public function insert(&$a, array $options = [])
     {
         if (! $this->ensureDocumentHasMongoId($a)) {
-            trigger_error(sprintf('%s expects parameter %d to be an array or object, %s given', __METHOD__, 1, gettype($a)), E_USER_WARNING);
+            trigger_error(sprintf('%s(): expects parameter %d to be an array or object, %s given', __METHOD__, 1, gettype($a)), E_USER_WARNING);
             return;
         }
 
@@ -354,7 +354,6 @@ class MongoCollection
             'syncMillis' => 0,
             'writtenTo' => null,
             'err' => null,
-            'errmsg' => null,
         ];
     }
 
@@ -907,16 +906,13 @@ class MongoCollection
     {
         $checkKeys = function($array) {
             foreach (array_keys($array) as $key) {
-                if (is_int($key) || empty($key) || strpos($key, '*') === 1) {
+                if (empty($key) || strpos($key, '*') === 1) {
                     throw new \MongoException('document contain invalid key');
                 }
             }
         };
 
         if (is_array($document)) {
-            if (empty($document)) {
-                throw new \MongoException('document cannot be empty');
-            }
             if (! isset($document['_id'])) {
                 $document['_id'] = new \MongoId();
             }
@@ -925,9 +921,6 @@ class MongoCollection
 
             return $document['_id'];
         } elseif (is_object($document)) {
-            if (empty((array) $document)) {
-                throw new \MongoException('document cannot be empty');
-            }
             if (! isset($document->_id)) {
                 $document->_id = new \MongoId();
             }

+ 70 - 72
tests/Alcaeus/MongoDbAdapter/MongoCollectionTest.php

@@ -43,7 +43,7 @@ class MongoCollectionTest extends TestCase
 
     public function testInsertInvalidData()
     {
-        $this->setExpectedException('PHPUnit_Framework_Error_Warning', 'ongoCollection::insert expects parameter 1 to be an array or object, integer given');
+        $this->setExpectedException('PHPUnit_Framework_Error_Warning', 'MongoCollection::insert(): expects parameter 1 to be an array or object, integer given');
 
         $document = 8;
         $this->getCollection()->insert($document);
@@ -51,33 +51,33 @@ class MongoCollectionTest extends TestCase
 
     public function testInsertEmptyArray()
     {
-        $this->setExpectedException('MongoException', 'document cannot be empty');
-
         $document = [];
         $this->getCollection()->insert($document);
+
+        $this->assertSame(1, $this->getCollection()->count());
     }
 
     public function testInsertArrayWithNumericKeys()
     {
-        $this->setExpectedException('MongoException', 'document contain invalid key');
-
         $document = [1 => 'foo'];
         $this->getCollection()->insert($document);
+
+        $this->assertSame(1, $this->getCollection()->count(['_id' => $document['_id']]));
     }
 
     public function testInsertEmptyObject()
     {
-        $this->setExpectedException('MongoException', 'document cannot be empty');
-
         $document = (object) [];
         $this->getCollection()->insert($document);
+
+        $this->assertSame(1, $this->getCollection()->count());
     }
 
     public function testInsertObjectWithPrivateProperties()
     {
-        $this->setExpectedException('MongoException', 'document contain invalid key');
+        $this->setExpectedException('MongoException', 'zero-length keys are not allowed, did you use $ with double quotes?');
 
-        $document = $this->getCollection();
+        $document = new PrivatePropertiesStub();
         $this->getCollection()->insert($document);
     }
 
@@ -91,14 +91,8 @@ class MongoCollectionTest extends TestCase
         $collection->insert($document);
 
         unset($document['_id']);
-        $this->assertArraySubset(
-            [
-                'ok' => 0.0,
-                'n' => 0,
-                'err' => 11000,
-            ],
-            $collection->insert($document)
-        );
+        $this->setExpectedException('MongoDuplicateKeyException');
+        $collection->insert($document);
     }
 
     public function testUnacknowledgedWrite()
@@ -109,7 +103,10 @@ class MongoCollectionTest extends TestCase
 
     public function testInsertWriteConcernException()
     {
-        $this->setExpectedException('MongoConnectionException');
+        $this->setExpectedException(
+            'MongoWriteConcernException',
+            "localhost:27017: cannot use 'w' > 1 when a host is not replicated"
+        );
 
         $document = ['foo' => 'bar'];
         $this->getCollection()->insert($document, ['w' => 2]);
@@ -118,19 +115,18 @@ class MongoCollectionTest extends TestCase
     public function testInsertMany()
     {
         $expected = [
-            'connectionId' => 0,
+            'ok' => 1.0,
             'n' => 0,
             'syncMillis' => 0,
             'writtenTo' => null,
             'err' => null,
-            'errmsg' => null
         ];
 
         $documents = [
             ['foo' => 'bar'],
             ['bar' => 'foo']
         ];
-        $this->assertSame($expected, $this->getCollection()->batchInsert($documents));
+        $this->assertArraySubset($expected, $this->getCollection()->batchInsert($documents));
 
         foreach ($documents as $document) {
             $this->assertInstanceOf('MongoId', $document['_id']);
@@ -141,19 +137,18 @@ class MongoCollectionTest extends TestCase
     public function testInsertManyWithNonNumericKeys()
     {
         $expected = [
-            'connectionId' => 0,
+            'ok' => 1.0,
             'n' => 0,
             'syncMillis' => 0,
             'writtenTo' => null,
             'err' => null,
-            'errmsg' => null
         ];
 
         $documents = [
             'a' => ['foo' => 'bar'],
             'b' => ['bar' => 'foo']
         ];
-        $this->assertSame($expected, $this->getCollection()->batchInsert($documents));
+        $this->assertArraySubset($expected, $this->getCollection()->batchInsert($documents));
 
         $newCollection = $this->getCheckDatabase()->selectCollection('test');
         $this->assertSame(2, $newCollection->count());
@@ -162,19 +157,18 @@ class MongoCollectionTest extends TestCase
     public function testBatchInsertContinuesOnError()
     {
         $expected = [
-            'connectionId' => 0,
+            'ok' => 1.0,
             'n' => 0,
             'syncMillis' => 0,
             'writtenTo' => null,
             'err' => null,
-            'errmsg' => null
         ];
 
         $documents = [
             8,
             'b' => ['bar' => 'foo']
         ];
-        $this->assertSame($expected, $this->getCollection()->batchInsert($documents, ['continueOnError' => true]));
+        $this->assertArraySubset($expected, $this->getCollection()->batchInsert($documents, ['continueOnError' => true]));
 
         $newCollection = $this->getCheckDatabase()->selectCollection('test');
         $this->assertSame(1, $newCollection->count());
@@ -182,7 +176,7 @@ class MongoCollectionTest extends TestCase
 
     public function testBatchInsertException()
     {
-        $this->setExpectedException('MongoConnectionException');
+        $this->setExpectedException('MongoResultException', 'localhost:27017: cannot use \'w\' > 1 when a host is not replicated');
 
         $documents = [['foo' => 'bar']];
         $this->getCollection()->batchInsert($documents, ['w' => 2]);
@@ -198,7 +192,7 @@ class MongoCollectionTest extends TestCase
 
     public function testUpdateWriteConcern()
     {
-        $this->setExpectedException('MongoConnectionException'); // does not match driver
+        $this->setExpectedException('MongoWriteConcernException', "localhost:27017: cannot use 'w' > 1 when a host is not replicated");
 
         $this->getCollection()->update([], ['$set' => ['foo' => 'bar']], ['w' => 2]);
     }
@@ -237,16 +231,8 @@ class MongoCollectionTest extends TestCase
         $document = ['foo' => 'foo'];
         $collection->insert($document);
 
-        $this->assertArraySubset(
-            [
-                'ok' => 0.0,
-                'nModified' => 0,
-                'n' => 0,
-                'err' => 11000,
-                'updatedExisting' => true,
-            ],
-            $collection->update(['foo' => 'bar'], ['$set' => ['foo' => 'foo']])
-        );
+        $this->setExpectedException('MongoDuplicateKeyException');
+        $collection->update(['foo' => 'bar'], ['$set' => ['foo' => 'foo']]);
     }
 
     public function testUpdateMany()
@@ -377,11 +363,11 @@ class MongoCollectionTest extends TestCase
 
     public function testFindOneConnectionIssue()
     {
+        $this->setExpectedException('MongoConnectionException');
+
         $client = $this->getClient([], 'mongodb://localhost:28888?connectTimeoutMS=1');
         $collection = $client->selectCollection('mongo-php-adapter', 'test');
 
-        $this->setExpectedException('MongoConnectionException');
-
         $collection->findOne();
     }
 
@@ -497,7 +483,7 @@ class MongoCollectionTest extends TestCase
         $this->assertSame(['type' => \MongoClient::RP_SECONDARY_PREFERRED, 'tagsets' => [['a' => 'b']]], $collection->getReadPreference());
 
         $this->assertTrue($collection->setSlaveOkay(false));
-        $this->assertSame(['type' => \MongoClient::RP_PRIMARY], $collection->getReadPreference());
+        $this->assertArraySubset(['type' => \MongoClient::RP_PRIMARY], $collection->getReadPreference());
     }
 
     public function testReadPreferenceIsSetInDriver()
@@ -527,18 +513,9 @@ class MongoCollectionTest extends TestCase
     public function testWriteConcern()
     {
         $collection = $this->getCollection();
-        $this->assertSame(['w' => 1, 'wtimeout' => 0], $collection->getWriteConcern());
-        $this->assertSame(1, $collection->w);
-        $this->assertSame(0, $collection->wtimeout);
 
         $this->assertTrue($collection->setWriteConcern('majority', 100));
         $this->assertSame(['w' => 'majority', 'wtimeout' => 100], $collection->getWriteConcern());
-
-        $collection->w = 2;
-        $this->assertSame(['w' => 2, 'wtimeout' => 100], $collection->getWriteConcern());
-
-        $collection->wtimeout = -1;
-        $this->assertSame(['w' => 2, 'wtimeout' => 0], $collection->getWriteConcern());
     }
 
     public function testWriteConcernIsSetInDriver()
@@ -569,17 +546,19 @@ class MongoCollectionTest extends TestCase
         $id = '54203e08d51d4a1f868b456e';
         $collection = $this->getCollection();
 
+        $objectId = new \MongoId($id);
         $expected = [
             'ok' => 1.0,
             'nModified' => 0,
-            'n' => 0,
+            'n' => 1,
             'err' => null,
             'errmsg' => null,
+            'upserted' => $objectId,
             'updatedExisting' => false,
         ];
 
-        $document = ['_id' => new \MongoId($id), 'foo' => 'bar'];
-        $this->assertSame($expected, $collection->save($document));
+        $document = ['_id' => $objectId, 'foo' => 'bar'];
+        $this->assertEquals($expected, $collection->save($document));
 
         $newCollection = $this->getCheckDatabase()->selectCollection('test');
         $this->assertSame(1, $newCollection->count());
@@ -679,26 +658,18 @@ class MongoCollectionTest extends TestCase
 
     public function testSaveEmptyKeys()
     {
-        $this->setExpectedException('MongoException');
-
         $document = [];
         $this->getCollection()->save($document);
+
+        $this->assertSame(1, $this->getCollection()->count());
     }
 
     public function testSaveEmptyObject()
     {
-        $this->setExpectedException('MongoException');
-
         $document = (object) [];
         $this->getCollection()->save($document);
-    }
-
-    public function testSaveWrite()
-    {
-        $this->setExpectedException('MongoConnectionException'); // should be MongoCursorException
 
-        $document = ['foo' => 'bar'];
-        $this->getCollection()->save($document, ['w' => 2, 'wtimeout' => 1000]);
+        $this->assertSame(1, $this->getCollection()->count());
     }
 
     public function testGetDBRef()
@@ -751,7 +722,7 @@ class MongoCollectionTest extends TestCase
 
     public function testCreateIndexInvalid()
     {
-        $this->setExpectedException('MongoException', 'keys cannot be empty');
+        $this->setExpectedException('MongoException', 'index specification has no elements');
 
         $this->getCollection()->createIndex([]);
     }
@@ -762,8 +733,8 @@ class MongoCollectionTest extends TestCase
 
         $expected = [
             'createdCollectionAutomatically' => false,
-            'numIndexesBefore' => 1,
-            'numIndexesAfter' => 1,
+            'numIndexesBefore' => 2,
+            'numIndexesAfter' => 2,
             'note' => 'all indexes already exist',
             'ok' => 1.0
         ];
@@ -790,8 +761,15 @@ class MongoCollectionTest extends TestCase
 
     public function testEnsureIndex()
     {
+        $expected = [
+            'createdCollectionAutomatically' => true,
+            'numIndexesBefore' => 1,
+            'numIndexesAfter' => 2,
+            'ok' => 1.0
+        ];
+
         $collection = $this->getCollection();
-        $this->assertTrue($collection->ensureIndex(['bar' => 1], ['unique' => true]));
+        $this->assertEquals($expected, $collection->ensureIndex(['bar' => 1], ['unique' => true]));
 
         $newCollection = $this->getCheckDatabase()->selectCollection('test');
         $indexes = iterator_to_array($newCollection->listIndexes());
@@ -809,6 +787,22 @@ class MongoCollectionTest extends TestCase
 
         $expected = [
             'nIndexesWas' => 2,
+            'errmsg' => 'index not found with name [bar_1]',
+            'ok' => 0.0,
+            'code' => 27,
+        ];
+        $this->assertEquals($expected, $this->getCollection()->deleteIndex('bar'));
+
+        $this->assertCount(2, iterator_to_array($newCollection->listIndexes()));
+    }
+
+    public function testDeleteIndexUsingField()
+    {
+        $newCollection = $this->getCheckDatabase()->selectCollection('test');
+        $newCollection->createIndex(['bar' => 1]);
+
+        $expected = [
+            'nIndexesWas' => 2,
             'ok' => 1.0,
         ];
         $this->assertSame($expected, $this->getCollection()->deleteIndex('bar'));
@@ -1060,8 +1054,12 @@ class MongoCollectionTest extends TestCase
     {
         $collection = $this->getCollection();
 
-        $this->setExpectedException('Exception', 'Collection name cannot contain null bytes');
-
-        $collection->{'foo' . chr(0)};
+        $this->assertInstanceOf('MongoCollection', $collection->{'foo' . chr(0)});
+        $this->assertSame('test', $collection->getName());
     }
 }
+
+class PrivatePropertiesStub
+{
+    private $foo = 'bar';
+}

+ 10 - 7
tests/Alcaeus/MongoDbAdapter/MongoCommandCursorTest.php

@@ -17,7 +17,7 @@ class MongoCommandCursorTest extends TestCase
         $expected = [
             'ns' => 'mongo-php-adapter.test',
             'limit' => 0,
-            'batchSize' => null,
+            'batchSize' => 0,
             'skip' => 0,
             'flags' => 0,
             'query' => [
@@ -27,25 +27,28 @@ class MongoCommandCursorTest extends TestCase
                         '$match' => ['foo' => 'bar']
                     ]
                 ],
-                'cursor' => true,
+                'cursor' => new \stdClass(),
             ],
             'fields' => null,
             'started_iterating' => false,
         ];
-        $this->assertEquals($expected, $cursor->info());
+        $info = $cursor->info();
+        $this->assertEquals($expected, $info);
 
         // Ensure cursor started iterating
         $array = iterator_to_array($cursor);
 
         $expected['started_iterating'] = true;
         $expected += [
-            'id' => '0',
-            'at' => null,
-            'numReturned' => null,
+            'id' => 0,
+            'at' => 0,
+            'numReturned' => 0,
             'server' => 'localhost:27017;-;.;' . getmypid(),
             'host' => 'localhost',
             'port' => 27017,
-            'connection_type_desc' => 'STANDALONE'
+            'connection_type_desc' => 'STANDALONE',
+            'firstBatchAt' => 2,
+            'firstBatchNumReturned' => 2,
         ];
 
         $this->assertEquals($expected, $cursor->info());

+ 7 - 7
tests/Alcaeus/MongoDbAdapter/MongoCursorTest.php

@@ -42,7 +42,7 @@ class MongoCursorTest extends TestCase
 
     public function testCountCannotConnect()
     {
-        $client = $this->getClient([], 'mongodb://localhost:28888');
+        $client = $this->getClient(['connect' => false], 'mongodb://localhost:28888');
         $cursor = $client->selectCollection('mongo-php-adapter', 'test')->find();
 
         $this->setExpectedException('MongoConnectionException');
@@ -292,7 +292,7 @@ class MongoCursorTest extends TestCase
         $expected = [
             'ns' => 'mongo-php-adapter.test',
             'limit' => 3,
-            'batchSize' => null,
+            'batchSize' => 0,
             'skip' => 1,
             'flags' => 0,
             'query' => ['foo' => 'bar'],
@@ -300,23 +300,23 @@ class MongoCursorTest extends TestCase
             'started_iterating' => false,
         ];
 
-        $this->assertSame($expected, $cursor->info());
+        $this->assertEquals($expected, $cursor->info());
 
         // Ensure cursor started iterating
         iterator_to_array($cursor);
 
         $expected['started_iterating'] = true;
         $expected += [
-            'id' => '0',
-            'at' => null,
-            'numReturned' => null,
+            'id' => 0,
+            'at' => 1,
+            'numReturned' => 1,
             'server' => 'localhost:27017;-;.;' . getmypid(),
             'host' => 'localhost',
             'port' => 27017,
             'connection_type_desc' => 'STANDALONE'
         ];
 
-        $this->assertSame($expected, $cursor->info());
+        $this->assertEquals($expected, $cursor->info());
     }
 
     public function testReadPreferenceIsInherited()

+ 3 - 2
tests/Alcaeus/MongoDbAdapter/MongoDBRefTest.php

@@ -34,17 +34,18 @@ class MongoDBRefTest extends TestCase
     public static function dataCreateThroughMongoDB()
     {
         $id = new \MongoId();
-        $validRef = ['$ref' => 'test', '$id' => $id, '$db' => 'mongo-php-adapter'];
+        $validRef = ['$ref' => 'test', '$id' => $id];
 
         $object = new \stdClass();
         $object->_id = $id;
 
+        $objectWithoutId = new \stdClass();
         return [
             'simpleId' => [$validRef, $id],
             'arrayWithIdProperty' => [$validRef, ['_id' => $id]],
             'objectWithIdProperty' => [$validRef, $object],
             'arrayWithoutId' => [null, []],
-            'objectWithoutId' => [null, new \stdClass()],
+            'objectWithoutId' => [['$ref' => 'test', '$id' => $objectWithoutId], $objectWithoutId],
         ];
     }
 

+ 3 - 11
tests/Alcaeus/MongoDbAdapter/MongoDeleteBatchTest.php

@@ -17,12 +17,8 @@ class MongoDeleteBatchTest extends TestCase
         $this->assertTrue($batch->add(['q' => ['foo' => 'bar'], 'limit' => 1]));
 
         $expected = [
-            'ok' => 1.0,
-            'nInserted' => 0,
-            'nMatched' => 0,
-            'nModified' => 0,
-            'nUpserted' => 0,
             'nRemoved' => 1,
+            'ok' => true,
         ];
 
         $this->assertSame($expected, $batch->execute());
@@ -44,12 +40,8 @@ class MongoDeleteBatchTest extends TestCase
         $this->assertTrue($batch->add(['q' => ['foo' => 'bar'], 'limit' => 0]));
 
         $expected = [
-            'ok' => 1.0,
-            'nInserted' => 0,
-            'nMatched' => 0,
-            'nModified' => 0,
-            'nUpserted' => 0,
             'nRemoved' => 2,
+            'ok' => true,
         ];
 
         $this->assertSame($expected, $batch->execute());
@@ -64,7 +56,7 @@ class MongoDeleteBatchTest extends TestCase
         $collection = $this->getCollection();
         $batch = new \MongoDeleteBatch($collection);
 
-        $this->setExpectedException('Exception', 'invalid item');
+        $this->setExpectedException('Exception', "Expected \$item to contain 'q' key");
 
         $batch->add([]);
     }

+ 1 - 4
tests/Alcaeus/MongoDbAdapter/MongoGridFSFileTest.php

@@ -70,7 +70,7 @@ class MongoGridFSFileTest extends TestCase
 
     public function testGetResource()
     {
-        $id = $this->prepareFile();
+        $id = $this->prepareFile(str_repeat('a', 300 * 1024));
         $file = $this->getFile(['_id' => $id, 'length' => 4]);
 
         $result = $file->getResource();
@@ -101,9 +101,6 @@ class MongoGridFSFileTest extends TestCase
     {
         $collection = $this->getGridFS();
 
-        // to make sure we have multiple chunks
-        $extra += ['chunkSize' => 2];
-
         return $collection->storeBytes($data, $extra);
     }
 

+ 4 - 2
tests/Alcaeus/MongoDbAdapter/MongoIdTest.php

@@ -1,6 +1,7 @@
 <?php
 
 namespace Alcaeus\MongoDbAdapter\Tests;
+use MongoDB\BSON\ObjectID;
 
 /**
  * @author alcaeus <alcaeus@alcaeus.org>
@@ -48,7 +49,7 @@ class MongoIdTest extends TestCase
         $this->skipTestIf(extension_loaded('mongo'));
 
         $original = '54203e08d51d4a1f868b456e';
-        $objectId = new \MongoDB\BSON\ObjectID($original);
+        $objectId = new ObjectID($original);
 
         $id = new \MongoId($objectId);
         $this->assertSame($original, (string) $id);
@@ -61,6 +62,7 @@ class MongoIdTest extends TestCase
      */
     public function testIsValid($expected, $value)
     {
+        $this->skipTestIf($value instanceof ObjectID && extension_loaded('mongo'));
         $this->assertSame($expected, \MongoId::isValid($value));
     }
 
@@ -71,7 +73,7 @@ class MongoIdTest extends TestCase
         return [
             'validId' => [true, '' . $original . ''],
             'MongoId' => [true, new \MongoId($original)],
-            'ObjectID' => [true, new \MongoDB\BSON\ObjectID($original)],
+            'ObjectID' => [true, new ObjectID($original)],
             'invalidString' => [false, 'abc'],
         ];
     }

+ 15 - 11
tests/Alcaeus/MongoDbAdapter/MongoInsertBatchTest.php

@@ -12,12 +12,8 @@ class MongoInsertBatchTest extends TestCase
         $this->assertTrue($batch->add(['bar' => 'foo']));
 
         $expected = [
-            'ok' => 1.0,
             'nInserted' => 2,
-            'nMatched' => 0,
-            'nModified' => 0,
-            'nUpserted' => 0,
-            'nRemoved' => 0,
+            'ok' => true,
         ];
 
         $this->assertSame($expected, $batch->execute());
@@ -40,14 +36,22 @@ class MongoInsertBatchTest extends TestCase
         $this->assertTrue($batch->add(['foo' => 'bar']));
 
         $expected = [
-            'ok' => 0.0,
+            'writeErrors' => [
+                [
+                    'index' => 1,
+                    'code' => 11000,
+                    'errmsg' => 'E11000 duplicate key error collection: mongo-php-adapter.test index: foo_1 dup key: { : "bar" }',
+                ]
+            ],
             'nInserted' => 1,
-            'nMatched' => 0,
-            'nModified' => 0,
-            'nUpserted' => 0,
-            'nRemoved' => 0,
+            'ok' => true,
         ];
 
-        $this->assertSame($expected, $batch->execute());
+        try {
+            $batch->execute();
+        } catch (\MongoWriteConcernException $e) {
+            $this->assertSame('Failed write', $e->getMessage());
+            $this->assertSame($expected, $e->getDocument());
+        }
     }
 }

+ 13 - 11
tests/Alcaeus/MongoDbAdapter/MongoUpdateBatchTest.php

@@ -17,12 +17,10 @@ class MongoUpdateBatchTest extends TestCase
         $this->assertTrue($batch->add(['q' => ['foo' => 'bar'], 'u' => ['$set' => ['foo' => 'foo']]]));
 
         $expected = [
-            'ok' => 1.0,
-            'nInserted' => 0,
             'nMatched' => 1,
             'nModified' => 1,
             'nUpserted' => 0,
-            'nRemoved' => 0,
+            'ok' => true,
         ];
 
         $this->assertSame($expected, $batch->execute());
@@ -49,12 +47,10 @@ class MongoUpdateBatchTest extends TestCase
         $this->assertTrue($batch->add(['q' => ['foo' => 'bar'], 'u' => ['$set' => ['foo' => 'foo']], 'multi' => true]));
 
         $expected = [
-            'ok' => 1.0,
-            'nInserted' => 0,
             'nMatched' => 2,
             'nModified' => 2,
             'nUpserted' => 0,
-            'nRemoved' => 0,
+            'ok' => true,
         ];
 
         $this->assertSame($expected, $batch->execute());
@@ -74,15 +70,21 @@ class MongoUpdateBatchTest extends TestCase
         $this->assertTrue($batch->add(['q' => [], 'u' => ['$set' => ['foo' => 'bar']], 'upsert' => true]));
 
         $expected = [
-            'ok' => 1.0,
-            'nInserted' => 0,
+            'upserted' => [
+                [
+                    'index' => 0,
+                ]
+            ],
             'nMatched' => 0,
             'nModified' => 0,
             'nUpserted' => 1,
-            'nRemoved' => 0,
+            'ok' => true,
         ];
 
-        $this->assertSame($expected, $batch->execute());
+        $result = $batch->execute();
+        $this->assertArraySubset($expected, $result);
+
+        $this->assertInstanceOf('MongoId', $result['upserted'][0]['_id']);
 
         $newCollection = $this->getCheckDatabase()->selectCollection('test');
         $this->assertSame(1, $newCollection->count());
@@ -97,7 +99,7 @@ class MongoUpdateBatchTest extends TestCase
         $collection = $this->getCollection();
         $batch = new \MongoUpdateBatch($collection);
 
-        $this->setExpectedException('Exception', 'invalid item');
+        $this->setExpectedException('Exception', "Expected \$item to contain 'q' key");
 
         $batch->add([]);
     }