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 ...@@ -89,16 +89,7 @@ class Client
/** /**
* Select a collection. * Select a collection.
* *
* Supported options: * @see Collection::__construct() for 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.
*
* @param string $databaseName Name of the database containing the collection * @param string $databaseName Name of the database containing the collection
* @param string $collectionName Name of the collection to select * @param string $collectionName Name of the collection to select
* @param array $options Collection constructor options * @param array $options Collection constructor options
......
...@@ -655,36 +655,17 @@ class Collection ...@@ -655,36 +655,17 @@ class Collection
/** /**
* Get a clone of this collection with different options. * Get a clone of this collection with different options.
* *
* Supported options: * @see Collection::__construct() for 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.
*
* @param array $options Collection constructor options * @param array $options Collection constructor options
* @return Collection * @return Collection
*/ */
public function withOptions(array $options = []) public function withOptions(array $options = [])
{ {
if ( ! isset($options['readConcern'])) { $options += [
$options['readConcern'] = $this->readConcern; 'readConcern' => $this->readConcern,
} 'readPreference' => $this->readPreference,
'writeConcern' => $this->writeConcern,
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); return new Collection($this->manager, $this->databaseName . '.' . $this->collectionName, $options);
} }
......
...@@ -201,37 +201,18 @@ class Database ...@@ -201,37 +201,18 @@ class Database
/** /**
* Select a collection within this database. * Select a collection within this database.
* *
* Supported options: * @see Collection::__construct() for 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.
*
* @param string $collectionName Name of the collection to select * @param string $collectionName Name of the collection to select
* @param array $options Collection constructor options * @param array $options Collection constructor options
* @return Collection * @return Collection
*/ */
public function selectCollection($collectionName, array $options = []) public function selectCollection($collectionName, array $options = [])
{ {
if ( ! isset($options['readConcern'])) { $options += [
$options['readConcern'] = $this->readConcern; 'readConcern' => $this->readConcern,
} 'readPreference' => $this->readPreference,
'writeConcern' => $this->writeConcern,
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); return new Collection($this->manager, $this->databaseName . '.' . $collectionName, $options);
} }
...@@ -239,36 +220,17 @@ class Database ...@@ -239,36 +220,17 @@ class Database
/** /**
* Get a clone of this database with different options. * Get a clone of this database with different options.
* *
* Supported options: * @see Database::__construct() for 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.
*
* @param array $options Database constructor options * @param array $options Database constructor options
* @return Database * @return Database
*/ */
public function withOptions(array $options = []) public function withOptions(array $options = [])
{ {
if ( ! isset($options['readConcern'])) { $options += [
$options['readConcern'] = $this->readConcern; 'readConcern' => $this->readConcern,
} 'readPreference' => $this->readPreference,
'writeConcern' => $this->writeConcern,
if ( ! isset($options['readPreference'])) { ];
$options['readPreference'] = $this->readPreference;
}
if ( ! isset($options['writeConcern'])) {
$options['writeConcern'] = $this->writeConcern;
}
return new Database($this->manager, $this->databaseName, $options); return new Database($this->manager, $this->databaseName, $options);
} }
......
...@@ -3,6 +3,7 @@ ...@@ -3,6 +3,7 @@
namespace MongoDB\Tests; namespace MongoDB\Tests;
use MongoDB\Client; use MongoDB\Client;
use MongoDB\Driver\ReadConcern;
use MongoDB\Driver\ReadPreference; use MongoDB\Driver\ReadPreference;
use MongoDB\Driver\WriteConcern; use MongoDB\Driver\WriteConcern;
...@@ -25,26 +26,32 @@ class ClientTest extends TestCase ...@@ -25,26 +26,32 @@ class ClientTest extends TestCase
$this->assertSame($this->getUri(), (string) $client); $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', 'readPreference' => 'secondaryPreferred',
'w' => WriteConcern::MAJORITY, 'w' => WriteConcern::MAJORITY,
]; ];
$client = new Client($this->getUri(), $clientOptions); $client = new Client($this->getUri(), $uriOptions);
$collection = $client->selectCollection($this->getDatabaseName(), $this->getCollectionName()); $collection = $client->selectCollection($this->getDatabaseName(), $this->getCollectionName());
$debug = $collection->__debugInfo(); $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->assertInstanceOf('MongoDB\Driver\ReadPreference', $debug['readPreference']);
$this->assertSame(ReadPreference::RP_SECONDARY_PREFERRED, $debug['readPreference']->getMode()); $this->assertSame(ReadPreference::RP_SECONDARY_PREFERRED, $debug['readPreference']->getMode());
$this->assertInstanceOf('MongoDB\Driver\WriteConcern', $debug['writeConcern']); $this->assertInstanceOf('MongoDB\Driver\WriteConcern', $debug['writeConcern']);
$this->assertSame(WriteConcern::MAJORITY, $debug['writeConcern']->getW()); $this->assertSame(WriteConcern::MAJORITY, $debug['writeConcern']->getW());
} }
public function testSelectCollectionPassesReadPreferenceAndWriteConcern() public function testSelectCollectionPassesOptions()
{ {
$collectionOptions = [ $collectionOptions = [
'readConcern' => new ReadConcern(ReadConcern::LOCAL),
'readPreference' => new ReadPreference(ReadPreference::RP_SECONDARY_PREFERRED), 'readPreference' => new ReadPreference(ReadPreference::RP_SECONDARY_PREFERRED),
'writeConcern' => new WriteConcern(WriteConcern::MAJORITY), 'writeConcern' => new WriteConcern(WriteConcern::MAJORITY),
]; ];
...@@ -53,32 +60,40 @@ class ClientTest extends TestCase ...@@ -53,32 +60,40 @@ class ClientTest extends TestCase
$collection = $client->selectCollection($this->getDatabaseName(), $this->getCollectionName(), $collectionOptions); $collection = $client->selectCollection($this->getDatabaseName(), $this->getCollectionName(), $collectionOptions);
$debug = $collection->__debugInfo(); $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->assertInstanceOf('MongoDB\Driver\ReadPreference', $debug['readPreference']);
$this->assertSame(ReadPreference::RP_SECONDARY_PREFERRED, $debug['readPreference']->getMode()); $this->assertSame(ReadPreference::RP_SECONDARY_PREFERRED, $debug['readPreference']->getMode());
$this->assertInstanceOf('MongoDB\Driver\WriteConcern', $debug['writeConcern']); $this->assertInstanceOf('MongoDB\Driver\WriteConcern', $debug['writeConcern']);
$this->assertSame(WriteConcern::MAJORITY, $debug['writeConcern']->getW()); $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', 'readPreference' => 'secondaryPreferred',
'w' => WriteConcern::MAJORITY, 'w' => WriteConcern::MAJORITY,
]; ];
$client = new Client($this->getUri(), $clientOptions); $client = new Client($this->getUri(), $uriOptions);
$database = $client->selectDatabase($this->getDatabaseName()); $database = $client->selectDatabase($this->getDatabaseName());
$debug = $database->__debugInfo(); $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->assertInstanceOf('MongoDB\Driver\ReadPreference', $debug['readPreference']);
$this->assertSame(ReadPreference::RP_SECONDARY_PREFERRED, $debug['readPreference']->getMode()); $this->assertSame(ReadPreference::RP_SECONDARY_PREFERRED, $debug['readPreference']->getMode());
$this->assertInstanceOf('MongoDB\Driver\WriteConcern', $debug['writeConcern']); $this->assertInstanceOf('MongoDB\Driver\WriteConcern', $debug['writeConcern']);
$this->assertSame(WriteConcern::MAJORITY, $debug['writeConcern']->getW()); $this->assertSame(WriteConcern::MAJORITY, $debug['writeConcern']->getW());
} }
public function testSelectDatabasePassesReadPreferenceAndWriteConcern() public function testSelectDatabasePassesOptions()
{ {
$databaseOptions = [ $databaseOptions = [
'readConcern' => new ReadConcern(ReadConcern::LOCAL),
'readPreference' => new ReadPreference(ReadPreference::RP_SECONDARY_PREFERRED), 'readPreference' => new ReadPreference(ReadPreference::RP_SECONDARY_PREFERRED),
'writeConcern' => new WriteConcern(WriteConcern::MAJORITY), 'writeConcern' => new WriteConcern(WriteConcern::MAJORITY),
]; ];
...@@ -87,6 +102,8 @@ class ClientTest extends TestCase ...@@ -87,6 +102,8 @@ class ClientTest extends TestCase
$database = $client->selectDatabase($this->getDatabaseName(), $databaseOptions); $database = $client->selectDatabase($this->getDatabaseName(), $databaseOptions);
$debug = $database->__debugInfo(); $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->assertInstanceOf('MongoDB\Driver\ReadPreference', $debug['readPreference']);
$this->assertSame(ReadPreference::RP_SECONDARY_PREFERRED, $debug['readPreference']->getMode()); $this->assertSame(ReadPreference::RP_SECONDARY_PREFERRED, $debug['readPreference']->getMode());
$this->assertInstanceOf('MongoDB\Driver\WriteConcern', $debug['writeConcern']); $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