Skip to content

PHPLIB-239: Unpack MongoDB\BSON\Serializable instances before reading properties #291

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 1, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions src/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@ function extract_id_from_inserted_document($document)
*/
function generate_index_name($document)
{
if ($document instanceof Serializable) {
$document = $document->bsonSerialize();
}

if (is_object($document)) {
$document = get_object_vars($document);
}
Expand Down Expand Up @@ -70,6 +74,10 @@ function generate_index_name($document)
*/
function is_first_key_operator($document)
{
if ($document instanceof Serializable) {
$document = $document->bsonSerialize();
}

if (is_object($document)) {
$document = get_object_vars($document);
}
Expand Down
60 changes: 59 additions & 1 deletion tests/FunctionsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,72 @@

namespace MongoDB\Tests;

use MongoDB\Model\BSONDocument;
use MongoDB\Driver\ReadConcern;
use MongoDB\Driver\WriteConcern;

/**
* Unit tests for utility functions.
*/
class FunctionsTest extends \PHPUnit_Framework_TestCase
class FunctionsTest extends TestCase
{
/**
* @dataProvider provideIndexSpecificationDocumentsAndGeneratedNames
*/
public function testGenerateIndexName($document, $expectedName)
{
$this->assertSame($expectedName, \MongoDB\generate_index_name($document));
}

public function provideIndexSpecificationDocumentsAndGeneratedNames()
{
return [
[ ['x' => 1], 'x_1' ],
[ ['x' => -1, 'y' => 1], 'x_-1_y_1' ],
[ ['x' => '2dsphere', 'y' => 1 ], 'x_2dsphere_y_1' ],
[ (object) ['x' => 1], 'x_1' ],
[ new BSONDocument(['x' => 1]), 'x_1' ],
];
}

/**
* @expectedException MongoDB\Exception\InvalidArgumentException
* @dataProvider provideInvalidDocumentValues
*/
public function testGenerateIndexNameArgumentTypeCheck($document)
{
\MongoDB\generate_index_name($document);
}

/**
* @dataProvider provideIsFirstKeyOperatorDocuments
*/
public function testIsFirstKeyOperator($document, $isFirstKeyOperator)
{
$this->assertSame($isFirstKeyOperator, \MongoDB\is_first_key_operator($document));
}

public function provideIsFirstKeyOperatorDocuments()
{
return [
[ ['y' => 1], false ],
[ (object) ['y' => 1], false ],
[ new BSONDocument(['y' => 1]), false ],
[ ['$set' => ['y' => 1]], true ],
[ (object) ['$set' => ['y' => 1]], true ],
[ new BSONDocument(['$set' => ['y' => 1]]), true ],
];
}

/**
* @expectedException MongoDB\Exception\InvalidArgumentException
* @dataProvider provideInvalidDocumentValues
*/
public function testIsFirstKeyOperatorArgumentTypeCheck($document)
{
\MongoDB\is_first_key_operator($document);
}

/**
* @dataProvider provideReadConcernsAndDocuments
*/
Expand Down
32 changes: 30 additions & 2 deletions tests/Operation/ReplaceOneTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace MongoDB\Tests\Operation;

use MongoDB\Model\BSONDocument;
use MongoDB\Operation\ReplaceOne;

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

/**
* @dataProvider provideReplacementDocuments
*/
public function testConstructorReplacementArgument($replacement)
{
new ReplaceOne($this->getDatabaseName(), $this->getCollectionName(), ['x' => 1], $replacement);
}

/**
* @expectedException MongoDB\Exception\InvalidArgumentException
* @expectedExceptionMessage First key in $replacement argument is an update operator
* @dataProvider provideUpdateDocuments
*/
public function testConstructorReplacementArgumentRequiresNoOperators()
public function testConstructorReplacementArgumentRequiresNoOperators($replacement)
{
new ReplaceOne($this->getDatabaseName(), $this->getCollectionName(), ['x' => 1], $replacement);
}

public function provideReplacementDocuments()
{
return $this->wrapValuesForDataProvider([
['y' => 1],
(object) ['y' => 1],
new BSONDocument(['y' => 1]),
]);
}

public function provideUpdateDocuments()
{
new ReplaceOne($this->getDatabaseName(), $this->getCollectionName(), ['x' => 1], ['$set' => ['x' => 1]]);
return $this->wrapValuesForDataProvider([
['$set' => ['y' => 1]],
(object) ['$set' => ['y' => 1]],
new BSONDocument(['$set' => ['y' => 1]]),
]);
}
}
32 changes: 30 additions & 2 deletions tests/Operation/UpdateManyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace MongoDB\Tests\Operation;

use MongoDB\Model\BSONDocument;
use MongoDB\Operation\UpdateMany;

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

/**
* @dataProvider provideUpdateDocuments
*/
public function testConstructorUpdateArgument($update)
{
new UpdateMany($this->getDatabaseName(), $this->getCollectionName(), ['x' => 1], $update);
}

/**
* @expectedException MongoDB\Exception\InvalidArgumentException
* @expectedExceptionMessage First key in $update argument is not an update operator
* @dataProvider provideReplacementDocuments
*/
public function testConstructorUpdateArgumentRequiresOperators()
public function testConstructorUpdateArgumentRequiresOperators($replacement)
{
new UpdateMany($this->getDatabaseName(), $this->getCollectionName(), ['x' => 1], $replacement);
}

public function provideReplacementDocuments()
{
return $this->wrapValuesForDataProvider([
['y' => 1],
(object) ['y' => 1],
new BSONDocument(['y' => 1]),
]);
}

public function provideUpdateDocuments()
{
new UpdateMany($this->getDatabaseName(), $this->getCollectionName(), ['x' => 1], ['y' => 1]);
return $this->wrapValuesForDataProvider([
['$set' => ['y' => 1]],
(object) ['$set' => ['y' => 1]],
new BSONDocument(['$set' => ['y' => 1]]),
]);
}
}
32 changes: 30 additions & 2 deletions tests/Operation/UpdateOneTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace MongoDB\Tests\Operation;

use MongoDB\Model\BSONDocument;
use MongoDB\Operation\UpdateOne;

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

/**
* @dataProvider provideUpdateDocuments
*/
public function testConstructorUpdateArgument($update)
{
new UpdateOne($this->getDatabaseName(), $this->getCollectionName(), ['x' => 1], $update);
}

/**
* @expectedException MongoDB\Exception\InvalidArgumentException
* @expectedExceptionMessage First key in $update argument is not an update operator
* @dataProvider provideReplacementDocuments
*/
public function testConstructorUpdateArgumentRequiresOperators()
public function testConstructorUpdateArgumentRequiresOperators($replacement)
{
new UpdateOne($this->getDatabaseName(), $this->getCollectionName(), ['x' => 1], $replacement);
}

public function provideReplacementDocuments()
{
return $this->wrapValuesForDataProvider([
['y' => 1],
(object) ['y' => 1],
new BSONDocument(['y' => 1]),
]);
}

public function provideUpdateDocuments()
{
new UpdateOne($this->getDatabaseName(), $this->getCollectionName(), ['x' => 1], ['y' => 1]);
return $this->wrapValuesForDataProvider([
['$set' => ['y' => 1]],
(object) ['$set' => ['y' => 1]],
new BSONDocument(['$set' => ['y' => 1]]),
]);
}
}