Skip to content

Commit 52b81e9

Browse files
committed
PHPLIB-49: withOptions() clone method for Database and Collection
1 parent d13e110 commit 52b81e9

File tree

4 files changed

+126
-0
lines changed

4 files changed

+126
-0
lines changed

src/Collection.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -598,4 +598,33 @@ public function updateOne($filter, $update, array $options = [])
598598

599599
return $operation->execute($server);
600600
}
601+
602+
/**
603+
* Get a clone of this collection with different options.
604+
*
605+
* Supported options:
606+
*
607+
* * readPreference (MongoDB\Driver\ReadPreference): The default read
608+
* preference to use for collection operations. Defaults to this
609+
* Collection's read preference.
610+
*
611+
* * writeConcern (MongoDB\Driver\WriteConcern): The default write concern
612+
* to use for collection operations. Defaults to this Collection's write
613+
* concern.
614+
*
615+
* @param array $options Collection constructor options
616+
* @return Collection
617+
*/
618+
public function withOptions(array $options = [])
619+
{
620+
if ( ! isset($options['readPreference'])) {
621+
$options['readPreference'] = $this->readPreference;
622+
}
623+
624+
if ( ! isset($options['writeConcern'])) {
625+
$options['writeConcern'] = $this->writeConcern;
626+
}
627+
628+
return new Collection($this->manager, $this->databaseName . '.' . $this->collectionName, $options);
629+
}
601630
}

src/Database.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,4 +189,33 @@ public function selectCollection($collectionName, array $options = [])
189189

190190
return new Collection($this->manager, $this->databaseName . '.' . $collectionName, $options);
191191
}
192+
193+
/**
194+
* Get a clone of this database with different options.
195+
*
196+
* Supported options:
197+
*
198+
* * readPreference (MongoDB\Driver\ReadPreference): The default read
199+
* preference to use for database operations and selected collections.
200+
* Defaults to this Database's read preference.
201+
*
202+
* * writeConcern (MongoDB\Driver\WriteConcern): The default write concern
203+
* to use for database operations and selected collections. Defaults to
204+
* this Database's write concern.
205+
*
206+
* @param array $options Database constructor options
207+
* @return Database
208+
*/
209+
public function withOptions(array $options = [])
210+
{
211+
if ( ! isset($options['readPreference'])) {
212+
$options['readPreference'] = $this->readPreference;
213+
}
214+
215+
if ( ! isset($options['writeConcern'])) {
216+
$options['writeConcern'] = $this->writeConcern;
217+
}
218+
219+
return new Database($this->manager, $this->databaseName, $options);
220+
}
192221
}

tests/Collection/CollectionFunctionalTest.php

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
use MongoDB\Collection;
66
use MongoDB\Driver\BulkWrite;
7+
use MongoDB\Driver\ReadPreference;
8+
use MongoDB\Driver\WriteConcern;
79

810
/**
911
* Functional tests for the Collection class.
@@ -109,6 +111,39 @@ public function testFindOne()
109111
$this->assertEquals($expected, $this->collection->findOne($filter, $options));
110112
}
111113

114+
public function testWithOptionsInheritsReadPreferenceAndWriteConcern()
115+
{
116+
$collectionOptions = [
117+
'readPreference' => new ReadPreference(ReadPreference::RP_SECONDARY_PREFERRED),
118+
'writeConcern' => new WriteConcern(WriteConcern::MAJORITY),
119+
];
120+
121+
$collection = new Collection($this->manager, $this->getNamespace(), $collectionOptions);
122+
$clone = $collection->withOptions();
123+
$debug = $clone->__debugInfo();
124+
125+
$this->assertInstanceOf('MongoDB\Driver\ReadPreference', $debug['readPreference']);
126+
$this->assertSame(ReadPreference::RP_SECONDARY_PREFERRED, $debug['readPreference']->getMode());
127+
$this->assertInstanceOf('MongoDB\Driver\WriteConcern', $debug['writeConcern']);
128+
$this->assertSame(WriteConcern::MAJORITY, $debug['writeConcern']->getW());
129+
}
130+
131+
public function testWithOptionsPassesReadPreferenceAndWriteConcern()
132+
{
133+
$collectionOptions = [
134+
'readPreference' => new ReadPreference(ReadPreference::RP_SECONDARY_PREFERRED),
135+
'writeConcern' => new WriteConcern(WriteConcern::MAJORITY),
136+
];
137+
138+
$clone = $this->collection->withOptions($collectionOptions);
139+
$debug = $clone->__debugInfo();
140+
141+
$this->assertInstanceOf('MongoDB\Driver\ReadPreference', $debug['readPreference']);
142+
$this->assertSame(ReadPreference::RP_SECONDARY_PREFERRED, $debug['readPreference']->getMode());
143+
$this->assertInstanceOf('MongoDB\Driver\WriteConcern', $debug['writeConcern']);
144+
$this->assertSame(WriteConcern::MAJORITY, $debug['writeConcern']->getW());
145+
}
146+
112147
/**
113148
* Create data fixtures.
114149
*

tests/Database/DatabaseFunctionalTest.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,4 +109,37 @@ public function testSelectCollectionPassesReadPreferenceAndWriteConcern()
109109
$this->assertInstanceOf('MongoDB\Driver\WriteConcern', $debug['writeConcern']);
110110
$this->assertSame(WriteConcern::MAJORITY, $debug['writeConcern']->getW());
111111
}
112+
113+
public function testWithOptionsInheritsReadPreferenceAndWriteConcern()
114+
{
115+
$databaseOptions = [
116+
'readPreference' => new ReadPreference(ReadPreference::RP_SECONDARY_PREFERRED),
117+
'writeConcern' => new WriteConcern(WriteConcern::MAJORITY),
118+
];
119+
120+
$database = new Database($this->manager, $this->getDatabaseName(), $databaseOptions);
121+
$clone = $database->withOptions();
122+
$debug = $clone->__debugInfo();
123+
124+
$this->assertInstanceOf('MongoDB\Driver\ReadPreference', $debug['readPreference']);
125+
$this->assertSame(ReadPreference::RP_SECONDARY_PREFERRED, $debug['readPreference']->getMode());
126+
$this->assertInstanceOf('MongoDB\Driver\WriteConcern', $debug['writeConcern']);
127+
$this->assertSame(WriteConcern::MAJORITY, $debug['writeConcern']->getW());
128+
}
129+
130+
public function testWithOptionsPassesReadPreferenceAndWriteConcern()
131+
{
132+
$databaseOptions = [
133+
'readPreference' => new ReadPreference(ReadPreference::RP_SECONDARY_PREFERRED),
134+
'writeConcern' => new WriteConcern(WriteConcern::MAJORITY),
135+
];
136+
137+
$clone = $this->database->withOptions($databaseOptions);
138+
$debug = $clone->__debugInfo();
139+
140+
$this->assertInstanceOf('MongoDB\Driver\ReadPreference', $debug['readPreference']);
141+
$this->assertSame(ReadPreference::RP_SECONDARY_PREFERRED, $debug['readPreference']->getMode());
142+
$this->assertInstanceOf('MongoDB\Driver\WriteConcern', $debug['writeConcern']);
143+
$this->assertSame(WriteConcern::MAJORITY, $debug['writeConcern']->getW());
144+
}
112145
}

0 commit comments

Comments
 (0)