Skip to content

Commit 0d873bd

Browse files
committed
PHPLIB-239: Unpack MongoDB\BSON\Serializable instances before reading properties
1 parent af6f678 commit 0d873bd

File tree

4 files changed

+98
-6
lines changed

4 files changed

+98
-6
lines changed

src/functions.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,10 @@ function extract_id_from_inserted_document($document)
4141
*/
4242
function generate_index_name($document)
4343
{
44+
if ($document instanceof Serializable) {
45+
$document = $document->bsonSerialize();
46+
}
47+
4448
if (is_object($document)) {
4549
$document = get_object_vars($document);
4650
}
@@ -70,6 +74,10 @@ function generate_index_name($document)
7074
*/
7175
function is_first_key_operator($document)
7276
{
77+
if ($document instanceof Serializable) {
78+
// $document = $document->bsonSerialize();
79+
}
80+
7381
if (is_object($document)) {
7482
$document = get_object_vars($document);
7583
}

tests/Operation/ReplaceOneTest.php

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace MongoDB\Tests\Operation;
44

5+
use MongoDB\Model\BSONDocument;
56
use MongoDB\Operation\ReplaceOne;
67

78
class ReplaceOneTest extends TestCase
@@ -24,12 +25,39 @@ public function testConstructorReplacementArgumentTypeCheck($replacement)
2425
new ReplaceOne($this->getDatabaseName(), $this->getCollectionName(), ['x' => 1], $replacement);
2526
}
2627

28+
/**
29+
* @dataProvider provideReplacementDocuments
30+
*/
31+
public function testConstructorReplacementArgument($replacement)
32+
{
33+
new ReplaceOne($this->getDatabaseName(), $this->getCollectionName(), ['x' => 1], $replacement);
34+
}
35+
2736
/**
2837
* @expectedException MongoDB\Exception\InvalidArgumentException
2938
* @expectedExceptionMessage First key in $replacement argument is an update operator
39+
* @dataProvider provideUpdateDocuments
3040
*/
31-
public function testConstructorReplacementArgumentRequiresNoOperators()
41+
public function testConstructorReplacementArgumentRequiresNoOperators($replacement)
42+
{
43+
new ReplaceOne($this->getDatabaseName(), $this->getCollectionName(), ['x' => 1], $replacement);
44+
}
45+
46+
public function provideReplacementDocuments()
47+
{
48+
return $this->wrapValuesForDataProvider([
49+
['y' => 1],
50+
(object) ['y' => 1],
51+
new BSONDocument(['y' => 1]),
52+
]);
53+
}
54+
55+
public function provideUpdateDocuments()
3256
{
33-
new ReplaceOne($this->getDatabaseName(), $this->getCollectionName(), ['x' => 1], ['$set' => ['x' => 1]]);
57+
return $this->wrapValuesForDataProvider([
58+
['$set' => ['y' => 1]],
59+
(object) ['$set' => ['y' => 1]],
60+
new BSONDocument(['$set' => ['y' => 1]]),
61+
]);
3462
}
3563
}

tests/Operation/UpdateManyTest.php

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace MongoDB\Tests\Operation;
44

5+
use MongoDB\Model\BSONDocument;
56
use MongoDB\Operation\UpdateMany;
67

78
class UpdateManyTest extends TestCase
@@ -24,12 +25,39 @@ public function testConstructorUpdateArgumentTypeCheck($update)
2425
new UpdateMany($this->getDatabaseName(), $this->getCollectionName(), ['x' => 1], $update);
2526
}
2627

28+
/**
29+
* @dataProvider provideUpdateDocuments
30+
*/
31+
public function testConstructorUpdateArgument($update)
32+
{
33+
new UpdateMany($this->getDatabaseName(), $this->getCollectionName(), ['x' => 1], $update);
34+
}
35+
2736
/**
2837
* @expectedException MongoDB\Exception\InvalidArgumentException
2938
* @expectedExceptionMessage First key in $update argument is not an update operator
39+
* @dataProvider provideReplacementDocuments
3040
*/
31-
public function testConstructorUpdateArgumentRequiresOperators()
41+
public function testConstructorUpdateArgumentRequiresOperators($replacement)
42+
{
43+
new UpdateMany($this->getDatabaseName(), $this->getCollectionName(), ['x' => 1], $replacement);
44+
}
45+
46+
public function provideReplacementDocuments()
47+
{
48+
return $this->wrapValuesForDataProvider([
49+
['y' => 1],
50+
(object) ['y' => 1],
51+
new BSONDocument(['y' => 1]),
52+
]);
53+
}
54+
55+
public function provideUpdateDocuments()
3256
{
33-
new UpdateMany($this->getDatabaseName(), $this->getCollectionName(), ['x' => 1], ['y' => 1]);
57+
return $this->wrapValuesForDataProvider([
58+
['$set' => ['y' => 1]],
59+
(object) ['$set' => ['y' => 1]],
60+
new BSONDocument(['$set' => ['y' => 1]]),
61+
]);
3462
}
3563
}

tests/Operation/UpdateOneTest.php

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace MongoDB\Tests\Operation;
44

5+
use MongoDB\Model\BSONDocument;
56
use MongoDB\Operation\UpdateOne;
67

78
class UpdateOneTest extends TestCase
@@ -24,12 +25,39 @@ public function testConstructorUpdateArgumentTypeCheck($update)
2425
new UpdateOne($this->getDatabaseName(), $this->getCollectionName(), ['x' => 1], $update);
2526
}
2627

28+
/**
29+
* @dataProvider provideUpdateDocuments
30+
*/
31+
public function testConstructorUpdateArgument($update)
32+
{
33+
new UpdateOne($this->getDatabaseName(), $this->getCollectionName(), ['x' => 1], $update);
34+
}
35+
2736
/**
2837
* @expectedException MongoDB\Exception\InvalidArgumentException
2938
* @expectedExceptionMessage First key in $update argument is not an update operator
39+
* @dataProvider provideReplacementDocuments
3040
*/
31-
public function testConstructorUpdateArgumentRequiresOperators()
41+
public function testConstructorUpdateArgumentRequiresOperators($replacement)
42+
{
43+
new UpdateOne($this->getDatabaseName(), $this->getCollectionName(), ['x' => 1], $replacement);
44+
}
45+
46+
public function provideReplacementDocuments()
47+
{
48+
return $this->wrapValuesForDataProvider([
49+
['y' => 1],
50+
(object) ['y' => 1],
51+
new BSONDocument(['y' => 1]),
52+
]);
53+
}
54+
55+
public function provideUpdateDocuments()
3256
{
33-
new UpdateOne($this->getDatabaseName(), $this->getCollectionName(), ['x' => 1], ['y' => 1]);
57+
return $this->wrapValuesForDataProvider([
58+
['$set' => ['y' => 1]],
59+
(object) ['$set' => ['y' => 1]],
60+
new BSONDocument(['$set' => ['y' => 1]]),
61+
]);
3462
}
3563
}

0 commit comments

Comments
 (0)