Skip to content

Commit 740d7e6

Browse files
committed
PHPLIB-58: Functional tests for CRUD spec read methods
1 parent c991d3a commit 740d7e6

File tree

5 files changed

+230
-0
lines changed

5 files changed

+230
-0
lines changed
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
<?php
2+
3+
namespace MongoDB\Tests\Collection\CrudSpec;
4+
5+
use MongoDB\Collection;
6+
7+
/**
8+
* Functional tests for aggregate() from the CRUD specification.
9+
*
10+
* @see https://github.com/mongodb/specifications/tree/master/source/crud/tests
11+
*/
12+
class AggregateFunctionalTest extends FunctionalTestCase
13+
{
14+
public function setUp()
15+
{
16+
parent::setUp();
17+
18+
$this->createFixtures(3);
19+
}
20+
21+
public function testAggregateWithMultipleStages()
22+
{
23+
$cursor = $this->collection->aggregate(
24+
array(
25+
array('$sort' => array('x' => 1)),
26+
array('$match' => array('_id' => array('$gt' => 1))),
27+
),
28+
array('batchSize' => 2)
29+
);
30+
31+
$expected = array(
32+
array('_id' => 2, 'x' => 22),
33+
array('_id' => 3, 'x' => 33),
34+
);
35+
36+
$this->assertSame($expected, $cursor->toArray());
37+
}
38+
39+
public function testAggregateWithOut()
40+
{
41+
$outputCollection = new Collection($this->manager, $this->getNamespace() . '_output');
42+
$this->dropCollectionIfItExists($outputCollection);
43+
44+
$this->collection->aggregate(
45+
array(
46+
array('$sort' => array('x' => 1)),
47+
array('$match' => array('_id' => array('$gt' => 1))),
48+
array('$out' => $outputCollection->getCollectionName()),
49+
)
50+
);
51+
52+
$expected = array(
53+
array('_id' => 2, 'x' => 22),
54+
array('_id' => 3, 'x' => 33),
55+
);
56+
57+
$this->assertSame($expected, $outputCollection->find()->toArray());
58+
}
59+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
3+
namespace MongoDB\Tests\Collection\CrudSpec;
4+
5+
use MongoDB\Collection;
6+
7+
/**
8+
* Functional tests for aggregate() from the CRUD specification.
9+
*
10+
* @see https://github.com/mongodb/specifications/tree/master/source/crud/tests
11+
*/
12+
class CountFunctionalTest extends FunctionalTestCase
13+
{
14+
public function setUp()
15+
{
16+
parent::setUp();
17+
18+
$this->createFixtures(3);
19+
}
20+
21+
public function testCountWithoutFilter()
22+
{
23+
$this->assertSame(3, $this->collection->count());
24+
}
25+
26+
public function testCountWithFilter()
27+
{
28+
$filter = array('_id' => array('$gt' => 1));
29+
30+
$this->assertSame(2, $this->collection->count($filter));
31+
}
32+
33+
public function testCountWithSkipAndLimit()
34+
{
35+
$filter = array();
36+
$options = array('skip' => 1, 'limit' => 3);
37+
38+
$this->assertSame(2, $this->collection->count($filter, $options));
39+
}
40+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
3+
namespace MongoDB\Tests\Collection\CrudSpec;
4+
5+
use MongoDB\Collection;
6+
7+
/**
8+
* Functional tests for aggregate() from the CRUD specification.
9+
*
10+
* @see https://github.com/mongodb/specifications/tree/master/source/crud/tests
11+
*/
12+
class DistinctFunctionalTest extends FunctionalTestCase
13+
{
14+
public function setUp()
15+
{
16+
parent::setUp();
17+
18+
$this->createFixtures(3);
19+
}
20+
21+
public function testDistinctWithoutFilter()
22+
{
23+
$this->assertSame(array(11, 22, 33), $this->collection->distinct('x'));
24+
}
25+
26+
public function testDistinctWithFilter()
27+
{
28+
$filter = array('_id' => array('$gt' => 1));
29+
30+
$this->assertSame(array(22, 33), $this->collection->distinct('x', $filter));
31+
}
32+
}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
<?php
2+
3+
namespace MongoDB\Tests\Collection\CrudSpec;
4+
5+
use MongoDB\Collection;
6+
7+
/**
8+
* Functional tests for find() from the CRUD specification.
9+
*
10+
* @see https://github.com/mongodb/specifications/tree/master/source/crud/tests
11+
*/
12+
class FindFunctionalTest extends FunctionalTestCase
13+
{
14+
public function setUp()
15+
{
16+
parent::setUp();
17+
18+
$this->createFixtures(5);
19+
}
20+
21+
public function testFindWithFilter()
22+
{
23+
$filter = array('_id' => 1);
24+
25+
$expected = array(
26+
array('_id' => 1, 'x' => 11),
27+
);
28+
29+
$this->assertSame($expected, $this->collection->find($filter)->toArray());
30+
}
31+
32+
public function testFindWithFilterSortSkipAndLimit()
33+
{
34+
$filter = array('_id' => array('$gt' => 2));
35+
$options = array(
36+
'sort' => array('_id' => 1),
37+
'skip' => 2,
38+
'limit' => 2,
39+
);
40+
41+
$expected = array(
42+
array('_id' => 5, 'x' => 55),
43+
);
44+
45+
$this->assertSame($expected, $this->collection->find($filter, $options)->toArray());
46+
}
47+
48+
public function testFindWithLimitSortAndBatchSize()
49+
{
50+
$filter = array();
51+
$options = array(
52+
'sort' => array('_id' => 1),
53+
'limit' => 4,
54+
'batchSize' => 2,
55+
);
56+
57+
$expected = array(
58+
array('_id' => 1, 'x' => 11),
59+
array('_id' => 2, 'x' => 22),
60+
array('_id' => 3, 'x' => 33),
61+
array('_id' => 4, 'x' => 44),
62+
);
63+
64+
$this->assertSame($expected, $this->collection->find($filter, $options)->toArray());
65+
}
66+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
3+
namespace MongoDB\Tests\Collection\CrudSpec;
4+
5+
use MongoDB\Driver\BulkWrite;
6+
use MongoDB\Tests\Collection\FunctionalTestCase as BaseFunctionalTestCase;
7+
8+
/**
9+
* Base class for Collection CRUD spec functional tests.
10+
*/
11+
abstract class FunctionalTestCase extends BaseFunctionalTestCase
12+
{
13+
/**
14+
* Create data fixtures.
15+
*
16+
* @param integer $n
17+
*/
18+
protected function createFixtures($n)
19+
{
20+
$bulkWrite = new BulkWrite(true);
21+
22+
for ($i = 1; $i <= $n; $i++) {
23+
$bulkWrite->insert(array(
24+
'_id' => $i,
25+
'x' => (integer) ($i . $i),
26+
));
27+
}
28+
29+
$result = $this->manager->executeBulkWrite($this->getNamespace(), $bulkWrite);
30+
31+
$this->assertEquals($n, $result->getInsertedCount());
32+
}
33+
}

0 commit comments

Comments
 (0)