Skip to content

Commit 362a27f

Browse files
committed
PHPLIB-58: Functional tests for CRUD spec write methods
1 parent 89391d2 commit 362a27f

10 files changed

+952
-0
lines changed
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<?php
2+
3+
namespace MongoDB\Tests\Collection\CrudSpec;
4+
5+
/**
6+
* CRUD spec functional tests for deleteMany().
7+
*
8+
* @see https://github.com/mongodb/specifications/tree/master/source/crud/tests
9+
*/
10+
class DeleteManyFunctionalTest extends FunctionalTestCase
11+
{
12+
public function setUp()
13+
{
14+
parent::setUp();
15+
16+
$this->createFixtures(3);
17+
}
18+
19+
public function testDeleteManyWhenManyDocumentsMatch()
20+
{
21+
$filter = array('_id' => array('$gt' => 1));
22+
23+
$result = $this->collection->deleteMany($filter);
24+
$this->assertSame(2, $result->getDeletedCount());
25+
26+
$expected = array(
27+
array('_id' => 1, 'x' => 11),
28+
);
29+
30+
$this->assertSame($expected, $this->collection->find()->toArray());
31+
}
32+
33+
public function testDeleteManyWhenNoDocumentsMatch()
34+
{
35+
$filter = array('_id' => 4);
36+
37+
$result = $this->collection->deleteMany($filter);
38+
$this->assertSame(0, $result->getDeletedCount());
39+
40+
$expected = array(
41+
array('_id' => 1, 'x' => 11),
42+
array('_id' => 2, 'x' => 22),
43+
array('_id' => 3, 'x' => 33),
44+
);
45+
46+
$this->assertSame($expected, $this->collection->find()->toArray());
47+
}
48+
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
<?php
2+
3+
namespace MongoDB\Tests\Collection\CrudSpec;
4+
5+
/**
6+
* CRUD spec functional tests for deleteOne().
7+
*
8+
* @see https://github.com/mongodb/specifications/tree/master/source/crud/tests
9+
*/
10+
class DeleteOneFunctionalTest extends FunctionalTestCase
11+
{
12+
public function setUp()
13+
{
14+
parent::setUp();
15+
16+
$this->createFixtures(3);
17+
}
18+
19+
public function testDeleteOneWhenManyDocumentsMatch()
20+
{
21+
$filter = array('_id' => array('$gt' => 1));
22+
23+
$result = $this->collection->deleteOne($filter);
24+
$this->assertSame(1, $result->getDeletedCount());
25+
26+
$expected = array(
27+
array('_id' => 1, 'x' => 11),
28+
array('_id' => 3, 'x' => 33),
29+
);
30+
31+
$this->assertSame($expected, $this->collection->find()->toArray());
32+
}
33+
34+
public function testDeleteOneWhenOneDocumentMatches()
35+
{
36+
$filter = array('_id' => 2);
37+
38+
$result = $this->collection->deleteOne($filter);
39+
$this->assertSame(1, $result->getDeletedCount());
40+
41+
$expected = array(
42+
array('_id' => 1, 'x' => 11),
43+
array('_id' => 3, 'x' => 33),
44+
);
45+
46+
$this->assertSame($expected, $this->collection->find()->toArray());
47+
}
48+
49+
public function testDeleteOneWhenNoDocumentsMatch()
50+
{
51+
$filter = array('_id' => 4);
52+
53+
$result = $this->collection->deleteOne($filter);
54+
$this->assertSame(0, $result->getDeletedCount());
55+
56+
$expected = array(
57+
array('_id' => 1, 'x' => 11),
58+
array('_id' => 2, 'x' => 22),
59+
array('_id' => 3, 'x' => 33),
60+
);
61+
62+
$this->assertSame($expected, $this->collection->find()->toArray());
63+
}
64+
}
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
<?php
2+
3+
namespace MongoDB\Tests\Collection\CrudSpec;
4+
5+
/**
6+
* CRUD spec functional tests for findOneAndDelete().
7+
*
8+
* @see https://github.com/mongodb/specifications/tree/master/source/crud/tests
9+
*/
10+
class FindOneAndDeleteFunctionalTest extends FunctionalTestCase
11+
{
12+
public function setUp()
13+
{
14+
parent::setUp();
15+
16+
$this->createFixtures(3);
17+
}
18+
19+
public function testFindOneAndDeleteWhenManyDocumentsMatch()
20+
{
21+
$filter = array('_id' => array('$gt' => 1));
22+
$options = array(
23+
'projection' => array('x' => 1, '_id' => 0),
24+
'sort' => array('x' => 1),
25+
);
26+
27+
$document = $this->collection->findOneAndDelete($filter, $options);
28+
$this->assertSame(array('x' => 22), $document);
29+
30+
$expected = array(
31+
array('_id' => 1, 'x' => 11),
32+
array('_id' => 3, 'x' => 33),
33+
);
34+
35+
$this->assertSame($expected, $this->collection->find()->toArray());
36+
}
37+
38+
public function testFindOneAndDeleteWhenOneDocumentMatches()
39+
{
40+
$filter = array('_id' => 2);
41+
$options = array(
42+
'projection' => array('x' => 1, '_id' => 0),
43+
'sort' => array('x' => 1),
44+
);
45+
46+
$document = $this->collection->findOneAndDelete($filter, $options);
47+
$this->assertSame(array('x' => 22), $document);
48+
49+
$expected = array(
50+
array('_id' => 1, 'x' => 11),
51+
array('_id' => 3, 'x' => 33),
52+
);
53+
54+
$this->assertSame($expected, $this->collection->find()->toArray());
55+
}
56+
57+
public function testFindOneAndDeleteWhenNoDocumentsMatch()
58+
{
59+
$filter = array('_id' => 4);
60+
$options = array(
61+
'projection' => array('x' => 1, '_id' => 0),
62+
'sort' => array('x' => 1),
63+
);
64+
65+
$document = $this->collection->findOneAndDelete($filter, $options);
66+
$this->assertNull($document);
67+
68+
$expected = array(
69+
array('_id' => 1, 'x' => 11),
70+
array('_id' => 2, 'x' => 22),
71+
array('_id' => 3, 'x' => 33),
72+
);
73+
74+
$this->assertSame($expected, $this->collection->find()->toArray());
75+
}
76+
}
Lines changed: 196 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,196 @@
1+
<?php
2+
3+
namespace MongoDB\Tests\Collection\CrudSpec;
4+
5+
use MongoDB\Collection;
6+
7+
/**
8+
* CRUD spec functional tests for findOneAndReplace().
9+
*
10+
* @see https://github.com/mongodb/specifications/tree/master/source/crud/tests
11+
*/
12+
class FindOneAndReplaceFunctionalTest extends FunctionalTestCase
13+
{
14+
public function setUp()
15+
{
16+
parent::setUp();
17+
18+
$this->createFixtures(3);
19+
}
20+
21+
public function testFindOneAndReplaceWhenManyDocumentsMatchReturningDocumentBeforeModification()
22+
{
23+
$filter = array('_id' => array('$gt' => 1));
24+
$replacement = array('x' => 32);
25+
$options = array(
26+
'projection' => array('x' => 1, '_id' => 0),
27+
'sort' => array('x' => 1),
28+
);
29+
30+
$document = $this->collection->findOneAndReplace($filter, $replacement, $options);
31+
$this->assertSame(array('x' => 22), $document);
32+
33+
$expected = array(
34+
array('_id' => 1, 'x' => 11),
35+
array('_id' => 2, 'x' => 32),
36+
array('_id' => 3, 'x' => 33),
37+
);
38+
39+
$this->assertSame($expected, $this->collection->find()->toArray());
40+
}
41+
42+
public function testFindOneAndReplaceWhenManyDocumentsMatchReturningDocumentAfterModification()
43+
{
44+
$filter = array('_id' => array('$gt' => 1));
45+
$replacement = array('x' => 32);
46+
$options = array(
47+
'projection' => array('x' => 1, '_id' => 0),
48+
'sort' => array('x' => 1),
49+
'returnDocument' => Collection::FIND_ONE_AND_RETURN_AFTER,
50+
);
51+
52+
$document = $this->collection->findOneAndReplace($filter, $replacement, $options);
53+
$this->assertSame(array('x' => 32), $document);
54+
55+
$expected = array(
56+
array('_id' => 1, 'x' => 11),
57+
array('_id' => 2, 'x' => 32),
58+
array('_id' => 3, 'x' => 33),
59+
);
60+
61+
$this->assertSame($expected, $this->collection->find()->toArray());
62+
}
63+
64+
public function testFindOneAndReplaceWhenOneDocumentMatchesReturningDocumentBeforeModification()
65+
{
66+
$filter = array('_id' => 2);
67+
$replacement = array('x' => 32);
68+
$options = array(
69+
'projection' => array('x' => 1, '_id' => 0),
70+
'sort' => array('x' => 1),
71+
);
72+
73+
$document = $this->collection->findOneAndReplace($filter, $replacement, $options);
74+
$this->assertSame(array('x' => 22), $document);
75+
76+
$expected = array(
77+
array('_id' => 1, 'x' => 11),
78+
array('_id' => 2, 'x' => 32),
79+
array('_id' => 3, 'x' => 33),
80+
);
81+
82+
$this->assertSame($expected, $this->collection->find()->toArray());
83+
}
84+
85+
public function testFindOneAndReplaceWhenOneDocumentMatchesReturningDocumentAfterModification()
86+
{
87+
$filter = array('_id' => 2);
88+
$replacement = array('x' => 32);
89+
$options = array(
90+
'projection' => array('x' => 1, '_id' => 0),
91+
'sort' => array('x' => 1),
92+
'returnDocument' => Collection::FIND_ONE_AND_RETURN_AFTER,
93+
);
94+
95+
$document = $this->collection->findOneAndReplace($filter, $replacement, $options);
96+
$this->assertSame(array('x' => 32), $document);
97+
98+
$expected = array(
99+
array('_id' => 1, 'x' => 11),
100+
array('_id' => 2, 'x' => 32),
101+
array('_id' => 3, 'x' => 33),
102+
);
103+
104+
$this->assertSame($expected, $this->collection->find()->toArray());
105+
}
106+
107+
public function testFindOneAndReplaceWhenNoDocumentsMatchReturningDocumentBeforeModification()
108+
{
109+
$filter = array('_id' => 4);
110+
$replacement = array('x' => 44);
111+
$options = array(
112+
'projection' => array('x' => 1, '_id' => 0),
113+
'sort' => array('x' => 1),
114+
);
115+
116+
$document = $this->collection->findOneAndReplace($filter, $replacement, $options);
117+
$this->assertNull($document);
118+
119+
$expected = array(
120+
array('_id' => 1, 'x' => 11),
121+
array('_id' => 2, 'x' => 22),
122+
array('_id' => 3, 'x' => 33),
123+
);
124+
125+
$this->assertSame($expected, $this->collection->find()->toArray());
126+
}
127+
128+
public function testFindOneAndReplaceWithUpsertWhenNoDocumentsMatchReturningDocumentBeforeModification()
129+
{
130+
$filter = array('_id' => 4);
131+
$replacement = array('x' => 44);
132+
$options = array(
133+
'projection' => array('x' => 1, '_id' => 0),
134+
'sort' => array('x' => 1),
135+
'upsert' => true,
136+
);
137+
138+
$document = $this->collection->findOneAndReplace($filter, $replacement, $options);
139+
$this->assertNull($document);
140+
141+
$expected = array(
142+
array('_id' => 1, 'x' => 11),
143+
array('_id' => 2, 'x' => 22),
144+
array('_id' => 3, 'x' => 33),
145+
array('_id' => 4, 'x' => 44),
146+
);
147+
148+
$this->assertSame($expected, $this->collection->find()->toArray());
149+
}
150+
151+
public function testFindOneAndReplaceWhenNoDocumentsMatchReturningDocumentAfterModification()
152+
{
153+
$filter = array('_id' => 4);
154+
$replacement = array('x' => 44);
155+
$options = array(
156+
'projection' => array('x' => 1, '_id' => 0),
157+
'sort' => array('x' => 1),
158+
'returnDocument' => Collection::FIND_ONE_AND_RETURN_AFTER,
159+
);
160+
161+
$document = $this->collection->findOneAndReplace($filter, $replacement, $options);
162+
$this->assertNull($document);
163+
164+
$expected = array(
165+
array('_id' => 1, 'x' => 11),
166+
array('_id' => 2, 'x' => 22),
167+
array('_id' => 3, 'x' => 33),
168+
);
169+
170+
$this->assertSame($expected, $this->collection->find()->toArray());
171+
}
172+
173+
public function testFindOneAndReplaceWithUpsertWhenNoDocumentsMatchReturningDocumentAfterModification()
174+
{
175+
$filter = array('_id' => 4);
176+
$replacement = array('x' => 44);
177+
$options = array(
178+
'projection' => array('x' => 1, '_id' => 0),
179+
'sort' => array('x' => 1),
180+
'returnDocument' => Collection::FIND_ONE_AND_RETURN_AFTER,
181+
'upsert' => true,
182+
);
183+
184+
$document = $this->collection->findOneAndReplace($filter, $replacement, $options);
185+
$this->assertSame(array('x' => 44), $document);
186+
187+
$expected = array(
188+
array('_id' => 1, 'x' => 11),
189+
array('_id' => 2, 'x' => 22),
190+
array('_id' => 3, 'x' => 33),
191+
array('_id' => 4, 'x' => 44),
192+
);
193+
194+
$this->assertSame($expected, $this->collection->find()->toArray());
195+
}
196+
}

0 commit comments

Comments
 (0)