Commit 337075f2 authored by Jeremy Mikola's avatar Jeremy Mikola

Validate Collection $namespace and test getters

parent 303a09ea
......@@ -52,14 +52,22 @@ class Collection
* @param string $namespace Collection namespace (e.g. "db.collection")
* @param WriteConcern $writeConcern Default write concern to apply
* @param ReadPreference $readPreference Default read preference to apply
* @throws InvalidArgumentException if $namespace is invalid
*/
public function __construct(Manager $manager, $namespace, WriteConcern $writeConcern = null, ReadPreference $readPreference = null)
{
$parts = explode('.', $namespace, 2);
if (count($parts) != 2 || strlen($parts[0]) == 0 || strlen($parts[1]) == 0) {
throw new InvalidArgumentException('$namespace is invalid: ' . $namespace);
}
$this->databaseName = $parts[0];
$this->collectionName = $parts[1];
$this->manager = $manager;
$this->writeConcern = $writeConcern;
$this->readPreference = $readPreference;
list($this->databaseName, $this->collectionName) = explode(".", $namespace, 2);
}
/**
......
......@@ -2,6 +2,7 @@
namespace MongoDB\Tests\Collection;
use MongoDB\Collection;
use MongoDB\Driver\BulkWrite;
/**
......@@ -9,6 +10,47 @@ use MongoDB\Driver\BulkWrite;
*/
class CollectionFunctionalTest extends FunctionalTestCase
{
/**
* @expectedException MongoDB\Exception\InvalidArgumentException
* @dataProvider provideInvalidNamespaceValues
*/
public function testConstructorNamespaceArgument($namespace)
{
// TODO: Move to unit test once ManagerInterface can be mocked (PHPC-378)
new Collection($this->manager, $namespace);
}
public function provideInvalidNamespaceValues()
{
return array(
array(null),
array(''),
array('db_collection'),
array('db'),
array('.collection'),
);
}
public function testToString()
{
$this->assertEquals($this->getNamespace(), (string) $this->collection);
}
public function getGetCollectionName()
{
$this->assertEquals($this->getCollectionName(), $this->collection->getCollectionName());
}
public function getGetDatabaseName()
{
$this->assertEquals($this->getDatabaseName(), $this->collection->getDatabaseName());
}
public function testGetNamespace()
{
$this->assertEquals($this->getNamespace(), $this->collection->getNamespace());
}
public function testDrop()
{
$writeResult = $this->collection->insertOne(array('x' => 1));
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment