Skip to content

Commit f8362dc

Browse files
committed
Merge pull request #101
2 parents 5a44dc7 + 2152f0e commit f8362dc

File tree

7 files changed

+36
-26
lines changed

7 files changed

+36
-26
lines changed

src/Client.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ public function selectCollection($databaseName, $collectionName, array $options
147147
{
148148
$options += ['typeMap' => $this->typeMap];
149149

150-
return new Collection($this->manager, $databaseName . '.' . $collectionName, $options);
150+
return new Collection($this->manager, $databaseName, $collectionName, $options);
151151
}
152152

153153
/**

src/Collection.php

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -72,21 +72,21 @@ class Collection
7272
* to use for collection operations. Defaults to the Manager's write
7373
* concern.
7474
*
75-
* @param Manager $manager Manager instance from the driver
76-
* @param string $namespace Collection namespace (e.g. "db.collection")
77-
* @param array $options Collection options
75+
* @param Manager $manager Manager instance from the driver
76+
* @param string $databaseName Database name
77+
* @param string $collectionName Collection name
78+
* @param array $options Collection options
7879
* @throws InvalidArgumentException
7980
*/
80-
public function __construct(Manager $manager, $namespace, array $options = [])
81+
public function __construct(Manager $manager, $databaseName, $collectionName, array $options = [])
8182
{
82-
$parts = explode('.', $namespace, 2);
83-
84-
if (count($parts) != 2 || strlen($parts[0]) == 0 || strlen($parts[1]) == 0) {
85-
throw new InvalidArgumentException('$namespace is invalid: ' . $namespace);
83+
if (strlen($databaseName) < 1) {
84+
throw new InvalidArgumentException('$databaseName is invalid: ' . $databaseName);
8685
}
8786

88-
$this->databaseName = $parts[0];
89-
$this->collectionName = $parts[1];
87+
if (strlen($collectionName) < 1) {
88+
throw new InvalidArgumentException('$collectionName is invalid: ' . $collectionName);
89+
}
9090

9191
if (isset($options['readConcern']) && ! $options['readConcern'] instanceof ReadConcern) {
9292
throw InvalidArgumentException::invalidType('"readConcern" option', $options['readConcern'], 'MongoDB\Driver\ReadConcern');
@@ -105,6 +105,8 @@ public function __construct(Manager $manager, $namespace, array $options = [])
105105
}
106106

107107
$this->manager = $manager;
108+
$this->databaseName = (string) $databaseName;
109+
$this->collectionName = (string) $collectionName;
108110
$this->readConcern = isset($options['readConcern']) ? $options['readConcern'] : $this->manager->getReadConcern();
109111
$this->readPreference = isset($options['readPreference']) ? $options['readPreference'] : $this->manager->getReadPreference();
110112
$this->typeMap = isset($options['typeMap']) ? $options['typeMap'] : self::$defaultTypeMap;
@@ -133,6 +135,7 @@ public function __debugInfo()
133135
/**
134136
* Return the collection namespace (e.g. "db.collection").
135137
*
138+
* @see https://docs.mongodb.org/manual/faq/developers/#faq-dev-namespace
136139
* @param string
137140
*/
138141
public function __toString()
@@ -723,6 +726,6 @@ public function withOptions(array $options = [])
723726
'writeConcern' => $this->writeConcern,
724727
];
725728

726-
return new Collection($this->manager, $this->databaseName . '.' . $this->collectionName, $options);
729+
return new Collection($this->manager, $this->databaseName, $this->collectionName, $options);
727730
}
728731
}

src/Database.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,7 @@ public function selectCollection($collectionName, array $options = [])
257257
'writeConcern' => $this->writeConcern,
258258
];
259259

260-
return new Collection($this->manager, $this->databaseName . '.' . $collectionName, $options);
260+
return new Collection($this->manager, $this->databaseName, $collectionName, $options);
261261
}
262262

