Commit 99635c0b authored by Jeremy Mikola's avatar Jeremy Mikola

Refactor option handling for Client, Database, and Collection

Parsing the "readconcernlevel" URI option depends on PHPC-523, so we'll skip those tests for now.
parent 2c6a7768
......@@ -89,16 +89,7 @@ class Client
/**
* Select a collection.
*
* Supported options:
*
* * 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.
*
* @see Collection::__construct() for supported options
* @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
......
......@@ -655,36 +655,17 @@ class Collection
/**
* Get a clone of this collection with different options.
*
* Supported options:
*
* * readConcern (MongoDB\Driver\ReadConcern): The default read concern to
* use for collection operations. Defaults to this Collection's read
* concern.
*
* * 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.
*
* @see Collection::__construct() for supported options
* @param array $options Collection constructor options
* @return Collection
*/
public function withOptions(array $options = [])
{
if ( ! isset($options['readConcern'])) {
$options['readConcern'] = $this->readConcern;
}
if ( ! isset($options['readPreference'])) {
$options['readPreference'] = $this->readPreference;
}
if ( ! isset($options['writeConcern'])) {
$options['writeConcern'] = $this->writeConcern;
}
$options += [
'readConcern' => $this->readConcern,
'readPreference' => $this->readPreference,
'writeConcern' => $this->writeConcern,
];
return new Collection($this->manager, $this->databaseName . '.' . $this->collectionName, $options);
}
......
......@@ -201,37 +201,18 @@ class Database
/**
* Select a collection within this database.
*
* Supported options:
*
* * readConcern (MongoDB\Driver\ReadConcern): The default read concern to
* use for collection operations. Defaults to the Database's read
* concern.
*
* * readPreference (MongoDB\Driver\ReadPreference): The default read
* preference to use for collection operations. Defaults to the
* Database's read preference.
*
* * writeConcern (MongoDB\Driver\WriteConcern): The default write concern
* to use for collection operations. Defaults to the Database's write
* concern.
*
* @see Collection::__construct() for supported options
* @param string $collectionName Name of the collection to select
* @param array $options Collection constructor options
* @return Collection
*/
public function selectCollection($collectionName, array $options = [])
{
if ( ! isset($options['readConcern'])) {
$options['readConcern'] = $this->readConcern;
}
if ( ! isset($options['readPreference'])) {
$options['readPreference'] = $this->readPreference;
}
if ( ! isset($options['writeConcern'])) {
$options['writeConcern'] = $this->writeConcern;
}
$options += [
'readConcern' => $this->readConcern,
'readPreference' => $this->readPreference,
'writeConcern' => $this->writeConcern,
];
return new Collection($this->manager, $this->databaseName . '.' . $collectionName, $options);
}
......@@ -239,36 +220,17 @@ class Database
/**
* Get a clone of this database with different options.
*
* Supported options:
*
* * readConcern (MongoDB\Driver\ReadConcern): The default read concern to
* use for database operations and selected collections. Defaults to this
* Database's read concern.
*
* * 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.
*
* @see Database::__construct() for supported options
* @param array $options Database constructor options
* @return Database
*/
public function withOptions(array $options = [])
{
if ( ! isset($options['readConcern'])) {
$options['readConcern'] = $this->readConcern;
}
if ( ! isset($options['readPreference'])) {
$options['readPreference'] = $this->readPreference;
}
if ( ! isset($options['writeConcern'])) {
$options['writeConcern'] = $this->writeConcern;
}
$options += [
'readConcern' => $this->readConcern,
'readPreference' => $this->readPreference,
'writeConcern' => $this->writeConcern,
];
return new Database($this->manager, $this->databaseName, $options);
}
......
......@@ -3,6 +3,7 @@
namespace MongoDB\Tests;
use MongoDB\Client;
use MongoDB\Driver\ReadConcern;
use MongoDB\Driver\ReadPreference;
use MongoDB\Driver\WriteConcern;
......@@ -25,26 +26,32 @@ class ClientTest extends TestCase
$this->assertSame($this->getUri(), (string) $client);
}
public function testSelectCollectionInheritsReadPreferenceAndWriteConcern()
public function testSelectCollectionInheritsOptions()
{
$clientOptions = [
$this->markTestSkipped('Depends on https://jira.mongodb.org/browse/PHPC-523');
$uriOptions = [
'readConcernLevel' => ReadConcern::LOCAL,
'readPreference' => 'secondaryPreferred',
'w' => WriteConcern::MAJORITY,
];
$client = new Client($this->getUri(), $clientOptions);
$client = new Client($this->getUri(), $uriOptions);
$collection = $client->selectCollection($this->getDatabaseName(), $this->getCollectionName());
$debug = $collection->__debugInfo();
$this->assertInstanceOf('MongoDB\Driver\ReadConcern', $debug['readConcern']);
$this->assertSame(ReadConcern::LOCAL, $debug['readConcern']->getLevel());
$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()
public function testSelectCollectionPassesOptions()
{
$collectionOptions = [
'readConcern' => new ReadConcern(ReadConcern::LOCAL),
'readPreference' => new ReadPreference(ReadPreference::RP_SECONDARY_PREFERRED),
'writeConcern' => new WriteConcern(WriteConcern::MAJORITY),
];
......@@ -53,32 +60,40 @@ class ClientTest extends TestCase
$collection = $client->selectCollection($this->getDatabaseName(), $this->getCollectionName(), $collectionOptions);
$debug = $collection->__debugInfo();
$this->assertInstanceOf('MongoDB\Driver\ReadConcern', $debug['readConcern']);
$this->assertSame(ReadConcern::LOCAL, $debug['readConcern']->getLevel());
$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()
public function testSelectDatabaseInheritsOptions()
{
$clientOptions = [
$this->markTestSkipped('Depends on https://jira.mongodb.org/browse/PHPC-523');
$uriOptions = [
'readConcernLevel' => ReadConcern::LOCAL,
'readPreference' => 'secondaryPreferred',
'w' => WriteConcern::MAJORITY,
];
$client = new Client($this->getUri(), $clientOptions);
$client = new Client($this->getUri(), $uriOptions);
$database = $client->selectDatabase($this->getDatabaseName());
$debug = $database->__debugInfo();
$this->assertInstanceOf('MongoDB\Driver\ReadConcern', $debug['readConcern']);
$this->assertSame(ReadConcern::LOCAL, $debug['readConcern']->getLevel());
$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()
public function testSelectDatabasePassesOptions()
{
$databaseOptions = [
'readConcern' => new ReadConcern(ReadConcern::LOCAL),
'readPreference' => new ReadPreference(ReadPreference::RP_SECONDARY_PREFERRED),
'writeConcern' => new WriteConcern(WriteConcern::MAJORITY),
];
......@@ -87,6 +102,8 @@ class ClientTest extends TestCase
$database = $client->selectDatabase($this->getDatabaseName(), $databaseOptions);
$debug = $database->__debugInfo();
$this->assertInstanceOf('MongoDB\Driver\ReadConcern', $debug['readConcern']);
$this->assertSame(ReadConcern::LOCAL, $debug['readConcern']->getLevel());
$this->assertInstanceOf('MongoDB\Driver\ReadPreference', $debug['readPreference']);
$this->assertSame(ReadPreference::RP_SECONDARY_PREFERRED, $debug['readPreference']->getMode());
$this->assertInstanceOf('MongoDB\Driver\WriteConcern', $debug['writeConcern']);
......
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