Browse Source

added Order, Limit, Skip methods to MongoStatement

master
Anton Grebnev 13 years ago
parent
commit
594ec034f9
  1. 12
      model/DbStatement.php
  2. 30
      model/MongoStatement.php
  3. 12
      model/MySQLiStatement.php
  4. 6
      tests/model/DbStatementTest.php
  5. 39
      tests/model/MongoDriverTest.php
  6. 90
      tests/model/MongoStatementTest.php

12
model/DbStatement.php

@ -88,18 +88,6 @@ abstract class DbStatement
return false;
}
/**
* @return array
*/
public function fetchPairs()
{
$data = array();
while ($row = $this->fetch(Db::FETCH_NUM)) {
$data[$row[0]] = $row[1];
}
return $data;
}
/* Abstract methods */
abstract public function bindParam($param, &$value);

30
model/MongoStatement.php

@ -14,6 +14,36 @@
class MongoStatement extends DbStatement
{
public function order($sort = array())
{
if ($this->result instanceof MongoCursor) {
$this->result->sort($sort);
return $this;
} else {
throw new Exception('MongoStatement error. Impossible order results of opened cursor.');
}
}
public function skip($skip = 0)
{
if ($this->result instanceof MongoCursor) {
$this->result->skip($skip);
return $this;
} else {
throw new Exception('MongoStatement error. Impossible skip results of opened cursor.');
}
}
public function limit($limit = 0)
{
if ($this->result instanceof MongoCursor) {
$this->result->limit($limit);
return $this;
} else {
throw new Exception('MongoStatement error. Impossible limit results of opened cursor.');
}
}
public function fetch($style = Db::FETCH_OBJ)
{
if (!$this->result) {

12
model/MySQLiStatement.php

@ -117,6 +117,18 @@ class MySQLiStatement extends DbStatement
{
return $this->result->fetch_object($class);
}
/**
* @return array
*/
public function fetchPairs()
{
$data = array();
while ($row = $this->fetch(Db::FETCH_NUM)) {
$data[$row[0]] = $row[1];
}
return $data;
}
public function close()
{

6
tests/model/DbStatementTest.php

@ -65,9 +65,9 @@ class DbStatementTest extends PHPUnit_Framework_TestCase
$this->assertEquals(11, $result[0]->one);
$this->assertEquals(32, $result[2]->two);
reset($this->testData);
$result = $this->stmt->fetchPairs();
$this->assertEquals(31, $result['one']);
// reset($this->testData);
// $result = $this->stmt->fetchPairs();
// $this->assertEquals(31, $result['one']);
}
public function dbStatementFetch($style)

39
tests/model/MongoDriverTest.php

@ -105,7 +105,6 @@ class MongoDriverTest extends PHPUnit_Framework_TestCase
$this->assertFalse($mongo->isConnected());
}
/**
* @runInSeparateProcess
*/
@ -139,6 +138,44 @@ class MongoDriverTest extends PHPUnit_Framework_TestCase
/**
* @runInSeparateProcess
*/
public function testOrderSkipLimit()
{
if (!defined('DEBUG')) {
define('DEBUG', false);
}
$mongo = new MongoDriver($this->conf);
$count = $mongo->find('items', array())->numRows();
$this->assertEquals(1, $mongo->find('items', array('name' => 'bread'))->numRows());
$this->assertEquals(2, $mongo->find('items', array('name' => 'eggs'))->numRows());
$mongo->insert('items', array('name' => 'fdsbssc'));
$mongo->insert('items', array('name' => 'boc'));
$mongo->insert('items', array('name' => 'abc'));
$mongo->insert('items', array('name' => 'vcxxc'));
$mongo->insert('items', array('name' => 'abbc'));
$mongo->insert('items', array('name' => 'dsbssc'));
$mongo->insert('items', array('name' => 'bssc'));
$data = $mongo->find('items', array());
$this->assertEquals($count + 7, $data->numRows());
$data->order(array('name' => 1));
$this->assertEquals('abbc', $data->fetch()->name);
$this->assertEquals('abc', $data->fetch()->name);
$this->assertEquals('boc', $data->fetch()->name);
$data = $mongo->find('items', array());
$data->order(array('name' => -1));
$data->skip(3);
$data->limit(1);
while($row = $data->fetch()) {
$this->assertEquals('fdsbssc', $row->name);
}
}
/**
* @runInSeparateProcess
*/
public function testGet()
{
if (!defined('DEBUG')) {

90
tests/model/MongoStatementTest.php

@ -271,7 +271,84 @@ class MongoStatementTest extends PHPUnit_Framework_TestCase
->will($this->returnValue(array('some' => 'val')));
$this->stmt->execute();
$result = $this->stmt->fetch(222);
$this->stmt->fetch(222);
}
/**
* @runInSeparateProcess
*/
public function testSkipOrderLimit()
{
if (!defined('DEBUG')) {
define('DEBUG', true);
}
$this->setDriverGetConnectionMethod()->setRequestForFetch();
$this->stmt->execute();
$this->assertInstanceOf('MongoStatement', $this->stmt->order(array('id' => 1)));
$this->stmt->fetch();
$this->stmt->fetch();
}
/**
* @runInSeparateProcess
* @expectedException Exception
* @expectedExceptionMessage MongoStatement error. Impossible order results of opened cursor
*/
public function testOrderException()
{
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->stmt->order(array('id' => 1));
}
/**
* @runInSeparateProcess
* @expectedException Exception
* @expectedExceptionMessage MongoStatement error. Impossible skip results of opened cursor
*/
public function testSkipException()
{
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->stmt->skip(array('id' => 1));
}
/**
* @runInSeparateProcess
* @expectedException Exception
* @expectedExceptionMessage MongoStatement error. Impossible limit results of opened cursor
*/
public function testLimitException()
{
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->stmt->limit(array('id' => 1));
}
private function setDriverGetConnectionMethod()
@ -308,12 +385,21 @@ class MongoStatementTest extends PHPUnit_Framework_TestCase
public function setRequestForFetch()
{
$resultMock = $this->getMockBuilder('MongoCursor')
->setMethods(array('count', 'getNext'))
->setMethods(array('count', 'getNext', 'limit', 'sort', 'skip'))
->disableOriginalConstructor()
->getMock();
$resultMock
->expects($this->any())
->method('limit');
$resultMock
->expects($this->any())
->method('sort');
$resultMock
->expects($this->any())
->method('skip');
$resultMock
->expects($this->any())
->method('count')
->will($this->returnValue(10));
$resultMock

Loading…
Cancel
Save