Skip to content

Commit 99635c0

Browse files
committed
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.
1 parent 2c6a776 commit 99635c0

File tree

4 files changed

+44
-93
lines changed

4 files changed

+44
-93
lines changed

src/Client.php

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -89,16 +89,7 @@ public function listDatabases(array $options = [])
8989
/**
9090
* Select a collection.
9191
*
92-
* Supported options:
93-
*
94-
* * readPreference (MongoDB\Driver\ReadPreference): The default read
95-
* preference to use for collection operations. Defaults to the Client's
96-
* read preference.
97-
*
98-
* * writeConcern (MongoDB\Driver\WriteConcern): The default write concern
99-
* to use for collection operations. Defaults to the Client's write
100-
* concern.
101-
*
92+
* @see Collection::__construct() for supported options
10293
* @param string $databaseName Name of the database containing the collection
10394
* @param string $collectionName Name of the collection to select
10495
* @param array $options Collection constructor options

src/Collection.php

Lines changed: 6 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -655,36 +655,17 @@ public function updateOne($filter, $update, array $options = [])
655655
/**
656656
* Get a clone of this collection with different options.
657657
*
658-
* Supported options:
659-
*
660-
* * readConcern (MongoDB\Driver\ReadConcern): The default read concern to
661-
* use for collection operations. Defaults to this Collection's read
662-
* concern.
663-
*
664-
* * readPreference (MongoDB\Driver\ReadPreference): The default read
665-
* preference to use for collection operations. Defaults to this
666-
* Collection's read preference.
667-
*
668-
* * writeConcern (MongoDB\Driver\WriteConcern): The default write concern
669-
* to use for collection operations. Defaults to this Collection's write
670-
* concern.
671-
*
658+
* @see Collection::__construct() for supported options
672659
* @param array $options Collection constructor options
673660
* @return Collection
674661
*/
675662
public function withOptions(array $options = [])
676663
{
677-
if ( ! isset($options['readConcern'])) {
678-
$options['readConcern'] = $this->readConcern;
679-
}
680-
681-
if ( ! isset($options['readPreference'])) {
682-
$options['readPreference'] = $this->readPreference;
683-
}
684-
685-
if ( ! isset($options['writeConcern'])) {
686-
$options['writeConcern'] = $this->writeConcern;
687-
}
664+
$options += [
665+
'readConcern' => $this->readConcern,
666+
'readPreference' => $this->readPreference,
667+
'writeConcern' => $this->writeConcern,
668+
];
688669

689670
return new Collection($this->manager, $this->databaseName . '.' . $this->collectionName, $options);
690671
}

src/Database.php

Lines changed: 12 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -201,74 +201,36 @@ public function listCollections(array $options = [])
201201
/**
202202
* Select a collection within this database.
203203
*
204-
* Supported options:
205-
*
206-
* * readConcern (MongoDB\Driver\ReadConcern): The default read concern to
207-
* use for collection operations. Defaults to the Database's read
208-
* concern.
209-
*
210-
* * readPreference (MongoDB\Driver\ReadPreference): The default read
211-
* preference to use for collection operations. Defaults to the
212-
* Database's read preference.
213-
*
214-
* * writeConcern (MongoDB\Driver\WriteConcern): The default write concern
215-
* to use for collection operations. Defaults to the Database's write
216-
* concern.
217-
*
204+
* @see Collection::__construct() for supported options
218205
* @param string $collectionName Name of the collection to select
219206
* @param array $options Collection constructor options
220207
* @return Collection
221208
*/
222209
public function selectCollection($collectionName, array $options = [])
223210
{
224-
if ( ! isset($options['readConcern'])) {
225-
$options['readConcern'] = $this->readConcern;
226-
}
227-
228-
if ( ! isset($options['readPreference'])) {
229-
$options['readPreference'] = $this->readPreference;
230-
}
231-
232-
if ( ! isset($options['writeConcern'])) {
233-
$options['writeConcern'] = $this->writeConcern;
234-
}
211+
$options += [
212+
'readConcern' => $this->readConcern,
213+
'readPreference' => $this->readPreference,
214+
'writeConcern' => $this->writeConcern,
215+
];
235216

236217
return new Collection($this->manager, $this->databaseName . '.' . $collectionName, $options);
237218
}
238219

