Commit 87cd9899 authored by Jeremy Mikola's avatar Jeremy Mikola

PHPLIB-58: Functional tests for CRUD spec read methods

parent c991d3a3
<?php
namespace MongoDB\Tests\Collection\CrudSpec;
use MongoDB\Collection;
/**
* CRUD spec functional tests for aggregate().
*
* @see https://github.com/mongodb/specifications/tree/master/source/crud/tests
*/
class AggregateFunctionalTest extends FunctionalTestCase
{
public function setUp()
{
parent::setUp();
$this->createFixtures(3);
}
public function testAggregateWithMultipleStages()
{
$cursor = $this->collection->aggregate(
array(
array('$sort' => array('x' => 1)),
array('$match' => array('_id' => array('$gt' => 1))),
),
array('batchSize' => 2)
);
$expected = array(
array('_id' => 2, 'x' => 22),
array('_id' => 3, 'x' => 33),
);
$this->assertSame($expected, $cursor->toArray());
}
public function testAggregateWithOut()
{
$outputCollection = new Collection($this->manager, $this->getNamespace() . '_output');
$this->dropCollectionIfItExists($outputCollection);
$this->collection->aggregate(
array(
array('$sort' => array('x' => 1)),
array('$match' => array('_id' => array('$gt' => 1))),
array('$out' => $outputCollection->getCollectionName()),
)
);
$expected = array(
array('_id' => 2, 'x' => 22),
array('_id' => 3, 'x' => 33),
);
$this->assertSame($expected, $outputCollection->find()->toArray());
}
}
<?php
namespace MongoDB\Tests\Collection\CrudSpec;
/**
* CRUD spec functional tests for count().
*
* @see https://github.com/mongodb/specifications/tree/master/source/crud/tests
*/
class CountFunctionalTest extends FunctionalTestCase
{
public function setUp()
{
parent::setUp();
$this->createFixtures(3);
}
public function testCountWithoutFilter()
{
$this->assertSame(3, $this->collection->count());
}
public function testCountWithFilter()
{
$filter = array('_id' => array('$gt' => 1));
$this->assertSame(2, $this->collection->count($filter));
}
public function testCountWithSkipAndLimit()
{
$filter = array();
$options = array('skip' => 1, 'limit' => 3);
$this->assertSame(2, $this->collection->count($filter, $options));
}
}
<?php
namespace MongoDB\Tests\Collection\CrudSpec;
/**
* CRUD spec functional tests for distinct().
*
* @see https://github.com/mongodb/specifications/tree/master/source/crud/tests
*/
class DistinctFunctionalTest extends FunctionalTestCase
{
public function setUp()
{
parent::setUp();
$this->createFixtures(3);
}
public function testDistinctWithoutFilter()
{
$this->assertSame(array(11, 22, 33), $this->collection->distinct('x'));
}
public function testDistinctWithFilter()
{
$filter = array('_id' => array('$gt' => 1));
$this->assertSame(array(22, 33), $this->collection->distinct('x', $filter));
}
}
<?php
namespace MongoDB\Tests\Collection\CrudSpec;
/**
* CRUD spec functional tests for find().
*
* @see https://github.com/mongodb/specifications/tree/master/source/crud/tests
*/
class FindFunctionalTest extends FunctionalTestCase
{
public function setUp()
{
parent::setUp();
$this->createFixtures(5);
}
public function testFindWithFilter()
{
$filter = array('_id' => 1);
$expected = array(
array('_id' => 1, 'x' => 11),
);
$this->assertSame($expected, $this->collection->find($filter)->toArray());
}
public function testFindWithFilterSortSkipAndLimit()
{
$filter = array('_id' => array('$gt' => 2));
$options = array(
'sort' => array('_id' => 1),
'skip' => 2,
'limit' => 2,
);
$expected = array(
array('_id' => 5, 'x' => 55),
);
$this->assertSame($expected, $this->collection->find($filter, $options)->toArray());
}
public function testFindWithLimitSortAndBatchSize()
{
$filter = array();
$options = array(
'sort' => array('_id' => 1),
'limit' => 4,
'batchSize' => 2,
);
$expected = array(
array('_id' => 1, 'x' => 11),
array('_id' => 2, 'x' => 22),
array('_id' => 3, 'x' => 33),
array('_id' => 4, 'x' => 44),
);
$this->assertSame($expected, $this->collection->find($filter, $options)->toArray());
}
}
<?php
namespace MongoDB\Tests\Collection\CrudSpec;
use MongoDB\Driver\BulkWrite;
use MongoDB\Tests\Collection\FunctionalTestCase as BaseFunctionalTestCase;
/**
* Base class for Collection CRUD spec functional tests.
*/
abstract class FunctionalTestCase extends BaseFunctionalTestCase
{
/**
* Create data fixtures.
*
* @param integer $n
*/
protected function createFixtures($n)
{
$bulkWrite = new BulkWrite(true);
for ($i = 1; $i <= $n; $i++) {
$bulkWrite->insert(array(
'_id' => $i,
'x' => (integer) ($i . $i),
));
}
$result = $this->manager->executeBulkWrite($this->getNamespace(), $bulkWrite);
$this->assertEquals($n, $result->getInsertedCount());
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment