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