diff --git a/src/Operation/Aggregate.php b/src/Operation/Aggregate.php index 37621d34c..6e12d05d1 100644 --- a/src/Operation/Aggregate.php +++ b/src/Operation/Aggregate.php @@ -122,20 +122,21 @@ public function execute(Server $server) return $cursor; } + $cursor->setTypeMap(array('document' => 'stdClass')); $result = current($cursor->toArray()); - if (empty($result['ok'])) { - throw new RuntimeException(isset($result['errmsg']) ? $result['errmsg'] : 'Unknown error'); + // TODO: Remove this once PHPC-318 is implemented + is_array($result) and $result = (object) $result; + + if (empty($result->ok)) { + throw new RuntimeException(isset($result->errmsg) ? $result->errmsg : 'Unknown error'); } - if ( ! isset($result['result']) || ! is_array($result['result'])) { + if ( ! isset($result->result) || ! is_array($result->result)) { throw new UnexpectedValueException('aggregate command did not return a "result" array'); } - return new ArrayIterator(array_map( - function (stdClass $document) { return (array) $document; }, - $result['result'] - )); + return new ArrayIterator($result->result); } /** diff --git a/src/Operation/Count.php b/src/Operation/Count.php index 137fd47d9..c989761cb 100644 --- a/src/Operation/Count.php +++ b/src/Operation/Count.php @@ -85,6 +85,7 @@ public function __construct($databaseName, $collectionName, array $filter = arra public function execute(Server $server) { $cursor = $server->executeCommand($this->databaseName, $this->createCommand()); + $cursor->setTypeMap(array('document' => 'array')); $result = current($cursor->toArray()); if (empty($result['ok'])) { diff --git a/src/Operation/CreateCollection.php b/src/Operation/CreateCollection.php index 8b71d388c..d62564e39 100644 --- a/src/Operation/CreateCollection.php +++ b/src/Operation/CreateCollection.php @@ -106,10 +106,14 @@ public function __construct($databaseName, $collectionName, array $options = arr public function execute(Server $server) { $cursor = $server->executeCommand($this->databaseName, $this->createCommand()); + $cursor->setTypeMap(array('document' => 'stdClass')); $result = current($cursor->toArray()); - if (empty($result['ok'])) { - throw new RuntimeException(isset($result['errmsg']) ? $result['errmsg'] : 'Unknown error'); + // TODO: Remove this once PHPC-318 is implemented + is_array($result) and $result = (object) $result; + + if (empty($result->ok)) { + throw new RuntimeException(isset($result->errmsg) ? $result->errmsg : 'Unknown error'); } return $result; diff --git a/src/Operation/CreateIndexes.php b/src/Operation/CreateIndexes.php index 858d31e8b..9d4b708a4 100644 --- a/src/Operation/CreateIndexes.php +++ b/src/Operation/CreateIndexes.php @@ -91,6 +91,7 @@ private function executeCommand(Server $server) )); $cursor = $server->executeCommand($this->databaseName, $command); + $cursor->setTypeMap(array('document' => 'array')); $result = current($cursor->toArray()); if (empty($result['ok'])) { diff --git a/src/Operation/Distinct.php b/src/Operation/Distinct.php index 576627054..fa4c9bf42 100644 --- a/src/Operation/Distinct.php +++ b/src/Operation/Distinct.php @@ -62,17 +62,21 @@ public function __construct($databaseName, $collectionName, $fieldName, array $f public function execute(Server $server) { $cursor = $server->executeCommand($this->databaseName, $this->createCommand()); + $cursor->setTypeMap(array('document' => 'stdClass')); $result = current($cursor->toArray()); - if (empty($result['ok'])) { - throw new RuntimeException(isset($result['errmsg']) ? $result['errmsg'] : 'Unknown error'); + // TODO: Remove this once PHPC-318 is implemented + is_array($result) and $result = (object) $result; + + if (empty($result->ok)) { + throw new RuntimeException(isset($result->errmsg) ? $result->errmsg : 'Unknown error'); } - if ( ! isset($result['values']) || ! is_array($result['values'])) { + if ( ! isset($result->values) || ! is_array($result->values)) { throw new UnexpectedValueException('distinct command did not return a "values" array'); } - return $result['values']; + return $result->values; } /** diff --git a/src/Operation/DropCollection.php b/src/Operation/DropCollection.php index 437ccfcb1..2cd3f5330 100644 --- a/src/Operation/DropCollection.php +++ b/src/Operation/DropCollection.php @@ -41,10 +41,14 @@ public function __construct($databaseName, $collectionName) public function execute(Server $server) { $cursor = $server->executeCommand($this->databaseName, new Command(array('drop' => $this->collectionName))); + $cursor->setTypeMap(array('document' => 'stdClass')); $result = current($cursor->toArray()); - if (empty($result['ok'])) { - throw new RuntimeException(isset($result['errmsg']) ? $result['errmsg'] : 'Unknown error'); + // TODO: Remove this once PHPC-318 is implemented + is_array($result) and $result = (object) $result; + + if (empty($result->ok)) { + throw new RuntimeException(isset($result->errmsg) ? $result->errmsg : 'Unknown error'); } return $result; diff --git a/src/Operation/DropDatabase.php b/src/Operation/DropDatabase.php index 4c4313d3e..7c22fc08c 100644 --- a/src/Operation/DropDatabase.php +++ b/src/Operation/DropDatabase.php @@ -39,10 +39,14 @@ public function __construct($databaseName) public function execute(Server $server) { $cursor = $server->executeCommand($this->databaseName, new Command(array('dropDatabase' => 1))); + $cursor->setTypeMap(array('document' => 'stdClass')); $result = current($cursor->toArray()); - if (empty($result['ok'])) { - throw new RuntimeException(isset($result['errmsg']) ? $result['errmsg'] : 'Unknown error'); + // TODO: Remove this once PHPC-318 is implemented + is_array($result) and $result = (object) $result; + + if (empty($result->ok)) { + throw new RuntimeException(isset($result->errmsg) ? $result->errmsg : 'Unknown error'); } return $result; diff --git a/src/Operation/DropIndexes.php b/src/Operation/DropIndexes.php index 78ab4de6e..c79b21cdc 100644 --- a/src/Operation/DropIndexes.php +++ b/src/Operation/DropIndexes.php @@ -56,10 +56,14 @@ public function execute(Server $server) ); $cursor = $server->executeCommand($this->databaseName, new Command($cmd)); + $cursor->setTypeMap(array('document' => 'stdClass')); $result = current($cursor->toArray()); - if (empty($result['ok'])) { - throw new RuntimeException(isset($result['errmsg']) ? $result['errmsg'] : 'Unknown error'); + // TODO: Remove this once PHPC-318 is implemented + is_array($result) and $result = (object) $result; + + if (empty($result->ok)) { + throw new RuntimeException(isset($result->errmsg) ? $result->errmsg : 'Unknown error'); } return $result; diff --git a/src/Operation/FindAndModify.php b/src/Operation/FindAndModify.php index 0c288b058..bf0a76bd7 100644 --- a/src/Operation/FindAndModify.php +++ b/src/Operation/FindAndModify.php @@ -118,13 +118,17 @@ public function __construct($databaseName, $collectionName, array $options) public function execute(Server $server) { $cursor = $server->executeCommand($this->databaseName, $this->createCommand()); + $cursor->setTypeMap(array('document' => 'stdClass')); $result = current($cursor->toArray()); - if (empty($result['ok'])) { - throw new RuntimeException(isset($result['errmsg']) ? $result['errmsg'] : 'Unknown error'); + // TODO: Remove this once PHPC-318 is implemented + is_array($result) and $result = (object) $result; + + if (empty($result->ok)) { + throw new RuntimeException(isset($result->errmsg) ? $result->errmsg : 'Unknown error'); } - if ( ! isset($result['value'])) { + if ( ! isset($result->value)) { return null; } @@ -133,17 +137,17 @@ public function execute(Server $server) * requested. */ if ($this->options['upsert'] && ! $this->options['new'] && - isset($result['lastErrorObject']->updatedExisting) && - ! $result['lastErrorObject']->updatedExisting) { + isset($result->lastErrorObject->updatedExisting) && + ! $result->lastErrorObject->updatedExisting) { return null; } - if ( ! is_object($result['value'])) { + if ( ! is_object($result->value)) { throw new UnexpectedValueException('findAndModify command did not return a "value" document'); } - return $result['value']; + return $result->value; } /** diff --git a/tests/Collection/BulkWriteFunctionalTest.php b/tests/Collection/BulkWriteFunctionalTest.php index 73a913732..29a80f644 100644 --- a/tests/Collection/BulkWriteFunctionalTest.php +++ b/tests/Collection/BulkWriteFunctionalTest.php @@ -35,7 +35,7 @@ public function testInserts() array('_id' => $insertedIds[1], 'x' => 22), ); - $this->assertEquals($expected, $this->collection->find()->toArray()); + $this->assertSameDocuments($expected, $this->collection->find()); } public function testUpdates() @@ -69,7 +69,7 @@ public function testUpdates() array('_id' => $upsertedIds[3], 'x' => 67), ); - $this->assertEquals($expected, $this->collection->find()->toArray()); + $this->assertSameDocuments($expected, $this->collection->find()); } public function testDeletes() @@ -89,7 +89,7 @@ public function testDeletes() array('_id' => 2, 'x' => 22), ); - $this->assertEquals($expected, $this->collection->find()->toArray()); + $this->assertSameDocuments($expected, $this->collection->find()); } public function testMixedOrderedOperations() @@ -123,7 +123,7 @@ public function testMixedOrderedOperations() array('_id' => 4, 'x' => 44), ); - $this->assertEquals($expected, $this->collection->find()->toArray()); + $this->assertSameDocuments($expected, $this->collection->find()); } /** diff --git a/tests/Collection/CrudSpec/AggregateFunctionalTest.php b/tests/Collection/CrudSpec/AggregateFunctionalTest.php index 183cda284..2e69ade4a 100644 --- a/tests/Collection/CrudSpec/AggregateFunctionalTest.php +++ b/tests/Collection/CrudSpec/AggregateFunctionalTest.php @@ -36,8 +36,7 @@ public function testAggregateWithMultipleStages() array('_id' => 3, 'x' => 33), ); - // Use iterator_to_array() here since aggregate() may return an ArrayIterator - $this->assertEquals($expected, iterator_to_array($cursor)); + $this->assertSameDocuments($expected, $cursor); } public function testAggregateWithOut() @@ -64,7 +63,7 @@ public function testAggregateWithOut() array('_id' => 3, 'x' => 33), ); - $this->assertEquals($expected, $outputCollection->find()->toArray()); + $this->assertSameDocuments($expected, $outputCollection->find()); // Manually clean up our output collection $this->dropCollectionIfItExists($outputCollection); diff --git a/tests/Collection/CrudSpec/DeleteManyFunctionalTest.php b/tests/Collection/CrudSpec/DeleteManyFunctionalTest.php index adfe5df5c..cbde7687c 100644 --- a/tests/Collection/CrudSpec/DeleteManyFunctionalTest.php +++ b/tests/Collection/CrudSpec/DeleteManyFunctionalTest.php @@ -27,7 +27,7 @@ public function testDeleteManyWhenManyDocumentsMatch() array('_id' => 1, 'x' => 11), ); - $this->assertSame($expected, $this->collection->find()->toArray()); + $this->assertSameDocuments($expected, $this->collection->find()); } public function testDeleteManyWhenNoDocumentsMatch() @@ -43,6 +43,6 @@ public function testDeleteManyWhenNoDocumentsMatch() array('_id' => 3, 'x' => 33), ); - $this->assertSame($expected, $this->collection->find()->toArray()); + $this->assertSameDocuments($expected, $this->collection->find()); } } diff --git a/tests/Collection/CrudSpec/DeleteOneFunctionalTest.php b/tests/Collection/CrudSpec/DeleteOneFunctionalTest.php index e89c4b708..031cbc21b 100644 --- a/tests/Collection/CrudSpec/DeleteOneFunctionalTest.php +++ b/tests/Collection/CrudSpec/DeleteOneFunctionalTest.php @@ -28,7 +28,7 @@ public function testDeleteOneWhenManyDocumentsMatch() array('_id' => 3, 'x' => 33), ); - $this->assertSame($expected, $this->collection->find()->toArray()); + $this->assertSameDocuments($expected, $this->collection->find()); } public function testDeleteOneWhenOneDocumentMatches() @@ -43,7 +43,7 @@ public function testDeleteOneWhenOneDocumentMatches() array('_id' => 3, 'x' => 33), ); - $this->assertSame($expected, $this->collection->find()->toArray()); + $this->assertSameDocuments($expected, $this->collection->find()); } public function testDeleteOneWhenNoDocumentsMatch() @@ -59,6 +59,6 @@ public function testDeleteOneWhenNoDocumentsMatch() array('_id' => 3, 'x' => 33), ); - $this->assertSame($expected, $this->collection->find()->toArray()); + $this->assertSameDocuments($expected, $this->collection->find()); } } diff --git a/tests/Collection/CrudSpec/FindFunctionalTest.php b/tests/Collection/CrudSpec/FindFunctionalTest.php index c82da7d64..b7f0339a9 100644 --- a/tests/Collection/CrudSpec/FindFunctionalTest.php +++ b/tests/Collection/CrudSpec/FindFunctionalTest.php @@ -24,7 +24,7 @@ public function testFindWithFilter() array('_id' => 1, 'x' => 11), ); - $this->assertSame($expected, $this->collection->find($filter)->toArray()); + $this->assertSameDocuments($expected, $this->collection->find($filter)); } public function testFindWithFilterSortSkipAndLimit() @@ -40,7 +40,7 @@ public function testFindWithFilterSortSkipAndLimit() array('_id' => 5, 'x' => 55), ); - $this->assertSame($expected, $this->collection->find($filter, $options)->toArray()); + $this->assertSameDocuments($expected, $this->collection->find($filter, $options)); } public function testFindWithLimitSortAndBatchSize() @@ -59,6 +59,6 @@ public function testFindWithLimitSortAndBatchSize() array('_id' => 4, 'x' => 44), ); - $this->assertSame($expected, $this->collection->find($filter, $options)->toArray()); + $this->assertSameDocuments($expected, $this->collection->find($filter, $options)); } } diff --git a/tests/Collection/CrudSpec/FindOneAndDeleteFunctionalTest.php b/tests/Collection/CrudSpec/FindOneAndDeleteFunctionalTest.php index ecd321220..c9d72bdae 100644 --- a/tests/Collection/CrudSpec/FindOneAndDeleteFunctionalTest.php +++ b/tests/Collection/CrudSpec/FindOneAndDeleteFunctionalTest.php @@ -25,14 +25,14 @@ public function testFindOneAndDeleteWhenManyDocumentsMatch() ); $document = $this->collection->findOneAndDelete($filter, $options); - $this->assertEquals((object) array('x' => 22), $document); + $this->assertSameDocument(array('x' => 22), $document); $expected = array( array('_id' => 1, 'x' => 11), array('_id' => 3, 'x' => 33), ); - $this->assertSame($expected, $this->collection->find()->toArray()); + $this->assertSameDocuments($expected, $this->collection->find()); } public function testFindOneAndDeleteWhenOneDocumentMatches() @@ -44,14 +44,14 @@ public function testFindOneAndDeleteWhenOneDocumentMatches() ); $document = $this->collection->findOneAndDelete($filter, $options); - $this->assertEquals((object) array('x' => 22), $document); + $this->assertSameDocument(array('x' => 22), $document); $expected = array( array('_id' => 1, 'x' => 11), array('_id' => 3, 'x' => 33), ); - $this->assertSame($expected, $this->collection->find()->toArray()); + $this->assertSameDocuments($expected, $this->collection->find()); } public function testFindOneAndDeleteWhenNoDocumentsMatch() @@ -71,6 +71,6 @@ public function testFindOneAndDeleteWhenNoDocumentsMatch() array('_id' => 3, 'x' => 33), ); - $this->assertSame($expected, $this->collection->find()->toArray()); + $this->assertSameDocuments($expected, $this->collection->find()); } } diff --git a/tests/Collection/CrudSpec/FindOneAndReplaceFunctionalTest.php b/tests/Collection/CrudSpec/FindOneAndReplaceFunctionalTest.php index 9f4480d2b..4ca979603 100644 --- a/tests/Collection/CrudSpec/FindOneAndReplaceFunctionalTest.php +++ b/tests/Collection/CrudSpec/FindOneAndReplaceFunctionalTest.php @@ -29,7 +29,7 @@ public function testFindOneAndReplaceWhenManyDocumentsMatchReturningDocumentBefo ); $document = $this->collection->findOneAndReplace($filter, $replacement, $options); - $this->assertEquals((object) array('x' => 22), $document); + $this->assertSameDocument(array('x' => 22), $document); $expected = array( array('_id' => 1, 'x' => 11), @@ -37,7 +37,7 @@ public function testFindOneAndReplaceWhenManyDocumentsMatchReturningDocumentBefo array('_id' => 3, 'x' => 33), ); - $this->assertSame($expected, $this->collection->find()->toArray()); + $this->assertSameDocuments($expected, $this->collection->find()); } public function testFindOneAndReplaceWhenManyDocumentsMatchReturningDocumentAfterModification() @@ -51,7 +51,7 @@ public function testFindOneAndReplaceWhenManyDocumentsMatchReturningDocumentAfte ); $document = $this->collection->findOneAndReplace($filter, $replacement, $options); - $this->assertEquals((object) array('x' => 32), $document); + $this->assertSameDocument(array('x' => 32), $document); $expected = array( array('_id' => 1, 'x' => 11), @@ -59,7 +59,7 @@ public function testFindOneAndReplaceWhenManyDocumentsMatchReturningDocumentAfte array('_id' => 3, 'x' => 33), ); - $this->assertSame($expected, $this->collection->find()->toArray()); + $this->assertSameDocuments($expected, $this->collection->find()); } public function testFindOneAndReplaceWhenOneDocumentMatchesReturningDocumentBeforeModification() @@ -72,7 +72,7 @@ public function testFindOneAndReplaceWhenOneDocumentMatchesReturningDocumentBefo ); $document = $this->collection->findOneAndReplace($filter, $replacement, $options); - $this->assertEquals((object) array('x' => 22), $document); + $this->assertSameDocument(array('x' => 22), $document); $expected = array( array('_id' => 1, 'x' => 11), @@ -80,7 +80,7 @@ public function testFindOneAndReplaceWhenOneDocumentMatchesReturningDocumentBefo array('_id' => 3, 'x' => 33), ); - $this->assertSame($expected, $this->collection->find()->toArray()); + $this->assertSameDocuments($expected, $this->collection->find()); } public function testFindOneAndReplaceWhenOneDocumentMatchesReturningDocumentAfterModification() @@ -94,7 +94,7 @@ public function testFindOneAndReplaceWhenOneDocumentMatchesReturningDocumentAfte ); $document = $this->collection->findOneAndReplace($filter, $replacement, $options); - $this->assertEquals((object) array('x' => 32), $document); + $this->assertSameDocument(array('x' => 32), $document); $expected = array( array('_id' => 1, 'x' => 11), @@ -102,7 +102,7 @@ public function testFindOneAndReplaceWhenOneDocumentMatchesReturningDocumentAfte array('_id' => 3, 'x' => 33), ); - $this->assertSame($expected, $this->collection->find()->toArray()); + $this->assertSameDocuments($expected, $this->collection->find()); } public function testFindOneAndReplaceWhenNoDocumentsMatchReturningDocumentBeforeModification() @@ -123,7 +123,7 @@ public function testFindOneAndReplaceWhenNoDocumentsMatchReturningDocumentBefore array('_id' => 3, 'x' => 33), ); - $this->assertSame($expected, $this->collection->find()->toArray()); + $this->assertSameDocuments($expected, $this->collection->find()); } public function testFindOneAndReplaceWithUpsertWhenNoDocumentsMatchReturningDocumentBeforeModification() @@ -147,7 +147,7 @@ public function testFindOneAndReplaceWithUpsertWhenNoDocumentsMatchReturningDocu array('_id' => 4, 'x' => 44), ); - $this->assertSame($expected, $this->collection->find()->toArray()); + $this->assertSameDocuments($expected, $this->collection->find()); } public function testFindOneAndReplaceWhenNoDocumentsMatchReturningDocumentAfterModification() @@ -169,7 +169,7 @@ public function testFindOneAndReplaceWhenNoDocumentsMatchReturningDocumentAfterM array('_id' => 3, 'x' => 33), ); - $this->assertSame($expected, $this->collection->find()->toArray()); + $this->assertSameDocuments($expected, $this->collection->find()); } public function testFindOneAndReplaceWithUpsertWhenNoDocumentsMatchReturningDocumentAfterModification() @@ -185,7 +185,7 @@ public function testFindOneAndReplaceWithUpsertWhenNoDocumentsMatchReturningDocu ); $document = $this->collection->findOneAndReplace($filter, $replacement, $options); - $this->assertEquals((object) array('x' => 44), $document); + $this->assertSameDocument(array('x' => 44), $document); $expected = array( array('_id' => 1, 'x' => 11), @@ -194,6 +194,6 @@ public function testFindOneAndReplaceWithUpsertWhenNoDocumentsMatchReturningDocu array('_id' => 4, 'x' => 44), ); - $this->assertSame($expected, $this->collection->find()->toArray()); + $this->assertSameDocuments($expected, $this->collection->find()); } } diff --git a/tests/Collection/CrudSpec/FindOneAndUpdateFunctionalTest.php b/tests/Collection/CrudSpec/FindOneAndUpdateFunctionalTest.php index c07b9a98c..b54a5f7d4 100644 --- a/tests/Collection/CrudSpec/FindOneAndUpdateFunctionalTest.php +++ b/tests/Collection/CrudSpec/FindOneAndUpdateFunctionalTest.php @@ -29,7 +29,7 @@ public function testFindOneAndUpdateWhenManyDocumentsMatchReturningDocumentBefor ); $document = $this->collection->findOneAndUpdate($filter, $update, $options); - $this->assertEquals((object) array('x' => 22), $document); + $this->assertSameDocument(array('x' => 22), $document); $expected = array( array('_id' => 1, 'x' => 11), @@ -37,7 +37,7 @@ public function testFindOneAndUpdateWhenManyDocumentsMatchReturningDocumentBefor array('_id' => 3, 'x' => 33), ); - $this->assertSame($expected, $this->collection->find()->toArray()); + $this->assertSameDocuments($expected, $this->collection->find()); } public function testFindOneAndUpdateWhenManyDocumentsMatchReturningDocumentAfterModification() @@ -51,7 +51,7 @@ public function testFindOneAndUpdateWhenManyDocumentsMatchReturningDocumentAfter ); $document = $this->collection->findOneAndUpdate($filter, $update, $options); - $this->assertEquals((object) array('x' => 23), $document); + $this->assertSameDocument(array('x' => 23), $document); $expected = array( array('_id' => 1, 'x' => 11), @@ -59,7 +59,7 @@ public function testFindOneAndUpdateWhenManyDocumentsMatchReturningDocumentAfter array('_id' => 3, 'x' => 33), ); - $this->assertSame($expected, $this->collection->find()->toArray()); + $this->assertSameDocuments($expected, $this->collection->find()); } public function testFindOneAndUpdateWhenOneDocumentMatchesReturningDocumentBeforeModification() @@ -72,7 +72,7 @@ public function testFindOneAndUpdateWhenOneDocumentMatchesReturningDocumentBefor ); $document = $this->collection->findOneAndUpdate($filter, $update, $options); - $this->assertEquals((object) array('x' => 22), $document); + $this->assertSameDocument(array('x' => 22), $document); $expected = array( array('_id' => 1, 'x' => 11), @@ -80,7 +80,7 @@ public function testFindOneAndUpdateWhenOneDocumentMatchesReturningDocumentBefor array('_id' => 3, 'x' => 33), ); - $this->assertSame($expected, $this->collection->find()->toArray()); + $this->assertSameDocuments($expected, $this->collection->find()); } public function testFindOneAndUpdateWhenOneDocumentMatchesReturningDocumentAfterModification() @@ -94,7 +94,7 @@ public function testFindOneAndUpdateWhenOneDocumentMatchesReturningDocumentAfter ); $document = $this->collection->findOneAndUpdate($filter, $update, $options); - $this->assertEquals((object) array('x' => 23), $document); + $this->assertSameDocument(array('x' => 23), $document); $expected = array( array('_id' => 1, 'x' => 11), @@ -102,7 +102,7 @@ public function testFindOneAndUpdateWhenOneDocumentMatchesReturningDocumentAfter array('_id' => 3, 'x' => 33), ); - $this->assertSame($expected, $this->collection->find()->toArray()); + $this->assertSameDocuments($expected, $this->collection->find()); } public function testFindOneAndUpdateWhenNoDocumentsMatchReturningDocumentBeforeModification() @@ -123,7 +123,7 @@ public function testFindOneAndUpdateWhenNoDocumentsMatchReturningDocumentBeforeM array('_id' => 3, 'x' => 33), ); - $this->assertSame($expected, $this->collection->find()->toArray()); + $this->assertSameDocuments($expected, $this->collection->find()); } public function testFindOneAndUpdateWithUpsertWhenNoDocumentsMatchReturningDocumentBeforeModification() @@ -146,7 +146,7 @@ public function testFindOneAndUpdateWithUpsertWhenNoDocumentsMatchReturningDocum array('_id' => 4, 'x' => 1), ); - $this->assertSame($expected, $this->collection->find()->toArray()); + $this->assertSameDocuments($expected, $this->collection->find()); } public function testFindOneAndUpdateWhenNoDocumentsMatchReturningDocumentAfterModification() @@ -168,7 +168,7 @@ public function testFindOneAndUpdateWhenNoDocumentsMatchReturningDocumentAfterMo array('_id' => 3, 'x' => 33), ); - $this->assertSame($expected, $this->collection->find()->toArray()); + $this->assertSameDocuments($expected, $this->collection->find()); } public function testFindOneAndUpdateWithUpsertWhenNoDocumentsMatchReturningDocumentAfterModification() @@ -183,7 +183,7 @@ public function testFindOneAndUpdateWithUpsertWhenNoDocumentsMatchReturningDocum ); $document = $this->collection->findOneAndUpdate($filter, $update, $options); - $this->assertEquals((object) array('x' => 1), $document); + $this->assertSameDocument(array('x' => 1), $document); $expected = array( array('_id' => 1, 'x' => 11), @@ -192,6 +192,6 @@ public function testFindOneAndUpdateWithUpsertWhenNoDocumentsMatchReturningDocum array('_id' => 4, 'x' => 1), ); - $this->assertSame($expected, $this->collection->find()->toArray()); + $this->assertSameDocuments($expected, $this->collection->find()); } } diff --git a/tests/Collection/CrudSpec/InsertManyFunctionalTest.php b/tests/Collection/CrudSpec/InsertManyFunctionalTest.php index a5b550dc6..577adff74 100644 --- a/tests/Collection/CrudSpec/InsertManyFunctionalTest.php +++ b/tests/Collection/CrudSpec/InsertManyFunctionalTest.php @@ -33,6 +33,6 @@ public function testInsertManyWithNonexistentDocuments() array('_id' => 3, 'x' => 33), ); - $this->assertSame($expected, $this->collection->find()->toArray()); + $this->assertSameDocuments($expected, $this->collection->find()); } } diff --git a/tests/Collection/CrudSpec/InsertOneFunctionalTest.php b/tests/Collection/CrudSpec/InsertOneFunctionalTest.php index 9b198a371..62363464c 100644 --- a/tests/Collection/CrudSpec/InsertOneFunctionalTest.php +++ b/tests/Collection/CrudSpec/InsertOneFunctionalTest.php @@ -29,6 +29,6 @@ public function testInsertOneWithANonexistentDocument() array('_id' => 2, 'x' => 22), ); - $this->assertSame($expected, $this->collection->find()->toArray()); + $this->assertSameDocuments($expected, $this->collection->find()); } } diff --git a/tests/Collection/CrudSpec/ReplaceOneFunctionalTest.php b/tests/Collection/CrudSpec/ReplaceOneFunctionalTest.php index 40c8f1ef1..04a205cf8 100644 --- a/tests/Collection/CrudSpec/ReplaceOneFunctionalTest.php +++ b/tests/Collection/CrudSpec/ReplaceOneFunctionalTest.php @@ -35,7 +35,7 @@ public function testReplaceOneWhenManyDocumentsMatch() array('_id' => 3, 'x' => 33), ); - $this->assertSame($expected, $this->collection->find()->toArray()); + $this->assertSameDocuments($expected, $this->collection->find()); } public function testReplaceOneWhenOneDocumentMatches() @@ -53,7 +53,7 @@ public function testReplaceOneWhenOneDocumentMatches() array('_id' => 3, 'x' => 33), ); - $this->assertSame($expected, $this->collection->find()->toArray()); + $this->assertSameDocuments($expected, $this->collection->find()); } public function testReplaceOneWhenNoDocumentsMatch() @@ -71,7 +71,7 @@ public function testReplaceOneWhenNoDocumentsMatch() array('_id' => 3, 'x' => 33), ); - $this->assertSame($expected, $this->collection->find()->toArray()); + $this->assertSameDocuments($expected, $this->collection->find()); } public function testReplaceOneWithUpsertWhenNoDocumentsMatchWithAnIdSpecified() @@ -92,7 +92,7 @@ public function testReplaceOneWithUpsertWhenNoDocumentsMatchWithAnIdSpecified() array('_id' => 4, 'x' => 1), ); - $this->assertSame($expected, $this->collection->find()->toArray()); + $this->assertSameDocuments($expected, $this->collection->find()); } public function testReplaceOneWithUpsertWhenNoDocumentsMatchWithoutAnIdSpecified() @@ -114,6 +114,6 @@ public function testReplaceOneWithUpsertWhenNoDocumentsMatchWithoutAnIdSpecified array('_id' => 4, 'x' => 1), ); - $this->assertSame($expected, $this->collection->find()->toArray()); + $this->assertSameDocuments($expected, $this->collection->find()); } } diff --git a/tests/Collection/CrudSpec/UpdateManyFunctionalTest.php b/tests/Collection/CrudSpec/UpdateManyFunctionalTest.php index 71012e4b9..69789bb86 100644 --- a/tests/Collection/CrudSpec/UpdateManyFunctionalTest.php +++ b/tests/Collection/CrudSpec/UpdateManyFunctionalTest.php @@ -35,7 +35,7 @@ public function testUpdateManyWhenManyDocumentsMatch() array('_id' => 3, 'x' => 34), ); - $this->assertSame($expected, $this->collection->find()->toArray()); + $this->assertSameDocuments($expected, $this->collection->find()); } public function testUpdateManyWhenOneDocumentMatches() @@ -53,7 +53,7 @@ public function testUpdateManyWhenOneDocumentMatches() array('_id' => 3, 'x' => 33), ); - $this->assertSame($expected, $this->collection->find()->toArray()); + $this->assertSameDocuments($expected, $this->collection->find()); } public function testUpdateManyWhenNoDocumentsMatch() @@ -71,7 +71,7 @@ public function testUpdateManyWhenNoDocumentsMatch() array('_id' => 3, 'x' => 33), ); - $this->assertSame($expected, $this->collection->find()->toArray()); + $this->assertSameDocuments($expected, $this->collection->find()); } public function testUpdateManyWithUpsertWhenNoDocumentsMatch() @@ -92,6 +92,6 @@ public function testUpdateManyWithUpsertWhenNoDocumentsMatch() array('_id' => 4, 'x' => 1), ); - $this->assertSame($expected, $this->collection->find()->toArray()); + $this->assertSameDocuments($expected, $this->collection->find()); } } diff --git a/tests/Collection/CrudSpec/UpdateOneFunctionalTest.php b/tests/Collection/CrudSpec/UpdateOneFunctionalTest.php index 3d45ea84a..f572eddcf 100644 --- a/tests/Collection/CrudSpec/UpdateOneFunctionalTest.php +++ b/tests/Collection/CrudSpec/UpdateOneFunctionalTest.php @@ -35,7 +35,7 @@ public function testUpdateOneWhenManyDocumentsMatch() array('_id' => 3, 'x' => 33), ); - $this->assertSame($expected, $this->collection->find()->toArray()); + $this->assertSameDocuments($expected, $this->collection->find()); } public function testUpdateOneWhenOneDocumentMatches() @@ -53,7 +53,7 @@ public function testUpdateOneWhenOneDocumentMatches() array('_id' => 3, 'x' => 33), ); - $this->assertSame($expected, $this->collection->find()->toArray()); + $this->assertSameDocuments($expected, $this->collection->find()); } public function testUpdateOneWhenNoDocumentsMatch() @@ -71,7 +71,7 @@ public function testUpdateOneWhenNoDocumentsMatch() array('_id' => 3, 'x' => 33), ); - $this->assertSame($expected, $this->collection->find()->toArray()); + $this->assertSameDocuments($expected, $this->collection->find()); } public function testUpdateOneWithUpsertWhenNoDocumentsMatch() @@ -92,6 +92,6 @@ public function testUpdateOneWithUpsertWhenNoDocumentsMatch() array('_id' => 4, 'x' => 1), ); - $this->assertSame($expected, $this->collection->find()->toArray()); + $this->assertSameDocuments($expected, $this->collection->find()); } } diff --git a/tests/FunctionalTestCase.php b/tests/FunctionalTestCase.php index 94c98e2f9..18a897885 100644 --- a/tests/FunctionalTestCase.php +++ b/tests/FunctionalTestCase.php @@ -6,6 +6,8 @@ use MongoDB\Driver\Cursor; use MongoDB\Driver\Manager; use MongoDB\Driver\ReadPreference; +use stdClass; +use Traversable; abstract class FunctionalTestCase extends TestCase { @@ -38,6 +40,34 @@ protected function assertCommandSucceeded($document) $this->assertEquals(1, $document['ok']); } + protected function assertSameDocument($expectedDocument, $actualDocument) + { + $this->assertEquals( + ($expectedDocument instanceof stdClass) ? (array) $expectedDocument : $expectedDocument, + ($actualDocument instanceof stdClass) ? (array) $actualDocument : $actualDocument + ); + } + + protected function assertSameDocuments(array $expectedDocuments, $actualDocuments) + { + if ($actualDocuments instanceof Traversable) { + $actualDocuments = iterator_to_array($actualDocuments); + } + + if ( ! is_array($actualDocuments)) { + throw new InvalidArgumentException('$actualDocuments is not an array or Traversable'); + } + + $normalizeRootDocuments = function($document) { + return ($document instanceof stdClass) ? (array) $document : $document; + }; + + $this->assertEquals( + array_map($normalizeRootDocuments, $expectedDocuments), + array_map($normalizeRootDocuments, $actualDocuments) + ); + } + protected function getServerVersion(ReadPreference $readPreference = null) { $cursor = $this->manager->executeCommand(