added MongoStatement method to count results before skip/limit
This commit is contained in:
@ -16,6 +16,15 @@ class MongoStatement extends DbStatement
|
||||
|
||||
protected $insertId = false;
|
||||
|
||||
public function count()
|
||||
{
|
||||
if ($this->result instanceof MongoCursor) {
|
||||
return $this->result->count();
|
||||
} else {
|
||||
throw new GeneralException('MongoStatement error. Impossible count result of opened cursor.');
|
||||
}
|
||||
}
|
||||
|
||||
public function order($sort = array())
|
||||
{
|
||||
if ($this->result instanceof MongoCursor) {
|
||||
|
@ -197,6 +197,21 @@ class MongoDriverTest extends PHPUnit_Framework_TestCase
|
||||
* @runInSeparateProcess
|
||||
* @group Mongo
|
||||
*/
|
||||
public function testCursorCount()
|
||||
{
|
||||
if (!defined('DEBUG')) {
|
||||
define('DEBUG', false);
|
||||
}
|
||||
$mongo = new MongoDriver($this->conf);
|
||||
$this->assertEquals(5, $mongo->find('items')->count(5));
|
||||
$this->assertCount(3, $mongo->find('items')->limit(3)->fetchAll());
|
||||
$this->assertEquals(5, $mongo->count('items'));
|
||||
}
|
||||
|
||||
/**
|
||||
* @runInSeparateProcess
|
||||
* @group Mongo
|
||||
*/
|
||||
public function testGet()
|
||||
{
|
||||
if (!defined('DEBUG')) {
|
||||
|
@ -338,6 +338,43 @@ class MongoStatementTest extends PHPUnit_Framework_TestCase
|
||||
* @runInSeparateProcess
|
||||
* @group Mongo
|
||||
*/
|
||||
public function testCount()
|
||||
{
|
||||
if (!defined('DEBUG')) {
|
||||
define('DEBUG', true);
|
||||
}
|
||||
$this->setDriverGetConnectionMethod()->setRequestForFetch();
|
||||
|
||||
$this->stmt->execute();
|
||||
$this->assertSame(10, $this->stmt->count());
|
||||
|
||||
$this->stmt->fetch();
|
||||
}
|
||||
|
||||
/**
|
||||
* @runInSeparateProcess
|
||||
* @group Mongo
|
||||
*/
|
||||
public function testCountException()
|
||||
{
|
||||
if (!defined('DEBUG')) {
|
||||
define('DEBUG', true);
|
||||
}
|
||||
$this->setDriverGetConnectionMethod();
|
||||
$this->request
|
||||
->expects($this->once())
|
||||
->method('execute')
|
||||
->will($this->returnValue(array('some' => 'val')));
|
||||
|
||||
$this->stmt->execute();
|
||||
$this->setExpectedException('GeneralException', 'MongoStatement error. Impossible count result of opened cursor');
|
||||
$this->stmt->count();
|
||||
}
|
||||
|
||||
/**
|
||||
* @runInSeparateProcess
|
||||
* @group Mongo
|
||||
*/
|
||||
public function testOrderException()
|
||||
{
|
||||
if (!defined('DEBUG')) {
|
||||
|
Reference in New Issue
Block a user