Skip to content

Commit d13e110

Browse files
committed
PHPLIB-145: __debugInfo for Client, Database, and Collection
This allows the selectDatabase() and selectCollection() to be tested (PHPLIB-144).
1 parent 68acf01 commit d13e110

File tree

5 files changed

+152
-0
lines changed

5 files changed

+152
-0
lines changed

src/Client.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,20 @@ public function __construct($uri = 'mongodb://localhost:27017', array $options =
3434
$this->uri = (string) $uri;
3535
}
3636

37+
/**
38+
* Return internal properties for debugging purposes.
39+
*
40+
* @see http://php.net/manual/en/language.oop5.magic.php#language.oop5.magic.debuginfo
41+
* @param array
42+
*/
43+
public function __debugInfo()
44+
{
45+
return [
46+
'manager' => $this->manager,
47+
'uri' => $this->uri,
48+
];
49+
}
50+
3751
/**
3852
* Return the connection string (i.e. URI).
3953
*

src/Collection.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,23 @@ public function __construct(Manager $manager, $namespace, array $options = [])
8888
$this->writeConcern = isset($options['writeConcern']) ? $options['writeConcern'] : $this->manager->getWriteConcern();
8989
}
9090

91+
/**
92+
* Return internal properties for debugging purposes.
93+
*
94+
* @see http://php.net/manual/en/language.oop5.magic.php#language.oop5.magic.debuginfo
95+
* @param array
96+
*/
97+
public function __debugInfo()
98+
{
99+
return [
100+
'collectionName' => $this->collectionName,
101+
'databaseName' => $this->databaseName,
102+
'manager' => $this->manager,
103+
'readPreference' => $this->readPreference,
104+
'writeConcern' => $this->writeConcern,
105+
];
106+
}
107+
91108
/**
92109
* Return the collection namespace (e.g. "db.collection").
93110
*

src/Database.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,22 @@ public function __construct(Manager $manager, $databaseName, array $options = []
6666
$this->writeConcern = isset($options['writeConcern']) ? $options['writeConcern'] : $this->manager->getWriteConcern();
6767
}
6868

69+
/**
70+
* Return internal properties for debugging purposes.
71+
*
72+
* @see http://php.net/manual/en/language.oop5.magic.php#language.oop5.magic.debuginfo
73+
* @param array
74+
*/
75+
public function __debugInfo()
76+
{
77+
return [
78+
'databaseName' => $this->databaseName,
79+
'manager' => $this->manager,
80+
'readPreference' => $this->readPreference,
81+
'writeConcern' => $this->writeConcern,
82+
];
83+
}
84+
6985
/**
7086
* Return the database name.
7187
*

tests/ClientTest.php

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

55
use MongoDB\Client;
6+
use MongoDB\Driver\ReadPreference;
7+
use MongoDB\Driver\WriteConcern;
68

79
/**
810
* Unit tests for the Client class.
@@ -22,4 +24,72 @@ public function testToString()
2224

2325
$this->assertSame($this->getUri(), (string) $client);
2426
}
27+
28+
public function testSelectCollectionInheritsReadPreferenceAndWriteConcern()
29+
{
30+
$clientOptions = [
31+
'readPreference' => 'secondaryPreferred',
32+
'w' => WriteConcern::MAJORITY,
33+
];
34+
35+
$client = new Client($this->getUri(), $clientOptions);
36+
$collection = $client->selectCollection($this->getDatabaseName(), $this->getCollectionName());
37+
$debug = $collection->__debugInfo();
38+
39+
$this->assertInstanceOf('MongoDB\Driver\ReadPreference', $debug['readPreference']);
40+
$this->assertSame(ReadPreference::RP_SECONDARY_PREFERRED, $debug['readPreference']->getMode());
41+
$this->assertInstanceOf('MongoDB\Driver\WriteConcern', $debug['writeConcern']);
42+
$this->assertSame(WriteConcern::MAJORITY, $debug['writeConcern']->getW());
43+
}
44+
45+
public function testSelectCollectionPassesReadPreferenceAndWriteConcern()
46+
{
47+
$collectionOptions = [
48+
'readPreference' => new ReadPreference(ReadPreference::RP_SECONDARY_PREFERRED),
49+
'writeConcern' => new WriteConcern(WriteConcern::MAJORITY),
50+
];
51+
52+
$client = new Client($this->getUri());
53+
$collection = $client->selectCollection($this->getDatabaseName(), $this->getCollectionName(), $collectionOptions);
54+
$debug = $collection->__debugInfo();
55+
56+
$this->assertInstanceOf('MongoDB\Driver\ReadPreference', $debug['readPreference']);
57+
$this->assertSame(ReadPreference::RP_SECONDARY_PREFERRED, $debug['readPreference']->getMode());
58+
$this->assertInstanceOf('MongoDB\Driver\WriteConcern', $debug['writeConcern']);
59+
$this->assertSame(WriteConcern::MAJORITY, $debug['writeConcern']->getW());
60+
}
61+
62+
public function testSelectDatabaseInheritsReadPreferenceAndWriteConcern()
63+
{
64+
$clientOptions = [
65+
'readPreference' => 'secondaryPreferred',
66+
'w' => WriteConcern::MAJORITY,
67+
];
68+
69+
$client = new Client($this->getUri(), $clientOptions);
70+
$database = $client->selectDatabase($this->getDatabaseName());
71+
$debug = $database->__debugInfo();
72+
73+
$this->assertInstanceOf('MongoDB\Driver\ReadPreference', $debug['readPreference']);
74+
$this->assertSame(ReadPreference::RP_SECONDARY_PREFERRED, $debug['readPreference']->getMode());
75+
$this->assertInstanceOf('MongoDB\Driver\WriteConcern', $debug['writeConcern']);
76+
$this->assertSame(WriteConcern::MAJORITY, $debug['writeConcern']->getW());
77+
}
78+
79+
public function testSelectDatabasePassesReadPreferenceAndWriteConcern()
80+
{
81+
$databaseOptions = [
82+
'readPreference' => new ReadPreference(ReadPreference::RP_SECONDARY_PREFERRED),
83+
'writeConcern' => new WriteConcern(WriteConcern::MAJORITY),
84+
];
85+
86+
$client = new Client($this->getUri());
87+
$database = $client->selectDatabase($this->getDatabaseName(), $databaseOptions);
88+
$debug = $database->__debugInfo();
89+
90+
$this->assertInstanceOf('MongoDB\Driver\ReadPreference', $debug['readPreference']);
91+
$this->assertSame(ReadPreference::RP_SECONDARY_PREFERRED, $debug['readPreference']->getMode());
92+
$this->assertInstanceOf('MongoDB\Driver\WriteConcern', $debug['writeConcern']);
93+
$this->assertSame(WriteConcern::MAJORITY, $debug['writeConcern']->getW());
94+
}
2595
}

tests/Database/DatabaseFunctionalTest.php

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

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

810
/**
911
* Functional tests for the Database class.
@@ -74,4 +76,37 @@ public function testDrop()
7476
$this->assertCommandSucceeded($commandResult);
7577
$this->assertCollectionCount($this->getNamespace(), 0);
7678
}
79+
80+
public function testSelectCollectionInheritsReadPreferenceAndWriteConcern()
81+
{
82+
$databaseOptions = [
83+
'readPreference' => new ReadPreference(ReadPreference::RP_SECONDARY_PREFERRED),
84+
'writeConcern' => new WriteConcern(WriteConcern::MAJORITY),
85+
];
86+
87+
$database = new Database($this->manager, $this->getDatabaseName(), $databaseOptions);
88+
$collection = $database->selectCollection($this->getCollectionName());
89+
$debug = $collection->__debugInfo();
90+
91+
$this->assertInstanceOf('MongoDB\Driver\ReadPreference', $debug['readPreference']);
92+
$this->assertSame(ReadPreference::RP_SECONDARY_PREFERRED, $debug['readPreference']->getMode());
93+
$this->assertInstanceOf('MongoDB\Driver\WriteConcern', $debug['writeConcern']);
94+
$this->assertSame(WriteConcern::MAJORITY, $debug['writeConcern']->getW());
95+
}
96+
97+
public function testSelectCollectionPassesReadPreferenceAndWriteConcern()
98+
{
99+
$collectionOptions = [
100+
'readPreference' => new ReadPreference(ReadPreference::RP_SECONDARY_PREFERRED),
101+
'writeConcern' => new WriteConcern(WriteConcern::MAJORITY),
102+
];
103+
104+
$collection = $this->database->selectCollection($this->getCollectionName(), $collectionOptions);
105+
$debug = $collection->__debugInfo();
106+
107+
$this->assertInstanceOf('MongoDB\Driver\ReadPreference', $debug['readPreference']);
108+
$this->assertSame(ReadPreference::RP_SECONDARY_PREFERRED, $debug['readPreference']->getMode());
109+
$this->assertInstanceOf('MongoDB\Driver\WriteConcern', $debug['writeConcern']);
110+
$this->assertSame(WriteConcern::MAJORITY, $debug['writeConcern']->getW());
111+
}
77112
}

0 commit comments

Comments
 (0)