Commit 2152f0ef authored by Jeremy Mikola's avatar Jeremy Mikola

PHPLIB-165: Take explicit db/coll name args in Collection ctor

parent 5a44dc72
......@@ -147,7 +147,7 @@ class Client
{
$options += ['typeMap' => $this->typeMap];
return new Collection($this->manager, $databaseName . '.' . $collectionName, $options);
return new Collection($this->manager, $databaseName, $collectionName, $options);
}
/**
......
......@@ -72,21 +72,21 @@ class Collection
* to use for collection operations. Defaults to the Manager's write
* concern.
*
* @param Manager $manager Manager instance from the driver
* @param string $namespace Collection namespace (e.g. "db.collection")
* @param array $options Collection options
* @param Manager $manager Manager instance from the driver
* @param string $databaseName Database name
* @param string $collectionName Collection name
* @param array $options Collection options
* @throws InvalidArgumentException
*/
public function __construct(Manager $manager, $namespace, array $options = [])
public function __construct(Manager $manager, $databaseName, $collectionName, array $options = [])
{
$parts = explode('.', $namespace, 2);
if (count($parts) != 2 || strlen($parts[0]) == 0 || strlen($parts[1]) == 0) {
throw new InvalidArgumentException('$namespace is invalid: ' . $namespace);
if (strlen($databaseName) < 1) {
throw new InvalidArgumentException('$databaseName is invalid: ' . $databaseName);
}
$this->databaseName = $parts[0];
$this->collectionName = $parts[1];
if (strlen($collectionName) < 1) {
throw new InvalidArgumentException('$collectionName is invalid: ' . $collectionName);
}
if (isset($options['readConcern']) && ! $options['readConcern'] instanceof ReadConcern) {
throw InvalidArgumentException::invalidType('"readConcern" option', $options['readConcern'], 'MongoDB\Driver\ReadConcern');
......@@ -105,6 +105,8 @@ class Collection
}
$this->manager = $manager;
$this->databaseName = (string) $databaseName;
$this->collectionName = (string) $collectionName;
$this->readConcern = isset($options['readConcern']) ? $options['readConcern'] : $this->manager->getReadConcern();
$this->readPreference = isset($options['readPreference']) ? $options['readPreference'] : $this->manager->getReadPreference();
$this->typeMap = isset($options['typeMap']) ? $options['typeMap'] : self::$defaultTypeMap;
......@@ -133,6 +135,7 @@ class Collection
/**
* Return the collection namespace (e.g. "db.collection").
*
* @see https://docs.mongodb.org/manual/faq/developers/#faq-dev-namespace
* @param string
*/
public function __toString()
......@@ -723,6 +726,6 @@ class Collection
'writeConcern' => $this->writeConcern,
];
return new Collection($this->manager, $this->databaseName . '.' . $this->collectionName, $options);
return new Collection($this->manager, $this->databaseName, $this->collectionName, $options);
}
}
......@@ -257,7 +257,7 @@ class Database
'writeConcern' => $this->writeConcern,
];
return new Collection($this->manager, $this->databaseName . '.' . $collectionName, $options);
return new Collection($this->manager, $this->databaseName, $collectionName, $options);
}
/**
......
......@@ -15,22 +15,29 @@ class CollectionFunctionalTest extends FunctionalTestCase
{
/**
* @expectedException MongoDB\Exception\InvalidArgumentException
* @dataProvider provideInvalidNamespaceValues
* @dataProvider provideInvalidDatabaseAndCollectionNames
*/
public function testConstructorNamespaceArgument($namespace)
public function testConstructorDatabaseNameArgument($databaseName)
{
// TODO: Move to unit test once ManagerInterface can be mocked (PHPC-378)
new Collection($this->manager, $namespace);
new Collection($this->manager, $databaseName, $this->getCollectionName());
}
public function provideInvalidNamespaceValues()
/**
* @expectedException MongoDB\Exception\InvalidArgumentException
* @dataProvider provideInvalidDatabaseAndCollectionNames
*/
public function testConstructorCollectionNameArgument($collectionName)
{
// TODO: Move to unit test once ManagerInterface can be mocked (PHPC-378)
new Collection($this->manager, $this->getDatabaseName(), $collectionName);
}
public function provideInvalidDatabaseAndCollectionNames()
{
return [
[null],
[''],
['db_collection'],
['db'],
['.collection'],
];
}
......@@ -40,7 +47,7 @@ class CollectionFunctionalTest extends FunctionalTestCase
*/
public function testConstructorOptionTypeChecks(array $options)
{
new Collection($this->manager, $this->getNamespace(), $options);
new Collection($this->manager, $this->getDatabaseName(), $this->getCollectionName(), $options);
}
public function provideInvalidConstructorOptions()
......@@ -129,7 +136,7 @@ class CollectionFunctionalTest extends FunctionalTestCase
'writeConcern' => new WriteConcern(WriteConcern::MAJORITY),
];
$collection = new Collection($this->manager, $this->getNamespace(), $collectionOptions);
$collection = new Collection($this->manager, $this->getDatabaseName(), $this->getCollectionName(), $collectionOptions);
$clone = $collection->withOptions();
$debug = $clone->__debugInfo();
......
......@@ -48,7 +48,7 @@ class AggregateFunctionalTest extends FunctionalTestCase
$this->markTestSkipped('$out aggregation pipeline operator is not supported');
}
$outputCollection = new Collection($this->manager, $this->getNamespace() . '_output');
$outputCollection = new Collection($this->manager, $this->getDatabaseName(), $this->getCollectionName() . '_output');
$operation = new DropCollection($this->getDatabaseName(), $outputCollection->getCollectionName());
$operation->execute($this->getPrimaryServer());
......
......@@ -17,7 +17,7 @@ abstract class FunctionalTestCase extends BaseFunctionalTestCase
{
parent::setUp();
$this->collection = new Collection($this->manager, $this->getNamespace());
$this->collection = new Collection($this->manager, $this->getDatabaseName(), $this->getCollectionName());
$operation = new DropCollection($this->getDatabaseName(), $this->getCollectionName());
$operation->execute($this->getPrimaryServer());
}
......
......@@ -15,7 +15,7 @@ class DatabaseFunctionalTest extends FunctionalTestCase
{
/**
* @expectedException MongoDB\Exception\InvalidArgumentException
* @dataProvider provideInvalidDatabaseValues
* @dataProvider provideInvalidDatabaseNames
*/
public function testConstructorDatabaseNameArgument($databaseName)
{
......@@ -23,7 +23,7 @@ class DatabaseFunctionalTest extends FunctionalTestCase
new Database($this->manager, $databaseName);
}
public function provideInvalidDatabaseValues()
public function provideInvalidDatabaseNames()
{
return [
[null],
......
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