Skip to content

Compatibility fixes for PHPC and HHVM #21

New issue

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

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

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Jun 19, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 8 additions & 7 deletions src/Operation/Aggregate.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

/**
Expand Down
1 change: 1 addition & 0 deletions src/Operation/Count.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'])) {
Expand Down
8 changes: 6 additions & 2 deletions src/Operation/CreateCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
1 change: 1 addition & 0 deletions src/Operation/CreateIndexes.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'])) {
Expand Down
12 changes: 8 additions & 4 deletions src/Operation/Distinct.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

/**
Expand Down
8 changes: 6 additions & 2 deletions src/Operation/DropCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
8 changes: 6 additions & 2 deletions src/Operation/DropDatabase.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
8 changes: 6 additions & 2 deletions src/Operation/DropIndexes.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
18 changes: 11 additions & 7 deletions src/Operation/FindAndModify.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand All @@ -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;
}

/**
Expand Down
8 changes: 4 additions & 4 deletions tests/Collection/BulkWriteFunctionalTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good idea. I thought about creating such an assertion.

}

public function testUpdates()
Expand Down Expand Up @@ -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()
Expand All @@ -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()
Expand Down Expand Up @@ -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());
}

/**
Expand Down
5 changes: 2 additions & 3 deletions tests/Collection/CrudSpec/AggregateFunctionalTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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);
Expand Down
4 changes: 2 additions & 2 deletions tests/Collection/CrudSpec/DeleteManyFunctionalTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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());
}
}
6 changes: 3 additions & 3 deletions tests/Collection/CrudSpec/DeleteOneFunctionalTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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()
Expand All @@ -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());
}
}
6 changes: 3 additions & 3 deletions tests/Collection/CrudSpec/FindFunctionalTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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()
Expand All @@ -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));
}
}
10 changes: 5 additions & 5 deletions tests/Collection/CrudSpec/FindOneAndDeleteFunctionalTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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()
Expand All @@ -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());
}
}
Loading