Commit 0ffbae83 authored by Jeremy Mikola's avatar Jeremy Mikola

Handle new Cursor and toArray() API in extension

PHPC-224 consolidated the Result and Cursor classes into one. Additionally, PHPC-203 changed toArray() to return iterator_to_array($this) instead of only the first result document. For commands, this means we need to extract the first element in toArray()'s return value.
parent 92ee3c51
......@@ -3,9 +3,9 @@
namespace MongoDB;
use MongoDB\Driver\Command;
use MongoDB\Driver\Cursor;
use MongoDB\Driver\Manager;
use MongoDB\Driver\ReadPreference;
use MongoDB\Driver\Result;
use MongoDB\Driver\WriteConcern;
use ArrayIterator;
use stdClass;
......@@ -39,7 +39,7 @@ class Client
*
* @see http://docs.mongodb.org/manual/reference/command/dropDatabase/
* @param string $databaseName
* @return Result
* @return Cursor
*/
public function dropDatabase($databaseName)
{
......@@ -61,9 +61,8 @@ class Client
{
$command = new Command(array('listDatabases' => 1));
$result = $this->manager->executeCommand('admin', $command);
$result = iterator_to_array($result);
$result = current($result);
$cursor = $this->manager->executeCommand('admin', $command);
$result = current($cursor->toArray());
if ( ! isset($result['databases']) || ! is_array($result['databases'])) {
throw new UnexpectedValueException('listDatabases command did not return a "databases" array');
......
......@@ -3,10 +3,10 @@
namespace MongoDB;
use MongoDB\Driver\Command;
use MongoDB\Driver\Cursor;
use MongoDB\Driver\Manager;
use MongoDB\Driver\Query;
use MongoDB\Driver\ReadPreference;
use MongoDB\Driver\Result;
use MongoDB\Driver\BulkWrite;
use MongoDB\Driver\WriteConcern;
......@@ -86,14 +86,16 @@ class Collection
"pipeline" => $pipeline,
) + $options;
$result = $this->_runCommand($this->dbname, $cmd);
$doc = $result->toArray();
$cursor = $this->_runCommand($this->dbname, $cmd);
if (isset($cmd["cursor"]) && $cmd["cursor"]) {
return $result;
} else {
if ($doc["ok"]) {
return new \ArrayIterator($doc["result"]);
}
return $cursor;
}
$doc = current($cursor->toArray());
if ($doc["ok"]) {
return new \ArrayIterator($doc["result"]);
}
throw $this->_generateCommandException($doc);
......@@ -234,7 +236,7 @@ class Collection
"query" => $filter,
) + $options;
$doc = $this->_runCommand($this->dbname, $cmd)->toArray();
$doc = current($this->_runCommand($this->dbname, $cmd)->toArray());
if ($doc["ok"]) {
return $doc["n"];
}
......@@ -324,7 +326,7 @@ class Collection
"query" => $filter,
) + $options;
$doc = $this->_runCommand($this->dbname, $cmd)->toArray();
$doc = current($this->_runCommand($this->dbname, $cmd)->toArray());
if ($doc["ok"]) {
return $doc["values"];
}
......@@ -335,7 +337,7 @@ class Collection
* Drop this collection.
*
* @see http://docs.mongodb.org/manual/reference/command/drop/
* @return Result
* @return Cursor
*/
public function drop()
{
......@@ -351,7 +353,7 @@ class Collection
* @see http://docs.mongodb.org/manual/reference/command/dropIndexes/
* @see http://docs.mongodb.org/manual/reference/method/db.collection.dropIndex/
* @param string $indexName
* @return Result
* @return Cursor
* @throws InvalidArgumentException if "*" is specified
*/
public function dropIndex($indexName)
......@@ -364,7 +366,7 @@ class Collection
*
* @see http://docs.mongodb.org/manual/reference/command/dropIndexes/
* @see http://docs.mongodb.org/manual/reference/method/db.collection.dropIndexes/
* @return Result
* @return Cursor
*/
public function dropIndexes()
{
......@@ -379,7 +381,7 @@ class Collection
*
* @param array $filter The find query to execute
* @param array $options Additional options
* @return Result
* @return Cursor
*/
public function find(array $filter = array(), array $options = array())
{
......@@ -437,7 +439,7 @@ class Collection
"query" => $filter,
) + $options;
$doc = $this->_runCommand($this->dbname, $cmd)->toArray();
$doc = current($this->_runCommand($this->dbname, $cmd)->toArray());
if ($doc["ok"]) {
return $doc["value"];
}
......@@ -474,7 +476,7 @@ class Collection
"query" => $filter,
) + $options;
$doc = $this->_runCommand($this->dbname, $cmd)->toArray();
$doc = current($this->_runCommand($this->dbname, $cmd)->toArray());
if ($doc["ok"]) {
return $doc["value"];
}
......@@ -512,7 +514,7 @@ class Collection
"query" => $filter,
) + $options;
$doc = $this->_runCommand($this->dbname, $cmd)->toArray();
$doc = current($this->_runCommand($this->dbname, $cmd)->toArray());
if ($doc["ok"]) {
return $doc["value"];
}
......@@ -951,7 +953,7 @@ class Collection
*
* @see http://docs.mongodb.org/manual/reference/command/listIndexes/
* @see http://docs.mongodb.org/manual/reference/method/db.collection.getIndexes/
* @return Result
* @return Cursor
*/
public function listIndexes()
{
......
......@@ -4,10 +4,10 @@ namespace MongoDB;
use MongoDB\Collection;
use MongoDB\Driver\Command;
use MongoDB\Driver\Cursor;
use MongoDB\Driver\Manager;
use MongoDB\Driver\Query;
use MongoDB\Driver\ReadPreference;
use MongoDB\Driver\Result;
use MongoDB\Driver\WriteConcern;
use MongoDB\Model\CollectionInfoIterator;
use MongoDB\Model\CollectionInfoCommandIterator;
......@@ -47,7 +47,7 @@ class Database
* @see http://docs.mongodb.org/manual/reference/method/db.createCollection/
* @param string $collectionName
* @param array $options
* @return Result
* @return Cursor
*/
public function createCollection($collectionName, array $options = array())
{
......@@ -58,7 +58,7 @@ class Database
* Drop this database.
*
* @see http://docs.mongodb.org/manual/reference/command/dropDatabase/
* @return Result
* @return Cursor
*/
public function drop()
{
......@@ -73,7 +73,7 @@ class Database
*
* @see http://docs.mongodb.org/manual/reference/command/drop/
* @param string $collectionName
* @return Result
* @return Cursor
*/
public function dropCollection($collectionName)
{
......@@ -130,9 +130,9 @@ class Database
// TODO: Relax RP if connected to a secondary node in standalone mode
$readPreference = new ReadPreference(ReadPreference::RP_PRIMARY);
$result = $this->manager->executeCommand($this->databaseName, $command, $readPreference);
$cursor = $this->manager->executeCommand($this->databaseName, $command, $readPreference);
return new CollectionInfoCommandIterator($result);
return new CollectionInfoCommandIterator($cursor);
}
/**
......@@ -166,8 +166,8 @@ class Database
// TODO: Relax RP if connected to a secondary node in standalone mode
$readPreference = new ReadPreference(ReadPreference::RP_PRIMARY);
$result = $this->manager->executeQuery($namespace, $query, $readPreference);
$cursor = $this->manager->executeQuery($namespace, $query, $readPreference);
return new CollectionInfoLegacyIterator($result);
return new CollectionInfoLegacyIterator($cursor);
}
}
......@@ -49,7 +49,7 @@ class CollectionFunctionalTest extends FunctionalTestCase
$count = $this->collection->count($query);
$this->assertEquals(1, $count);
$cursor = $this->collection->find($query);
$this->assertInstanceOf('MongoDB\Driver\Result', $cursor);
$this->assertInstanceOf('MongoDB\Driver\Cursor', $cursor);
foreach($cursor as $n => $person) {
$this->assertInternalType("array", $person);
......
......@@ -4,7 +4,7 @@ namespace MongoDB\Tests;
use MongoDB\Driver\Command;
use MongoDB\Driver\Manager;
use MongoDB\Driver\Result;
use MongoDB\Driver\Cursor;
abstract class FunctionalTestCase extends TestCase
{
......@@ -19,16 +19,16 @@ abstract class FunctionalTestCase extends TestCase
{
list($databaseName, $collectionName) = explode('.', $namespace, 2);
$result = $this->manager->executeCommand($databaseName, new Command(array('count' => $collectionName)));
$cursor = $this->manager->executeCommand($databaseName, new Command(array('count' => $collectionName)));
$document = $result->toArray();
$document = current($cursor->toArray());
$this->assertArrayHasKey('n', $document);
$this->assertEquals($count, $document['n']);
}
public function assertCommandSucceeded(Result $result)
public function assertCommandSucceeded(Cursor $cursor)
{
$document = $result->toArray();
$document = current($cursor->toArray());
$this->assertArrayHasKey('ok', $document);
$this->assertEquals(1, $document['ok']);
}
......
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