Skip to content

Commit 337075f

Browse files
committed
Validate Collection $namespace and test getters
1 parent 303a09e commit 337075f

File tree

2 files changed

+52
-2
lines changed

2 files changed

+52
-2
lines changed

src/Collection.php

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,14 +52,22 @@ class Collection
5252
* @param string $namespace Collection namespace (e.g. "db.collection")
5353
* @param WriteConcern $writeConcern Default write concern to apply
5454
* @param ReadPreference $readPreference Default read preference to apply
55+
* @throws InvalidArgumentException if $namespace is invalid
5556
*/
5657
public function __construct(Manager $manager, $namespace, WriteConcern $writeConcern = null, ReadPreference $readPreference = null)
5758
{
59+
$parts = explode('.', $namespace, 2);
60+
61+
if (count($parts) != 2 || strlen($parts[0]) == 0 || strlen($parts[1]) == 0) {
62+
throw new InvalidArgumentException('$namespace is invalid: ' . $namespace);
63+
}
64+
65+
$this->databaseName = $parts[0];
66+
$this->collectionName = $parts[1];
67+
5868
$this->manager = $manager;
5969
$this->writeConcern = $writeConcern;
6070
$this->readPreference = $readPreference;
61-
62-
list($this->databaseName, $this->collectionName) = explode(".", $namespace, 2);
6371
}
6472

6573
/**

tests/Collection/CollectionFunctionalTest.php

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,55 @@
22

33
namespace MongoDB\Tests\Collection;
44

5+
use MongoDB\Collection;
56
use MongoDB\Driver\BulkWrite;
67

78
/**
89
* Functional tests for the Collection class.
910
*/
1011
class CollectionFunctionalTest extends FunctionalTestCase
1112
{
13+
/**
14+
* @expectedException MongoDB\Exception\InvalidArgumentException
15+
* @dataProvider provideInvalidNamespaceValues
16+
*/
17+
public function testConstructorNamespaceArgument($namespace)
18+
{
19+
// TODO: Move to unit test once ManagerInterface can be mocked (PHPC-378)
20+
new Collection($this->manager, $namespace);
21+
}
22+
23+
public function provideInvalidNamespaceValues()
24+
{
25+
return array(
26+
array(null),
27+
array(''),
28+
array('db_collection'),
29+
array('db'),
30+
array('.collection'),
31+
);
32+
}
33+
34+
public function testToString()
35+
{
36+
$this->assertEquals($this->getNamespace(), (string) $this->collection);
37+
}
38+
39+
public function getGetCollectionName()
40+
{
41+
$this->assertEquals($this->getCollectionName(), $this->collection->getCollectionName());
42+
}
43+
44+
public function getGetDatabaseName()
45+
{
46+
$this->assertEquals($this->getDatabaseName(), $this->collection->getDatabaseName());
47+
}
48+
49+
public function testGetNamespace()
50+
{
51+
$this->assertEquals($this->getNamespace(), $this->collection->getNamespace());
52+
}
53+
1254
public function testDrop()
1355
{
1456
$writeResult = $this->collection->insertOne(array('x' => 1));

0 commit comments

Comments
 (0)