Skip to content

Commit c894ebc

Browse files
committed
Refactor unit tests for write operations
1 parent 7556a36 commit c894ebc

File tree

9 files changed

+170
-104
lines changed

9 files changed

+170
-104
lines changed

tests/Operation/BulkWriteTest.php

Lines changed: 36 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -64,10 +64,10 @@ public function testInsertOneDocumentArgumentMissing()
6464

6565
/**
6666
* @expectedException MongoDB\Exception\InvalidArgumentTypeException
67-
* @expectedExceptionMessageRegExp /Expected \$operations\[0\]\["insertOne"\]\[0\] to have type "[\w ]+" but found "[\w ]+"/
68-
* @dataProvider provideInvalidDocumentArguments
67+
* @expectedExceptionMessageRegExp /Expected \$operations\[0\]\["insertOne"\]\[0\] to have type "array or object" but found "[\w ]+"/
68+
* @dataProvider provideInvalidDocumentValues
6969
*/
70-
public function testInsertOneDocumentArgumentType($document)
70+
public function testInsertOneDocumentArgumentTypeCheck($document)
7171
{
7272
new BulkWrite($this->getDatabaseName(), $this->getCollectionName(), array(
7373
array(BulkWrite::INSERT_ONE => array($document)),
@@ -87,10 +87,10 @@ public function testDeleteManyFilterArgumentMissing()
8787

8888
/**
8989
* @expectedException MongoDB\Exception\InvalidArgumentTypeException
90-
* @expectedExceptionMessageRegExp /Expected \$operations\[0\]\["deleteMany"\]\[0\] to have type "[\w ]+" but found "[\w ]+"/
91-
* @dataProvider provideInvalidDocumentArguments
90+
* @expectedExceptionMessageRegExp /Expected \$operations\[0\]\["deleteMany"\]\[0\] to have type "array or object" but found "[\w ]+"/
91+
* @dataProvider provideInvalidDocumentValues
9292
*/
93-
public function testDeleteManyFilterArgumentType($document)
93+
public function testDeleteManyFilterArgumentTypeCheck($document)
9494
{
9595
new BulkWrite($this->getDatabaseName(), $this->getCollectionName(), array(
9696
array(BulkWrite::DELETE_MANY => array($document)),
@@ -110,10 +110,10 @@ public function testDeleteOneFilterArgumentMissing()
110110

111111
/**
112112
* @expectedException MongoDB\Exception\InvalidArgumentTypeException
113-
* @expectedExceptionMessageRegExp /Expected \$operations\[0\]\["deleteOne"\]\[0\] to have type "[\w ]+" but found "[\w ]+"/
114-
* @dataProvider provideInvalidDocumentArguments
113+
* @expectedExceptionMessageRegExp /Expected \$operations\[0\]\["deleteOne"\]\[0\] to have type "array or object" but found "[\w ]+"/
114+
* @dataProvider provideInvalidDocumentValues
115115
*/
116-
public function testDeleteOneFilterArgumentType($document)
116+
public function testDeleteOneFilterArgumentTypeCheck($document)
117117
{
118118
new BulkWrite($this->getDatabaseName(), $this->getCollectionName(), array(
119119
array(BulkWrite::DELETE_ONE => array($document)),
@@ -133,10 +133,10 @@ public function testReplaceOneFilterArgumentMissing()
133133

134134
/**
135135
* @expectedException MongoDB\Exception\InvalidArgumentTypeException
136-
* @expectedExceptionMessageRegExp /Expected \$operations\[0\]\["replaceOne"\]\[0\] to have type "[\w ]+" but found "[\w ]+"/
137-
* @dataProvider provideInvalidDocumentArguments
136+
* @expectedExceptionMessageRegExp /Expected \$operations\[0\]\["replaceOne"\]\[0\] to have type "array or object" but found "[\w ]+"/
137+
* @dataProvider provideInvalidDocumentValues
138138
*/
139-
public function testReplaceOneFilterArgumentType($filter)
139+
public function testReplaceOneFilterArgumentTypeCheck($filter)
140140
{
141141
new BulkWrite($this->getDatabaseName(), $this->getCollectionName(), array(
142142
array(BulkWrite::REPLACE_ONE => array($filter, array('y' => 1))),
@@ -156,10 +156,10 @@ public function testReplaceOneReplacementArgumentMissing()
156156

157157
/**
158158
* @expectedException MongoDB\Exception\InvalidArgumentTypeException
159-
* @expectedExceptionMessageRegExp /Expected \$operations\[0\]\["replaceOne"\]\[1\] to have type "[\w ]+" but found "[\w ]+"/
160-
* @dataProvider provideInvalidDocumentArguments
159+
* @expectedExceptionMessageRegExp /Expected \$operations\[0\]\["replaceOne"\]\[1\] to have type "array or object" but found "[\w ]+"/
160+
* @dataProvider provideInvalidDocumentValues
161161
*/
162-
public function testReplaceOneReplacementArgumentType($replacement)
162+
public function testReplaceOneReplacementArgumentTypeCheck($replacement)
163163
{
164164
new BulkWrite($this->getDatabaseName(), $this->getCollectionName(), array(
165165
array(BulkWrite::REPLACE_ONE => array(array('x' => 1), $replacement)),
@@ -179,10 +179,10 @@ public function testReplaceOneReplacementArgumentRequiresNoOperators()
179179

180180
/**
181181
* @expectedException MongoDB\Exception\InvalidArgumentTypeException
182-
* @expectedExceptionMessageRegExp /Expected \$operations\[0\]\["replaceOne"\]\[2\]\["upsert"\] to have type "[\w ]+" but found "[\w ]+"/
183-
* @dataProvider provideInvalidBooleanArguments
182+
* @expectedExceptionMessageRegExp /Expected \$operations\[0\]\["replaceOne"\]\[2\]\["upsert"\] to have type "boolean" but found "[\w ]+"/
183+
* @dataProvider provideInvalidBooleanValues
184184
*/
185-
public function testReplaceOneUpsertOptionType($upsert)
185+
public function testReplaceOneUpsertOptionTypeCheck($upsert)
186186
{
187187
new BulkWrite($this->getDatabaseName(), $this->getCollectionName(), array(
188188
array(BulkWrite::REPLACE_ONE => array(array('x' => 1), array('y' => 1), array('upsert' => $upsert))),
@@ -202,10 +202,10 @@ public function testUpdateManyFilterArgumentMissing()
202202

203203
/**
204204
* @expectedException MongoDB\Exception\InvalidArgumentTypeException
205-
* @expectedExceptionMessageRegExp /Expected \$operations\[0\]\["updateMany"\]\[0\] to have type "[\w ]+" but found "[\w ]+"/
206-
* @dataProvider provideInvalidDocumentArguments
205+
* @expectedExceptionMessageRegExp /Expected \$operations\[0\]\["updateMany"\]\[0\] to have type "array or object" but found "[\w ]+"/
206+
* @dataProvider provideInvalidDocumentValues
207207
*/
208-
public function testUpdateManyFilterArgumentType($filter)
208+
public function testUpdateManyFilterArgumentTypeCheck($filter)
209209
{
210210
new BulkWrite($this->getDatabaseName(), $this->getCollectionName(), array(
211211
array(BulkWrite::UPDATE_MANY => array($filter, array('$set' => array('x' => 1)))),
@@ -225,10 +225,10 @@ public function testUpdateManyUpdateArgumentMissing()
225225

226226
/**
227227
* @expectedException MongoDB\Exception\InvalidArgumentTypeException
228-
* @expectedExceptionMessageRegExp /Expected \$operations\[0\]\["updateMany"\]\[1\] to have type "[\w ]+" but found "[\w ]+"/
229-
* @dataProvider provideInvalidDocumentArguments
228+
* @expectedExceptionMessageRegExp /Expected \$operations\[0\]\["updateMany"\]\[1\] to have type "array or object" but found "[\w ]+"/
229+
* @dataProvider provideInvalidDocumentValues
230230
*/
231-
public function testUpdateManyUpdateArgumentType($update)
231+
public function testUpdateManyUpdateArgumentTypeCheck($update)
232232
{
233233
new BulkWrite($this->getDatabaseName(), $this->getCollectionName(), array(
234234
array(BulkWrite::UPDATE_MANY => array(array('x' => 1), $update)),
@@ -248,10 +248,10 @@ public function testUpdateManyUpdateArgumentRequiresOperators()
248248

249249
/**
250250
* @expectedException MongoDB\Exception\InvalidArgumentTypeException
251-
* @expectedExceptionMessageRegExp /Expected \$operations\[0\]\["updateMany"\]\[2\]\["upsert"\] to have type "[\w ]+" but found "[\w ]+"/
252-
* @dataProvider provideInvalidBooleanArguments
251+
* @expectedExceptionMessageRegExp /Expected \$operations\[0\]\["updateMany"\]\[2\]\["upsert"\] to have type "boolean" but found "[\w ]+"/
252+
* @dataProvider provideInvalidBooleanValues
253253
*/
254-
public function testUpdateManyUpsertOptionType($upsert)
254+
public function testUpdateManyUpsertOptionTypeCheck($upsert)
255255
{
256256
new BulkWrite($this->getDatabaseName(), $this->getCollectionName(), array(
257257
array(BulkWrite::UPDATE_MANY => array(array('x' => 1), array('$set' => array('x' => 1)), array('upsert' => $upsert))),
@@ -271,10 +271,10 @@ public function testUpdateOneFilterArgumentMissing()
271271

272272
/**
273273
* @expectedException MongoDB\Exception\InvalidArgumentTypeException
274-
* @expectedExceptionMessageRegExp /Expected \$operations\[0\]\["updateOne"\]\[0\] to have type "[\w ]+" but found "[\w ]+"/
275-
* @dataProvider provideInvalidDocumentArguments
274+
* @expectedExceptionMessageRegExp /Expected \$operations\[0\]\["updateOne"\]\[0\] to have type "array or object" but found "[\w ]+"/
275+
* @dataProvider provideInvalidDocumentValues
276276
*/
277-
public function testUpdateOneFilterArgumentType($filter)
277+
public function testUpdateOneFilterArgumentTypeCheck($filter)
278278
{
279279
new BulkWrite($this->getDatabaseName(), $this->getCollectionName(), array(
280280
array(BulkWrite::UPDATE_ONE => array($filter, array('$set' => array('x' => 1)))),
@@ -294,10 +294,10 @@ public function testUpdateOneUpdateArgumentMissing()
294294

295295
/**
296296
* @expectedException MongoDB\Exception\InvalidArgumentTypeException
297-
* @expectedExceptionMessageRegExp /Expected \$operations\[0\]\["updateOne"\]\[1\] to have type "[\w ]+" but found "[\w ]+"/
298-
* @dataProvider provideInvalidDocumentArguments
297+
* @expectedExceptionMessageRegExp /Expected \$operations\[0\]\["updateOne"\]\[1\] to have type "array or object" but found "[\w ]+"/
298+
* @dataProvider provideInvalidDocumentValues
299299
*/
300-
public function testUpdateOneUpdateArgumentType($update)
300+
public function testUpdateOneUpdateArgumentTypeCheck($update)
301301
{
302302
new BulkWrite($this->getDatabaseName(), $this->getCollectionName(), array(
303303
array(BulkWrite::UPDATE_ONE => array(array('x' => 1), $update)),
@@ -317,10 +317,10 @@ public function testUpdateOneUpdateArgumentRequiresOperators()
317317

318318
/**
319319
* @expectedException MongoDB\Exception\InvalidArgumentTypeException
320-
* @expectedExceptionMessageRegExp /Expected \$operations\[0\]\["updateOne"\]\[2\]\["upsert"\] to have type "[\w ]+" but found "[\w ]+"/
321-
* @dataProvider provideInvalidBooleanArguments
320+
* @expectedExceptionMessageRegExp /Expected \$operations\[0\]\["updateOne"\]\[2\]\["upsert"\] to have type "boolean" but found "[\w ]+"/
321+
* @dataProvider provideInvalidBooleanValues
322322
*/
323-
public function testUpdateOneUpsertOptionType($upsert)
323+
public function testUpdateOneUpsertOptionTypeCheck($upsert)
324324
{
325325
new BulkWrite($this->getDatabaseName(), $this->getCollectionName(), array(
326326
array(BulkWrite::UPDATE_ONE => array(array('x' => 1), array('$set' => array('x' => 1)), array('upsert' => $upsert))),

tests/Operation/DeleteTest.php

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,27 +8,45 @@ class DeleteTest extends TestCase
88
{
99
/**
1010
* @expectedException MongoDB\Exception\InvalidArgumentTypeException
11-
* @dataProvider provideInvalidDocumentArguments
11+
* @dataProvider provideInvalidDocumentValues
1212
*/
13-
public function testConstructorFilterArgumentType($filter)
13+
public function testConstructorFilterArgumentTypeCheck($filter)
1414
{
1515
new Delete($this->getDatabaseName(), $this->getCollectionName(), $filter, 0);
1616
}
1717

1818
/**
1919
* @expectedException MongoDB\Exception\InvalidArgumentException
20-
* @dataProvider provideInvalidDocumentArguments
20+
* @expectedExceptionMessage $limit must be 0 or 1
21+
* @dataProvider provideInvalidLimitValues
2122
*/
22-
public function testConstructorLimitArgumentMustBeOneOrZero()
23+
public function testConstructorLimitArgumentMustBeOneOrZero($limit)
2324
{
24-
new Delete($this->getDatabaseName(), $this->getCollectionName(), array(), 2);
25+
new Delete($this->getDatabaseName(), $this->getCollectionName(), array(), $limit);
26+
}
27+
28+
public function provideInvalidLimitValues()
29+
{
30+
return $this->wrapValuesForDataProvider(array_merge($this->getInvalidIntegerValues(), array(-1, 2)));
2531
}
2632

2733
/**
2834
* @expectedException MongoDB\Exception\InvalidArgumentTypeException
35+
* @dataProvider provideInvalidConstructorOptions
2936
*/
30-
public function testConstructorWriteConcernOptionType()
37+
public function testConstructorOptionTypeChecks(array $options)
38+
{
39+
new Delete($this->getDatabaseName(), $this->getCollectionName(), array(), 1, $options);
40+
}
41+
42+
public function provideInvalidConstructorOptions()
3143
{
32-
new Delete($this->getDatabaseName(), $this->getCollectionName(), array(), 1, array('writeConcern' => null));
44+
$options = array();
45+
46+
foreach ($this->getInvalidWriteConcernValues() as $value) {
47+
$options[][] = array('writeConcern' => $value);
48+
}
49+
50+
return $options;
3351
}
3452
}

tests/Operation/InsertManyTest.php

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ class InsertManyTest extends TestCase
88
{
99
/**
1010
* @expectedException MongoDB\Exception\InvalidArgumentException
11+
* @expectedExceptionMessage $documents is empty
1112
*/
1213
public function testConstructorDocumentsMustNotBeEmpty()
1314
{
@@ -16,6 +17,7 @@ public function testConstructorDocumentsMustNotBeEmpty()
1617

1718
/**
1819
* @expectedException MongoDB\Exception\InvalidArgumentException
20+
* @expectedExceptionMessage $documents is not a list (unexpected index: "1")
1921
*/
2022
public function testConstructorDocumentsMustBeAList()
2123
{
@@ -24,27 +26,35 @@ public function testConstructorDocumentsMustBeAList()
2426

2527
/**
2628
* @expectedException MongoDB\Exception\InvalidArgumentTypeException
27-
* @dataProvider provideInvalidDocumentArguments
29+
* @expectedExceptionMessageRegExp /Expected \$documents\[0\] to have type "array or object" but found "[\w ]+"/
30+
* @dataProvider provideInvalidDocumentValues
2831
*/
29-
public function testConstructorDocumentsElementType($document)
32+
public function testConstructorDocumentsArgumentElementTypeChecks($document)
3033
{
3134
new InsertMany($this->getDatabaseName(), $this->getCollectionName(), array($document));
3235
}
3336

3437
/**
3538
* @expectedException MongoDB\Exception\InvalidArgumentTypeException
36-
* @dataProvider provideInvalidBooleanArguments
39+
* @dataProvider provideInvalidConstructorOptions
3740
*/
38-
public function testConstructorOrderedOptionType($ordered)
41+
public function testConstructorOptionTypeChecks(array $options)
3942
{
40-
new InsertMany($this->getDatabaseName(), $this->getCollectionName(), array(array('x' => 1)), array('ordered' => $ordered));
43+
new InsertMany($this->getDatabaseName(), $this->getCollectionName(), array(array('x' => 1)), $options);
4144
}
4245

43-
/**
44-
* @expectedException MongoDB\Exception\InvalidArgumentTypeException
45-
*/
46-
public function testConstructorWriteConcernOptionType()
46+
public function provideInvalidConstructorOptions()
4747
{
48-
new InsertMany($this->getDatabaseName(), $this->getCollectionName(), array(array('x' => 1)), array('writeConcern' => null));
48+
$options = array();
49+
50+
foreach ($this->getInvalidBooleanValues() as $value) {
51+
$options[][] = array('ordered' => $value);
52+
}
53+
54+
foreach ($this->getInvalidWriteConcernValues() as $value) {
55+
$options[][] = array('writeConcern' => $value);
56+
}
57+
58+
return $options;
4959
}
5060
}

tests/Operation/InsertOneTest.php

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,30 @@ class InsertOneTest extends TestCase
88
{
99
/**
1010
* @expectedException MongoDB\Exception\InvalidArgumentTypeException
11-
* @dataProvider provideInvalidDocumentArguments
11+
* @dataProvider provideInvalidDocumentValues
1212
*/
13-
public function testConstructorDocumentArgumentType($document)
13+
public function testConstructorDocumentArgumentTypeCheck($document)
1414
{
1515
new InsertOne($this->getDatabaseName(), $this->getCollectionName(), $document);
1616
}
1717

1818
/**
1919
* @expectedException MongoDB\Exception\InvalidArgumentTypeException
20+
* @dataProvider provideInvalidConstructorOptions
2021
*/
21-
public function testConstructorWriteConcernOptionType()
22+
public function testConstructorOptionTypeChecks(array $options)
2223
{
23-
new InsertOne($this->getDatabaseName(), $this->getCollectionName(), array('x' => 1), array('writeConcern' => null));
24+
new InsertOne($this->getDatabaseName(), $this->getCollectionName(), array('x' => 1), $options);
25+
}
26+
27+
public function provideInvalidConstructorOptions()
28+
{
29+
$options = array();
30+
31+
foreach ($this->getInvalidWriteConcernValues() as $value) {
32+
$options[][] = array('writeConcern' => $value);
33+
}
34+
35+
return $options;
2436
}
2537
}

tests/Operation/ReplaceOneTest.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,24 +8,25 @@ class ReplaceOneTest extends TestCase
88
{
99
/**
1010
* @expectedException MongoDB\Exception\InvalidArgumentTypeException
11-
* @dataProvider provideInvalidDocumentArguments
11+
* @dataProvider provideInvalidDocumentValues
1212
*/
13-
public function testConstructorFilterArgumentType($filter)
13+
public function testConstructorFilterArgumentTypeCheck($filter)
1414
{
1515
new ReplaceOne($this->getDatabaseName(), $this->getCollectionName(), $filter, array('y' => 1));
1616
}
1717

1818
/**
1919
* @expectedException MongoDB\Exception\InvalidArgumentTypeException
20-
* @dataProvider provideInvalidDocumentArguments
20+
* @dataProvider provideInvalidDocumentValues
2121
*/
22-
public function testConstructorReplacementArgumentType($replacement)
22+
public function testConstructorReplacementArgumentTypeCheck($replacement)
2323
{
2424
new ReplaceOne($this->getDatabaseName(), $this->getCollectionName(), array('x' => 1), $replacement);
2525
}
2626

2727
/**
2828
* @expectedException MongoDB\Exception\InvalidArgumentException
29+
* @expectedExceptionMessage First key in $replacement argument is an update operator
2930
*/
3031
public function testConstructorReplacementArgumentRequiresNoOperators()
3132
{

0 commit comments

Comments
 (0)