Commit 57e05c13 authored by Jeremy Mikola's avatar Jeremy Mikola

Merge pull request #51

parents be188e3e 52b81e99
......@@ -34,6 +34,20 @@ class Client
$this->uri = (string) $uri;
}
/**
* Return internal properties for debugging purposes.
*
* @see http://php.net/manual/en/language.oop5.magic.php#language.oop5.magic.debuginfo
* @param array
*/
public function __debugInfo()
{
return [
'manager' => $this->manager,
'uri' => $this->uri,
];
}
/**
* Return the connection string (i.e. URI).
*
......@@ -75,40 +89,45 @@ class Client
/**
* Select a collection.
*
* If a write concern or read preference is not specified, the write concern
* or read preference of the Client will be applied, respectively.
* Supported options:
*
* @param string $databaseName Name of the database containing the collection
* @param string $collectionName Name of the collection to select
* @param WriteConcern $writeConcern Default write concern to apply
* @param ReadPreference $readPreference Default read preference to apply
* * readPreference (MongoDB\Driver\ReadPreference): The default read
* preference to use for collection operations. Defaults to the Client's
* read preference.
*
* * writeConcern (MongoDB\Driver\WriteConcern): The default write concern
* to use for collection operations. Defaults to the Client's write
* concern.
*
* @param string $databaseName Name of the database containing the collection
* @param string $collectionName Name of the collection to select
* @param array $options Collection constructor options
* @return Collection
*/
public function selectCollection($databaseName, $collectionName, WriteConcern $writeConcern = null, ReadPreference $readPreference = null)
public function selectCollection($databaseName, $collectionName, array $options = [])
{
$namespace = $databaseName . '.' . $collectionName;
$writeConcern = $writeConcern ?: $this->manager->getWriteConcern();
$readPreference = $readPreference ?: $this->manager->getReadPreference();
return new Collection($this->manager, $namespace, $writeConcern, $readPreference);
return new Collection($this->manager, $databaseName . '.' . $collectionName, $options);
}
/**
* Select a database.
*
* If a write concern or read preference is not specified, the write concern
* or read preference of the Client will be applied, respectively.
* Supported options:
*
* @param string $databaseName Name of the database to select
* @param WriteConcern $writeConcern Default write concern to apply
* @param ReadPreference $readPreference Default read preference to apply
* * readPreference (MongoDB\Driver\ReadPreference): The default read
* preference to use for database operations and selected collections.
* Defaults to the Client's read preference.
*
* * writeConcern (MongoDB\Driver\WriteConcern): The default write concern
* to use for database operations and selected collections. Defaults to
* the Client's write concern.
*
* @param string $databaseName Name of the database to select
* @param array $options Database constructor options
* @return Database
*/
public function selectDatabase($databaseName, WriteConcern $writeConcern = null, ReadPreference $readPreference = null)
public function selectDatabase($databaseName, array $options = [])
{
$writeConcern = $writeConcern ?: $this->manager->getWriteConcern();
$readPreference = $readPreference ?: $this->manager->getReadPreference();
return new Database($this->manager, $databaseName, $writeConcern, $readPreference);
return new Database($this->manager, $databaseName, $options);
}
}
......@@ -9,6 +9,7 @@ use MongoDB\Driver\ReadPreference;
use MongoDB\Driver\Server;
use MongoDB\Driver\WriteConcern;
use MongoDB\Exception\InvalidArgumentException;
use MongoDB\Exception\InvalidArgumentTypeException;
use MongoDB\Exception\UnexpectedTypeException;
use MongoDB\Model\IndexInfoIterator;
use MongoDB\Model\IndexInput;
......@@ -48,13 +49,22 @@ class Collection
* This class provides methods for collection-specific operations, such as
* CRUD (i.e. create, read, update, and delete) and index management.
*
* @param Manager $manager Manager instance from the driver
* @param string $namespace Collection namespace (e.g. "db.collection")
* @param WriteConcern $writeConcern Default write concern to apply
* @param ReadPreference $readPreference Default read preference to apply
* @throws InvalidArgumentException if $namespace is invalid
* Supported options:
*
* * readPreference (MongoDB\Driver\ReadPreference): The default read
* preference to use for collection operations. Defaults to the Manager's
* read preference.
*
* * writeConcern (MongoDB\Driver\WriteConcern): The default write concern
* to use for collection operations. Defaults to the Manager's write
* concern.
*
* @param Manager $manager Manager instance from the driver
* @param string $namespace Collection namespace (e.g. "db.collection")
* @param array $options Collection options
* @throws InvalidArgumentException
*/
public function __construct(Manager $manager, $namespace, WriteConcern $writeConcern = null, ReadPreference $readPreference = null)
public function __construct(Manager $manager, $namespace, array $options = [])
{
$parts = explode('.', $namespace, 2);
......@@ -65,13 +75,38 @@ class Collection
$this->databaseName = $parts[0];
$this->collectionName = $parts[1];
if (isset($options['readPreference']) && ! $options['readPreference'] instanceof ReadPreference) {
throw new InvalidArgumentTypeException('"readPreference" option', $options['readPreference'], 'MongoDB\Driver\ReadPreference');
}
if (isset($options['writeConcern']) && ! $options['writeConcern'] instanceof WriteConcern) {
throw new InvalidArgumentTypeException('"writeConcern" option', $options['writeConcern'], 'MongoDB\Driver\WriteConcern');
}
$this->manager = $manager;
$this->writeConcern = $writeConcern ?: $this->manager->getWriteConcern();
$this->readPreference = $readPreference ?: $this->manager->getReadPreference();
$this->readPreference = isset($options['readPreference']) ? $options['readPreference'] : $this->manager->getReadPreference();
$this->writeConcern = isset($options['writeConcern']) ? $options['writeConcern'] : $this->manager->getWriteConcern();
}
/**
* Return the collection namespace.
* Return internal properties for debugging purposes.
*
* @see http://php.net/manual/en/language.oop5.magic.php#language.oop5.magic.debuginfo
* @param array
*/
public function __debugInfo()
{
return [
'collectionName' => $this->collectionName,
'databaseName' => $this->databaseName,
'manager' => $this->manager,
'readPreference' => $this->readPreference,
'writeConcern' => $this->writeConcern,
];
}
/**
* Return the collection namespace (e.g. "db.collection").
*
* @param string
*/
......@@ -563,4 +598,33 @@ class Collection
return $operation->execute($server);
}
/**
* Get a clone of this collection with different options.
*
* Supported options:
*
* * readPreference (MongoDB\Driver\ReadPreference): The default read
* preference to use for collection operations. Defaults to this
* Collection's read preference.
*
* * writeConcern (MongoDB\Driver\WriteConcern): The default write concern
* to use for collection operations. Defaults to this Collection's write
* concern.
*
* @param array $options Collection constructor options
* @return Collection
*/
public function withOptions(array $options = [])
{
if ( ! isset($options['readPreference'])) {
$options['readPreference'] = $this->readPreference;
}
if ( ! isset($options['writeConcern'])) {
$options['writeConcern'] = $this->writeConcern;
}
return new Collection($this->manager, $this->databaseName . '.' . $this->collectionName, $options);
}
}
......@@ -11,6 +11,7 @@ use MongoDB\Driver\ReadPreference;
use MongoDB\Driver\Server;
use MongoDB\Driver\WriteConcern;
use MongoDB\Exception\InvalidArgumentException;
use MongoDB\Exception\InvalidArgumentTypeException;
use MongoDB\Model\CollectionInfoIterator;
use MongoDB\Operation\CreateCollection;
use MongoDB\Operation\DropCollection;
......@@ -30,22 +31,55 @@ class Database
* This class provides methods for database-specific operations and serves
* as a gateway for accessing collections.
*
* @param Manager $manager Manager instance from the driver
* @param string $databaseName Database name
* @param WriteConcern $writeConcern Default write concern to apply
* @param ReadPreference $readPreference Default read preference to apply
* @throws InvalidArgumentException if $databaseName is invalid
* Supported options:
*
* * readPreference (MongoDB\Driver\ReadPreference): The default read
* preference to use for database operations and selected collections.
* Defaults to the Manager's read preference.
*
* * writeConcern (MongoDB\Driver\WriteConcern): The default write concern
* to use for database operations and selected collections. Defaults to
* the Manager's write concern.
*
* @param Manager $manager Manager instance from the driver
* @param string $databaseName Database name
* @param array $options Database options
* @throws InvalidArgumentException
*/
public function __construct(Manager $manager, $databaseName, WriteConcern $writeConcern = null, ReadPreference $readPreference = null)
public function __construct(Manager $manager, $databaseName, array $options = [])
{
if (strlen($databaseName) < 1) {
throw new InvalidArgumentException('$databaseName is invalid: ' . $databaseName);
}
if (isset($options['readPreference']) && ! $options['readPreference'] instanceof ReadPreference) {
throw new InvalidArgumentTypeException('"readPreference" option', $options['readPreference'], 'MongoDB\Driver\ReadPreference');
}
if (isset($options['writeConcern']) && ! $options['writeConcern'] instanceof WriteConcern) {
throw new InvalidArgumentTypeException('"writeConcern" option', $options['writeConcern'], 'MongoDB\Driver\WriteConcern');
}
$this->manager = $manager;
$this->databaseName = (string) $databaseName;
$this->writeConcern = $writeConcern ?: $this->manager->getWriteConcern();
$this->readPreference = $readPreference ?: $this->manager->getReadPreference();
$this->readPreference = isset($options['readPreference']) ? $options['readPreference'] : $this->manager->getReadPreference();
$this->writeConcern = isset($options['writeConcern']) ? $options['writeConcern'] : $this->manager->getWriteConcern();
}
/**
* Return internal properties for debugging purposes.
*
* @see http://php.net/manual/en/language.oop5.magic.php#language.oop5.magic.debuginfo
* @param array
*/
public function __debugInfo()
{
return [
'databaseName' => $this->databaseName,
'manager' => $this->manager,
'readPreference' => $this->readPreference,
'writeConcern' => $this->writeConcern,
];
}
/**
......@@ -129,20 +163,59 @@ class Database
/**
* Select a collection within this database.
*
* If a write concern or read preference is not specified, the write concern
* or read preference of the Database will be applied, respectively.
* Supported options:
*
* * readPreference (MongoDB\Driver\ReadPreference): The default read
* preference to use for collection operations. Defaults to the
* Database's read preference.
*
* @param string $collectionName Name of the collection to select
* @param WriteConcern $writeConcern Default write concern to apply
* @param ReadPreference $readPreference Default read preference to apply
* * writeConcern (MongoDB\Driver\WriteConcern): The default write concern
* to use for collection operations. Defaults to the Database's write
* concern.
*
* @param string $collectionName Name of the collection to select
* @param array $options Collection constructor options
* @return Collection
*/
public function selectCollection($collectionName, WriteConcern $writeConcern = null, ReadPreference $readPreference = null)
public function selectCollection($collectionName, array $options = [])
{
$namespace = $this->databaseName . '.' . $collectionName;
$writeConcern = $writeConcern ?: $this->writeConcern;
$readPreference = $readPreference ?: $this->readPreference;
if ( ! isset($options['readPreference'])) {
$options['readPreference'] = $this->readPreference;
}
if ( ! isset($options['writeConcern'])) {
$options['writeConcern'] = $this->writeConcern;
}
return new Collection($this->manager, $this->databaseName . '.' . $collectionName, $options);
}
/**
* Get a clone of this database with different options.
*
* Supported options:
*
* * readPreference (MongoDB\Driver\ReadPreference): The default read
* preference to use for database operations and selected collections.
* Defaults to this Database's read preference.
*
* * writeConcern (MongoDB\Driver\WriteConcern): The default write concern
* to use for database operations and selected collections. Defaults to
* this Database's write concern.
*
* @param array $options Database constructor options
* @return Database
*/
public function withOptions(array $options = [])
{
if ( ! isset($options['readPreference'])) {
$options['readPreference'] = $this->readPreference;
}
if ( ! isset($options['writeConcern'])) {
$options['writeConcern'] = $this->writeConcern;
}
return new Collection($this->manager, $namespace, $writeConcern, $readPreference);
return new Database($this->manager, $this->databaseName, $options);
}
}
......@@ -3,6 +3,8 @@
namespace MongoDB\Tests;
use MongoDB\Client;
use MongoDB\Driver\ReadPreference;
use MongoDB\Driver\WriteConcern;
/**
* Unit tests for the Client class.
......@@ -22,4 +24,72 @@ class ClientTest extends TestCase
$this->assertSame($this->getUri(), (string) $client);
}
public function testSelectCollectionInheritsReadPreferenceAndWriteConcern()
{
$clientOptions = [
'readPreference' => 'secondaryPreferred',
'w' => WriteConcern::MAJORITY,
];
$client = new Client($this->getUri(), $clientOptions);
$collection = $client->selectCollection($this->getDatabaseName(), $this->getCollectionName());
$debug = $collection->__debugInfo();
$this->assertInstanceOf('MongoDB\Driver\ReadPreference', $debug['readPreference']);
$this->assertSame(ReadPreference::RP_SECONDARY_PREFERRED, $debug['readPreference']->getMode());
$this->assertInstanceOf('MongoDB\Driver\WriteConcern', $debug['writeConcern']);
$this->assertSame(WriteConcern::MAJORITY, $debug['writeConcern']->getW());
}
public function testSelectCollectionPassesReadPreferenceAndWriteConcern()
{
$collectionOptions = [
'readPreference' => new ReadPreference(ReadPreference::RP_SECONDARY_PREFERRED),
'writeConcern' => new WriteConcern(WriteConcern::MAJORITY),
];
$client = new Client($this->getUri());
$collection = $client->selectCollection($this->getDatabaseName(), $this->getCollectionName(), $collectionOptions);
$debug = $collection->__debugInfo();
$this->assertInstanceOf('MongoDB\Driver\ReadPreference', $debug['readPreference']);
$this->assertSame(ReadPreference::RP_SECONDARY_PREFERRED, $debug['readPreference']->getMode());
$this->assertInstanceOf('MongoDB\Driver\WriteConcern', $debug['writeConcern']);
$this->assertSame(WriteConcern::MAJORITY, $debug['writeConcern']->getW());
}
public function testSelectDatabaseInheritsReadPreferenceAndWriteConcern()
{
$clientOptions = [
'readPreference' => 'secondaryPreferred',
'w' => WriteConcern::MAJORITY,
];
$client = new Client($this->getUri(), $clientOptions);
$database = $client->selectDatabase($this->getDatabaseName());
$debug = $database->__debugInfo();
$this->assertInstanceOf('MongoDB\Driver\ReadPreference', $debug['readPreference']);
$this->assertSame(ReadPreference::RP_SECONDARY_PREFERRED, $debug['readPreference']->getMode());
$this->assertInstanceOf('MongoDB\Driver\WriteConcern', $debug['writeConcern']);
$this->assertSame(WriteConcern::MAJORITY, $debug['writeConcern']->getW());
}
public function testSelectDatabasePassesReadPreferenceAndWriteConcern()
{
$databaseOptions = [
'readPreference' => new ReadPreference(ReadPreference::RP_SECONDARY_PREFERRED),
'writeConcern' => new WriteConcern(WriteConcern::MAJORITY),
];
$client = new Client($this->getUri());
$database = $client->selectDatabase($this->getDatabaseName(), $databaseOptions);
$debug = $database->__debugInfo();
$this->assertInstanceOf('MongoDB\Driver\ReadPreference', $debug['readPreference']);
$this->assertSame(ReadPreference::RP_SECONDARY_PREFERRED, $debug['readPreference']->getMode());
$this->assertInstanceOf('MongoDB\Driver\WriteConcern', $debug['writeConcern']);
$this->assertSame(WriteConcern::MAJORITY, $debug['writeConcern']->getW());
}
}
......@@ -4,6 +4,8 @@ namespace MongoDB\Tests\Collection;
use MongoDB\Collection;
use MongoDB\Driver\BulkWrite;
use MongoDB\Driver\ReadPreference;
use MongoDB\Driver\WriteConcern;
/**
* Functional tests for the Collection class.
......@@ -31,6 +33,30 @@ class CollectionFunctionalTest extends FunctionalTestCase
];
}
/**
* @expectedException MongoDB\Exception\InvalidArgumentTypeException
* @dataProvider provideInvalidConstructorOptions
*/
public function testConstructorOptionTypeChecks(array $options)
{
new Collection($this->manager, $this->getNamespace(), $options);
}
public function provideInvalidConstructorOptions()
{
$options = [];
foreach ($this->getInvalidReadPreferenceValues() as $value) {
$options[][] = ['readPreference' => $value];
}
foreach ($this->getInvalidWriteConcernValues() as $value) {
$options[][] = ['writeConcern' => $value];
}
return $options;
}
public function testToString()
{
$this->assertEquals($this->getNamespace(), (string) $this->collection);
......@@ -85,6 +111,39 @@ class CollectionFunctionalTest extends FunctionalTestCase
$this->assertEquals($expected, $this->collection->findOne($filter, $options));
}
public function testWithOptionsInheritsReadPreferenceAndWriteConcern()
{
$collectionOptions = [
'readPreference' => new ReadPreference(ReadPreference::RP_SECONDARY_PREFERRED),
'writeConcern' => new WriteConcern(WriteConcern::MAJORITY),
];
$collection = new Collection($this->manager, $this->getNamespace(), $collectionOptions);
$clone = $collection->withOptions();
$debug = $clone->__debugInfo();
$this->assertInstanceOf('MongoDB\Driver\ReadPreference', $debug['readPreference']);
$this->assertSame(ReadPreference::RP_SECONDARY_PREFERRED, $debug['readPreference']->getMode());
$this->assertInstanceOf('MongoDB\Driver\WriteConcern', $debug['writeConcern']);
$this->assertSame(WriteConcern::MAJORITY, $debug['writeConcern']->getW());
}
public function testWithOptionsPassesReadPreferenceAndWriteConcern()
{
$collectionOptions = [
'readPreference' => new ReadPreference(ReadPreference::RP_SECONDARY_PREFERRED),
'writeConcern' => new WriteConcern(WriteConcern::MAJORITY),
];
$clone = $this->collection->withOptions($collectionOptions);
$debug = $clone->__debugInfo();
$this->assertInstanceOf('MongoDB\Driver\ReadPreference', $debug['readPreference']);
$this->assertSame(ReadPreference::RP_SECONDARY_PREFERRED, $debug['readPreference']->getMode());
$this->assertInstanceOf('MongoDB\Driver\WriteConcern', $debug['writeConcern']);
$this->assertSame(WriteConcern::MAJORITY, $debug['writeConcern']->getW());
}
/**
* Create data fixtures.
*
......
......@@ -4,6 +4,8 @@ namespace MongoDB\Tests\Database;
use MongoDB\Database;
use MongoDB\Driver\BulkWrite;
use MongoDB\Driver\ReadPreference;
use MongoDB\Driver\WriteConcern;
/**
* Functional tests for the Database class.
......@@ -28,6 +30,30 @@ class DatabaseFunctionalTest extends FunctionalTestCase
];
}
/**
* @expectedException MongoDB\Exception\InvalidArgumentTypeException
* @dataProvider provideInvalidConstructorOptions
*/
public function testConstructorOptionTypeChecks(array $options)
{
new Database($this->manager, $this->getDatabaseName(), $options);
}
public function provideInvalidConstructorOptions()
{
$options = [];
foreach ($this->getInvalidReadPreferenceValues() as $value) {
$options[][] = ['readPreference' => $value];
}
foreach ($this->getInvalidWriteConcernValues() as $value) {
$options[][] = ['writeConcern' => $value];
}
return $options;
}
public function testToString()
{
$this->assertEquals($this->getDatabaseName(), (string) $this->database);
......@@ -50,4 +76,70 @@ class DatabaseFunctionalTest extends FunctionalTestCase
$this->assertCommandSucceeded($commandResult);
$this->assertCollectionCount($this->getNamespace(), 0);
}
public function testSelectCollectionInheritsReadPreferenceAndWriteConcern()
{
$databaseOptions = [
'readPreference' => new ReadPreference(ReadPreference::RP_SECONDARY_PREFERRED),
'writeConcern' => new WriteConcern(WriteConcern::MAJORITY),
];
$database = new Database($this->manager, $this->getDatabaseName(), $databaseOptions);
$collection = $database->selectCollection($this->getCollectionName());
$debug = $collection->__debugInfo();
$this->assertInstanceOf('MongoDB\Driver\ReadPreference', $debug['readPreference']);
$this->assertSame(ReadPreference::RP_SECONDARY_PREFERRED, $debug['readPreference']->getMode());
$this->assertInstanceOf('MongoDB\Driver\WriteConcern', $debug['writeConcern']);
$this->assertSame(WriteConcern::MAJORITY, $debug['writeConcern']->getW());
}
public function testSelectCollectionPassesReadPreferenceAndWriteConcern()
{
$collectionOptions = [
'readPreference' => new ReadPreference(ReadPreference::RP_SECONDARY_PREFERRED),
'writeConcern' => new WriteConcern(WriteConcern::MAJORITY),
];
$collection = $this->database->selectCollection($this->getCollectionName(), $collectionOptions);
$debug = $collection->__debugInfo();
$this->assertInstanceOf('MongoDB\Driver\ReadPreference', $debug['readPreference']);
$this->assertSame(ReadPreference::RP_SECONDARY_PREFERRED, $debug['readPreference']->getMode());
$this->assertInstanceOf('MongoDB\Driver\WriteConcern', $debug['writeConcern']);
$this->assertSame(WriteConcern::MAJORITY, $debug['writeConcern']->getW());
}
public function testWithOptionsInheritsReadPreferenceAndWriteConcern()
{
$databaseOptions = [
'readPreference' => new ReadPreference(ReadPreference::RP_SECONDARY_PREFERRED),
'writeConcern' => new WriteConcern(WriteConcern::MAJORITY),
];
$database = new Database($this->manager, $this->getDatabaseName(), $databaseOptions);
$clone = $database->withOptions();
$debug = $clone->__debugInfo();
$this->assertInstanceOf('MongoDB\Driver\ReadPreference', $debug['readPreference']);
$this->assertSame(ReadPreference::RP_SECONDARY_PREFERRED, $debug['readPreference']->getMode());
$this->assertInstanceOf('MongoDB\Driver\WriteConcern', $debug['writeConcern']);
$this->assertSame(WriteConcern::MAJORITY, $debug['writeConcern']->getW());
}
public function testWithOptionsPassesReadPreferenceAndWriteConcern()
{
$databaseOptions = [
'readPreference' => new ReadPreference(ReadPreference::RP_SECONDARY_PREFERRED),
'writeConcern' => new WriteConcern(WriteConcern::MAJORITY),
];
$clone = $this->database->withOptions($databaseOptions);
$debug = $clone->__debugInfo();
$this->assertInstanceOf('MongoDB\Driver\ReadPreference', $debug['readPreference']);
$this->assertSame(ReadPreference::RP_SECONDARY_PREFERRED, $debug['readPreference']->getMode());
$this->assertInstanceOf('MongoDB\Driver\WriteConcern', $debug['writeConcern']);
$this->assertSame(WriteConcern::MAJORITY, $debug['writeConcern']->getW());
}
}
......@@ -45,16 +45,6 @@ abstract class TestCase extends BaseTestCase
return [123, 3.14, true, [], new stdClass];
}
protected function getInvalidReadPreferenceValues()
{
return [123, 3.14, 'foo', true, [], new stdClass];
}
protected function getInvalidWriteConcernValues()
{
return [123, 3.14, 'foo', true, [], new stdClass];
}
protected function wrapValuesForDataProvider(array $values)
{
return array_map(function($value) { return [$value]; }, $values);
......
......@@ -3,6 +3,7 @@
namespace MongoDB\Tests;
use ReflectionClass;
use stdClass;
abstract class TestCase extends \PHPUnit_Framework_TestCase
{
......@@ -28,6 +29,16 @@ abstract class TestCase extends \PHPUnit_Framework_TestCase
return getenv('MONGODB_DATABASE') ?: 'phplib_test';
}
protected function getInvalidReadPreferenceValues()
{
return [123, 3.14, 'foo', true, [], new stdClass];
}
protected function getInvalidWriteConcernValues()
{
return [123, 3.14, 'foo', true, [], new stdClass];
}
/**
* Return the test namespace.
*
......
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