Skip to content

Commit 87cd989

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

File tree

5 files changed

+224
-0
lines changed

5 files changed

+224
-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+
* CRUD spec functional tests for aggregate().
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: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
3+
namespace MongoDB\Tests\Collection\CrudSpec;
4+
5+
/**
6+
* CRUD spec functional tests for count().
7+
*
8+
* @see https://github.com/mongodb/specifications/tree/master/source/crud/tests
9+
*/
10+
class CountFunctionalTest extends FunctionalTestCase
11+
{
12+
public function setUp()
13+
{
14+
parent::setUp();
15+
16+
$this->createFixtures(3);
17+
}
18+
19+
public function testCountWithoutFilter()
20+
{
21+
$this->assertSame(3, $this->collection->count());
22+
}
23+
24+
public function testCountWithFilter()
25+
{
26+
$filter = array('_id' => array('$gt' => 1));
27+
28+
$this->assertSame(2, $this->collection->count($filter));
29+
}
30+
31+
public function testCountWithSkipAndLimit()
32+
{
33+
$filter = array();
34+
$options = array('skip' => 1, 'limit' => 3);
35+
36+
$this->assertSame(2, $this->collection->count($filter, $options));
37+
}
38+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
3+
namespace MongoDB\Tests\Collection\CrudSpec;
4+
5+
/**
6+
* CRUD spec functional tests for distinct().
7+
*
8+
* @see https://github.com/mongodb/specifications/tree/master/source/crud/tests
9+
*/
10+
class DistinctFunctionalTest extends FunctionalTestCase
11+
{
12+
public function setUp()
13+
{
14+
parent::setUp();
15+
16+
$this->createFixtures(3);
17+
}
18+
19+
public function testDistinctWithoutFilter()
20+
{
21+
$this->assertSame(array(11, 22, 33), $this->collection->distinct('x'));
22+
}
23+
24+
public function testDistinctWithFilter()
25+
{
26+
$filter = array('_id' => array('$gt' => 1));
27+
28+
$this->assertSame(array(22, 33), $this->collection->distinct('x', $filter));
29+
}
30+
}
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 find().
7+
*
8+
* @see https://github.com/mongodb/specifications/tree/master/source/crud/tests
9+
*/
10+
class FindFunctionalTest extends FunctionalTestCase
11+
{
12+
public function setUp()
13+
{
14+
parent::setUp();
15+
16+
$this->createFixtures(5);
17+
}
18+
19+
public function testFindWithFilter()
20+
{
21+
$filter = array('_id' => 1);
22+
23+
$expected = array(
24+
array('_id' => 1, 'x' => 11),
25+
);
26+
27+
$this->assertSame($expected, $this->collection->find($filter)->toArray());
28+
}
29+
30+
public function testFindWithFilterSortSkipAndLimit()
31+
{
32+
$filter = array('_id' => array('$gt' => 2));
33+
$options = array(
34+
'sort' => array('_id' => 1),
35+
'skip' => 2,
36+
'limit' => 2,
37+
);
38+
39+
$expected = array(
40+
array('_id' => 5, 'x' => 55),
41+
);
42+
43+
$this->assertSame($expected, $this->collection->find($filter, $options)->toArray());
44+
}
45+
46+
public function testFindWithLimitSortAndBatchSize()
47+
{
48+
$filter = array();
49+
$options = array(
50+
'sort' => array('_id' => 1),
51+
'limit' => 4,
52+
'batchSize' => 2,
53+
);
54+
55+
$expected = array(
56+
array('_id' => 1, 'x' => 11),
57+
array('_id' => 2, 'x' => 22),
58+
array('_id' => 3, 'x' => 33),
59+
array('_id' => 4, 'x' => 44),
60+
);
61+
62+
$this->assertSame($expected, $this->collection->find($filter, $options)->toArray());
63+
}
64+
}
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)