Skip to content

Commit 53c62cb

Browse files
committed
Remove redundant option validation from FindAndModify children
Options can be passed directly to FindAndModify after any necessary translation/conversion.
1 parent bd87d68 commit 53c62cb

File tree

7 files changed

+265
-40
lines changed

7 files changed

+265
-40
lines changed

src/Operation/FindOneAndDelete.php

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -44,28 +44,23 @@ public function __construct($databaseName, $collectionName, $filter, array $opti
4444
throw new InvalidArgumentTypeException('$filter', $filter, 'array or object');
4545
}
4646

47-
if (isset($options['maxTimeMS']) && ! is_integer($options['maxTimeMS'])) {
48-
throw new InvalidArgumentTypeException('"maxTimeMS" option', $options['maxTimeMS'], 'integer');
49-
}
50-
5147
if (isset($options['projection']) && ! is_array($options['projection']) && ! is_object($options['projection'])) {
5248
throw new InvalidArgumentTypeException('"projection" option', $options['projection'], 'array or object');
5349
}
5450

55-
if (isset($options['sort']) && ! is_array($options['sort']) && ! is_object($options['sort'])) {
56-
throw new InvalidArgumentTypeException('"sort" option', $options['sort'], 'array or object');
51+
if (isset($options['projection'])) {
52+
$options['fields'] = $options['projection'];
5753
}
5854

55+
unset($options['projection']);
56+
5957
$this->findAndModify = new FindAndModify(
6058
$databaseName,
6159
$collectionName,
6260
array(
63-
'fields' => isset($options['projection']) ? $options['projection'] : null,
64-
'maxTimeMS' => isset($options['maxTimeMS']) ? $options['maxTimeMS'] : null,
6561
'query' => $filter,
6662
'remove' => true,
67-
'sort' => isset($options['sort']) ? $options['sort'] : null,
68-
)
63+
) + $options
6964
);
7065
}
7166

src/Operation/FindOneAndReplace.php

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -68,10 +68,6 @@ public function __construct($databaseName, $collectionName, $filter, $replacemen
6868
'upsert' => false,
6969
);
7070

71-
if (isset($options['maxTimeMS']) && ! is_integer($options['maxTimeMS'])) {
72-
throw new InvalidArgumentTypeException('"maxTimeMS" option', $options['maxTimeMS'], 'integer');
73-
}
74-
7571
if (isset($options['projection']) && ! is_array($options['projection']) && ! is_object($options['projection'])) {
7672
throw new InvalidArgumentTypeException('"projection" option', $options['projection'], 'array or object');
7773
}
@@ -85,26 +81,21 @@ public function __construct($databaseName, $collectionName, $filter, $replacemen
8581
throw new InvalidArgumentException('Invalid value for "returnDocument" option: ' . $options['returnDocument']);
8682
}
8783

88-
if (isset($options['sort']) && ! is_array($options['sort']) && ! is_object($options['sort'])) {
89-
throw new InvalidArgumentTypeException('"sort" option', $options['sort'], 'array or object');
84+
if (isset($options['projection'])) {
85+
$options['fields'] = $options['projection'];
9086
}
9187

92-
if ( ! is_bool($options['upsert'])) {
93-
throw new InvalidArgumentTypeException('"upsert" option', $options['upsert'], 'boolean');
94-
}
88+
$options['new'] = $options['returnDocument'] === self::RETURN_DOCUMENT_AFTER;
89+
90+
unset($options['projection'], $options['returnDocument']);
9591

9692
$this->findAndModify = new FindAndModify(
9793
$databaseName,
9894
$collectionName,
9995
array(
100-
'fields' => isset($options['projection']) ? $options['projection'] : null,
101-
'maxTimeMS' => isset($options['maxTimeMS']) ? $options['maxTimeMS'] : null,
102-
'new' => $options['returnDocument'] === self::RETURN_DOCUMENT_AFTER,
10396
'query' => $filter,
104-
'sort' => isset($options['sort']) ? $options['sort'] : null,
10597
'update' => $replacement,
106-
'upsert' => $options['upsert'],
107-
)
98+
) + $options
10899
);
109100
}
110101

