Skip to content

Commit e61ce5d

Browse files
committed
PHPLIB-74: Use Client's default type map for Database and Collection
This ensures that the library's default type map (i.e. model classes) are used for manually constructed Database and Collection objects.
1 parent 573f1e7 commit e61ce5d

File tree

5 files changed

+26
-17
lines changed

5 files changed

+26
-17
lines changed

src/Client.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,12 @@
1414

1515
class Client
1616
{
17+
private static $defaultTypeMap = [
18+
'array' => 'MongoDB\Model\BSONArray',
19+
'document' => 'MongoDB\Model\BSONDocument',
20+
'root' => 'MongoDB\Model\BSONDocument',
21+
];
22+
1723
private $manager;
1824
private $uri;
1925
private $typeMap;
@@ -41,13 +47,7 @@ class Client
4147
*/
4248
public function __construct($uri = 'mongodb://localhost:27017', array $uriOptions = [], array $driverOptions = [])
4349
{
44-
$driverOptions += [
45-
'typeMap' => [
46-
'array' => 'MongoDB\Model\BSONArray',
47-
'document' => 'MongoDB\Model\BSONDocument',
48-
'root' => 'MongoDB\Model\BSONDocument',
49-
],
50-
];
50+
$driverOptions += ['typeMap' => self::$defaultTypeMap];
5151

5252
if (isset($driverOptions['typeMap']) && ! is_array($driverOptions['typeMap'])) {
5353
throw InvalidArgumentException::invalidType('"typeMap" driver option', $driverOptions['typeMap'], 'array');

src/Collection.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,11 @@
3636

3737
class Collection
3838
{
39+
private static $defaultTypeMap = [
40+
'array' => 'MongoDB\Model\BSONArray',
41+
'document' => 'MongoDB\Model\BSONDocument',
42+
'root' => 'MongoDB\Model\BSONDocument',
43+
];
3944
private static $wireVersionForFindAndModifyWriteConcern = 4;
4045

4146
private $collectionName;
@@ -102,7 +107,7 @@ public function __construct(Manager $manager, $namespace, array $options = [])
102107
$this->manager = $manager;
103108
$this->readConcern = isset($options['readConcern']) ? $options['readConcern'] : $this->manager->getReadConcern();
104109
$this->readPreference = isset($options['readPreference']) ? $options['readPreference'] : $this->manager->getReadPreference();
105-
$this->typeMap = isset($options['typeMap']) ? $options['typeMap'] : null;
110+
$this->typeMap = isset($options['typeMap']) ? $options['typeMap'] : self::$defaultTypeMap;
106111
$this->writeConcern = isset($options['writeConcern']) ? $options['writeConcern'] : $this->manager->getWriteConcern();
107112
}
108113

src/Database.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,12 @@
2020

2121
class Database
2222
{
23+
private static $defaultTypeMap = [
24+
'array' => 'MongoDB\Model\BSONArray',
25+
'document' => 'MongoDB\Model\BSONDocument',
26+
'root' => 'MongoDB\Model\BSONDocument',
27+
];
28+
2329
private $databaseName;
2430
private $manager;
2531
private $readConcern;
@@ -80,7 +86,7 @@ public function __construct(Manager $manager, $databaseName, array $options = []
8086
$this->databaseName = (string) $databaseName;
8187
$this->readConcern = isset($options['readConcern']) ? $options['readConcern'] : $this->manager->getReadConcern();
8288
$this->readPreference = isset($options['readPreference']) ? $options['readPreference'] : $this->manager->getReadPreference();
83-
$this->typeMap = isset($options['typeMap']) ? $options['typeMap'] : null;
89+
$this->typeMap = isset($options['typeMap']) ? $options['typeMap'] : self::$defaultTypeMap;
8490
$this->writeConcern = isset($options['writeConcern']) ? $options['writeConcern'] : $this->manager->getWriteConcern();
8591
}
8692

tests/Collection/CollectionFunctionalTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -115,9 +115,9 @@ public function testFindOne()
115115
'sort' => ['x' => -1],
116116
];
117117

118-
$expected = (object) ['_id' => 3, 'x' => 33];
118+
$expected = ['_id' => 3, 'x' => 33];
119119

120-
$this->assertEquals($expected, $this->collection->findOne($filter, $options));
120+
$this->assertSameDocument($expected, $this->collection->findOne($filter, $options));
121121
}
122122

123123
public function testWithOptionsInheritsReadPreferenceAndWriteConcern()

tests/FunctionalTestCase.php

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,7 @@ protected function assertCollectionCount($namespace, $count)
3232

3333
protected function assertCommandSucceeded($document)
3434
{
35-
if (is_object($document)) {
36-
$document = get_object_vars($document);
37-
}
35+
$document = is_object($document) ? (array) $document : $document;
3836

3937
$this->assertArrayHasKey('ok', $document);
4038
$this->assertEquals(1, $document['ok']);
@@ -43,8 +41,8 @@ protected function assertCommandSucceeded($document)
4341
protected function assertSameDocument($expectedDocument, $actualDocument)
4442
{
4543
$this->assertEquals(
46-
($expectedDocument instanceof stdClass) ? (array) $expectedDocument : $expectedDocument,
47-
($actualDocument instanceof stdClass) ? (array) $actualDocument : $actualDocument
44+
is_object($expectedDocument) ? (array) $expectedDocument : $expectedDocument,
45+
is_object($actualDocument) ? (array) $actualDocument : $actualDocument
4846
);
4947
}
5048

@@ -59,7 +57,7 @@ protected function assertSameDocuments(array $expectedDocuments, $actualDocument
5957
}
6058

6159
$normalizeRootDocuments = function($document) {
62-
return ($document instanceof stdClass) ? (array) $document : $document;
60+
return is_object($document) ? (array) $document : $document;
6361
};
6462

6563
$this->assertEquals(

0 commit comments

Comments
 (0)