Anton Grebnev
13 years ago
4 changed files with 374 additions and 4 deletions
-
171tests/model/DbDriverTest.php
-
24tests/model/DbExprTest.php
-
172tests/model/DbStatementTest.php
-
11tests/model/DbTest.php
@ -0,0 +1,171 @@ |
|||||
|
<?php |
||||
|
|
||||
|
/* |
||||
|
* @copyright NetMonsters <team@netmonsters.ru> |
||||
|
* @link http://netmonsters.ru |
||||
|
* @package Majestic |
||||
|
* @subpackage UnitTests |
||||
|
* @since 2011-11-3 |
||||
|
* |
||||
|
* Unit tests for DbDriver class |
||||
|
*/ |
||||
|
|
||||
|
require_once dirname(__FILE__) . '/../../model/DbExpr.php'; |
||||
|
require_once dirname(__FILE__) . '/../../model/Db.php'; |
||||
|
require_once dirname(__FILE__) . '/../../model/DbDriver.php'; |
||||
|
|
||||
|
class DbDriverTest extends PHPUnit_Framework_TestCase |
||||
|
{ |
||||
|
|
||||
|
private $driver; |
||||
|
|
||||
|
public function setUp() |
||||
|
{ |
||||
|
$conf = array('hostname' => 'localhost', 'database' => 'db', 'username' => 'test', 'password' => '1234'); |
||||
|
$this->driver = $this->getMockForAbstractClass('DbDriver', array($conf)); |
||||
|
} |
||||
|
|
||||
|
public function testConstruct() |
||||
|
{ |
||||
|
$conf = array('hostname' => 'localhost', 'database' => 'db', 'username' => 'test', 'password' => '1234'); |
||||
|
$this->driver = $this->getMockForAbstractClass('DbDriver', array($conf)); |
||||
|
} |
||||
|
|
||||
|
/** |
||||
|
* @expectedException Exception |
||||
|
* @expectedExceptionMessage Configuration must have a "username". |
||||
|
*/ |
||||
|
public function testConstructWrongConfig() |
||||
|
{ |
||||
|
$conf = array('hostname' => 'localhost', 'database' => 'db'); |
||||
|
$this->getMockForAbstractClass('DbDriver', array($conf)); |
||||
|
} |
||||
|
|
||||
|
public function testGetConnection() |
||||
|
{ |
||||
|
$this->assertNull($this->driver->getConnection()); |
||||
|
} |
||||
|
|
||||
|
public function testBeginTransaction() |
||||
|
{ |
||||
|
$this->assertEquals($this->driver, $this->driver->beginTransaction()); |
||||
|
} |
||||
|
|
||||
|
public function testCommit() |
||||
|
{ |
||||
|
$this->assertEquals($this->driver, $this->driver->commit()); |
||||
|
} |
||||
|
|
||||
|
public function testRollback() |
||||
|
{ |
||||
|
$this->assertEquals($this->driver, $this->driver->rollback()); |
||||
|
} |
||||
|
|
||||
|
public function testQuery() |
||||
|
{ |
||||
|
$this->setDriverPrepareFunction(); |
||||
|
|
||||
|
$stmt = $this->driver->query('SELECT * FROM table'); |
||||
|
$this->assertInstanceOf('DbStmt', $stmt); |
||||
|
$this->assertEquals('SELECT * FROM table', $stmt->string()); |
||||
|
|
||||
|
$stmt = $this->driver->query('SELECT * FROM table', 'simple'); |
||||
|
$this->assertInstanceOf('DbStmt', $stmt); |
||||
|
$this->assertEquals('SELECT * FROM table', $stmt->string()); |
||||
|
} |
||||
|
|
||||
|
public function testInsert() |
||||
|
{ |
||||
|
$this->setDriverPrepareFunction() |
||||
|
->setDriverQuoteFunction(); |
||||
|
$bind = array('table.name' => 'tony', 'chair.age' => 21, 'height' => 185); |
||||
|
$sql = $this->driver->insert('users', $bind); |
||||
|
$this->assertEquals('INSERT INTO `users` (`table`.`name`, `chair`.`age`, `height`) VALUES (tony, 21, 185)', $sql); |
||||
|
} |
||||
|
|
||||
|
public function testUpdate() |
||||
|
{ |
||||
|
$this->setDriverPrepareFunction() |
||||
|
->setDriverQuoteFunction(); |
||||
|
|
||||
|
$bind = array('table.name' => 'tony', 'chair.age' => 21, 'height' => 185); |
||||
|
$sql = $this->driver->update('users', $bind); |
||||
|
$this->assertEquals('UPDATE `users` SET `table`.`name`=tony, `chair`.`age`=21, `height`=185', $sql); |
||||
|
} |
||||
|
|
||||
|
public function testDeleteNoWHERE() |
||||
|
{ |
||||
|
$this->setDriverPrepareFunction(); |
||||
|
|
||||
|
$sql = $this->driver->delete('users'); |
||||
|
$this->assertEquals('DELETE FROM `users`', $sql); |
||||
|
} |
||||
|
|
||||
|
public function testDeleteWithWHEREArray() |
||||
|
{ |
||||
|
$this->setDriverPrepareFunction() |
||||
|
->setDriverQuoteFunction(); |
||||
|
|
||||
|
$sql = $this->driver->delete('users', array('name?tony' => new DbExpr('='), 'height?185' => '>')); |
||||
|
$this->assertEquals('DELETE FROM `users` WHERE name=tony AND height>185', $sql); |
||||
|
} |
||||
|
|
||||
|
public function testDeleteWithWHERESimpleCond() |
||||
|
{ |
||||
|
$this->setDriverPrepareFunction(); |
||||
|
|
||||
|
$sql = $this->driver->delete('users', 'name=tony'); |
||||
|
$this->assertEquals('DELETE FROM `users` WHERE name=tony', $sql); |
||||
|
} |
||||
|
|
||||
|
public function testDeleteWithWHEREKeyArray() |
||||
|
{ |
||||
|
$this->setDriverPrepareFunction(); |
||||
|
|
||||
|
$sql = $this->driver->delete('users', array('name=tony' => 0)); |
||||
|
$this->assertEquals('DELETE FROM `users` WHERE name=tony', $sql); |
||||
|
} |
||||
|
|
||||
|
public function testDeleteWithWHEREDbExpr() |
||||
|
{ |
||||
|
$this->setDriverPrepareFunction(); |
||||
|
|
||||
|
$sql = $this->driver->delete('users', new DbExpr('name=tony')); |
||||
|
$this->assertEquals('DELETE FROM `users` WHERE name=tony', $sql); |
||||
|
} |
||||
|
|
||||
|
protected function setDriverPrepareFunction() |
||||
|
{ |
||||
|
$this->driver |
||||
|
->expects($this->any()) |
||||
|
->method('prepare') |
||||
|
->with($this->anything()) |
||||
|
->will($this->returnCallback(array($this, 'dbDriverPrepare'))); |
||||
|
return $this; |
||||
|
} |
||||
|
|
||||
|
protected function setDriverQuoteFunction() |
||||
|
{ |
||||
|
$this->driver |
||||
|
->expects($this->any()) |
||||
|
->method('driverQuote') |
||||
|
->with($this->anything()) |
||||
|
->will($this->returnArgument(0)); |
||||
|
return $this; |
||||
|
} |
||||
|
|
||||
|
public function dbDriverPrepare($sql) |
||||
|
{ |
||||
|
$stmt = $this->getMock('DbStmt', array('execute', 'string', 'affectedRows')); |
||||
|
$stmt->expects($this->any()) |
||||
|
->method('execute') |
||||
|
->with($this->anything()); |
||||
|
$stmt->expects($this->any()) |
||||
|
->method('string') |
||||
|
->will($this->returnValue($sql)); |
||||
|
$stmt->expects($this->any()) |
||||
|
->method('affectedRows') |
||||
|
->will($this->returnValue($sql)); |
||||
|
return $stmt; |
||||
|
} |
||||
|
} |
@ -0,0 +1,24 @@ |
|||||
|
<?php |
||||
|
|
||||
|
/* |
||||
|
* @copyright NetMonsters <team@netmonsters.ru> |
||||
|
* @link http://netmonsters.ru |
||||
|
* @package Majestic |
||||
|
* @subpackage UnitTests |
||||
|
* @since 2011-11-3 |
||||
|
* |
||||
|
* Unit tests for DbExpr class |
||||
|
*/ |
||||
|
|
||||
|
require_once dirname(__FILE__) . '/../../model/DbExpr.php'; |
||||
|
|
||||
|
class DbExprTest extends PHPUnit_Framework_TestCase |
||||
|
{ |
||||
|
|
||||
|
public function testExpr() |
||||
|
{ |
||||
|
$expr = new DbExpr('THIS IS SQL EXPRESSION'); |
||||
|
$str = (string) $expr; |
||||
|
$this->assertEquals('THIS IS SQL EXPRESSION', $str); |
||||
|
} |
||||
|
} |
@ -0,0 +1,172 @@ |
|||||
|
<?php |
||||
|
|
||||
|
/* |
||||
|
* @copyright NetMonsters <team@netmonsters.ru> |
||||
|
* @link http://netmonsters.ru |
||||
|
* @package Majestic |
||||
|
* @subpackage UnitTests |
||||
|
* @since 2011-11-3 |
||||
|
* |
||||
|
* Unit tests for DbStatement class |
||||
|
*/ |
||||
|
|
||||
|
require_once dirname(__FILE__) . '/../../model/Db.php'; |
||||
|
require_once dirname(__FILE__) . '/../../model/DbDriver.php'; |
||||
|
require_once dirname(__FILE__) . '/../../model/DbStatement.php'; |
||||
|
|
||||
|
class DbStatementTest extends PHPUnit_Framework_TestCase |
||||
|
{ |
||||
|
private $driver; |
||||
|
|
||||
|
private $sql; |
||||
|
|
||||
|
private $stmt; |
||||
|
|
||||
|
private $testData = array( |
||||
|
array('one' => 11, 'two' => 12), |
||||
|
array('one' => 21, 'two' => 22), |
||||
|
array('one' => 31, 'two' => 32), |
||||
|
); |
||||
|
|
||||
|
public function setUp() |
||||
|
{ |
||||
|
if (!isset($this->stmt)) { |
||||
|
$this->driver = $this->getMockBuilder('DbDriverMock') |
||||
|
->disableOriginalConstructor() |
||||
|
->setMethods(array('quote')) |
||||
|
->getMock(); |
||||
|
$this->sql = 'SELECT * :place FROM :place AND :new'; |
||||
|
$this->stmt = $this->getMockForAbstractClass('DbStatement', array($this->driver, $this->sql)); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
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); |
||||
|
} |
||||
|
|
||||
|
public function testFetch() |
||||
|
{ |
||||
|
$this->stmt |
||||
|
->expects($this->any()) |
||||
|
->method('fetch') |
||||
|
->with($this->anything()) |
||||
|
->will($this->returnCallback(array($this, 'dbStatementFetch'))); |
||||
|
|
||||
|
$this->assertEquals(11, $this->stmt->fetchField('one')); |
||||
|
$this->assertFalse($this->stmt->fetchField('zero')); |
||||
|
|
||||
|
reset($this->testData); |
||||
|
$this->assertEquals(array(11, 21, 31), $this->stmt->fetchColumn('one')); |
||||
|
|
||||
|
reset($this->testData); |
||||
|
$result = $this->stmt->fetchAll(); |
||||
|
$this->assertEquals(11, $result[0]->one); |
||||
|
$this->assertEquals(32, $result[2]->two); |
||||
|
|
||||
|
reset($this->testData); |
||||
|
$result = $this->stmt->fetchPairs(); |
||||
|
$this->assertEquals(31, $result['one']); |
||||
|
} |
||||
|
|
||||
|
public function dbStatementAssemble($val) |
||||
|
{ |
||||
|
return $val; |
||||
|
} |
||||
|
|
||||
|
public function driverQuote($val) |
||||
|
{ |
||||
|
return $val; |
||||
|
} |
||||
|
|
||||
|
public function dbStatementFetch($style) |
||||
|
{ |
||||
|
$result = null; |
||||
|
switch ($style) { |
||||
|
case Db::FETCH_OBJ: |
||||
|
$data = each($this->testData); |
||||
|
if ($data !== false) { |
||||
|
$result = new ArrayObject($data['value'], ArrayObject::ARRAY_AS_PROPS); |
||||
|
} else { |
||||
|
$result = false; |
||||
|
} |
||||
|
break; |
||||
|
case Db::FETCH_ASSOC: |
||||
|
$data = each($this->testData); |
||||
|
$result = $data['value']; |
||||
|
break; |
||||
|
case Db::FETCH_NUM: |
||||
|
$data = each($this->testData); |
||||
|
if ($data !== false) { |
||||
|
$data = $data['value']; |
||||
|
$result[0] = key($data); |
||||
|
$result[1] = current($data); |
||||
|
} else { |
||||
|
$result = false; |
||||
|
} |
||||
|
} |
||||
|
return $result; |
||||
|
} |
||||
|
} |
Write
Preview
Loading…
Cancel
Save
Reference in new issue