src/Operation/FindOneAndUpdate.php

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -68,10 +68,6 @@ public function __construct($databaseName, $collectionName, $filter, $update, ar
6868
'upsert' => false,
6969
);
7070

71-
if (isset($options['maxTimeMS']) && ! is_integer($options['maxTimeMS'])) {
72-
throw new InvalidArgumentTypeException('"maxTimeMS" option', $options['maxTimeMS'], 'integer');
73-
}
74-
7571
if (isset($options['projection']) && ! is_array($options['projection']) && ! is_object($options['projection'])) {
7672
throw new InvalidArgumentTypeException('"projection" option', $options['projection'], 'array or object');
7773
}
@@ -85,26 +81,21 @@ public function __construct($databaseName, $collectionName, $filter, $update, ar
8581
throw new InvalidArgumentException('Invalid value for "returnDocument" option: ' . $options['returnDocument']);
8682
}
8783

88-
if (isset($options['sort']) && ! is_array($options['sort']) && ! is_object($options['sort'])) {
89-
throw new InvalidArgumentTypeException('"sort" option', $options['sort'], 'array or object');
84+
if (isset($options['projection'])) {
85+
$options['fields'] = $options['projection'];
9086
}
9187

92-
if ( ! is_bool($options['upsert'])) {
93-
throw new InvalidArgumentTypeException('"upsert" option', $options['upsert'], 'boolean');
94-
}
88+
$options['new'] = $options['returnDocument'] === self::RETURN_DOCUMENT_AFTER;
89+
90+
unset($options['projection'], $options['returnDocument']);
9591

9692
$this->findAndModify = new FindAndModify(
9793
$databaseName,
9894
$collectionName,
9995
array(
100-
'fields' => isset($options['projection']) ? $options['projection'] : null,
101-
'maxTimeMS' => isset($options['maxTimeMS']) ? $options['maxTimeMS'] : null,
102-
'new' => $options['returnDocument'] === self::RETURN_DOCUMENT_AFTER,
10396
'query' => $filter,
104-
'sort' => isset($options['sort']) ? $options['sort'] : null,
10597
'update' => $update,
106-
'upsert' => $options['upsert'],
107-
)
98+
) + $options
10899
);
109100
}
110101

