Skip to content

PHPLIB-70: Database and collection management #6

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 21 commits into from
Apr 13, 2015
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
c035bae
Create test bootstrap for autoloading
jmikola Mar 17, 2015
24d34dc
Create base classes for unit and functional tests
jmikola Mar 17, 2015
b85b91f
PHPLIB-73: Inherit default write concern and read preferences
jmikola Mar 24, 2015
10ea91c
PHPLIB-65: Database drop methods
jmikola Mar 24, 2015
d1b7a48
Cast expected string arguments
jmikola Mar 24, 2015
7ada7c2
Dump server buildInfo before running tests on Travis CI
jmikola Mar 25, 2015
46d0767
Use 127.0.0.1 instead of localhost for default URI
jmikola Mar 25, 2015
e9a5e8a
Restructure CollectionTest and fixture generation functions
jmikola Mar 25, 2015
6c1d6ac
PHPLIB-72: Database enumeration method
jmikola Mar 26, 2015
28da2e1
PHPLIB-71: Collection drop methods
jmikola Mar 26, 2015
da4fdf1
Setup Database object in DatabaseFunctionalTest
jmikola Mar 27, 2015
92ee3c5
PHPLIB-45: Collection enumeration methods
jmikola Mar 27, 2015
0ffbae8
Handle new Cursor and toArray() API in extension
jmikola Apr 6, 2015
fde9d8e
PHPLIB-64: Collection creation method
jmikola Apr 7, 2015
9e62cab
Allow any extension version for development
jmikola Apr 7, 2015
a20fa27
PHPLIB-45: Construct CollectionInfoLegacyIterator from Traversable
jmikola Apr 10, 2015
a32ac4f
PHPLIB-45: List collections according to wire protocol version
jmikola Apr 10, 2015
ccf1eba
PHPLIB-45: Support stdClass listCollections() filter option
jmikola Apr 10, 2015
e574c2d
PHPLIB-45: Test listCollections with filter option
jmikola Apr 10, 2015
fd0b861
Install latest PECL extension when testing
jmikola Apr 10, 2015
192d950
Don't be quiet when compiling the extension
jmikola Apr 10, 2015
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
Prev Previous commit
Next Next commit
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.
  • Loading branch information
jmikola committed Apr 13, 2015
commit 0ffbae83eb730cdbabf28f4cdb4fa114fdc775ac
9 changes: 4 additions & 5 deletions src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -39,7 +39,7 @@ public function __construct($uri, array $options = array(), array $driverOptions
*
* @see http://docs.mongodb.org/manual/reference/command/dropDatabase/
* @param string $databaseName
* @return Result
* @return Cursor
*/
public function dropDatabase($databaseName)
{
Expand All @@ -61,9 +61,8 @@ public function listDatabases()
{
$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');
Expand Down
38 changes: 20 additions & 18 deletions src/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -86,14 +86,16 @@ public function aggregate(array $pipeline, array $options = array())
"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);
Expand Down Expand Up @@ -234,7 +236,7 @@ public function count(array $filter = array(), array $options = array())
"query" => $filter,
) + $options;

$doc = $this->_runCommand($this->dbname, $cmd)->toArray();
$doc = current($this->_runCommand($this->dbname, $cmd)->toArray());
if ($doc["ok"]) {
return $doc["n"];
}
Expand Down Expand Up @@ -324,7 +326,7 @@ public function distinct($fieldName, array $filter = array(), array $options = a
"query" => $filter,
) + $options;

$doc = $this->_runCommand($this->dbname, $cmd)->toArray();
$doc = current($this->_runCommand($this->dbname, $cmd)->toArray());
if ($doc["ok"]) {
return $doc["values"];
}
Expand All @@ -335,7 +337,7 @@ public function distinct($fieldName, array $filter = array(), array $options = a
* Drop this collection.
*
* @see http://docs.mongodb.org/manual/reference/command/drop/
* @return Result
* @return Cursor
*/
public function drop()
{
Expand All @@ -351,7 +353,7 @@ public function drop()
* @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)
Expand All @@ -364,7 +366,7 @@ public function dropIndex($indexName)
*
* @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()
{
Expand All @@ -379,7 +381,7 @@ public function dropIndexes()
*
* @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())
{
Expand Down Expand Up @@ -437,7 +439,7 @@ public function findOneAndDelete(array $filter, array $options = array())
"query" => $filter,
) + $options;

$doc = $this->_runCommand($this->dbname, $cmd)->toArray();
$doc = current($this->_runCommand($this->dbname, $cmd)->toArray());
if ($doc["ok"]) {
return $doc["value"];
}
Expand Down Expand Up @@ -474,7 +476,7 @@ public function findOneAndReplace(array $filter, array $replacement, array $opti
"query" => $filter,
) + $options;

$doc = $this->_runCommand($this->dbname, $cmd)->toArray();
$doc = current($this->_runCommand($this->dbname, $cmd)->toArray());
if ($doc["ok"]) {
return $doc["value"];
}
Expand Down Expand Up @@ -512,7 +514,7 @@ public function findOneAndUpdate(array $filter, array $update, array $options =
"query" => $filter,
) + $options;

$doc = $this->_runCommand($this->dbname, $cmd)->toArray();
$doc = current($this->_runCommand($this->dbname, $cmd)->toArray());
if ($doc["ok"]) {
return $doc["value"];
}
Expand Down Expand Up @@ -951,7 +953,7 @@ public function insertOne(array $document)
*
* @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()
{
Expand Down
16 changes: 8 additions & 8 deletions src/Database.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@

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;
Expand Down Expand Up @@ -47,7 +47,7 @@ public function __construct(Manager $manager, $databaseName, WriteConcern $write
* @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())
{
Expand All @@ -58,7 +58,7 @@ public function createCollection($collectionName, array $options = array())
* Drop this database.
*
* @see http://docs.mongodb.org/manual/reference/command/dropDatabase/
* @return Result
* @return Cursor
*/
public function drop()
{
Expand All @@ -73,7 +73,7 @@ public function drop()
*
* @see http://docs.mongodb.org/manual/reference/command/drop/
* @param string $collectionName
* @return Result
* @return Cursor
*/
public function dropCollection($collectionName)
{
Expand Down Expand Up @@ -130,9 +130,9 @@ private function listCollectionsCommand(array $options = array())
// 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);
}

/**
Expand Down Expand Up @@ -166,8 +166,8 @@ private function listCollectionsLegacy(array $options = array())
// 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);
}
}
2 changes: 1 addition & 1 deletion tests/CollectionFunctionalTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ function testInsertAndRetrieve()
$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);
Expand Down
10 changes: 5 additions & 5 deletions tests/FunctionalTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

use MongoDB\Driver\Command;
use MongoDB\Driver\Manager;
use MongoDB\Driver\Result;
use MongoDB\Driver\Cursor;

abstract class FunctionalTestCase extends TestCase
{
Expand All @@ -19,16 +19,16 @@ public function assertCollectionCount($namespace, $count)
{
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']);
}
Expand Down