Skip to content

Commit 42683dc

Browse files
committed
Functional tests for Delete, Insert, and Update operations
1 parent ab15cda commit 42683dc

File tree

4 files changed

+293
-0
lines changed

4 files changed

+293
-0
lines changed
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
<?php
2+
3+
namespace MongoDB\Tests\Collection;
4+
5+
use MongoDB\Driver\BulkWrite;
6+
use MongoDB\Operation\Delete;
7+
8+
class DeleteFunctionalTest extends FunctionalTestCase
9+
{
10+
public function testDeleteOne()
11+
{
12+
$this->createFixtures(3);
13+
14+
$filter = ['_id' => 1];
15+
16+
$operation = new Delete($this->getDatabaseName(), $this->getCollectionName(), $filter, 1);
17+
$result = $operation->execute($this->getPrimaryServer());
18+
19+
$this->assertInstanceOf('MongoDB\DeleteResult', $result);
20+
$this->assertSame(1, $result->getDeletedCount());
21+
22+
$expected = [
23+
['_id' => 2, 'x' => 22],
24+
['_id' => 3, 'x' => 33],
25+
];
26+
27+
$this->assertSameDocuments($expected, $this->collection->find());
28+
}
29+
30+
public function testDeleteMany()
31+
{
32+
$this->createFixtures(3);
33+
34+
$filter = ['_id' => ['$gt' => 1]];
35+
36+
$operation = new Delete($this->getDatabaseName(), $this->getCollectionName(), $filter, 0);
37+
$result = $operation->execute($this->getPrimaryServer());
38+
39+
$this->assertInstanceOf('MongoDB\DeleteResult', $result);
40+
$this->assertSame(2, $result->getDeletedCount());
41+
42+
$expected = [
43+
['_id' => 1, 'x' => 11],
44+
];
45+
46+
$this->assertSameDocuments($expected, $this->collection->find());
47+
}
48+
49+
/**
50+
* Create data fixtures.
51+
*
52+
* @param integer $n
53+
*/
54+
private function createFixtures($n)
55+
{
56+
$bulkWrite = new BulkWrite(['ordered' => true]);
57+
58+
for ($i = 1; $i <= $n; $i++) {
59+
$bulkWrite->insert([
60+
'_id' => $i,
61+
'x' => (integer) ($i . $i),
62+
]);
63+
}
64+
65+
$result = $this->manager->executeBulkWrite($this->getNamespace(), $bulkWrite);
66+
67+
$this->assertEquals($n, $result->getInsertedCount());
68+
}
69+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
3+
namespace MongoDB\Tests\Collection;
4+
5+
use MongoDB\Operation\InsertMany;
6+
7+
class InsertManyFunctionalTest extends FunctionalTestCase
8+
{
9+
public function testInsertMany()
10+
{
11+
$documents = [
12+
['_id' => 'foo', 'x' => 11],
13+
['x' => 22],
14+
['_id' => 'bar', 'x' => 22],
15+
];
16+
17+
$operation = new InsertMany($this->getDatabaseName(), $this->getCollectionName(), $documents);
18+
$result = $operation->execute($this->getPrimaryServer());
19+
20+
$this->assertInstanceOf('MongoDB\InsertManyResult', $result);
21+
$this->assertSame(3, $result->getInsertedCount());
22+
23+
$insertedIds = $result->getInsertedIds();
24+
$this->assertSame('foo', $insertedIds[0]);
25+
$this->assertInstanceOf('MongoDB\BSON\ObjectId', $insertedIds[1]);
26+
$this->assertSame('bar', $insertedIds[2]);
27+
28+
$expected = [
29+
['_id' => 'foo', 'x' => 11],
30+
['_id' => $insertedIds[1], 'x' => 22],
31+
['_id' => 'bar', 'x' => 22],
32+
];
33+
34+
$this->assertSameDocuments($expected, $this->collection->find());
35+
}
36+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?php
2+
3+
namespace MongoDB\Tests\Collection;
4+
5+
use MongoDB\Operation\InsertOne;
6+
7+
class InsertOneFunctionalTest extends FunctionalTestCase
8+
{
9+
public function testInsertOneWithExistingId()
10+
{
11+
$document = ['_id' => 'foo', 'x' => 11];
12+
13+
$operation = new InsertOne($this->getDatabaseName(), $this->getCollectionName(), $document);
14+
$result = $operation->execute($this->getPrimaryServer());
15+
16+
$this->assertInstanceOf('MongoDB\InsertOneResult', $result);
17+
$this->assertSame(1, $result->getInsertedCount());
18+
$this->assertSame('foo', $result->getInsertedId());
19+
20+
$expected = [
21+
['_id' => 'foo', 'x' => 11],
22+
];
23+
24+
$this->assertSameDocuments($expected, $this->collection->find());
25+
}
26+
27+
public function testInsertOneWithGeneratedId()
28+
{
29+
$document = ['x' => 11];
30+
31+
$operation = new InsertOne($this->getDatabaseName(), $this->getCollectionName(), $document);
32+
$result = $operation->execute($this->getPrimaryServer());
33+
34+
$this->assertInstanceOf('MongoDB\InsertOneResult', $result);
35+
$this->assertSame(1, $result->getInsertedCount());
36+
$this->assertInstanceOf('MongoDB\BSON\ObjectId', $result->getInsertedId());
37+
38+
$expected = [
39+
['_id' => $result->getInsertedId(), 'x' => 11],
40+
];
41+
42+
$this->assertSameDocuments($expected, $this->collection->find());
43+
}
44+
}
Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
1+
<?php
2+
3+
namespace MongoDB\Tests\Collection;
4+
5+
use MongoDB\Driver\BulkWrite;
6+
use MongoDB\Operation\Update;
7+
8+
class UpdateFunctionalTest extends FunctionalTestCase
9+
{
10+
private $omitModifiedCount;
11+
12+
public function setUp()
13+
{
14+
parent::setUp();
15+
16+
$this->omitModifiedCount = version_compare($this->getServerVersion(), '2.6.0', '<');
17+
}
18+
19+
public function testUpdateOne()
20+
{
21+
$this->createFixtures(3);
22+
23+
$filter = ['_id' => ['$gt' => 1]];
24+
$update = ['$inc' => ['x' => 1]];
25+
26+
$operation = new Update($this->getDatabaseName(), $this->getCollectionName(), $filter, $update);
27+
$result = $operation->execute($this->getPrimaryServer());
28+
29+
$this->assertInstanceOf('MongoDB\UpdateResult', $result);
30+
$this->assertSame(1, $result->getMatchedCount());
31+
$this->omitModifiedCount or $this->assertSame(1, $result->getModifiedCount());
32+
$this->assertSame(0, $result->getUpsertedCount());
33+
$this->assertNull($result->getUpsertedId());
34+
35+
$expected = [
36+
['_id' => 1, 'x' => 11],
37+
['_id' => 2, 'x' => 23],
38+
['_id' => 3, 'x' => 33],
39+
];
40+
41+
$this->assertSameDocuments($expected, $this->collection->find());
42+
}
43+
44+
public function testUpdateMany()
45+
{
46+
$this->createFixtures(3);
47+
48+
$filter = ['_id' => ['$gt' => 1]];
49+
$update = ['$inc' => ['x' => 1]];
50+
$options = ['multi' => true];
51+
52+
$operation = new Update($this->getDatabaseName(), $this->getCollectionName(), $filter, $update, $options);
53+
$result = $operation->execute($this->getPrimaryServer());
54+
55+
$this->assertInstanceOf('MongoDB\UpdateResult', $result);
56+
$this->assertSame(2, $result->getMatchedCount());
57+
$this->omitModifiedCount or $this->assertSame(2, $result->getModifiedCount());
58+
$this->assertSame(0, $result->getUpsertedCount());
59+
$this->assertNull($result->getUpsertedId());
60+
61+
$expected = [
62+
['_id' => 1, 'x' => 11],
63+
['_id' => 2, 'x' => 23],
64+
['_id' => 3, 'x' => 34],
65+
];
66+
67+
$this->assertSameDocuments($expected, $this->collection->find());
68+
}
69+
70+
public function testUpdateManyWithExistingId()
71+
{
72+
$this->createFixtures(3);
73+
74+
$filter = ['_id' => 5];
75+
$update = ['$set' => ['x' => 55]];
76+
$options = ['upsert' => true];
77+
78+
$operation = new Update($this->getDatabaseName(), $this->getCollectionName(), $filter, $update, $options);
79+
$result = $operation->execute($this->getPrimaryServer());
80+
81+
$this->assertInstanceOf('MongoDB\UpdateResult', $result);
82+
$this->assertSame(0, $result->getMatchedCount());
83+
$this->omitModifiedCount or $this->assertSame(0, $result->getModifiedCount());
84+
$this->assertSame(1, $result->getUpsertedCount());
85+
$this->assertSame(5, $result->getUpsertedId());
86+
87+
$expected = [
88+
['_id' => 1, 'x' => 11],
89+
['_id' => 2, 'x' => 22],
90+
['_id' => 3, 'x' => 33],
91+
['_id' => 5, 'x' => 55],
92+
];
93+
94+
$this->assertSameDocuments($expected, $this->collection->find());
95+
}
96+
97+
public function testUpdateManyWithGeneratedId()
98+
{
99+
$this->createFixtures(3);
100+
101+
$filter = ['x' => 66];
102+
$update = ['$set' => ['x' => 66]];
103+
$options = ['upsert' => true];
104+
105+
$operation = new Update($this->getDatabaseName(), $this->getCollectionName(), $filter, $update, $options);
106+
$result = $operation->execute($this->getPrimaryServer());
107+
108+
$this->assertInstanceOf('MongoDB\UpdateResult', $result);
109+
$this->assertSame(0, $result->getMatchedCount());
110+
$this->omitModifiedCount or $this->assertSame(0, $result->getModifiedCount());
111+
$this->assertSame(1, $result->getUpsertedCount());
112+
$this->assertInstanceOf('MongoDB\BSON\ObjectId', $result->getUpsertedId());
113+
114+
$expected = [
115+
['_id' => 1, 'x' => 11],
116+
['_id' => 2, 'x' => 22],
117+
['_id' => 3, 'x' => 33],
118+
['_id' => $result->getUpsertedId(), 'x' => 66],
119+
];
120+
121+
$this->assertSameDocuments($expected, $this->collection->find());
122+
}
123+
124+
/**
125+
* Create data fixtures.
126+
*
127+
* @param integer $n
128+
*/
129+
private function createFixtures($n)
130+
{
131+
$bulkWrite = new BulkWrite(['ordered' => true]);
132+
133+
for ($i = 1; $i <= $n; $i++) {
134+
$bulkWrite->insert([
135+
'_id' => $i,
136+
'x' => (integer) ($i . $i),
137+
]);
138+
}
139+
140+
$result = $this->manager->executeBulkWrite($this->getNamespace(), $bulkWrite);
141+
142+
$this->assertEquals($n, $result->getInsertedCount());
143+
}
144+
}

0 commit comments

Comments
 (0)