tests/Operation/FindAndModifyTest.php

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
<?php
2+
3+
namespace MongoDB\Tests\Operation;
4+
5+
use MongoDB\Operation\FindAndModify;
6+
7+
class FindAndModifyTest extends TestCase
8+
{
9+
/**
10+
* @expectedException MongoDB\Exception\InvalidArgumentTypeException
11+
* @dataProvider provideInvalidConstructorOptions
12+
*/
13+
public function testConstructorOptionTypeChecks(array $options)
14+
{
15+
new FindAndModify($this->getDatabaseName(), $this->getCollectionName(), $options);
16+
}
17+
18+
public function provideInvalidConstructorOptions()
19+
{
20+
$options = [];
21+
22+
foreach ($this->getInvalidDocumentValues() as $value) {
23+
$options[][] = ['fields' => $value];
24+
}
25+
26+
foreach ($this->getInvalidIntegerValues() as $value) {
27+
$options[][] = ['maxTimeMS' => $value];
28+
}
29+
30+
foreach ($this->getInvalidBooleanValues() as $value) {
31+
$options[][] = ['new' => $value];
32+
}
33+
34+
foreach ($this->getInvalidDocumentValues() as $value) {
35+
$options[][] = ['query' => $value];
36+
}
37+
38+
foreach ($this->getInvalidBooleanValues() as $value) {
39+
$options[][] = ['remove' => $value];
40+
}
41+
42+
foreach ($this->getInvalidDocumentValues() as $value) {
43+
$options[][] = ['sort' => $value];
44+
}
45+
46+
foreach ($this->getInvalidDocumentValues() as $value) {
47+
$options[][] = ['update' => $value];
48+
}
49+
50+
foreach ($this->getInvalidBooleanValues() as $value) {
51+
$options[][] = ['upsert' => $value];
52+
}
53+
54+
return $options;
55+
}
56+
57+
/**
58+
* @expectedException MongoDB\Exception\InvalidArgumentException
59+
* @expectedExceptionMessage The "remove" option must be true or an "update" document must be specified, but not both
60+
*/
61+
public function testConstructorUpdateAndRemoveOptionsAreMutuallyExclusive()
62+
{
63+
new FindAndModify($this->getDatabaseName(), $this->getCollectionName(), ['remove' => true, 'update' => []]);
64+
}
65+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
3+
namespace MongoDB\Tests\Operation;
4+
5+
use MongoDB\Operation\FindOneAndDelete;
6+
7+
class FindOneAndDeleteTest extends TestCase
8+
{
9+
/**
10+
* @expectedException MongoDB\Exception\InvalidArgumentTypeException
11+
* @dataProvider provideInvalidDocumentValues
12+
*/
13+
public function testConstructorFilterArgumentTypeCheck($filter)
14+
{
15+
new FindOneAndDelete($this->getDatabaseName(), $this->getCollectionName(), $filter);
16+
}
17+
18+
/**
19+
* @expectedException MongoDB\Exception\InvalidArgumentTypeException
20+
* @dataProvider provideInvalidConstructorOptions
21+
*/
22+
public function testConstructorOptionTypeChecks(array $options)
23+
{
24+
new FindOneAndDelete($this->getDatabaseName(), $this->getCollectionName(), [], $options);
25+
}
26+
27+
public function provideInvalidConstructorOptions()
28+
{
29+
$options = [];
30+
31+
foreach ($this->getInvalidDocumentValues() as $value) {
32+
$options[][] = ['projection' => $value];
33+
}
34+
35+
return $options;
36+
}
37+
}
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
<?php
2+
3+
namespace MongoDB\Tests\Operation;
4+
5+
use MongoDB\Operation\FindOneAndReplace;
6+
7+
class FindOneAndReplaceTest extends TestCase
8+
{
9+
/**
10+
* @expectedException MongoDB\Exception\InvalidArgumentTypeException
11+
* @dataProvider provideInvalidDocumentValues
12+
*/
13+
public function testConstructorFilterArgumentTypeCheck($filter)
14+
{
15+
new FindOneAndReplace($this->getDatabaseName(), $this->getCollectionName(), $filter, []);
16+
}
17+
18+
/**
19+
* @expectedException MongoDB\Exception\InvalidArgumentTypeException
20+
* @dataProvider provideInvalidDocumentValues
21+
*/
22+
public function testConstructorReplacementArgumentTypeCheck($replacement)
23+
{
24+
new FindOneAndReplace($this->getDatabaseName(), $this->getCollectionName(), [], $replacement);
25+
}
26+
27+
/**
28+
* @expectedException MongoDB\Exception\InvalidArgumentException
29+
* @expectedExceptionMessage First key in $replacement argument is an update operator
30+
*/
31+
public function testConstructorReplacementArgumentRequiresNoOperators()
32+
{
33+
new FindOneAndReplace($this->getDatabaseName(), $this->getCollectionName(), [], ['$set' => ['x' => 1]]);
34+
}
35+
36+
/**
37+
* @expectedException MongoDB\Exception\InvalidArgumentTypeException
38+
* @dataProvider provideInvalidConstructorOptions
39+
*/
40+
public function testConstructorOptionTypeChecks(array $options)
41+
{
42+
new FindOneAndReplace($this->getDatabaseName(), $this->getCollectionName(), [], [], $options);
43+
}
44+
45+
public function provideInvalidConstructorOptions()
46+
{
47+
$options = [];
48+
49+
foreach ($this->getInvalidDocumentValues() as $value) {
50+
$options[][] = ['projection' => $value];
51+
}
52+
53+
foreach ($this->getInvalidIntegerValues() as $value) {
54+
$options[][] = ['returnDocument' => $value];
55+
}
56+
57+
return $options;
58+
}
59+
60+
/**
61+
* @expectedException MongoDB\Exception\InvalidArgumentException
62+
* @dataProvider provideInvalidConstructorReturnDocumentOptions
63+
*/
64+
public function testConstructorReturnDocumentOption($returnDocument)
65+
{
66+
new FindOneAndReplace($this->getDatabaseName(), $this->getCollectionName(), [], [], ['returnDocument' => $returnDocument]);
67+
}
68+
69+
public function provideInvalidConstructorReturnDocumentOptions()
70+
{
71+
return $this->wrapValuesForDataProvider([-1, 0, 3]);
72+
}
73+
}
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
<?php
2+
3+
namespace MongoDB\Tests\Operation;
4+
5+
use MongoDB\Operation\FindOneAndUpdate;
6+
7+
class FindOneAndUpdateTest extends TestCase
8+
{
9+
/**
10+
* @expectedException MongoDB\Exception\InvalidArgumentTypeException
11+
* @dataProvider provideInvalidDocumentValues
12+
*/
13+
public function testConstructorFilterArgumentTypeCheck($filter)
14+
{
15+
new FindOneAndUpdate($this->getDatabaseName(), $this->getCollectionName(), $filter, []);
16+
}
17+
18+
/**
19+
* @expectedException MongoDB\Exception\InvalidArgumentTypeException
20+
* @dataProvider provideInvalidDocumentValues
21+
*/
22+
public function testConstructorUpdateArgumentTypeCheck($update)
23+
{
24+
new FindOneAndUpdate($this->getDatabaseName(), $this->getCollectionName(), [], $update);
25+
}
26+
27+
/**
28+
* @expectedException MongoDB\Exception\InvalidArgumentException
29+
* @expectedExceptionMessage First key in $update argument is not an update operator
30+
*/
31+
public function testConstructorUpdateArgumentRequiresOperators()
32+
{
33+
new FindOneAndUpdate($this->getDatabaseName(), $this->getCollectionName(), [], []);
34+
}
35+
36+
/**
37+
* @expectedException MongoDB\Exception\InvalidArgumentTypeException
38+
* @dataProvider provideInvalidConstructorOptions
39+
*/
40+
public function testConstructorOptionTypeChecks(array $options)
41+
{
42+
new FindOneAndUpdate($this->getDatabaseName(), $this->getCollectionName(), [], ['$set' => ['x' => 1]], $options);
43+
}
44+
45+
public function provideInvalidConstructorOptions()
46+
{
47+
$options = [];
48+
49+
foreach ($this->getInvalidDocumentValues() as $value) {
50+
$options[][] = ['projection' => $value];
51+
}
52+
53+
foreach ($this->getInvalidIntegerValues() as $value) {
54+
$options[][] = ['returnDocument' => $value];
55+
}
56+
57+
return $options;
58+
}
59+
60+
/**
61+
* @expectedException MongoDB\Exception\InvalidArgumentException
62+
* @dataProvider provideInvalidConstructorReturnDocumentOptions
63+
*/
64+
public function testConstructorReturnDocumentOption($returnDocument)
65+
{
66+
new FindOneAndUpdate($this->getDatabaseName(), $this->getCollectionName(), [], [], ['returnDocument' => $returnDocument]);
67+
}
68+
69+
public function provideInvalidConstructorReturnDocumentOptions()
70+
{
71+
return $this->wrapValuesForDataProvider([-1, 0, 3]);
72+
}
73+
}

0 commit comments

Comments
 (0)