modified model classes with tests
This commit is contained in:
@ -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();
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -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
|
||||
*/
|
||||
|
@ -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
Normal file
60
tests/model/MyDbDriver.php
Normal file
@ -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;
|
||||
}
|
||||
}
|
@ -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')) {
|
||||
|
@ -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;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user