239220
/**
240221
* Get a clone of this database with different options.
241222
*
242-
* Supported options:
243-
*
244-
* * readConcern (MongoDB\Driver\ReadConcern): The default read concern to
245-
* use for database operations and selected collections. Defaults to this
246-
* Database's read concern.
247-
*
248-
* * readPreference (MongoDB\Driver\ReadPreference): The default read
249-
* preference to use for database operations and selected collections.
250-
* Defaults to this Database's read preference.
251-
*
252-
* * writeConcern (MongoDB\Driver\WriteConcern): The default write concern
253-
* to use for database operations and selected collections. Defaults to
254-
* this Database's write concern.
255-
*
223+
* @see Database::__construct() for supported options
256224
* @param array $options Database constructor options
257225
* @return Database
258226
*/
259227
public function withOptions(array $options = [])
260228
{
261-
if ( ! isset($options['readConcern'])) {
262-
$options['readConcern'] = $this->readConcern;
263-
}
264-
265-
if ( ! isset($options['readPreference'])) {
266-
$options['readPreference'] = $this->readPreference;
267-
}
268-
269-
if ( ! isset($options['writeConcern'])) {
270-
$options['writeConcern'] = $this->writeConcern;
271-
}
229+
$options += [
230+
'readConcern' => $this->readConcern,
231+
'readPreference' => $this->readPreference,
232+
'writeConcern' => $this->writeConcern,
233+
];
272234

273235
return new Database($this->manager, $this->databaseName, $options);
274236
}

tests/ClientTest.php

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace MongoDB\Tests;
44

55
use MongoDB\Client;
6+
use MongoDB\Driver\ReadConcern;
67
use MongoDB\Driver\ReadPreference;
78
use MongoDB\Driver\WriteConcern;
89

@@ -25,26 +26,32 @@ public function testToString()
2526
$this->assertSame($this->getUri(), (string) $client);
2627
}
2728

28-
public function testSelectCollectionInheritsReadPreferenceAndWriteConcern()
29+
public function testSelectCollectionInheritsOptions()
2930
{
30-
$clientOptions = [
31+
$this->markTestSkipped('Depends on https://jira.mongodb.org/browse/PHPC-523');
32+
33+
$uriOptions = [
34+
'readConcernLevel' => ReadConcern::LOCAL,
3135
'readPreference' => 'secondaryPreferred',
3236
'w' => WriteConcern::MAJORITY,
3337
];
3438

35-
$client = new Client($this->getUri(), $clientOptions);
39+
$client = new Client($this->getUri(), $uriOptions);
3640
$collection = $client->selectCollection($this->getDatabaseName(), $this->getCollectionName());
3741
$debug = $collection->__debugInfo();
3842

43+
$this->assertInstanceOf('MongoDB\Driver\ReadConcern', $debug['readConcern']);
44+
$this->assertSame(ReadConcern::LOCAL, $debug['readConcern']->getLevel());
3945
$this->assertInstanceOf('MongoDB\Driver\ReadPreference', $debug['readPreference']);
4046
$this->assertSame(ReadPreference::RP_SECONDARY_PREFERRED, $debug['readPreference']->getMode());
4147
$this->assertInstanceOf('MongoDB\Driver\WriteConcern', $debug['writeConcern']);
4248
$this->assertSame(WriteConcern::MAJORITY, $debug['writeConcern']->getW());
4349
}
4450

45-
public function testSelectCollectionPassesReadPreferenceAndWriteConcern()
51+
public function testSelectCollectionPassesOptions()
4652
{
4753
$collectionOptions = [
54+
'readConcern' => new ReadConcern(ReadConcern::LOCAL),
4855
'readPreference' => new ReadPreference(ReadPreference::RP_SECONDARY_PREFERRED),
4956
'writeConcern' => new WriteConcern(WriteConcern::MAJORITY),
5057
];
@@ -53,32 +60,40 @@ public function testSelectCollectionPassesReadPreferenceAndWriteConcern()
5360
$collection = $client->selectCollection($this->getDatabaseName(), $this->getCollectionName(), $collectionOptions);
5461
$debug = $collection->__debugInfo();
5562

63+
$this->assertInstanceOf('MongoDB\Driver\ReadConcern', $debug['readConcern']);
64+
$this->assertSame(ReadConcern::LOCAL, $debug['readConcern']->getLevel());
5665
$this->assertInstanceOf('MongoDB\Driver\ReadPreference', $debug['readPreference']);
5766
$this->assertSame(ReadPreference::RP_SECONDARY_PREFERRED, $debug['readPreference']->getMode());
5867
$this->assertInstanceOf('MongoDB\Driver\WriteConcern', $debug['writeConcern']);
5968
$this->assertSame(WriteConcern::MAJORITY, $debug['writeConcern']->getW());
6069
}
6170

62-
public function testSelectDatabaseInheritsReadPreferenceAndWriteConcern()
71+
public function testSelectDatabaseInheritsOptions()
6372
{
64-
$clientOptions = [
73+
$this->markTestSkipped('Depends on https://jira.mongodb.org/browse/PHPC-523');
74+
75+
$uriOptions = [
76+
'readConcernLevel' => ReadConcern::LOCAL,
6577
'readPreference' => 'secondaryPreferred',
6678
'w' => WriteConcern::MAJORITY,
6779
];
6880

69-
$client = new Client($this->getUri(), $clientOptions);
81+
$client = new Client($this->getUri(), $uriOptions);
7082
$database = $client->selectDatabase($this->getDatabaseName());
7183
$debug = $database->__debugInfo();
7284

85+
$this->assertInstanceOf('MongoDB\Driver\ReadConcern', $debug['readConcern']);
86+
$this->assertSame(ReadConcern::LOCAL, $debug['readConcern']->getLevel());
7387
$this->assertInstanceOf('MongoDB\Driver\ReadPreference', $debug['readPreference']);
7488
$this->assertSame(ReadPreference::RP_SECONDARY_PREFERRED, $debug['readPreference']->getMode());
7589
$this->assertInstanceOf('MongoDB\Driver\WriteConcern', $debug['writeConcern']);
7690
$this->assertSame(WriteConcern::MAJORITY, $debug['writeConcern']->getW());
7791
}
7892

79-
public function testSelectDatabasePassesReadPreferenceAndWriteConcern()
93+
public function testSelectDatabasePassesOptions()
8094
{
8195
$databaseOptions = [
96+
'readConcern' => new ReadConcern(ReadConcern::LOCAL),
8297
'readPreference' => new ReadPreference(ReadPreference::RP_SECONDARY_PREFERRED),
8398
'writeConcern' => new WriteConcern(WriteConcern::MAJORITY),
8499
];
@@ -87,6 +102,8 @@ public function testSelectDatabasePassesReadPreferenceAndWriteConcern()
87102
$database = $client->selectDatabase($this->getDatabaseName(), $databaseOptions);
88103
$debug = $database->__debugInfo();
89104

105+
$this->assertInstanceOf('MongoDB\Driver\ReadConcern', $debug['readConcern']);
106+
$this->assertSame(ReadConcern::LOCAL, $debug['readConcern']->getLevel());
90107
$this->assertInstanceOf('MongoDB\Driver\ReadPreference', $debug['readPreference']);
91108
$this->assertSame(ReadPreference::RP_SECONDARY_PREFERRED, $debug['readPreference']->getMode());
92109
$this->assertInstanceOf('MongoDB\Driver\WriteConcern', $debug['writeConcern']);

0 commit comments

Comments
 (0)