From b419611f72676f8259cfbe5fcab06273a872fade Mon Sep 17 00:00:00 2001 From: Derick Rethans Date: Thu, 18 Jun 2015 13:01:01 +0100 Subject: [PATCH 1/5] Use type map to force arrays instead of objects. Perhaps, an alternative could be to use access properties instead of array elements? --- src/Operation/Count.php | 1 + src/Operation/CreateCollection.php | 1 + src/Operation/Distinct.php | 1 + src/Operation/DropCollection.php | 1 + src/Operation/DropDatabase.php | 1 + src/Operation/FindAndModify.php | 1 + 6 files changed, 6 insertions(+) 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..b7b082f95 100644 --- a/src/Operation/CreateCollection.php +++ b/src/Operation/CreateCollection.php @@ -106,6 +106,7 @@ public function __construct($databaseName, $collectionName, array $options = arr 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/Distinct.php b/src/Operation/Distinct.php index 576627054..489e26a9a 100644 --- a/src/Operation/Distinct.php +++ b/src/Operation/Distinct.php @@ -62,6 +62,7 @@ public function __construct($databaseName, $collectionName, $fieldName, array $f 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/DropCollection.php b/src/Operation/DropCollection.php index 437ccfcb1..cd5af3215 100644 --- a/src/Operation/DropCollection.php +++ b/src/Operation/DropCollection.php @@ -41,6 +41,7 @@ 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' => 'array')); $result = current($cursor->toArray()); if (empty($result['ok'])) { diff --git a/src/Operation/DropDatabase.php b/src/Operation/DropDatabase.php index 4c4313d3e..f453b6a0e 100644 --- a/src/Operation/DropDatabase.php +++ b/src/Operation/DropDatabase.php @@ -39,6 +39,7 @@ public function __construct($databaseName) public function execute(Server $server) { $cursor = $server->executeCommand($this->databaseName, new Command(array('dropDatabase' => 1))); + $cursor->setTypeMap(array('document' => 'array')); $result = current($cursor->toArray()); if (empty($result['ok'])) { diff --git a/src/Operation/FindAndModify.php b/src/Operation/FindAndModify.php index 0c288b058..d2ef79394 100644 --- a/src/Operation/FindAndModify.php +++ b/src/Operation/FindAndModify.php @@ -118,6 +118,7 @@ public function __construct($databaseName, $collectionName, array $options) 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'])) { From 3558cfa8f03e53895473c94fbaeebbb097cb5bee Mon Sep 17 00:00:00 2001 From: Derick Rethans Date: Thu, 18 Jun 2015 14:35:57 +0100 Subject: [PATCH 2/5] Compare all arrays of documents by setting the typemap for documents to 'array'. --- tests/Collection/BulkWriteFunctionalTest.php | 8 ++++---- .../CrudSpec/AggregateFunctionalTest.php | 6 +++--- .../CrudSpec/DeleteManyFunctionalTest.php | 4 ++-- .../CrudSpec/DeleteOneFunctionalTest.php | 6 +++--- tests/Collection/CrudSpec/FindFunctionalTest.php | 6 +++--- .../CrudSpec/FindOneAndDeleteFunctionalTest.php | 6 +++--- .../CrudSpec/FindOneAndReplaceFunctionalTest.php | 16 ++++++++-------- .../CrudSpec/FindOneAndUpdateFunctionalTest.php | 16 ++++++++-------- .../CrudSpec/InsertManyFunctionalTest.php | 2 +- .../CrudSpec/InsertOneFunctionalTest.php | 2 +- .../CrudSpec/ReplaceOneFunctionalTest.php | 10 +++++----- .../CrudSpec/UpdateManyFunctionalTest.php | 8 ++++---- .../CrudSpec/UpdateOneFunctionalTest.php | 8 ++++---- tests/FunctionalTestCase.php | 6 ++++++ 14 files changed, 55 insertions(+), 49 deletions(-) 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..803c1d346 100644 --- a/tests/Collection/CrudSpec/AggregateFunctionalTest.php +++ b/tests/Collection/CrudSpec/AggregateFunctionalTest.php @@ -32,8 +32,8 @@ public function testAggregateWithMultipleStages() ); $expected = array( - array('_id' => 2, 'x' => 22), - array('_id' => 3, 'x' => 33), + (object) array('_id' => 2, 'x' => 22), + (object) array('_id' => 3, 'x' => 33), ); // Use iterator_to_array() here since aggregate() may return an ArrayIterator @@ -64,7 +64,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..8657b3880 100644 --- a/tests/Collection/CrudSpec/FindOneAndDeleteFunctionalTest.php +++ b/tests/Collection/CrudSpec/FindOneAndDeleteFunctionalTest.php @@ -32,7 +32,7 @@ public function testFindOneAndDeleteWhenManyDocumentsMatch() array('_id' => 3, 'x' => 33), ); - $this->assertSame($expected, $this->collection->find()->toArray()); + $this->assertSameDocuments($expected, $this->collection->find()); } public function testFindOneAndDeleteWhenOneDocumentMatches() @@ -51,7 +51,7 @@ public function testFindOneAndDeleteWhenOneDocumentMatches() 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..cdb89edcf 100644 --- a/tests/Collection/CrudSpec/FindOneAndReplaceFunctionalTest.php +++ b/tests/Collection/CrudSpec/FindOneAndReplaceFunctionalTest.php @@ -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() @@ -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() @@ -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() @@ -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() @@ -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..8aa1f334f 100644 --- a/tests/Collection/CrudSpec/FindOneAndUpdateFunctionalTest.php +++ b/tests/Collection/CrudSpec/FindOneAndUpdateFunctionalTest.php @@ -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() @@ -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() @@ -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() @@ -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() @@ -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..efec27c4a 100644 --- a/tests/FunctionalTestCase.php +++ b/tests/FunctionalTestCase.php @@ -38,6 +38,12 @@ protected function assertCommandSucceeded($document) $this->assertEquals(1, $document['ok']); } + protected function assertSameDocuments(array $expected, $cursor) + { + $cursor->setTypeMap(array('document' => 'array')); + $this->assertEquals($expected, iterator_to_array($cursor)); + } + protected function getServerVersion(ReadPreference $readPreference = null) { $cursor = $this->manager->executeCommand( From 771316b14b152e5a1bf5fc6fe10002149d04725f Mon Sep 17 00:00:00 2001 From: Derick Rethans Date: Thu, 18 Jun 2015 15:14:57 +0100 Subject: [PATCH 3/5] Because the typemap says 'document as array', we now need to change the return value with a cast to 'object' --- src/Operation/FindAndModify.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Operation/FindAndModify.php b/src/Operation/FindAndModify.php index d2ef79394..62569e069 100644 --- a/src/Operation/FindAndModify.php +++ b/src/Operation/FindAndModify.php @@ -140,11 +140,11 @@ public function execute(Server $server) return null; } - if ( ! is_object($result['value'])) { + if ( ! is_array($result['value'])) { throw new UnexpectedValueException('findAndModify command did not return a "value" document'); } - return $result['value']; + return (object) $result['value']; } /** From 82de6ec15345b66546feff499cdf9b8a5b8e29da Mon Sep 17 00:00:00 2001 From: Jeremy Mikola Date: Fri, 19 Jun 2015 16:31:19 -0400 Subject: [PATCH 4/5] Ensure operations return documents as objects by default Unless the operation returns a scalar or custom value (e.g. Count, CreateIndexes), we should explicitly convert documents to stdClass instances to return. This will ultimately be made configurable once PHPLIB-112 is implemented. A manual cast (of arrays to objects) was also necessary until PHPC-318 is implemented. --- src/Operation/Aggregate.php | 15 ++++++++------- src/Operation/CreateCollection.php | 9 ++++++--- src/Operation/CreateIndexes.php | 1 + src/Operation/Distinct.php | 13 ++++++++----- src/Operation/DropCollection.php | 9 ++++++--- src/Operation/DropDatabase.php | 9 ++++++--- src/Operation/DropIndexes.php | 8 ++++++-- src/Operation/FindAndModify.php | 19 +++++++++++-------- 8 files changed, 52 insertions(+), 31 deletions(-) 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/CreateCollection.php b/src/Operation/CreateCollection.php index b7b082f95..d62564e39 100644 --- a/src/Operation/CreateCollection.php +++ b/src/Operation/CreateCollection.php @@ -106,11 +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' => 'array')); + $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 489e26a9a..fa4c9bf42 100644 --- a/src/Operation/Distinct.php +++ b/src/Operation/Distinct.php @@ -62,18 +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' => 'array')); + $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 cd5af3215..2cd3f5330 100644 --- a/src/Operation/DropCollection.php +++ b/src/Operation/DropCollection.php @@ -41,11 +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' => 'array')); + $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 f453b6a0e..7c22fc08c 100644 --- a/src/Operation/DropDatabase.php +++ b/src/Operation/DropDatabase.php @@ -39,11 +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' => 'array')); + $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 62569e069..bf0a76bd7 100644 --- a/src/Operation/FindAndModify.php +++ b/src/Operation/FindAndModify.php @@ -118,14 +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' => 'array')); + $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; } @@ -134,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_array($result['value'])) { + if ( ! is_object($result->value)) { throw new UnexpectedValueException('findAndModify command did not return a "value" document'); } - return (object) $result['value']; + return $result->value; } /** From 0437bb8596f4623b0eac100415234002a638a414 Mon Sep 17 00:00:00 2001 From: Jeremy Mikola Date: Fri, 19 Jun 2015 16:36:43 -0400 Subject: [PATCH 5/5] Make expected document assertions more flexible Normalizing top-level documents as arrays will allow single and multi-document checks to be more flexible. This will not extend to embedded documents, where we'll still need to be explicit. Additionally, assertSameDocuments can now operate on any array or Traversable without needing to set its own type map. --- .../CrudSpec/AggregateFunctionalTest.php | 7 ++--- .../FindOneAndDeleteFunctionalTest.php | 4 +-- .../FindOneAndReplaceFunctionalTest.php | 10 +++---- .../FindOneAndUpdateFunctionalTest.php | 10 +++---- tests/FunctionalTestCase.php | 30 +++++++++++++++++-- 5 files changed, 42 insertions(+), 19 deletions(-) diff --git a/tests/Collection/CrudSpec/AggregateFunctionalTest.php b/tests/Collection/CrudSpec/AggregateFunctionalTest.php index 803c1d346..2e69ade4a 100644 --- a/tests/Collection/CrudSpec/AggregateFunctionalTest.php +++ b/tests/Collection/CrudSpec/AggregateFunctionalTest.php @@ -32,12 +32,11 @@ public function testAggregateWithMultipleStages() ); $expected = array( - (object) array('_id' => 2, 'x' => 22), - (object) array('_id' => 3, 'x' => 33), + array('_id' => 2, 'x' => 22), + 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() diff --git a/tests/Collection/CrudSpec/FindOneAndDeleteFunctionalTest.php b/tests/Collection/CrudSpec/FindOneAndDeleteFunctionalTest.php index 8657b3880..c9d72bdae 100644 --- a/tests/Collection/CrudSpec/FindOneAndDeleteFunctionalTest.php +++ b/tests/Collection/CrudSpec/FindOneAndDeleteFunctionalTest.php @@ -25,7 +25,7 @@ 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), @@ -44,7 +44,7 @@ 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), diff --git a/tests/Collection/CrudSpec/FindOneAndReplaceFunctionalTest.php b/tests/Collection/CrudSpec/FindOneAndReplaceFunctionalTest.php index cdb89edcf..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), @@ -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), @@ -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), @@ -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), @@ -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), diff --git a/tests/Collection/CrudSpec/FindOneAndUpdateFunctionalTest.php b/tests/Collection/CrudSpec/FindOneAndUpdateFunctionalTest.php index 8aa1f334f..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), @@ -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), @@ -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), @@ -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), @@ -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), diff --git a/tests/FunctionalTestCase.php b/tests/FunctionalTestCase.php index efec27c4a..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,10 +40,32 @@ protected function assertCommandSucceeded($document) $this->assertEquals(1, $document['ok']); } - protected function assertSameDocuments(array $expected, $cursor) + protected function assertSameDocument($expectedDocument, $actualDocument) { - $cursor->setTypeMap(array('document' => 'array')); - $this->assertEquals($expected, iterator_to_array($cursor)); + $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)