Skip to content

Commit af9bc65

Browse files
committed
Merge pull request #97
2 parents 7039bd7 + c56c7e2 commit af9bc65

File tree

4 files changed

+61
-0
lines changed

4 files changed

+61
-0
lines changed

src/Client.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,23 @@ public function __debugInfo()
7373
];
7474
}
7575

76+
/**
77+
* Select a database.
78+
*
79+
* Note: collections whose names contain special characters (e.g. "-") may
80+
* be selected with complex syntax (e.g. $client->{"that-database"}) or
81+
* {@link selectDatabase()}.
82+
*
83+
* @see http://php.net/oop5.overloading#object.get
84+
* @see http://php.net/types.string#language.types.string.parsing.complex
85+
* @param string $databaseName Name of the database to select
86+
* @return Database
87+
*/
88+
public function __get($databaseName)
89+
{
90+
return $this->selectDatabase($databaseName);
91+
}
92+
7693
/**
7794
* Return the connection string (i.e. URI).
7895
*

src/Database.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,23 @@ public function __debugInfo()
102102
];
103103
}
104104

105+
/**
106+
* Select a collection within this database.
107+
*
108+
* Note: collections whose names contain special characters (e.g. ".") may
109+
* be selected with complex syntax (e.g. $database->{"system.profile"}) or
110+
* {@link selectCollection()}.
111+
*
112+
* @see http://php.net/oop5.overloading#object.get
113+
* @see http://php.net/types.string#language.types.string.parsing.complex
114+
* @param string $collectionName Name of the collection to select
115+
* @return Collection
116+
*/
117+
public function __get($collectionName)
118+
{
119+
return $this->selectCollection($collectionName);
120+
}
121+
105122
/**
106123
* Return the database name.
107124
*

tests/ClientTest.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,19 @@ public function testSelectCollectionPassesOptions()
9797
$this->assertSame(WriteConcern::MAJORITY, $debug['writeConcern']->getW());
9898
}
9999

100+
public function testGetSelectsDatabaseAndInheritsOptions()
101+
{
102+
$uriOptions = ['w' => WriteConcern::MAJORITY];
103+
104+
$client = new Client($this->getUri(), $uriOptions);
105+
$database = $client->{$this->getDatabaseName()};
106+
$debug = $database->__debugInfo();
107+
108+
$this->assertSame($this->getDatabaseName(), $debug['databaseName']);
109+
$this->assertInstanceOf('MongoDB\Driver\WriteConcern', $debug['writeConcern']);
110+
$this->assertSame(WriteConcern::MAJORITY, $debug['writeConcern']->getW());
111+
}
112+
100113
public function testSelectDatabaseInheritsOptions()
101114
{
102115
$this->markTestSkipped('Depends on https://jira.mongodb.org/browse/PHPC-523');

tests/Database/DatabaseFunctionalTest.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,20 @@ public function testDrop()
131131
$this->assertCollectionCount($this->getNamespace(), 0);
132132
}
133133

134+
public function testGetSelectsCollectionAndInheritsOptions()
135+
{
136+
$databaseOptions = ['writeConcern' => new WriteConcern(WriteConcern::MAJORITY)];
137+
138+
$database = new Database($this->manager, $this->getDatabaseName(), $databaseOptions);
139+
$collection = $database->{$this->getCollectionName()};
140+
$debug = $collection->__debugInfo();
141+
142+
$this->assertSame($this->getCollectionName(), $debug['collectionName']);
143+
$this->assertSame($this->getDatabaseName(), $debug['databaseName']);
144+
$this->assertInstanceOf('MongoDB\Driver\WriteConcern', $debug['writeConcern']);
145+
$this->assertSame(WriteConcern::MAJORITY, $debug['writeConcern']->getW());
146+
}
147+
134148
public function testSelectCollectionInheritsOptions()
135149
{
136150
$databaseOptions = [

0 commit comments

Comments
 (0)