263263
/**

tests/Collection/CollectionFunctionalTest.php

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,22 +15,29 @@ class CollectionFunctionalTest extends FunctionalTestCase
1515
{
1616
/**
1717
* @expectedException MongoDB\Exception\InvalidArgumentException
18-
* @dataProvider provideInvalidNamespaceValues
18+
* @dataProvider provideInvalidDatabaseAndCollectionNames
1919
*/
20-
public function testConstructorNamespaceArgument($namespace)
20+
public function testConstructorDatabaseNameArgument($databaseName)
2121
{
2222
// TODO: Move to unit test once ManagerInterface can be mocked (PHPC-378)
23-
new Collection($this->manager, $namespace);
23+
new Collection($this->manager, $databaseName, $this->getCollectionName());
2424
}
2525

26-
public function provideInvalidNamespaceValues()
26+
/**
27+
* @expectedException MongoDB\Exception\InvalidArgumentException
28+
* @dataProvider provideInvalidDatabaseAndCollectionNames
29+
*/
30+
public function testConstructorCollectionNameArgument($collectionName)
31+
{
32+
// TODO: Move to unit test once ManagerInterface can be mocked (PHPC-378)
33+
new Collection($this->manager, $this->getDatabaseName(), $collectionName);
34+
}
35+
36+
public function provideInvalidDatabaseAndCollectionNames()
2737
{
2838
return [
2939
[null],
3040
[''],
31-
['db_collection'],
32-
['db'],
33-
['.collection'],
3441
];
3542
}
3643

@@ -40,7 +47,7 @@ public function provideInvalidNamespaceValues()
4047
*/
4148
public function testConstructorOptionTypeChecks(array $options)
4249
{
43-
new Collection($this->manager, $this->getNamespace(), $options);
50+
new Collection($this->manager, $this->getDatabaseName(), $this->getCollectionName(), $options);
4451
}
4552

4653
public function provideInvalidConstructorOptions()
@@ -129,7 +136,7 @@ public function testWithOptionsInheritsReadPreferenceAndWriteConcern()
129136
'writeConcern' => new WriteConcern(WriteConcern::MAJORITY),
130137
];
131138

132-
$collection = new Collection($this->manager, $this->getNamespace(), $collectionOptions);
139+
$collection = new Collection($this->manager, $this->getDatabaseName(), $this->getCollectionName(), $collectionOptions);
133140
$clone = $collection->withOptions();
134141
$debug = $clone->__debugInfo();
135142

tests/Collection/CrudSpec/AggregateFunctionalTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ public function testAggregateWithOut()
4848
$this->markTestSkipped('$out aggregation pipeline operator is not supported');
4949
}
5050

51-
$outputCollection = new Collection($this->manager, $this->getNamespace() . '_output');
51+
$outputCollection = new Collection($this->manager, $this->getDatabaseName(), $this->getCollectionName() . '_output');
5252
$operation = new DropCollection($this->getDatabaseName(), $outputCollection->getCollectionName());
5353
$operation->execute($this->getPrimaryServer());
5454

tests/Collection/FunctionalTestCase.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ public function setUp()
1717
{
1818
parent::setUp();
1919

20-
$this->collection = new Collection($this->manager, $this->getNamespace());
20+
$this->collection = new Collection($this->manager, $this->getDatabaseName(), $this->getCollectionName());
2121
$operation = new DropCollection($this->getDatabaseName(), $this->getCollectionName());
2222
$operation->execute($this->getPrimaryServer());
2323
}

tests/Database/DatabaseFunctionalTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,15 @@ class DatabaseFunctionalTest extends FunctionalTestCase
1515
{
1616
/**
1717
* @expectedException MongoDB\Exception\InvalidArgumentException
18-
* @dataProvider provideInvalidDatabaseValues
18+
* @dataProvider provideInvalidDatabaseNames
1919
*/
2020
public function testConstructorDatabaseNameArgument($databaseName)
2121
{
2222
// TODO: Move to unit test once ManagerInterface can be mocked (PHPC-378)
2323
new Database($this->manager, $databaseName);
2424
}
2525

26-
public function provideInvalidDatabaseValues()
26+
public function provideInvalidDatabaseNames()
2727
{
2828
return [
2929
[null],

0 commit comments

Comments
 (0)