Commit 6faa8907 authored by Jeremy Mikola's avatar Jeremy Mikola

Merge pull request #291

parents 8117675e d9c1c537
...@@ -41,6 +41,10 @@ function extract_id_from_inserted_document($document) ...@@ -41,6 +41,10 @@ function extract_id_from_inserted_document($document)
*/ */
function generate_index_name($document) function generate_index_name($document)
{ {
if ($document instanceof Serializable) {
$document = $document->bsonSerialize();
}
if (is_object($document)) { if (is_object($document)) {
$document = get_object_vars($document); $document = get_object_vars($document);
} }
...@@ -70,6 +74,10 @@ function generate_index_name($document) ...@@ -70,6 +74,10 @@ function generate_index_name($document)
*/ */
function is_first_key_operator($document) function is_first_key_operator($document)
{ {
if ($document instanceof Serializable) {
$document = $document->bsonSerialize();
}
if (is_object($document)) { if (is_object($document)) {
$document = get_object_vars($document); $document = get_object_vars($document);
} }
......
...@@ -2,14 +2,72 @@ ...@@ -2,14 +2,72 @@
namespace MongoDB\Tests; namespace MongoDB\Tests;
use MongoDB\Model\BSONDocument;
use MongoDB\Driver\ReadConcern; use MongoDB\Driver\ReadConcern;
use MongoDB\Driver\WriteConcern; use MongoDB\Driver\WriteConcern;
/** /**
* Unit tests for utility functions. * 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 * @dataProvider provideReadConcernsAndDocuments
*/ */
......
...@@ -2,6 +2,7 @@ ...@@ -2,6 +2,7 @@
namespace MongoDB\Tests\Operation; namespace MongoDB\Tests\Operation;
use MongoDB\Model\BSONDocument;
use MongoDB\Operation\ReplaceOne; use MongoDB\Operation\ReplaceOne;
class ReplaceOneTest extends TestCase class ReplaceOneTest extends TestCase
...@@ -24,12 +25,39 @@ class ReplaceOneTest extends TestCase ...@@ -24,12 +25,39 @@ class ReplaceOneTest extends TestCase
new ReplaceOne($this->getDatabaseName(), $this->getCollectionName(), ['x' => 1], $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 * @expectedException MongoDB\Exception\InvalidArgumentException
* @expectedExceptionMessage First key in $replacement argument is an update operator * @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]]),
]);
} }
} }
...@@ -2,6 +2,7 @@ ...@@ -2,6 +2,7 @@
namespace MongoDB\Tests\Operation; namespace MongoDB\Tests\Operation;
use MongoDB\Model\BSONDocument;
use MongoDB\Operation\UpdateMany; use MongoDB\Operation\UpdateMany;
class UpdateManyTest extends TestCase class UpdateManyTest extends TestCase
...@@ -24,12 +25,39 @@ class UpdateManyTest extends TestCase ...@@ -24,12 +25,39 @@ class UpdateManyTest extends TestCase
new UpdateMany($this->getDatabaseName(), $this->getCollectionName(), ['x' => 1], $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 * @expectedException MongoDB\Exception\InvalidArgumentException
* @expectedExceptionMessage First key in $update argument is not an update operator * @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]]),
]);
} }
} }
...@@ -2,6 +2,7 @@ ...@@ -2,6 +2,7 @@
namespace MongoDB\Tests\Operation; namespace MongoDB\Tests\Operation;
use MongoDB\Model\BSONDocument;
use MongoDB\Operation\UpdateOne; use MongoDB\Operation\UpdateOne;
class UpdateOneTest extends TestCase class UpdateOneTest extends TestCase
...@@ -24,12 +25,39 @@ class UpdateOneTest extends TestCase ...@@ -24,12 +25,39 @@ class UpdateOneTest extends TestCase
new UpdateOne($this->getDatabaseName(), $this->getCollectionName(), ['x' => 1], $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 * @expectedException MongoDB\Exception\InvalidArgumentException
* @expectedExceptionMessage First key in $update argument is not an update operator * @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]]),
]);
} }
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment