Browse Source

modified model classes with tests

master
Anton Grebnev 13 years ago
parent
commit
7e8fe0f94d
  1. 11
      model/MongoDbCommand.php
  2. 5
      model/MongoDriver.php
  3. 19
      model/MongoModel.php
  4. 10
      model/MongoStatement.php
  5. 28
      tests/model/MongoDbCommandTest.php
  6. 22
      tests/model/MongoDriverTest.php
  7. 39
      tests/model/MongoStatementTest.php
  8. 60
      tests/model/MyDbDriver.php
  9. 43
      tests/model/MySQLiStatementTest.php
  10. 60
      tests/model/SqlModelTest.php

11
model/MongoDbCommand.php

@ -92,9 +92,13 @@ class InsertMongoCommand extends MongoDbCommand
protected $safe = true;
protected $insertId = false;
protected function concreteExecute()
{
return $this->collection->insert($this->data, array('safe' => $this->safe));
$result = $this->collection->insert($this->data, array('safe' => $this->safe));
$this->insertId = $this->data['_id'];
return $result;
}
protected function checkParams()
@ -105,6 +109,11 @@ class InsertMongoCommand extends MongoDbCommand
return false;
}
}
public function getInsertId()
{
return $this->insertId;
}
}
class UpdateMongoCommand extends MongoDbCommand

5
model/MongoDriver.php

@ -55,8 +55,9 @@ class MongoDriver extends NoSqlDbDriver
'data' => $data,
'safe' => $safe
);
return $this->query($command, $params)->affectedRows();
$result = $this->query($command, $params);
$this->last_insert_id = $result->getInsertId();
return $result->affectedRows();
}
public function update($collection, $data, $condition = array(), $multiple = true, $upsert = false, $safe = true)

19
model/MongoModel.php

@ -13,26 +13,11 @@
abstract class MongoModel extends Model
{
public function find($condition)
public function find($condition = array())
{
return $this->db->find($this->table(), $condition);
}
protected function order(MongoStatement $cursor, $sort = array())
{
return $cursor->sort($sort);
}
protected function skip(MongoStatement $cursor, $skip = 0)
{
return $cursor->skip($skip);
}
protected function limit(MongoStatement $cursor, $limit = 0)
{
return $cursor->limit($limit);
}
public function get($id)
{
return $this->db->get($this->table(), array('_id' => $id))->fetch();
@ -40,7 +25,7 @@ abstract class MongoModel extends Model
public function delete($id)
{
return $this->db->delete($this->table(), array('id' => $id));
return $this->db->delete($this->table(), array('_id' => $id));
}
protected function fetchField($data, $params = array(), $field, $cache_key = null)

10
model/MongoStatement.php

@ -14,6 +14,8 @@
class MongoStatement extends DbStatement
{
protected $insertId = false;
public function order($sort = array())
{
if ($this->result instanceof MongoCursor) {
@ -133,6 +135,9 @@ class MongoStatement extends DbStatement
if ($result instanceof MongoCursor || is_array($result)) {
$this->result = $result;
}
if($request instanceof InsertMongoCommand) {
$this->insertId = $request->getInsertId();
}
return true;
} else {
throw new Exception('No connection to MongoDB server.');
@ -148,4 +153,9 @@ class MongoStatement extends DbStatement
{
return $this->request;
}
public function getInsertId()
{
return $this->insertId;
}
}

28
tests/model/MongoDbCommandTest.php

@ -65,6 +65,28 @@ class MongoDbCommandTest extends PHPUnit_Framework_TestCase
$cmd->bindParam('condition', array('name' => 'bread'))->bindParam('fields', array());
$result = $cmd->execute();
$this->assertEquals(0, $result->count());
$cmd = MongoCommandBuilder::factory(MongoCommandBuilder::INSERT, $this->collection);
$cmd
->bindParam('data', array('name' => 'insert'))
->bindParam('safe', true);
$cmd->execute();
$cmd = MongoCommandBuilder::factory(MongoCommandBuilder::INSERT, $this->collection);
$cmd
->bindParam('data', array('name' => 'insert'))
->bindParam('safe', true);
$cmd->execute();
$cmd = MongoCommandBuilder::factory(MongoCommandBuilder::FIND, $this->collection);
$cmd->bindParam('condition', array('name' => 'insert'))->bindParam('fields', array());
$this->assertEquals(2, $cmd->execute()->count());
$cmd
->bindParam('condition', array('name' => 'insert'))
->bindParam('fields', array())
->bindParam('multiple', false);
$result = $cmd->execute();
$this->assertEquals('insert', $result['name']);
}
/**
@ -81,10 +103,15 @@ class MongoDbCommandTest extends PHPUnit_Framework_TestCase
public function testInsertCommand()
{
$cmd = MongoCommandBuilder::factory(MongoCommandBuilder::INSERT, $this->collection);
$cmd
->bindParam('data', array('name' => 'insert'))
->bindParam('safe', true);
$this->assertFalse($cmd->getInsertId());
$this->assertArrayHasKey('n', $cmd->execute());
$this->assertNotEmpty($cmd->getInsertId());
$cmd = MongoCommandBuilder::factory(MongoCommandBuilder::FIND, $this->collection);
$cmd->bindParam('condition', array('name' => 'insert'))->bindParam('fields', array());
@ -172,5 +199,4 @@ class MongoDbCommandTest extends PHPUnit_Framework_TestCase
$cmd = MongoCommandBuilder::factory(MongoCommandBuilder::REMOVE, $this->collection);
$cmd->execute();
}
}

22
tests/model/MongoDriverTest.php

@ -219,12 +219,32 @@ class MongoDriverTest extends PHPUnit_Framework_TestCase
$mongo = new MongoDriver($this->conf);
$this->assertEquals(1, $mongo->find('items', array('name' => 'bread'))->numRows());
$this->assertEquals(0, $mongo->insert('items', array('name' => 'bread')));
//$this->assertNotEmpty($mongo->getInsertId());
$this->assertNotEmpty($mongo->getInsertId());
$this->assertEquals(2, $mongo->find('items', array('name' => 'bread'))->numRows());
$this->assertEquals(0, $mongo->insert('items', array('name' => 'meat', 'weight' => 230)));
$this->assertEquals(230, $mongo->get('items', array('name' => 'meat'))->fetch()->weight);
}
public function testGetInsertId()
{
if (!defined('DEBUG')) {
define('DEBUG', false);
}
$mongo = new MongoDriver($this->conf);
$this->assertEquals(0, $mongo->getInsertId());
$this->assertEquals(1, $mongo->find('items', array('name' => 'bread'))->numRows());
$this->assertEquals(0, $mongo->insert('items', array('name' => 'bread')));
$this->assertEquals(2, $mongo->find('items', array('name' => 'bread'))->numRows());
$id1 = $mongo->getInsertId();
$this->assertNotEmpty($id1);
$this->assertEquals(0, $mongo->insert('items', array('name' => 'bread')));
$id2 = $mongo->getInsertId();
$this->assertNotEmpty($id2);
$this->assertNotEquals($id1, $id2);
$this->assertEquals(3, $mongo->find('items', array('name' => 'bread'))->numRows());
}
/**
* @runInSeparateProcess
*/

39
tests/model/MongoStatementTest.php

@ -48,7 +48,7 @@ class MongoStatementTest extends PHPUnit_Framework_TestCase
->getMock();
$this->request = $this->getMockBuilder('MongoDbCommandMock')
->disableOriginalConstructor()
->setMethods(array('execute', 'bindParam'))
->setMethods(array('execute', 'bindParam', 'getInsertId'))
->getMock();
$this->stmt = new MongoStatement($this->driver, $this->request);
}
@ -84,13 +84,42 @@ class MongoStatementTest extends PHPUnit_Framework_TestCase
}
$this->setDriverGetConnectionMethod();
$this->request
->expects($this->any())
->expects($this->once())
->method('execute')
->will($this->returnValue(array('n' => 20, 'ok' => 1)));
$this->stmt->execute();
$this->assertEquals(20, $this->stmt->affectedRows());
}
/**
* @runInSeparateProcess
*/
public function testGetInsertId()
{
if (!defined('DEBUG')) {
define('DEBUG', false);
}
$this->setDriverGetConnectionMethod();
$this->request = $this->getMockBuilder('InsertMongoCommand')
->disableOriginalConstructor()
->setMethods(array('execute', 'bindParam', 'getInsertId'))
->getMock();
$this->request
->expects($this->once())
->method('execute')
->will($this->returnValue(array('n' => 20, 'ok' => 1)));
$this->request
->expects($this->once())
->method('getInsertId')
->will($this->returnValue('4b0rrs'));
$this->stmt = new MongoStatement($this->driver, $this->request);
$this->stmt->execute();
$this->assertEquals('4b0rrs', $this->stmt->getInsertId());
}
/**
* @runInSeparateProcess
@ -286,6 +315,8 @@ class MongoStatementTest extends PHPUnit_Framework_TestCase
$this->stmt->execute();
$this->assertInstanceOf('MongoStatement', $this->stmt->order(array('id' => 1)));
$this->assertInstanceOf('MongoStatement', $this->stmt->limit(10));
$this->assertInstanceOf('MongoStatement', $this->stmt->skip(1));
$this->stmt->fetch();
$this->stmt->fetch();
@ -368,7 +399,7 @@ class MongoStatementTest extends PHPUnit_Framework_TestCase
->setMethods(array('count'))
->disableOriginalConstructor()
->getMock();
$resultMock
->expects($this->any())
->method('count')
@ -381,7 +412,7 @@ class MongoStatementTest extends PHPUnit_Framework_TestCase
return $this;
}
public function setRequestForFetch()
{
$resultMock = $this->getMockBuilder('MongoCursor')

60
tests/model/MyDbDriver.php

@ -0,0 +1,60 @@
<?php
abstract class MyDbDriver extends DbDriver
{
public function getInsertId($table = null, $key = null)
{
return true;
}
public function quoteIdentifier($param)
{
return $param;
}
public function quote($param)
{
return $param;
}
public function insert($table, $bind, $on_duplicate = array())
{
return $table;
}
public function update($table, $bind, $where = '')
{
return $table;
}
public function delete($table, $where = '')
{
return $table;
}
public function query($sql, $params = array())
{
$conf = array('driver' => 'MockDbDriver', 'hostname' => 'somehost', 'database' => 'db', 'username' => 'test', 'password' => '1234');
return new MockDbDriver($conf);
}
public function execute()
{
return true;
}
public function fetch()
{
return true;
}
public function fetchField($field)
{
return $field;
}
public function fetchAll()
{
return true;
}
}

43
tests/model/MySQLiStatementTest.php

@ -175,6 +175,49 @@ class MySQLiStatementTest extends PHPUnit_Framework_TestCase
/**
* @runInSeparateProcess
*/
public function testFetchPairs()
{
if (!defined('DEBUG')) {
define('DEBUG', false);
}
$resultMock = $this->getMockBuilder('mysqli_result')
->disableOriginalConstructor()
->setMethods(array('fetch_array', 'close'))
->setMockClassName('Mysqli_Result_Mock')
->getMock();
$resultMock
->expects($this->at(0))
->method('fetch_array')
->will($this->returnValue(array('pair', 'value')));
$resultMock
->expects($this->at(1))
->method('fetch_array')
->will($this->returnValue(false));
$resultMock
->expects($this->any())
->method('close')
->will($this->returnValue(TRUE));
$mysqliMock = $this->getMock('MysqliDrvr', array('query'));
$mysqliMock
->expects($this->any())
->method('query')
->with($this->anything())
->will($this->returnValue($resultMock));
$this->driver
->expects($this->any())
->method('getConnection')
->will($this->returnValue($mysqliMock));
$this->stmt->execute(array('place' => 'place_val', 'new' => 'new_val'));
$this->assertEquals(array('pair' => 'value'), $this->stmt->fetchPairs());
}
/**
* @runInSeparateProcess
*/
public function testFetchWithDebug()
{
if (!defined('DEBUG')) {

60
tests/model/SqlModelTest.php

@ -16,6 +16,7 @@ require_once dirname(__FILE__) . '/../../cache/Cacher.php';
require_once dirname(__FILE__) . '/../../model/DbExpr.php';
require_once dirname(__FILE__) . '/../../model/Db.php';
require_once dirname(__FILE__) . '/../../model/DbDriver.php';
require_once dirname(__FILE__) . '/MyDbDriver.php';
require_once dirname(__FILE__) . '/../../model/Model.php';
require_once dirname(__FILE__) . '/../../model/SqlModel.php';
@ -246,63 +247,4 @@ class SqlModelTest extends PHPUnit_Framework_TestCase
return $className;
}
}
}
abstract class MyDbDriver extends DbDriver
{
public function getInsertId($table = null, $key = null)
{
return true;
}
public function quoteIdentifier($param)
{
return $param;
}
public function quote($param)
{
return $param;
}
public function insert($table, $bind, $on_duplicate = array())
{
return $table;
}
public function update($table, $bind, $where = '')
{
return $table;
}
public function delete($table, $where = '')
{
return $table;
}
public function query($sql, $params = array())
{
$conf = array('driver' => 'MockDbDriver', 'hostname' => 'localhost', 'database' => 'db', 'username' => 'test', 'password' => '1234');
return new MockDbDriver($conf);
}
public function execute()
{
return true;
}
public function fetch()
{
return true;
}
public function fetchField($field)
{
return $field;
}
public function fetchAll()
{
return true;
}
}
Loading…
Cancel
Save