Skip to content

Commit 4930bf6

Browse files
committed
Validate Database $databaseName and test getters
1 parent 337075f commit 4930bf6

File tree

2 files changed

+35
-0
lines changed

2 files changed

+35
-0
lines changed

src/Database.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,14 @@ class Database
3434
* @param string $databaseName Database name
3535
* @param WriteConcern $writeConcern Default write concern to apply
3636
* @param ReadPreference $readPreference Default read preference to apply
37+
* @throws InvalidArgumentException if $databaseName is invalid
3738
*/
3839
public function __construct(Manager $manager, $databaseName, WriteConcern $writeConcern = null, ReadPreference $readPreference = null)
3940
{
41+
if (strlen($databaseName) < 1) {
42+
throw new InvalidArgumentException('$databaseName is invalid: ' . $databaseName);
43+
}
44+
4045
$this->manager = $manager;
4146
$this->databaseName = (string) $databaseName;
4247
$this->writeConcern = $writeConcern;

tests/Database/DatabaseFunctionalTest.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,41 @@
22

33
namespace MongoDB\Tests\Database;
44

5+
use MongoDB\Database;
6+
57
/**
68
* Functional tests for the Database class.
79
*/
810
class DatabaseFunctionalTest extends FunctionalTestCase
911
{
12+
/**
13+
* @expectedException MongoDB\Exception\InvalidArgumentException
14+
* @dataProvider provideInvalidDatabaseValues
15+
*/
16+
public function testConstructorDatabaseNameArgument($databaseName)
17+
{
18+
// TODO: Move to unit test once ManagerInterface can be mocked (PHPC-378)
19+
new Database($this->manager, $databaseName);
20+
}
21+
22+
public function provideInvalidDatabaseValues()
23+
{
24+
return array(
25+
array(null),
26+
array(''),
27+
);
28+
}
29+
30+
public function testToString()
31+
{
32+
$this->assertEquals($this->getDatabaseName(), (string) $this->database);
33+
}
34+
35+
public function getGetDatabaseName()
36+
{
37+
$this->assertEquals($this->getDatabaseName(), $this->database->getDatabaseName());
38+
}
39+
1040
public function testDrop()
1141
{
1242
$writeResult = $this->manager->executeInsert($this->getNamespace(), array('x' => 1));

0 commit comments

Comments
 (0)