Commit 3e4d54b8 authored by Jeremy Mikola's avatar Jeremy Mikola

Merge pull request #21

parents 60931d82 0437bb85
...@@ -122,20 +122,21 @@ class Aggregate implements Executable ...@@ -122,20 +122,21 @@ class Aggregate implements Executable
return $cursor; return $cursor;
} }
$cursor->setTypeMap(array('document' => 'stdClass'));
$result = current($cursor->toArray()); $result = current($cursor->toArray());
if (empty($result['ok'])) { // TODO: Remove this once PHPC-318 is implemented
throw new RuntimeException(isset($result['errmsg']) ? $result['errmsg'] : 'Unknown error'); 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'); throw new UnexpectedValueException('aggregate command did not return a "result" array');
} }
return new ArrayIterator(array_map( return new ArrayIterator($result->result);
function (stdClass $document) { return (array) $document; },
$result['result']
));
} }
/** /**
......
...@@ -85,6 +85,7 @@ class Count implements Executable ...@@ -85,6 +85,7 @@ class Count implements Executable
public function execute(Server $server) public function execute(Server $server)
{ {
$cursor = $server->executeCommand($this->databaseName, $this->createCommand()); $cursor = $server->executeCommand($this->databaseName, $this->createCommand());
$cursor->setTypeMap(array('document' => 'array'));
$result = current($cursor->toArray()); $result = current($cursor->toArray());
if (empty($result['ok'])) { if (empty($result['ok'])) {
......
...@@ -106,10 +106,14 @@ class CreateCollection implements Executable ...@@ -106,10 +106,14 @@ class CreateCollection implements Executable
public function execute(Server $server) public function execute(Server $server)
{ {
$cursor = $server->executeCommand($this->databaseName, $this->createCommand()); $cursor = $server->executeCommand($this->databaseName, $this->createCommand());
$cursor->setTypeMap(array('document' => 'stdClass'));
$result = current($cursor->toArray()); $result = current($cursor->toArray());
if (empty($result['ok'])) { // TODO: Remove this once PHPC-318 is implemented
throw new RuntimeException(isset($result['errmsg']) ? $result['errmsg'] : 'Unknown error'); is_array($result) and $result = (object) $result;
if (empty($result->ok)) {
throw new RuntimeException(isset($result->errmsg) ? $result->errmsg : 'Unknown error');
} }
return $result; return $result;
......
...@@ -91,6 +91,7 @@ class CreateIndexes implements Executable ...@@ -91,6 +91,7 @@ class CreateIndexes implements Executable
)); ));
$cursor = $server->executeCommand($this->databaseName, $command); $cursor = $server->executeCommand($this->databaseName, $command);
$cursor->setTypeMap(array('document' => 'array'));
$result = current($cursor->toArray()); $result = current($cursor->toArray());
if (empty($result['ok'])) { if (empty($result['ok'])) {
......
...@@ -62,17 +62,21 @@ class Distinct implements Executable ...@@ -62,17 +62,21 @@ class Distinct implements Executable
public function execute(Server $server) public function execute(Server $server)
{ {
$cursor = $server->executeCommand($this->databaseName, $this->createCommand()); $cursor = $server->executeCommand($this->databaseName, $this->createCommand());
$cursor->setTypeMap(array('document' => 'stdClass'));
$result = current($cursor->toArray()); $result = current($cursor->toArray());
if (empty($result['ok'])) { // TODO: Remove this once PHPC-318 is implemented
throw new RuntimeException(isset($result['errmsg']) ? $result['errmsg'] : 'Unknown error'); 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'); throw new UnexpectedValueException('distinct command did not return a "values" array');
} }
return $result['values']; return $result->values;
} }
/** /**
......
...@@ -41,10 +41,14 @@ class DropCollection implements Executable ...@@ -41,10 +41,14 @@ class DropCollection implements Executable
public function execute(Server $server) public function execute(Server $server)
{ {
$cursor = $server->executeCommand($this->databaseName, new Command(array('drop' => $this->collectionName))); $cursor = $server->executeCommand($this->databaseName, new Command(array('drop' => $this->collectionName)));
$cursor->setTypeMap(array('document' => 'stdClass'));
$result = current($cursor->toArray()); $result = current($cursor->toArray());
if (empty($result['ok'])) { // TODO: Remove this once PHPC-318 is implemented
throw new RuntimeException(isset($result['errmsg']) ? $result['errmsg'] : 'Unknown error'); is_array($result) and $result = (object) $result;
if (empty($result->ok)) {
throw new RuntimeException(isset($result->errmsg) ? $result->errmsg : 'Unknown error');
} }
return $result; return $result;
......
...@@ -39,10 +39,14 @@ class DropDatabase implements Executable ...@@ -39,10 +39,14 @@ class DropDatabase implements Executable
public function execute(Server $server) public function execute(Server $server)
{ {
$cursor = $server->executeCommand($this->databaseName, new Command(array('dropDatabase' => 1))); $cursor = $server->executeCommand($this->databaseName, new Command(array('dropDatabase' => 1)));
$cursor->setTypeMap(array('document' => 'stdClass'));
$result = current($cursor->toArray()); $result = current($cursor->toArray());
if (empty($result['ok'])) { // TODO: Remove this once PHPC-318 is implemented
throw new RuntimeException(isset($result['errmsg']) ? $result['errmsg'] : 'Unknown error'); is_array($result) and $result = (object) $result;
if (empty($result->ok)) {
throw new RuntimeException(isset($result->errmsg) ? $result->errmsg : 'Unknown error');
} }
return $result; return $result;
......
...@@ -56,10 +56,14 @@ class DropIndexes implements Executable ...@@ -56,10 +56,14 @@ class DropIndexes implements Executable
); );
$cursor = $server->executeCommand($this->databaseName, new Command($cmd)); $cursor = $server->executeCommand($this->databaseName, new Command($cmd));
$cursor->setTypeMap(array('document' => 'stdClass'));
$result = current($cursor->toArray()); $result = current($cursor->toArray());
if (empty($result['ok'])) { // TODO: Remove this once PHPC-318 is implemented
throw new RuntimeException(isset($result['errmsg']) ? $result['errmsg'] : 'Unknown error'); is_array($result) and $result = (object) $result;
if (empty($result->ok)) {
throw new RuntimeException(isset($result->errmsg) ? $result->errmsg : 'Unknown error');
} }
return $result; return $result;
......
...@@ -118,13 +118,17 @@ class FindAndModify implements Executable ...@@ -118,13 +118,17 @@ class FindAndModify implements Executable
public function execute(Server $server) public function execute(Server $server)
{ {
$cursor = $server->executeCommand($this->databaseName, $this->createCommand()); $cursor = $server->executeCommand($this->databaseName, $this->createCommand());
$cursor->setTypeMap(array('document' => 'stdClass'));
$result = current($cursor->toArray()); $result = current($cursor->toArray());
if (empty($result['ok'])) { // TODO: Remove this once PHPC-318 is implemented
throw new RuntimeException(isset($result['errmsg']) ? $result['errmsg'] : 'Unknown error'); 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; return null;
} }
...@@ -133,17 +137,17 @@ class FindAndModify implements Executable ...@@ -133,17 +137,17 @@ class FindAndModify implements Executable
* requested. * requested.
*/ */
if ($this->options['upsert'] && ! $this->options['new'] && if ($this->options['upsert'] && ! $this->options['new'] &&
isset($result['lastErrorObject']->updatedExisting) && isset($result->lastErrorObject->updatedExisting) &&
! $result['lastErrorObject']->updatedExisting) { ! $result->lastErrorObject->updatedExisting) {
return null; return null;
} }
if ( ! is_object($result['value'])) { if ( ! is_object($result->value)) {
throw new UnexpectedValueException('findAndModify command did not return a "value" document'); throw new UnexpectedValueException('findAndModify command did not return a "value" document');
} }
return $result['value']; return $result->value;
} }
/** /**
......
...@@ -35,7 +35,7 @@ class BulkWriteFunctionalTest extends FunctionalTestCase ...@@ -35,7 +35,7 @@ class BulkWriteFunctionalTest extends FunctionalTestCase
array('_id' => $insertedIds[1], 'x' => 22), array('_id' => $insertedIds[1], 'x' => 22),
); );
$this->assertEquals($expected, $this->collection->find()->toArray()); $this->assertSameDocuments($expected, $this->collection->find());
} }
public function testUpdates() public function testUpdates()
...@@ -69,7 +69,7 @@ class BulkWriteFunctionalTest extends FunctionalTestCase ...@@ -69,7 +69,7 @@ class BulkWriteFunctionalTest extends FunctionalTestCase
array('_id' => $upsertedIds[3], 'x' => 67), array('_id' => $upsertedIds[3], 'x' => 67),
); );
$this->assertEquals($expected, $this->collection->find()->toArray()); $this->assertSameDocuments($expected, $this->collection->find());
} }
public function testDeletes() public function testDeletes()
...@@ -89,7 +89,7 @@ class BulkWriteFunctionalTest extends FunctionalTestCase ...@@ -89,7 +89,7 @@ class BulkWriteFunctionalTest extends FunctionalTestCase
array('_id' => 2, 'x' => 22), array('_id' => 2, 'x' => 22),
); );
$this->assertEquals($expected, $this->collection->find()->toArray()); $this->assertSameDocuments($expected, $this->collection->find());
} }
public function testMixedOrderedOperations() public function testMixedOrderedOperations()
...@@ -123,7 +123,7 @@ class BulkWriteFunctionalTest extends FunctionalTestCase ...@@ -123,7 +123,7 @@ class BulkWriteFunctionalTest extends FunctionalTestCase
array('_id' => 4, 'x' => 44), array('_id' => 4, 'x' => 44),
); );
$this->assertEquals($expected, $this->collection->find()->toArray()); $this->assertSameDocuments($expected, $this->collection->find());
} }
/** /**
......
...@@ -36,8 +36,7 @@ class AggregateFunctionalTest extends FunctionalTestCase ...@@ -36,8 +36,7 @@ class AggregateFunctionalTest extends FunctionalTestCase
array('_id' => 3, 'x' => 33), array('_id' => 3, 'x' => 33),
); );
// Use iterator_to_array() here since aggregate() may return an ArrayIterator $this->assertSameDocuments($expected, $cursor);
$this->assertEquals($expected, iterator_to_array($cursor));
} }
public function testAggregateWithOut() public function testAggregateWithOut()
...@@ -64,7 +63,7 @@ class AggregateFunctionalTest extends FunctionalTestCase ...@@ -64,7 +63,7 @@ class AggregateFunctionalTest extends FunctionalTestCase
array('_id' => 3, 'x' => 33), array('_id' => 3, 'x' => 33),
); );
$this->assertEquals($expected, $outputCollection->find()->toArray()); $this->assertSameDocuments($expected, $outputCollection->find());
// Manually clean up our output collection // Manually clean up our output collection
$this->dropCollectionIfItExists($outputCollection); $this->dropCollectionIfItExists($outputCollection);
......
...@@ -27,7 +27,7 @@ class DeleteManyFunctionalTest extends FunctionalTestCase ...@@ -27,7 +27,7 @@ class DeleteManyFunctionalTest extends FunctionalTestCase
array('_id' => 1, 'x' => 11), array('_id' => 1, 'x' => 11),
); );
$this->assertSame($expected, $this->collection->find()->toArray()); $this->assertSameDocuments($expected, $this->collection->find());
} }
public function testDeleteManyWhenNoDocumentsMatch() public function testDeleteManyWhenNoDocumentsMatch()
...@@ -43,6 +43,6 @@ class DeleteManyFunctionalTest extends FunctionalTestCase ...@@ -43,6 +43,6 @@ class DeleteManyFunctionalTest extends FunctionalTestCase
array('_id' => 3, 'x' => 33), array('_id' => 3, 'x' => 33),
); );
$this->assertSame($expected, $this->collection->find()->toArray()); $this->assertSameDocuments($expected, $this->collection->find());
} }
} }
...@@ -28,7 +28,7 @@ class DeleteOneFunctionalTest extends FunctionalTestCase ...@@ -28,7 +28,7 @@ class DeleteOneFunctionalTest extends FunctionalTestCase
array('_id' => 3, 'x' => 33), array('_id' => 3, 'x' => 33),
); );
$this->assertSame($expected, $this->collection->find()->toArray()); $this->assertSameDocuments($expected, $this->collection->find());
} }
public function testDeleteOneWhenOneDocumentMatches() public function testDeleteOneWhenOneDocumentMatches()
...@@ -43,7 +43,7 @@ class DeleteOneFunctionalTest extends FunctionalTestCase ...@@ -43,7 +43,7 @@ class DeleteOneFunctionalTest extends FunctionalTestCase
array('_id' => 3, 'x' => 33), array('_id' => 3, 'x' => 33),
); );
$this->assertSame($expected, $this->collection->find()->toArray()); $this->assertSameDocuments($expected, $this->collection->find());
} }
public function testDeleteOneWhenNoDocumentsMatch() public function testDeleteOneWhenNoDocumentsMatch()
...@@ -59,6 +59,6 @@ class DeleteOneFunctionalTest extends FunctionalTestCase ...@@ -59,6 +59,6 @@ class DeleteOneFunctionalTest extends FunctionalTestCase
array('_id' => 3, 'x' => 33), array('_id' => 3, 'x' => 33),
); );
$this->assertSame($expected, $this->collection->find()->toArray()); $this->assertSameDocuments($expected, $this->collection->find());
} }
} }
...@@ -24,7 +24,7 @@ class FindFunctionalTest extends FunctionalTestCase ...@@ -24,7 +24,7 @@ class FindFunctionalTest extends FunctionalTestCase
array('_id' => 1, 'x' => 11), array('_id' => 1, 'x' => 11),
); );
$this->assertSame($expected, $this->collection->find($filter)->toArray()); $this->assertSameDocuments($expected, $this->collection->find($filter));
} }
public function testFindWithFilterSortSkipAndLimit() public function testFindWithFilterSortSkipAndLimit()
...@@ -40,7 +40,7 @@ class FindFunctionalTest extends FunctionalTestCase ...@@ -40,7 +40,7 @@ class FindFunctionalTest extends FunctionalTestCase
array('_id' => 5, 'x' => 55), 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() public function testFindWithLimitSortAndBatchSize()
...@@ -59,6 +59,6 @@ class FindFunctionalTest extends FunctionalTestCase ...@@ -59,6 +59,6 @@ class FindFunctionalTest extends FunctionalTestCase
array('_id' => 4, 'x' => 44), array('_id' => 4, 'x' => 44),
); );
$this->assertSame($expected, $this->collection->find($filter, $options)->toArray()); $this->assertSameDocuments($expected, $this->collection->find($filter, $options));
} }
} }
...@@ -25,14 +25,14 @@ class FindOneAndDeleteFunctionalTest extends FunctionalTestCase ...@@ -25,14 +25,14 @@ class FindOneAndDeleteFunctionalTest extends FunctionalTestCase
); );
$document = $this->collection->findOneAndDelete($filter, $options); $document = $this->collection->findOneAndDelete($filter, $options);
$this->assertEquals((object) array('x' => 22), $document); $this->assertSameDocument(array('x' => 22), $document);
$expected = array( $expected = array(
array('_id' => 1, 'x' => 11), array('_id' => 1, 'x' => 11),
array('_id' => 3, 'x' => 33), array('_id' => 3, 'x' => 33),
); );
$this->assertSame($expected, $this->collection->find()->toArray()); $this->assertSameDocuments($expected, $this->collection->find());
} }
public function testFindOneAndDeleteWhenOneDocumentMatches() public function testFindOneAndDeleteWhenOneDocumentMatches()
...@@ -44,14 +44,14 @@ class FindOneAndDeleteFunctionalTest extends FunctionalTestCase ...@@ -44,14 +44,14 @@ class FindOneAndDeleteFunctionalTest extends FunctionalTestCase
); );
$document = $this->collection->findOneAndDelete($filter, $options); $document = $this->collection->findOneAndDelete($filter, $options);
$this->assertEquals((object) array('x' => 22), $document); $this->assertSameDocument(array('x' => 22), $document);
$expected = array( $expected = array(
array('_id' => 1, 'x' => 11), array('_id' => 1, 'x' => 11),
array('_id' => 3, 'x' => 33), array('_id' => 3, 'x' => 33),
); );
$this->assertSame($expected, $this->collection->find()->toArray()); $this->assertSameDocuments($expected, $this->collection->find());
} }
public function testFindOneAndDeleteWhenNoDocumentsMatch() public function testFindOneAndDeleteWhenNoDocumentsMatch()
...@@ -71,6 +71,6 @@ class FindOneAndDeleteFunctionalTest extends FunctionalTestCase ...@@ -71,6 +71,6 @@ class FindOneAndDeleteFunctionalTest extends FunctionalTestCase
array('_id' => 3, 'x' => 33), array('_id' => 3, 'x' => 33),
); );
$this->assertSame($expected, $this->collection->find()->toArray()); $this->assertSameDocuments($expected, $this->collection->find());
} }
} }
...@@ -29,7 +29,7 @@ class FindOneAndReplaceFunctionalTest extends FunctionalTestCase ...@@ -29,7 +29,7 @@ class FindOneAndReplaceFunctionalTest extends FunctionalTestCase
); );
$document = $this->collection->findOneAndReplace($filter, $replacement, $options); $document = $this->collection->findOneAndReplace($filter, $replacement, $options);
$this->assertEquals((object) array('x' => 22), $document); $this->assertSameDocument(array('x' => 22), $document);
$expected = array( $expected = array(
array('_id' => 1, 'x' => 11), array('_id' => 1, 'x' => 11),
...@@ -37,7 +37,7 @@ class FindOneAndReplaceFunctionalTest extends FunctionalTestCase ...@@ -37,7 +37,7 @@ class FindOneAndReplaceFunctionalTest extends FunctionalTestCase
array('_id' => 3, 'x' => 33), array('_id' => 3, 'x' => 33),
); );
$this->assertSame($expected, $this->collection->find()->toArray()); $this->assertSameDocuments($expected, $this->collection->find());
} }
public function testFindOneAndReplaceWhenManyDocumentsMatchReturningDocumentAfterModification() public function testFindOneAndReplaceWhenManyDocumentsMatchReturningDocumentAfterModification()
...@@ -51,7 +51,7 @@ class FindOneAndReplaceFunctionalTest extends FunctionalTestCase ...@@ -51,7 +51,7 @@ class FindOneAndReplaceFunctionalTest extends FunctionalTestCase
); );
$document = $this->collection->findOneAndReplace($filter, $replacement, $options); $document = $this->collection->findOneAndReplace($filter, $replacement, $options);
$this->assertEquals((object) array('x' => 32), $document); $this->assertSameDocument(array('x' => 32), $document);
$expected = array( $expected = array(
array('_id' => 1, 'x' => 11), array('_id' => 1, 'x' => 11),
...@@ -59,7 +59,7 @@ class FindOneAndReplaceFunctionalTest extends FunctionalTestCase ...@@ -59,7 +59,7 @@ class FindOneAndReplaceFunctionalTest extends FunctionalTestCase
array('_id' => 3, 'x' => 33), array('_id' => 3, 'x' => 33),
); );
$this->assertSame($expected, $this->collection->find()->toArray()); $this->assertSameDocuments($expected, $this->collection->find());
} }
public function testFindOneAndReplaceWhenOneDocumentMatchesReturningDocumentBeforeModification() public function testFindOneAndReplaceWhenOneDocumentMatchesReturningDocumentBeforeModification()
...@@ -72,7 +72,7 @@ class FindOneAndReplaceFunctionalTest extends FunctionalTestCase ...@@ -72,7 +72,7 @@ class FindOneAndReplaceFunctionalTest extends FunctionalTestCase
); );
$document = $this->collection->findOneAndReplace($filter, $replacement, $options); $document = $this->collection->findOneAndReplace($filter, $replacement, $options);
$this->assertEquals((object) array('x' => 22), $document); $this->assertSameDocument(array('x' => 22), $document);
$expected = array( $expected = array(
array('_id' => 1, 'x' => 11), array('_id' => 1, 'x' => 11),
...@@ -80,7 +80,7 @@ class FindOneAndReplaceFunctionalTest extends FunctionalTestCase ...@@ -80,7 +80,7 @@ class FindOneAndReplaceFunctionalTest extends FunctionalTestCase
array('_id' => 3, 'x' => 33), array('_id' => 3, 'x' => 33),
); );
$this->assertSame($expected, $this->collection->find()->toArray()); $this->assertSameDocuments($expected, $this->collection->find());
} }
public function testFindOneAndReplaceWhenOneDocumentMatchesReturningDocumentAfterModification() public function testFindOneAndReplaceWhenOneDocumentMatchesReturningDocumentAfterModification()
...@@ -94,7 +94,7 @@ class FindOneAndReplaceFunctionalTest extends FunctionalTestCase ...@@ -94,7 +94,7 @@ class FindOneAndReplaceFunctionalTest extends FunctionalTestCase
); );
$document = $this->collection->findOneAndReplace($filter, $replacement, $options); $document = $this->collection->findOneAndReplace($filter, $replacement, $options);
$this->assertEquals((object) array('x' => 32), $document); $this->assertSameDocument(array('x' => 32), $document);
$expected = array( $expected = array(
array('_id' => 1, 'x' => 11), array('_id' => 1, 'x' => 11),
...@@ -102,7 +102,7 @@ class FindOneAndReplaceFunctionalTest extends FunctionalTestCase ...@@ -102,7 +102,7 @@ class FindOneAndReplaceFunctionalTest extends FunctionalTestCase
array('_id' => 3, 'x' => 33), array('_id' => 3, 'x' => 33),
); );
$this->assertSame($expected, $this->collection->find()->toArray()); $this->assertSameDocuments($expected, $this->collection->find());
} }
public function testFindOneAndReplaceWhenNoDocumentsMatchReturningDocumentBeforeModification() public function testFindOneAndReplaceWhenNoDocumentsMatchReturningDocumentBeforeModification()
...@@ -123,7 +123,7 @@ class FindOneAndReplaceFunctionalTest extends FunctionalTestCase ...@@ -123,7 +123,7 @@ class FindOneAndReplaceFunctionalTest extends FunctionalTestCase
array('_id' => 3, 'x' => 33), array('_id' => 3, 'x' => 33),
); );
$this->assertSame($expected, $this->collection->find()->toArray()); $this->assertSameDocuments($expected, $this->collection->find());
} }
public function testFindOneAndReplaceWithUpsertWhenNoDocumentsMatchReturningDocumentBeforeModification() public function testFindOneAndReplaceWithUpsertWhenNoDocumentsMatchReturningDocumentBeforeModification()
...@@ -147,7 +147,7 @@ class FindOneAndReplaceFunctionalTest extends FunctionalTestCase ...@@ -147,7 +147,7 @@ class FindOneAndReplaceFunctionalTest extends FunctionalTestCase
array('_id' => 4, 'x' => 44), array('_id' => 4, 'x' => 44),
); );
$this->assertSame($expected, $this->collection->find()->toArray()); $this->assertSameDocuments($expected, $this->collection->find());
} }
public function testFindOneAndReplaceWhenNoDocumentsMatchReturningDocumentAfterModification() public function testFindOneAndReplaceWhenNoDocumentsMatchReturningDocumentAfterModification()
...@@ -169,7 +169,7 @@ class FindOneAndReplaceFunctionalTest extends FunctionalTestCase ...@@ -169,7 +169,7 @@ class FindOneAndReplaceFunctionalTest extends FunctionalTestCase
array('_id' => 3, 'x' => 33), array('_id' => 3, 'x' => 33),
); );
$this->assertSame($expected, $this->collection->find()->toArray()); $this->assertSameDocuments($expected, $this->collection->find());
} }
public function testFindOneAndReplaceWithUpsertWhenNoDocumentsMatchReturningDocumentAfterModification() public function testFindOneAndReplaceWithUpsertWhenNoDocumentsMatchReturningDocumentAfterModification()
...@@ -185,7 +185,7 @@ class FindOneAndReplaceFunctionalTest extends FunctionalTestCase ...@@ -185,7 +185,7 @@ class FindOneAndReplaceFunctionalTest extends FunctionalTestCase
); );
$document = $this->collection->findOneAndReplace($filter, $replacement, $options); $document = $this->collection->findOneAndReplace($filter, $replacement, $options);
$this->assertEquals((object) array('x' => 44), $document); $this->assertSameDocument(array('x' => 44), $document);
$expected = array( $expected = array(
array('_id' => 1, 'x' => 11), array('_id' => 1, 'x' => 11),
...@@ -194,6 +194,6 @@ class FindOneAndReplaceFunctionalTest extends FunctionalTestCase ...@@ -194,6 +194,6 @@ class FindOneAndReplaceFunctionalTest extends FunctionalTestCase
array('_id' => 4, 'x' => 44), array('_id' => 4, 'x' => 44),
); );
$this->assertSame($expected, $this->collection->find()->toArray()); $this->assertSameDocuments($expected, $this->collection->find());
} }
} }
...@@ -29,7 +29,7 @@ class FindOneAndUpdateFunctionalTest extends FunctionalTestCase ...@@ -29,7 +29,7 @@ class FindOneAndUpdateFunctionalTest extends FunctionalTestCase
); );
$document = $this->collection->findOneAndUpdate($filter, $update, $options); $document = $this->collection->findOneAndUpdate($filter, $update, $options);
$this->assertEquals((object) array('x' => 22), $document); $this->assertSameDocument(array('x' => 22), $document);
$expected = array( $expected = array(
array('_id' => 1, 'x' => 11), array('_id' => 1, 'x' => 11),
...@@ -37,7 +37,7 @@ class FindOneAndUpdateFunctionalTest extends FunctionalTestCase ...@@ -37,7 +37,7 @@ class FindOneAndUpdateFunctionalTest extends FunctionalTestCase
array('_id' => 3, 'x' => 33), array('_id' => 3, 'x' => 33),
); );
$this->assertSame($expected, $this->collection->find()->toArray()); $this->assertSameDocuments($expected, $this->collection->find());
} }
public function testFindOneAndUpdateWhenManyDocumentsMatchReturningDocumentAfterModification() public function testFindOneAndUpdateWhenManyDocumentsMatchReturningDocumentAfterModification()
...@@ -51,7 +51,7 @@ class FindOneAndUpdateFunctionalTest extends FunctionalTestCase ...@@ -51,7 +51,7 @@ class FindOneAndUpdateFunctionalTest extends FunctionalTestCase
); );
$document = $this->collection->findOneAndUpdate($filter, $update, $options); $document = $this->collection->findOneAndUpdate($filter, $update, $options);
$this->assertEquals((object) array('x' => 23), $document); $this->assertSameDocument(array('x' => 23), $document);
$expected = array( $expected = array(
array('_id' => 1, 'x' => 11), array('_id' => 1, 'x' => 11),
...@@ -59,7 +59,7 @@ class FindOneAndUpdateFunctionalTest extends FunctionalTestCase ...@@ -59,7 +59,7 @@ class FindOneAndUpdateFunctionalTest extends FunctionalTestCase
array('_id' => 3, 'x' => 33), array('_id' => 3, 'x' => 33),
); );
$this->assertSame($expected, $this->collection->find()->toArray()); $this->assertSameDocuments($expected, $this->collection->find());
} }
public function testFindOneAndUpdateWhenOneDocumentMatchesReturningDocumentBeforeModification() public function testFindOneAndUpdateWhenOneDocumentMatchesReturningDocumentBeforeModification()
...@@ -72,7 +72,7 @@ class FindOneAndUpdateFunctionalTest extends FunctionalTestCase ...@@ -72,7 +72,7 @@ class FindOneAndUpdateFunctionalTest extends FunctionalTestCase
); );
$document = $this->collection->findOneAndUpdate($filter, $update, $options); $document = $this->collection->findOneAndUpdate($filter, $update, $options);
$this->assertEquals((object) array('x' => 22), $document); $this->assertSameDocument(array('x' => 22), $document);
$expected = array( $expected = array(
array('_id' => 1, 'x' => 11), array('_id' => 1, 'x' => 11),
...@@ -80,7 +80,7 @@ class FindOneAndUpdateFunctionalTest extends FunctionalTestCase ...@@ -80,7 +80,7 @@ class FindOneAndUpdateFunctionalTest extends FunctionalTestCase
array('_id' => 3, 'x' => 33), array('_id' => 3, 'x' => 33),
); );
$this->assertSame($expected, $this->collection->find()->toArray()); $this->assertSameDocuments($expected, $this->collection->find());
} }
public function testFindOneAndUpdateWhenOneDocumentMatchesReturningDocumentAfterModification() public function testFindOneAndUpdateWhenOneDocumentMatchesReturningDocumentAfterModification()
...@@ -94,7 +94,7 @@ class FindOneAndUpdateFunctionalTest extends FunctionalTestCase ...@@ -94,7 +94,7 @@ class FindOneAndUpdateFunctionalTest extends FunctionalTestCase
); );
$document = $this->collection->findOneAndUpdate($filter, $update, $options); $document = $this->collection->findOneAndUpdate($filter, $update, $options);
$this->assertEquals((object) array('x' => 23), $document); $this->assertSameDocument(array('x' => 23), $document);
$expected = array( $expected = array(
array('_id' => 1, 'x' => 11), array('_id' => 1, 'x' => 11),
...@@ -102,7 +102,7 @@ class FindOneAndUpdateFunctionalTest extends FunctionalTestCase ...@@ -102,7 +102,7 @@ class FindOneAndUpdateFunctionalTest extends FunctionalTestCase
array('_id' => 3, 'x' => 33), array('_id' => 3, 'x' => 33),
); );
$this->assertSame($expected, $this->collection->find()->toArray()); $this->assertSameDocuments($expected, $this->collection->find());
} }
public function testFindOneAndUpdateWhenNoDocumentsMatchReturningDocumentBeforeModification() public function testFindOneAndUpdateWhenNoDocumentsMatchReturningDocumentBeforeModification()
...@@ -123,7 +123,7 @@ class FindOneAndUpdateFunctionalTest extends FunctionalTestCase ...@@ -123,7 +123,7 @@ class FindOneAndUpdateFunctionalTest extends FunctionalTestCase
array('_id' => 3, 'x' => 33), array('_id' => 3, 'x' => 33),
); );
$this->assertSame($expected, $this->collection->find()->toArray()); $this->assertSameDocuments($expected, $this->collection->find());
} }
public function testFindOneAndUpdateWithUpsertWhenNoDocumentsMatchReturningDocumentBeforeModification() public function testFindOneAndUpdateWithUpsertWhenNoDocumentsMatchReturningDocumentBeforeModification()
...@@ -146,7 +146,7 @@ class FindOneAndUpdateFunctionalTest extends FunctionalTestCase ...@@ -146,7 +146,7 @@ class FindOneAndUpdateFunctionalTest extends FunctionalTestCase
array('_id' => 4, 'x' => 1), array('_id' => 4, 'x' => 1),
); );
$this->assertSame($expected, $this->collection->find()->toArray()); $this->assertSameDocuments($expected, $this->collection->find());
} }
public function testFindOneAndUpdateWhenNoDocumentsMatchReturningDocumentAfterModification() public function testFindOneAndUpdateWhenNoDocumentsMatchReturningDocumentAfterModification()
...@@ -168,7 +168,7 @@ class FindOneAndUpdateFunctionalTest extends FunctionalTestCase ...@@ -168,7 +168,7 @@ class FindOneAndUpdateFunctionalTest extends FunctionalTestCase
array('_id' => 3, 'x' => 33), array('_id' => 3, 'x' => 33),
); );
$this->assertSame($expected, $this->collection->find()->toArray()); $this->assertSameDocuments($expected, $this->collection->find());
} }
public function testFindOneAndUpdateWithUpsertWhenNoDocumentsMatchReturningDocumentAfterModification() public function testFindOneAndUpdateWithUpsertWhenNoDocumentsMatchReturningDocumentAfterModification()
...@@ -183,7 +183,7 @@ class FindOneAndUpdateFunctionalTest extends FunctionalTestCase ...@@ -183,7 +183,7 @@ class FindOneAndUpdateFunctionalTest extends FunctionalTestCase
); );
$document = $this->collection->findOneAndUpdate($filter, $update, $options); $document = $this->collection->findOneAndUpdate($filter, $update, $options);
$this->assertEquals((object) array('x' => 1), $document); $this->assertSameDocument(array('x' => 1), $document);
$expected = array( $expected = array(
array('_id' => 1, 'x' => 11), array('_id' => 1, 'x' => 11),
...@@ -192,6 +192,6 @@ class FindOneAndUpdateFunctionalTest extends FunctionalTestCase ...@@ -192,6 +192,6 @@ class FindOneAndUpdateFunctionalTest extends FunctionalTestCase
array('_id' => 4, 'x' => 1), array('_id' => 4, 'x' => 1),
); );
$this->assertSame($expected, $this->collection->find()->toArray()); $this->assertSameDocuments($expected, $this->collection->find());
} }
} }
...@@ -33,6 +33,6 @@ class InsertManyFunctionalTest extends FunctionalTestCase ...@@ -33,6 +33,6 @@ class InsertManyFunctionalTest extends FunctionalTestCase
array('_id' => 3, 'x' => 33), array('_id' => 3, 'x' => 33),
); );
$this->assertSame($expected, $this->collection->find()->toArray()); $this->assertSameDocuments($expected, $this->collection->find());
} }
} }
...@@ -29,6 +29,6 @@ class InsertOneFunctionalTest extends FunctionalTestCase ...@@ -29,6 +29,6 @@ class InsertOneFunctionalTest extends FunctionalTestCase
array('_id' => 2, 'x' => 22), array('_id' => 2, 'x' => 22),
); );
$this->assertSame($expected, $this->collection->find()->toArray()); $this->assertSameDocuments($expected, $this->collection->find());
} }
} }
...@@ -35,7 +35,7 @@ class ReplaceOneFunctionalTest extends FunctionalTestCase ...@@ -35,7 +35,7 @@ class ReplaceOneFunctionalTest extends FunctionalTestCase
array('_id' => 3, 'x' => 33), array('_id' => 3, 'x' => 33),
); );
$this->assertSame($expected, $this->collection->find()->toArray()); $this->assertSameDocuments($expected, $this->collection->find());
} }
public function testReplaceOneWhenOneDocumentMatches() public function testReplaceOneWhenOneDocumentMatches()
...@@ -53,7 +53,7 @@ class ReplaceOneFunctionalTest extends FunctionalTestCase ...@@ -53,7 +53,7 @@ class ReplaceOneFunctionalTest extends FunctionalTestCase
array('_id' => 3, 'x' => 33), array('_id' => 3, 'x' => 33),
); );
$this->assertSame($expected, $this->collection->find()->toArray()); $this->assertSameDocuments($expected, $this->collection->find());
} }
public function testReplaceOneWhenNoDocumentsMatch() public function testReplaceOneWhenNoDocumentsMatch()
...@@ -71,7 +71,7 @@ class ReplaceOneFunctionalTest extends FunctionalTestCase ...@@ -71,7 +71,7 @@ class ReplaceOneFunctionalTest extends FunctionalTestCase
array('_id' => 3, 'x' => 33), array('_id' => 3, 'x' => 33),
); );
$this->assertSame($expected, $this->collection->find()->toArray()); $this->assertSameDocuments($expected, $this->collection->find());
} }
public function testReplaceOneWithUpsertWhenNoDocumentsMatchWithAnIdSpecified() public function testReplaceOneWithUpsertWhenNoDocumentsMatchWithAnIdSpecified()
...@@ -92,7 +92,7 @@ class ReplaceOneFunctionalTest extends FunctionalTestCase ...@@ -92,7 +92,7 @@ class ReplaceOneFunctionalTest extends FunctionalTestCase
array('_id' => 4, 'x' => 1), array('_id' => 4, 'x' => 1),
); );
$this->assertSame($expected, $this->collection->find()->toArray()); $this->assertSameDocuments($expected, $this->collection->find());
} }
public function testReplaceOneWithUpsertWhenNoDocumentsMatchWithoutAnIdSpecified() public function testReplaceOneWithUpsertWhenNoDocumentsMatchWithoutAnIdSpecified()
...@@ -114,6 +114,6 @@ class ReplaceOneFunctionalTest extends FunctionalTestCase ...@@ -114,6 +114,6 @@ class ReplaceOneFunctionalTest extends FunctionalTestCase
array('_id' => 4, 'x' => 1), array('_id' => 4, 'x' => 1),
); );
$this->assertSame($expected, $this->collection->find()->toArray()); $this->assertSameDocuments($expected, $this->collection->find());
} }
} }
...@@ -35,7 +35,7 @@ class UpdateManyFunctionalTest extends FunctionalTestCase ...@@ -35,7 +35,7 @@ class UpdateManyFunctionalTest extends FunctionalTestCase
array('_id' => 3, 'x' => 34), array('_id' => 3, 'x' => 34),
); );
$this->assertSame($expected, $this->collection->find()->toArray()); $this->assertSameDocuments($expected, $this->collection->find());
} }
public function testUpdateManyWhenOneDocumentMatches() public function testUpdateManyWhenOneDocumentMatches()
...@@ -53,7 +53,7 @@ class UpdateManyFunctionalTest extends FunctionalTestCase ...@@ -53,7 +53,7 @@ class UpdateManyFunctionalTest extends FunctionalTestCase
array('_id' => 3, 'x' => 33), array('_id' => 3, 'x' => 33),
); );
$this->assertSame($expected, $this->collection->find()->toArray()); $this->assertSameDocuments($expected, $this->collection->find());
} }
public function testUpdateManyWhenNoDocumentsMatch() public function testUpdateManyWhenNoDocumentsMatch()
...@@ -71,7 +71,7 @@ class UpdateManyFunctionalTest extends FunctionalTestCase ...@@ -71,7 +71,7 @@ class UpdateManyFunctionalTest extends FunctionalTestCase
array('_id' => 3, 'x' => 33), array('_id' => 3, 'x' => 33),
); );
$this->assertSame($expected, $this->collection->find()->toArray()); $this->assertSameDocuments($expected, $this->collection->find());
} }
public function testUpdateManyWithUpsertWhenNoDocumentsMatch() public function testUpdateManyWithUpsertWhenNoDocumentsMatch()
...@@ -92,6 +92,6 @@ class UpdateManyFunctionalTest extends FunctionalTestCase ...@@ -92,6 +92,6 @@ class UpdateManyFunctionalTest extends FunctionalTestCase
array('_id' => 4, 'x' => 1), array('_id' => 4, 'x' => 1),
); );
$this->assertSame($expected, $this->collection->find()->toArray()); $this->assertSameDocuments($expected, $this->collection->find());
} }
} }
...@@ -35,7 +35,7 @@ class UpdateOneFunctionalTest extends FunctionalTestCase ...@@ -35,7 +35,7 @@ class UpdateOneFunctionalTest extends FunctionalTestCase
array('_id' => 3, 'x' => 33), array('_id' => 3, 'x' => 33),
); );
$this->assertSame($expected, $this->collection->find()->toArray()); $this->assertSameDocuments($expected, $this->collection->find());
} }
public function testUpdateOneWhenOneDocumentMatches() public function testUpdateOneWhenOneDocumentMatches()
...@@ -53,7 +53,7 @@ class UpdateOneFunctionalTest extends FunctionalTestCase ...@@ -53,7 +53,7 @@ class UpdateOneFunctionalTest extends FunctionalTestCase
array('_id' => 3, 'x' => 33), array('_id' => 3, 'x' => 33),
); );
$this->assertSame($expected, $this->collection->find()->toArray()); $this->assertSameDocuments($expected, $this->collection->find());
} }
public function testUpdateOneWhenNoDocumentsMatch() public function testUpdateOneWhenNoDocumentsMatch()
...@@ -71,7 +71,7 @@ class UpdateOneFunctionalTest extends FunctionalTestCase ...@@ -71,7 +71,7 @@ class UpdateOneFunctionalTest extends FunctionalTestCase
array('_id' => 3, 'x' => 33), array('_id' => 3, 'x' => 33),
); );
$this->assertSame($expected, $this->collection->find()->toArray()); $this->assertSameDocuments($expected, $this->collection->find());
} }
public function testUpdateOneWithUpsertWhenNoDocumentsMatch() public function testUpdateOneWithUpsertWhenNoDocumentsMatch()
...@@ -92,6 +92,6 @@ class UpdateOneFunctionalTest extends FunctionalTestCase ...@@ -92,6 +92,6 @@ class UpdateOneFunctionalTest extends FunctionalTestCase
array('_id' => 4, 'x' => 1), array('_id' => 4, 'x' => 1),
); );
$this->assertSame($expected, $this->collection->find()->toArray()); $this->assertSameDocuments($expected, $this->collection->find());
} }
} }
...@@ -6,6 +6,8 @@ use MongoDB\Driver\Command; ...@@ -6,6 +6,8 @@ use MongoDB\Driver\Command;
use MongoDB\Driver\Cursor; use MongoDB\Driver\Cursor;
use MongoDB\Driver\Manager; use MongoDB\Driver\Manager;
use MongoDB\Driver\ReadPreference; use MongoDB\Driver\ReadPreference;
use stdClass;
use Traversable;
abstract class FunctionalTestCase extends TestCase abstract class FunctionalTestCase extends TestCase
{ {
...@@ -38,6 +40,34 @@ abstract class FunctionalTestCase extends TestCase ...@@ -38,6 +40,34 @@ abstract class FunctionalTestCase extends TestCase
$this->assertEquals(1, $document['ok']); $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) protected function getServerVersion(ReadPreference $readPreference = null)
{ {
$cursor = $this->manager->executeCommand( $cursor = $this->manager->executeCommand(
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment