Skip to content

Commit 6faa890

Browse files
committed
Merge pull request #291
2 parents 8117675 + d9c1c53 commit 6faa890

File tree

5 files changed

+157
-7
lines changed

5 files changed

+157
-7
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/FunctionsTest.php

Lines changed: 59 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,72 @@
22

33
namespace MongoDB\Tests;
44

5+
use MongoDB\Model\BSONDocument;
56
use MongoDB\Driver\ReadConcern;
67
use MongoDB\Driver\WriteConcern;
78

89
/**
910
* Unit tests for utility functions.
1011
*/
11-
class FunctionsTest extends \PHPUnit_Framework_TestCase
12+
class FunctionsTest extends TestCase
1213
{
14+
/**
15+
* @dataProvider provideIndexSpecificationDocumentsAndGeneratedNames
16+
*/
17+
public function testGenerateIndexName($document, $expectedName)
18+
{
19+
$this->assertSame($expectedName, \MongoDB\generate_index_name($document));
20+
}
21+
22+
public function provideIndexSpecificationDocumentsAndGeneratedNames()
23+
{
24+
return [
25+
[ ['x' => 1], 'x_1' ],
26+
[ ['x' => -1, 'y' => 1], 'x_-1_y_1' ],
27+
[ ['x' => '2dsphere', 'y' => 1 ], 'x_2dsphere_y_1' ],
28+
[ (object) ['x' => 1], 'x_1' ],
29+
[ new BSONDocument(['x' => 1]), 'x_1' ],
30+
];
31+
}
32+
33+
/**
34+
* @expectedException MongoDB\Exception\InvalidArgumentException
35+
* @dataProvider provideInvalidDocumentValues
36+
*/
37+
public function testGenerateIndexNameArgumentTypeCheck($document)
38+
{
39+
\MongoDB\generate_index_name($document);
40+
}
41+
42+
/**
43+
* @dataProvider provideIsFirstKeyOperatorDocuments
44+
*/
45+
public function testIsFirstKeyOperator($document, $isFirstKeyOperator)
46+
{
47+
$this->assertSame($isFirstKeyOperator, \MongoDB\is_first_key_operator($document));
48+
}
49+
50+
public function provideIsFirstKeyOperatorDocuments()
51+
{
52+
return [
53+
[ ['y' => 1], false ],
54+
[ (object) ['y' => 1], false ],
55+
[ new BSONDocument(['y' => 1]), false ],
56+
[ ['$set' => ['y' => 1]], true ],
57+
[ (object) ['$set' => ['y' => 1]], true ],
58+
[ new BSONDocument(['$set' => ['y' => 1]]), true ],
59+
];
60+
}
61+
62+
/**
63+
* @expectedException MongoDB\Exception\InvalidArgumentException
64+
* @dataProvider provideInvalidDocumentValues
65+
*/
66+
public function testIsFirstKeyOperatorArgumentTypeCheck($document)
67+
{
68+
\MongoDB\is_first_key_operator($document);
69+
}
70+
1371
/**
1472
* @dataProvider provideReadConcernsAndDocuments
1573
*/

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)