diff --git a/model/DbStatement.php b/model/DbStatement.php index 323b61d..5fcf52c 100644 --- a/model/DbStatement.php +++ b/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); diff --git a/model/MongoStatement.php b/model/MongoStatement.php index 0a94548..8b86913 100644 --- a/model/MongoStatement.php +++ b/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) { diff --git a/model/MySQLiStatement.php b/model/MySQLiStatement.php index e0fb4cf..3024d67 100644 --- a/model/MySQLiStatement.php +++ b/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() { diff --git a/tests/model/DbStatementTest.php b/tests/model/DbStatementTest.php index b19a955..a2ec50c 100644 --- a/tests/model/DbStatementTest.php +++ b/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) diff --git a/tests/model/MongoDriverTest.php b/tests/model/MongoDriverTest.php index bfc2bef..5bbd441 100644 --- a/tests/model/MongoDriverTest.php +++ b/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')) { diff --git a/tests/model/MongoStatementTest.php b/tests/model/MongoStatementTest.php index c203dcc..c7e10c1 100644 --- a/tests/model/MongoStatementTest.php +++ b/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