Skip to content

Commit 748c14f

Browse files
committed
Add more fields to Bucket debug handler and test selectGridFSBucket()
1 parent 90155d9 commit 748c14f

File tree

2 files changed

+61
-0
lines changed

2 files changed

+61
-0
lines changed

src/GridFS/Bucket.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,12 @@ class Bucket
2727

2828
private $collectionWrapper;
2929
private $databaseName;
30+
private $manager;
3031
private $bucketName;
3132
private $chunkSizeBytes;
33+
private $readConcern;
34+
private $readPreference;
35+
private $writeConcern;
3236

3337
/**
3438
* Constructs a GridFS bucket.
@@ -79,9 +83,13 @@ public function __construct(Manager $manager, $databaseName, array $options = []
7983
throw InvalidArgumentException::invalidType('"writeConcern" option', $options['writeConcern'], 'MongoDB\Driver\WriteConcern');
8084
}
8185

86+
$this->manager = $manager;
8287
$this->databaseName = (string) $databaseName;
8388
$this->bucketName = $options['bucketName'];
8489
$this->chunkSizeBytes = $options['chunkSizeBytes'];
90+
$this->readConcern = isset($options['readConcern']) ? $options['readConcern'] : $this->manager->getReadConcern();
91+
$this->readPreference = isset($options['readPreference']) ? $options['readPreference'] : $this->manager->getReadPreference();
92+
$this->writeConcern = isset($options['writeConcern']) ? $options['writeConcern'] : $this->manager->getWriteConcern();
8593

8694
$collectionOptions = array_intersect_key($options, ['readConcern' => 1, 'readPreference' => 1, 'writeConcern' => 1]);
8795

@@ -100,7 +108,11 @@ public function __debugInfo()
100108
return [
101109
'bucketName' => $this->bucketName,
102110
'databaseName' => $this->databaseName,
111+
'manager' => $this->manager,
103112
'chunkSizeBytes' => $this->chunkSizeBytes,
113+
'readConcern' => $this->readConcern,
114+
'readPreference' => $this->readPreference,
115+
'writeConcern' => $this->writeConcern,
104116
];
105117
}
106118

tests/Database/DatabaseFunctionalTest.php

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,55 @@ public function testSelectCollectionPassesOptions()
190190
$this->assertSame(WriteConcern::MAJORITY, $debug['writeConcern']->getW());
191191
}
192192

193+
public function testSelectGridFSBucketInheritsOptions()
194+
{
195+
$databaseOptions = [
196+
'readConcern' => new ReadConcern(ReadConcern::LOCAL),
197+
'readPreference' => new ReadPreference(ReadPreference::RP_SECONDARY_PREFERRED),
198+
'writeConcern' => new WriteConcern(WriteConcern::MAJORITY),
199+
];
200+
201+
$database = new Database($this->manager, $this->getDatabaseName(), $databaseOptions);
202+
$bucket = $database->selectGridFSBucket();
203+
$debug = $bucket->__debugInfo();
204+
205+
$this->assertSame($this->manager, $debug['manager']);
206+
$this->assertSame($this->getDatabaseName(), $debug['databaseName']);
207+
$this->assertSame('fs', $debug['bucketName']);
208+
$this->assertSame(261120, $debug['chunkSizeBytes']);
209+
$this->assertInstanceOf('MongoDB\Driver\ReadConcern', $debug['readConcern']);
210+
$this->assertSame(ReadConcern::LOCAL, $debug['readConcern']->getLevel());
211+
$this->assertInstanceOf('MongoDB\Driver\ReadPreference', $debug['readPreference']);
212+
$this->assertSame(ReadPreference::RP_SECONDARY_PREFERRED, $debug['readPreference']->getMode());
213+
$this->assertInstanceOf('MongoDB\Driver\WriteConcern', $debug['writeConcern']);
214+
$this->assertSame(WriteConcern::MAJORITY, $debug['writeConcern']->getW());
215+
}
216+
217+
public function testSelectGridFSBucketPassesOptions()
218+
{
219+
$bucketOptions = [
220+
'bucketName' => 'custom_fs',
221+
'chunkSizeBytes' => 8192,
222+
'readConcern' => new ReadConcern(ReadConcern::LOCAL),
223+
'readPreference' => new ReadPreference(ReadPreference::RP_SECONDARY_PREFERRED),
224+
'writeConcern' => new WriteConcern(WriteConcern::MAJORITY),
225+
];
226+
227+
$database = new Database($this->manager, $this->getDatabaseName());
228+
$bucket = $database->selectGridFSBucket($bucketOptions);
229+
$debug = $bucket->__debugInfo();
230+
231+
$this->assertSame($this->getDatabaseName(), $debug['databaseName']);
232+
$this->assertSame('custom_fs', $debug['bucketName']);
233+
$this->assertSame(8192, $debug['chunkSizeBytes']);
234+
$this->assertInstanceOf('MongoDB\Driver\ReadConcern', $debug['readConcern']);
235+
$this->assertSame(ReadConcern::LOCAL, $debug['readConcern']->getLevel());
236+
$this->assertInstanceOf('MongoDB\Driver\ReadPreference', $debug['readPreference']);
237+
$this->assertSame(ReadPreference::RP_SECONDARY_PREFERRED, $debug['readPreference']->getMode());
238+
$this->assertInstanceOf('MongoDB\Driver\WriteConcern', $debug['writeConcern']);
239+
$this->assertSame(WriteConcern::MAJORITY, $debug['writeConcern']->getW());
240+
}
241+
193242
public function testWithOptionsInheritsOptions()
194243
{
195244
$databaseOptions = [

0 commit comments

Comments
 (0)