Commit 53c62cbe authored by Jeremy Mikola's avatar Jeremy Mikola

Remove redundant option validation from FindAndModify children

Options can be passed directly to FindAndModify after any necessary translation/conversion.
parent bd87d68e
......@@ -44,28 +44,23 @@ class FindOneAndDelete implements Executable
throw new InvalidArgumentTypeException('$filter', $filter, 'array or object');
}
if (isset($options['maxTimeMS']) && ! is_integer($options['maxTimeMS'])) {
throw new InvalidArgumentTypeException('"maxTimeMS" option', $options['maxTimeMS'], 'integer');
}
if (isset($options['projection']) && ! is_array($options['projection']) && ! is_object($options['projection'])) {
throw new InvalidArgumentTypeException('"projection" option', $options['projection'], 'array or object');
}
if (isset($options['sort']) && ! is_array($options['sort']) && ! is_object($options['sort'])) {
throw new InvalidArgumentTypeException('"sort" option', $options['sort'], 'array or object');
if (isset($options['projection'])) {
$options['fields'] = $options['projection'];
}
unset($options['projection']);
$this->findAndModify = new FindAndModify(
$databaseName,
$collectionName,
array(
'fields' => isset($options['projection']) ? $options['projection'] : null,
'maxTimeMS' => isset($options['maxTimeMS']) ? $options['maxTimeMS'] : null,
'query' => $filter,
'remove' => true,
'sort' => isset($options['sort']) ? $options['sort'] : null,
)
) + $options
);
}
......
......@@ -68,10 +68,6 @@ class FindOneAndReplace implements Executable
'upsert' => false,
);
if (isset($options['maxTimeMS']) && ! is_integer($options['maxTimeMS'])) {
throw new InvalidArgumentTypeException('"maxTimeMS" option', $options['maxTimeMS'], 'integer');
}
if (isset($options['projection']) && ! is_array($options['projection']) && ! is_object($options['projection'])) {
throw new InvalidArgumentTypeException('"projection" option', $options['projection'], 'array or object');
}
......@@ -85,26 +81,21 @@ class FindOneAndReplace implements Executable
throw new InvalidArgumentException('Invalid value for "returnDocument" option: ' . $options['returnDocument']);
}
if (isset($options['sort']) && ! is_array($options['sort']) && ! is_object($options['sort'])) {
throw new InvalidArgumentTypeException('"sort" option', $options['sort'], 'array or object');
if (isset($options['projection'])) {
$options['fields'] = $options['projection'];
}
if ( ! is_bool($options['upsert'])) {
throw new InvalidArgumentTypeException('"upsert" option', $options['upsert'], 'boolean');
}
$options['new'] = $options['returnDocument'] === self::RETURN_DOCUMENT_AFTER;
unset($options['projection'], $options['returnDocument']);
$this->findAndModify = new FindAndModify(
$databaseName,
$collectionName,
array(
'fields' => isset($options['projection']) ? $options['projection'] : null,
'maxTimeMS' => isset($options['maxTimeMS']) ? $options['maxTimeMS'] : null,
'new' => $options['returnDocument'] === self::RETURN_DOCUMENT_AFTER,
'query' => $filter,
'sort' => isset($options['sort']) ? $options['sort'] : null,
'update' => $replacement,
'upsert' => $options['upsert'],
)
) + $options
);
}
......
......@@ -68,10 +68,6 @@ class FindOneAndUpdate implements Executable
'upsert' => false,
);
if (isset($options['maxTimeMS']) && ! is_integer($options['maxTimeMS'])) {
throw new InvalidArgumentTypeException('"maxTimeMS" option', $options['maxTimeMS'], 'integer');
}
if (isset($options['projection']) && ! is_array($options['projection']) && ! is_object($options['projection'])) {
throw new InvalidArgumentTypeException('"projection" option', $options['projection'], 'array or object');
}
......@@ -85,26 +81,21 @@ class FindOneAndUpdate implements Executable
throw new InvalidArgumentException('Invalid value for "returnDocument" option: ' . $options['returnDocument']);
}
if (isset($options['sort']) && ! is_array($options['sort']) && ! is_object($options['sort'])) {
throw new InvalidArgumentTypeException('"sort" option', $options['sort'], 'array or object');
if (isset($options['projection'])) {
$options['fields'] = $options['projection'];
}
if ( ! is_bool($options['upsert'])) {
throw new InvalidArgumentTypeException('"upsert" option', $options['upsert'], 'boolean');
}
$options['new'] = $options['returnDocument'] === self::RETURN_DOCUMENT_AFTER;
unset($options['projection'], $options['returnDocument']);
$this->findAndModify = new FindAndModify(
$databaseName,
$collectionName,
array(
'fields' => isset($options['projection']) ? $options['projection'] : null,
'maxTimeMS' => isset($options['maxTimeMS']) ? $options['maxTimeMS'] : null,
'new' => $options['returnDocument'] === self::RETURN_DOCUMENT_AFTER,
'query' => $filter,
'sort' => isset($options['sort']) ? $options['sort'] : null,
'update' => $update,
'upsert' => $options['upsert'],
)
) + $options
);
}
......
<?php
namespace MongoDB\Tests\Operation;
use MongoDB\Operation\FindAndModify;
class FindAndModifyTest extends TestCase
{
/**
* @expectedException MongoDB\Exception\InvalidArgumentTypeException
* @dataProvider provideInvalidConstructorOptions
*/
public function testConstructorOptionTypeChecks(array $options)
{
new FindAndModify($this->getDatabaseName(), $this->getCollectionName(), $options);
}
public function provideInvalidConstructorOptions()
{
$options = [];
foreach ($this->getInvalidDocumentValues() as $value) {
$options[][] = ['fields' => $value];
}
foreach ($this->getInvalidIntegerValues() as $value) {
$options[][] = ['maxTimeMS' => $value];
}
foreach ($this->getInvalidBooleanValues() as $value) {
$options[][] = ['new' => $value];
}
foreach ($this->getInvalidDocumentValues() as $value) {
$options[][] = ['query' => $value];
}
foreach ($this->getInvalidBooleanValues() as $value) {
$options[][] = ['remove' => $value];
}
foreach ($this->getInvalidDocumentValues() as $value) {
$options[][] = ['sort' => $value];
}
foreach ($this->getInvalidDocumentValues() as $value) {
$options[][] = ['update' => $value];
}
foreach ($this->getInvalidBooleanValues() as $value) {
$options[][] = ['upsert' => $value];
}
return $options;
}
/**
* @expectedException MongoDB\Exception\InvalidArgumentException
* @expectedExceptionMessage The "remove" option must be true or an "update" document must be specified, but not both
*/
public function testConstructorUpdateAndRemoveOptionsAreMutuallyExclusive()
{
new FindAndModify($this->getDatabaseName(), $this->getCollectionName(), ['remove' => true, 'update' => []]);
}
}
<?php
namespace MongoDB\Tests\Operation;
use MongoDB\Operation\FindOneAndDelete;
class FindOneAndDeleteTest extends TestCase
{
/**
* @expectedException MongoDB\Exception\InvalidArgumentTypeException
* @dataProvider provideInvalidDocumentValues
*/
public function testConstructorFilterArgumentTypeCheck($filter)
{
new FindOneAndDelete($this->getDatabaseName(), $this->getCollectionName(), $filter);
}
/**
* @expectedException MongoDB\Exception\InvalidArgumentTypeException
* @dataProvider provideInvalidConstructorOptions
*/
public function testConstructorOptionTypeChecks(array $options)
{
new FindOneAndDelete($this->getDatabaseName(), $this->getCollectionName(), [], $options);
}
public function provideInvalidConstructorOptions()
{
$options = [];
foreach ($this->getInvalidDocumentValues() as $value) {
$options[][] = ['projection' => $value];
}
return $options;
}
}
<?php
namespace MongoDB\Tests\Operation;
use MongoDB\Operation\FindOneAndReplace;
class FindOneAndReplaceTest extends TestCase
{
/**
* @expectedException MongoDB\Exception\InvalidArgumentTypeException
* @dataProvider provideInvalidDocumentValues
*/
public function testConstructorFilterArgumentTypeCheck($filter)
{
new FindOneAndReplace($this->getDatabaseName(), $this->getCollectionName(), $filter, []);
}
/**
* @expectedException MongoDB\Exception\InvalidArgumentTypeException
* @dataProvider provideInvalidDocumentValues
*/
public function testConstructorReplacementArgumentTypeCheck($replacement)
{
new FindOneAndReplace($this->getDatabaseName(), $this->getCollectionName(), [], $replacement);
}
/**
* @expectedException MongoDB\Exception\InvalidArgumentException
* @expectedExceptionMessage First key in $replacement argument is an update operator
*/
public function testConstructorReplacementArgumentRequiresNoOperators()
{
new FindOneAndReplace($this->getDatabaseName(), $this->getCollectionName(), [], ['$set' => ['x' => 1]]);
}
/**
* @expectedException MongoDB\Exception\InvalidArgumentTypeException
* @dataProvider provideInvalidConstructorOptions
*/
public function testConstructorOptionTypeChecks(array $options)
{
new FindOneAndReplace($this->getDatabaseName(), $this->getCollectionName(), [], [], $options);
}
public function provideInvalidConstructorOptions()
{
$options = [];
foreach ($this->getInvalidDocumentValues() as $value) {
$options[][] = ['projection' => $value];
}
foreach ($this->getInvalidIntegerValues() as $value) {
$options[][] = ['returnDocument' => $value];
}
return $options;
}
/**
* @expectedException MongoDB\Exception\InvalidArgumentException
* @dataProvider provideInvalidConstructorReturnDocumentOptions
*/
public function testConstructorReturnDocumentOption($returnDocument)
{
new FindOneAndReplace($this->getDatabaseName(), $this->getCollectionName(), [], [], ['returnDocument' => $returnDocument]);
}
public function provideInvalidConstructorReturnDocumentOptions()
{
return $this->wrapValuesForDataProvider([-1, 0, 3]);
}
}
<?php
namespace MongoDB\Tests\Operation;
use MongoDB\Operation\FindOneAndUpdate;
class FindOneAndUpdateTest extends TestCase
{
/**
* @expectedException MongoDB\Exception\InvalidArgumentTypeException
* @dataProvider provideInvalidDocumentValues
*/
public function testConstructorFilterArgumentTypeCheck($filter)
{
new FindOneAndUpdate($this->getDatabaseName(), $this->getCollectionName(), $filter, []);
}
/**
* @expectedException MongoDB\Exception\InvalidArgumentTypeException
* @dataProvider provideInvalidDocumentValues
*/
public function testConstructorUpdateArgumentTypeCheck($update)
{
new FindOneAndUpdate($this->getDatabaseName(), $this->getCollectionName(), [], $update);
}
/**
* @expectedException MongoDB\Exception\InvalidArgumentException
* @expectedExceptionMessage First key in $update argument is not an update operator
*/
public function testConstructorUpdateArgumentRequiresOperators()
{
new FindOneAndUpdate($this->getDatabaseName(), $this->getCollectionName(), [], []);
}
/**
* @expectedException MongoDB\Exception\InvalidArgumentTypeException
* @dataProvider provideInvalidConstructorOptions
*/
public function testConstructorOptionTypeChecks(array $options)
{
new FindOneAndUpdate($this->getDatabaseName(), $this->getCollectionName(), [], ['$set' => ['x' => 1]], $options);
}
public function provideInvalidConstructorOptions()
{
$options = [];
foreach ($this->getInvalidDocumentValues() as $value) {
$options[][] = ['projection' => $value];
}
foreach ($this->getInvalidIntegerValues() as $value) {
$options[][] = ['returnDocument' => $value];
}
return $options;
}
/**
* @expectedException MongoDB\Exception\InvalidArgumentException
* @dataProvider provideInvalidConstructorReturnDocumentOptions
*/
public function testConstructorReturnDocumentOption($returnDocument)
{
new FindOneAndUpdate($this->getDatabaseName(), $this->getCollectionName(), [], [], ['returnDocument' => $returnDocument]);
}
public function provideInvalidConstructorReturnDocumentOptions()
{
return $this->wrapValuesForDataProvider([-1, 0, 3]);
}
}
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