Browse Source

modified tests for new DbDriver hierarchy

master
Anton Grebnev 13 years ago
parent
commit
b9353065b8
  1. 73
      tests/model/DbStatementTest.php
  2. 40
      tests/model/MongoDbCommandTest.php
  3. 115
      tests/model/MongoDriverTest.php
  4. 78
      tests/model/MySQLiStatementTest.php

73
tests/model/DbStatementTest.php

@ -43,68 +43,7 @@ class DbStatementTest extends PHPUnit_Framework_TestCase
public function testConstruct()
{
$this->assertAttributeEquals($this->driver, 'driver', $this->stmt);
$this->assertAttributeEquals($this->sql, 'sql', $this->stmt);
}
public function testBindParam()
{
$val = $this->getMockBuilder('DbExpr')
->disableOriginalConstructor()
->getMock();
$this->assertFalse($this->stmt->bindParam('var', $val));
$this->assertTrue($this->stmt->bindParam('place', $val));
}
/**
* @expectedException Exception
* @expectedExceptionMessage Placeholder must be an array or string
* @TODO: change Exception Message 'Placeholder must be an array or string' - no arrays available
*/
public function testBindParamExceptionParam()
{
$val = 1;
$this->stmt->bindParam(array(), $val);
}
/**
* @expectedException Exception
* @expectedExceptionMessage Objects excepts DbExpr not allowed.
*/
public function testBindParamExceptionWrongObject()
{
$val = $this->getMock('NotDbExpr');
$this->stmt->bindParam('paa', $val);
}
public function testExecute()
{
$this->stmt
->expects($this->once())
->method('driverExecute')
->with($this->anything())
->will($this->returnCallback(array($this, 'dbStatementAssemble')));
$this->driver
->expects($this->any())
->method('quote')
->with($this->anything())
->will($this->returnCallback(array($this, 'driverQuote')));
$result = $this->stmt->execute(array('place' => 'place_val', 'new' => 'new_val'));
$this->assertEquals('SELECT * place_val FROM place_val AND new_val', $result);
}
public function testExecuteNoPlaceholders()
{
$this->sql = 'PLAIN SQL';
$this->stmt = $this->getMockForAbstractClass('DbStatement', array($this->driver, $this->sql));
$this->stmt
->expects($this->once())
->method('driverExecute')
->with($this->anything())
->will($this->returnCallback(array($this, 'dbStatementAssemble')));
$result = $this->stmt->execute(array());
$this->assertEquals('PLAIN SQL', $result);
$this->assertAttributeEquals($this->sql, 'request', $this->stmt);
}
public function testFetch()
@ -131,16 +70,6 @@ class DbStatementTest extends PHPUnit_Framework_TestCase
$this->assertEquals(31, $result['one']);
}
public function dbStatementAssemble($val)
{
return $val;
}
public function driverQuote($val)
{
return $val;
}
public function dbStatementFetch($style)
{
$result = null;

40
tests/model/MongoDbCommandTest.php

@ -22,6 +22,8 @@ class MongoDbCommandTest extends PHPUnit_Framework_TestCase
private $driver;
private $collection;
public function setUp()
{
$this->conf = array(
@ -38,6 +40,7 @@ class MongoDbCommandTest extends PHPUnit_Framework_TestCase
$db->authenticate($this->conf['username'], $this->conf['password']);
$collection = 'items';
$db->dropCollection($collection);
$this->collection = $db->selectCollection($collection);
}
public function testCommandFactory()
@ -58,8 +61,7 @@ class MongoDbCommandTest extends PHPUnit_Framework_TestCase
public function testFindCommand()
{
$collection = $this->driver->getCollection('items');
$cmd = MongoCommandBuilder::factory(MongoCommandBuilder::FIND, $collection);
$cmd = MongoCommandBuilder::factory(MongoCommandBuilder::FIND, $this->collection);
$cmd->bindParam('condition', array('name' => 'bread'))->bindParam('fields', array());
$result = $cmd->execute();
$this->assertEquals(0, $result->count());
@ -71,22 +73,20 @@ class MongoDbCommandTest extends PHPUnit_Framework_TestCase
*/
public function testFindCommandNotAllParamsBinded()
{
$collection = $this->driver->getCollection('items');
$cmd = MongoCommandBuilder::factory(MongoCommandBuilder::FIND, $collection);
$cmd = MongoCommandBuilder::factory(MongoCommandBuilder::FIND, $this->collection);
$cmd->bindParam('condition', array('name' => 'bread'));
$cmd->execute();
}
public function testInsertCommand()
{
$collection = $this->driver->getCollection('items');
$cmd = MongoCommandBuilder::factory(MongoCommandBuilder::INSERT, $collection);
$cmd = MongoCommandBuilder::factory(MongoCommandBuilder::INSERT, $this->collection);
$cmd
->bindParam('data', array('name' => 'insert'))
->bindParam('safe', true);
$this->assertArrayHasKey('n', $cmd->execute());
$cmd = MongoCommandBuilder::factory(MongoCommandBuilder::FIND, $collection);
$cmd = MongoCommandBuilder::factory(MongoCommandBuilder::FIND, $this->collection);
$cmd->bindParam('condition', array('name' => 'insert'))->bindParam('fields', array());
$result = $cmd->execute();
$this->assertEquals(1, $result->count());
@ -98,22 +98,19 @@ class MongoDbCommandTest extends PHPUnit_Framework_TestCase
*/
public function testInsertCommandNotAllParamsBinded()
{
$collection = $this->driver->getCollection('items');
$cmd = MongoCommandBuilder::factory(MongoCommandBuilder::INSERT, $collection);
$cmd->bindParam('data', array('name' => 'bread'));
$cmd = MongoCommandBuilder::factory(MongoCommandBuilder::INSERT, $this->collection);
$cmd->execute();
}
public function testUpdateCommand()
{
$collection = $this->driver->getCollection('items');
$cmd = MongoCommandBuilder::factory(MongoCommandBuilder::INSERT, $collection);
$cmd = MongoCommandBuilder::factory(MongoCommandBuilder::INSERT, $this->collection);
$cmd
->bindParam('data', array('name' => 'insert'))
->bindParam('safe', true);
$this->assertArrayHasKey('n', $cmd->execute());
$cmd = MongoCommandBuilder::factory(MongoCommandBuilder::UPDATE, $collection);
$cmd = MongoCommandBuilder::factory(MongoCommandBuilder::UPDATE, $this->collection);
$cmd
->bindParam('condition', array('name' => 'insert'))
->bindParam('data', array('$set' => array('name' => 'update')))
@ -121,7 +118,7 @@ class MongoDbCommandTest extends PHPUnit_Framework_TestCase
->bindParam('safe', true);
$this->assertArrayHasKey('n', $cmd->execute());
$cmd = MongoCommandBuilder::factory(MongoCommandBuilder::FIND, $collection);
$cmd = MongoCommandBuilder::factory(MongoCommandBuilder::FIND, $this->collection);
$cmd->bindParam('condition', array('name' => 'insert'))->bindParam('fields', array());
$result = $cmd->execute();
$this->assertEquals(0, $result->count());
@ -136,33 +133,31 @@ class MongoDbCommandTest extends PHPUnit_Framework_TestCase
*/
public function testUpdateCommandNotAllParamsBinded()
{
$collection = $this->driver->getCollection('items');
$cmd = MongoCommandBuilder::factory(MongoCommandBuilder::UPDATE, $collection);
$cmd = MongoCommandBuilder::factory(MongoCommandBuilder::UPDATE, $this->collection);
$cmd->bindParam('data', array('name' => 'bread'));
$cmd->execute();
}
public function testRemoveCommand()
{
$collection = $this->driver->getCollection('items');
$cmd = MongoCommandBuilder::factory(MongoCommandBuilder::INSERT, $collection);
$cmd = MongoCommandBuilder::factory(MongoCommandBuilder::INSERT, $this->collection);
$cmd
->bindParam('data', array('name' => 'insert'))
->bindParam('safe', true);
$this->assertArrayHasKey('n', $cmd->execute());
$cmd = MongoCommandBuilder::factory(MongoCommandBuilder::FIND, $collection);
$cmd = MongoCommandBuilder::factory(MongoCommandBuilder::FIND, $this->collection);
$cmd->bindParam('condition', array('name' => 'insert'))->bindParam('fields', array());
$result = $cmd->execute();
$this->assertEquals(1, $result->count());
$cmd = MongoCommandBuilder::factory(MongoCommandBuilder::REMOVE, $collection);
$cmd = MongoCommandBuilder::factory(MongoCommandBuilder::REMOVE, $this->collection);
$cmd
->bindParam('condition', array('name' => 'insert'))
->bindParam('safe', true);
$this->assertArrayHasKey('n', $cmd->execute());
$cmd = MongoCommandBuilder::factory(MongoCommandBuilder::FIND, $collection);
$cmd = MongoCommandBuilder::factory(MongoCommandBuilder::FIND, $this->collection);
$cmd->bindParam('condition', array('name' => 'insert'))->bindParam('fields', array());
$result = $cmd->execute();
$this->assertEquals(0, $result->count());
@ -174,8 +169,7 @@ class MongoDbCommandTest extends PHPUnit_Framework_TestCase
*/
public function testRemoveCommandNotAllParamsBinded()
{
$collection = $this->driver->getCollection('items');
$cmd = MongoCommandBuilder::factory(MongoCommandBuilder::REMOVE, $collection);
$cmd = MongoCommandBuilder::factory(MongoCommandBuilder::REMOVE, $this->collection);
$cmd->execute();
}

115
tests/model/MongoDriverTest.php

@ -10,8 +10,12 @@
* Unit tests for MongoDriver class
*/
require_once dirname(__FILE__) . '/../../model/Db.php';
require_once dirname(__FILE__) . '/../../model/DbDriver.php';
require_once dirname(__FILE__) . '/../../model/NoSqlDbDriver.php';
require_once dirname(__FILE__) . '/../../model/MongoDbCommand.php';
require_once dirname(__FILE__) . '/../../model/DbStatement.php';
require_once dirname(__FILE__) . '/../../model/MongoStatement.php';
require_once dirname(__FILE__) . '/../../model/MongoDriver.php';
class MongoDriverTest extends PHPUnit_Framework_TestCase
@ -101,66 +105,121 @@ class MongoDriverTest extends PHPUnit_Framework_TestCase
$this->assertFalse($mongo->isConnected());
}
/**
* @runInSeparateProcess
*/
public function testFind()
{
if (!defined('DEBUG')) {
define('DEBUG', false);
}
$mongo = new MongoDriver($this->conf);
$mongo->getConnection();
$this->assertEquals(1, $mongo->find('items', array('name' => 'bread'))->count());
$this->assertEquals(2, $mongo->find('items', array('name' => 'eggs'))->count());
$this->assertEquals(3, $mongo->find('items', array('price' => array('$lt' => 3.5)))->count());
$this->assertEquals(5, $mongo->find('items', array())->count());
$this->assertEquals(1, $mongo->find('items', array('name' => 'bread'))->numRows());
$this->assertEquals(2, $mongo->find('items', array('name' => 'eggs'))->numRows());
$eggs = $mongo->find('items', array('name' => 'eggs'));
$egg = $eggs->fetch();
$this->assertEquals(20, $egg->quantity);
$egg = $eggs->fetchObject();
$this->assertEquals('eggs', $egg->name);
$this->assertFalse($eggs->fetchObject());
$this->assertEquals(3, $mongo->find('items', array('price' => array('$lt' => 3.5)))->numRows());
$data = $mongo->find('items', array('price' => array('$lt' => 3.5)));
$count = 0;
while($row = $data->fetch(Db::FETCH_ASSOC)) {
$count++;
$this->assertLessThan(3.5, $row['price']);
}
$this->assertEquals(3, $count);
$this->assertEquals(5, $mongo->find('items', array())->numRows());
}
/**
* @runInSeparateProcess
*/
public function testGet()
{
if (!defined('DEBUG')) {
define('DEBUG', false);
}
$mongo = new MongoDriver($this->conf);
$mongo->getConnection();
$bread = $mongo->get('items', array('name' => 'bread'));
$this->assertEquals(3.2, $bread['price']);
$eggs = $mongo->get('items', array('name' => 'eggs'))->fetchObject();
$this->assertEquals(20, $eggs->quantity);
$eggs = $mongo->get('items', array('name' => 'eggs'))->fetch();
$this->assertEquals('eggs', $eggs->name);
$this->assertInstanceOf('MongoId', $eggs->_id);
}
/**
* @runInSeparateProcess
*/
public function testRemove()
{
if (!defined('DEBUG')) {
define('DEBUG', false);
}
$mongo = new MongoDriver($this->conf);
$mongo->getConnection();
$this->assertEquals(1, $mongo->find('items', array('name' => 'bread'))->count());
$this->assertEquals(1, $mongo->find('items', array('name' => 'bread'))->numRows());
$this->assertEquals(0, $mongo->delete('items', array('name' => 'esggs')));
$this->assertEquals(2, $mongo->delete('items', array('name' => 'eggs')));
$this->assertEquals(1, $mongo->delete('items', array('name' => 'bread')));
$this->assertEquals(0, $mongo->find('items', array('name' => 'bread'))->count());
$this->assertEquals(0, $mongo->find('items', array('name' => 'bread'))->numRows());
}
/**
* @runInSeparateProcess
*/
public function testInsert()
{
if (!defined('DEBUG')) {
define('DEBUG', false);
}
$mongo = new MongoDriver($this->conf);
$mongo->getConnection();
$this->assertEquals(1, $mongo->find('items', array('name' => 'bread'))->count());
$this->assertTrue($mongo->insert('items', array('name' => 'bread')));
$this->assertNotEmpty($mongo->getInsertId());
$this->assertEquals(2, $mongo->find('items', array('name' => 'bread'))->count());
$this->assertEquals(1, $mongo->find('items', array('name' => 'bread'))->numRows());
$this->assertEquals(0, $mongo->insert('items', array('name' => 'bread')));
//$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);
}
/**
* @runInSeparateProcess
*/
public function testUpdate()
{
if (!defined('DEBUG')) {
define('DEBUG', false);
}
$mongo = new MongoDriver($this->conf);
$mongo->getConnection();
$this->assertEquals(1, $mongo->find('items', array('name' => 'bread'))->count());
$mongo->update('items', array('$set' => array('price' => 200, 'date' => 'today')), array('name' => 'fish'));
$fish = $mongo->get('items', array('name' => 'fish'));
$this->assertEquals(200, $fish['price']);
$this->assertEquals('today', $fish['date']);
$this->assertEquals(1, $mongo->find('items', array('name' => 'bread'))->numRows());
$this->assertEquals(1, $mongo->update('items', array('$set' => array('price' => 200, 'date' => 'today')), array('name' => 'fish')));
$this->assertEquals(2, $mongo->update('items', array('$set' => array('price' => 1)), array('name' => 'eggs')));
$fish = $mongo->get('items', array('name' => 'fish'))->fetch();
$this->assertEquals(200, $fish->price);
$this->assertEquals('today', $fish->date);
$this->assertEquals(0, $mongo->update('items', array('$set' => array('price' => 200, 'date' => 'today')), array('name' => 'ball')));
}
/**
* @runInSeparateProcess
*/
public function testUpsert()
{
if (!defined('DEBUG')) {
define('DEBUG', false);
}
$mongo = new MongoDriver($this->conf);
$mongo->getConnection();
$this->assertTrue($mongo->insert('items', array('name' => 'bread')));
$id = $mongo->getInsertId();
$this->assertNotEmpty($id);
$this->assertEquals(1, $mongo->update('items', array('$set' => array('price' => 200, 'date' => 'today')), array('name' => 'ball'), true));
$this->assertNotEquals($id, $mongo->getInsertId());
$mongo->insert('items', array('name' => 'bread'));
$this->assertEquals(1, $mongo->update('items', array('$set' => array('price' => 200, 'date' => 'today')), array('name' => 'ball'), true, true));
$this->assertEquals('today', $mongo->get('items', array('name' => 'ball'))->fetch()->date);
}
}

78
tests/model/MySQLiStatementTest.php

@ -25,6 +25,8 @@ class MySQLiStatementTest extends PHPUnit_Framework_TestCase
private $stmt;
private $sql;
private $testData = array(
array('one' => 11, 'two' => 12),
array('one' => 21, 'two' => 22),
@ -50,6 +52,72 @@ class MySQLiStatementTest extends PHPUnit_Framework_TestCase
}
}
public function testBindParam()
{
$val = $this->getMockBuilder('DbExpr')
->disableOriginalConstructor()
->getMock();
$this->assertFalse($this->stmt->bindParam('var', $val));
$this->assertTrue($this->stmt->bindParam('place', $val));
}
/**
* @expectedException Exception
* @expectedExceptionMessage Placeholder must be an array or string
* @TODO: change Exception Message 'Placeholder must be an array or string' - no arrays available
*/
public function testBindParamExceptionParam()
{
$val = 1;
$this->stmt->bindParam(array(), $val);
}
/**
* @expectedException Exception
* @expectedExceptionMessage Objects excepts DbExpr not allowed.
*/
public function testBindParamExceptionWrongObject()
{
$val = $this->getMock('NotDbExpr');
$this->stmt->bindParam('paa', $val);
}
/**
* @runInSeparateProcess
*/
public function testExecute()
{
if (!defined('DEBUG')) {
define('DEBUG', false);
}
$this->driver
->expects($this->any())
->method('quote')
->with($this->anything())
->will($this->returnCallback(array($this, 'driverQuote')));
$this->setDriverGetConnectionMethod();
$result = $this->stmt->execute(array('place' => 'place_val', 'new' => 'new_val'));
$this->assertEquals('SELECT * place_val FROM place_val AND new_val', $result);
}
/**
* @runInSeparateProcess
*/
public function testExecuteNoPlaceholders()
{
if (!defined('DEBUG')) {
define('DEBUG', false);
}
$this->setDriverGetConnectionMethod();
$this->sql = 'PLAIN SQL';
$result = $this->stmt->execute(array());
$this->assertEquals('PLAIN SQL', $result);
}
/**
* @runInSeparateProcess
*/
@ -240,4 +308,14 @@ class MySQLiStatementTest extends PHPUnit_Framework_TestCase
return $this;
}
public function driverQuote($val)
{
return $val;
}
public function dbStatementAssemble($val)
{
return $val;
}
}
Loading…
Cancel
Save