MySQLiStatement class tested
This commit is contained in:
242
tests/model/MySQLiStatementTest.php
Normal file
242
tests/model/MySQLiStatementTest.php
Normal file
@ -0,0 +1,242 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* @copyright NetMonsters <team@netmonsters.ru>
|
||||
* @link http://netmonsters.ru
|
||||
* @package Majestic
|
||||
* @subpackage UnitTests
|
||||
* @since 2011-11-4
|
||||
*
|
||||
* Unit tests for MySQLiStatement class
|
||||
*/
|
||||
|
||||
require_once dirname(__FILE__) . '/../../util/profiler/CommandProfiler.php';
|
||||
require_once dirname(__FILE__) . '/../../util/profiler/Profiler.php';
|
||||
require_once dirname(__FILE__) . '/../../model/Db.php';
|
||||
require_once dirname(__FILE__) . '/../../model/DbDriver.php';
|
||||
require_once dirname(__FILE__) . '/../../model/DbStatement.php';
|
||||
require_once dirname(__FILE__) . '/../../model/MySQLiStatement.php';
|
||||
|
||||
class MySQLiStatementTest extends PHPUnit_Framework_TestCase
|
||||
{
|
||||
|
||||
|
||||
private $driver;
|
||||
|
||||
private $stmt;
|
||||
|
||||
private $testData = array(
|
||||
array('one' => 11, 'two' => 12),
|
||||
array('one' => 21, 'two' => 22),
|
||||
array('one' => 31, 'two' => 32),
|
||||
);
|
||||
|
||||
|
||||
public function run(PHPUnit_Framework_TestResult $result = NULL)
|
||||
{
|
||||
$this->setPreserveGlobalState(false);
|
||||
return parent::run($result);
|
||||
}
|
||||
|
||||
public function setUp()
|
||||
{
|
||||
if (!isset($this->stmt)) {
|
||||
$this->driver = $this->getMockBuilder('DbDriverMock')
|
||||
->disableOriginalConstructor()
|
||||
->setMethods(array('quote', 'getConnection'))
|
||||
->getMock();
|
||||
$this->sql = 'SELECT * :place FROM :place AND :new';
|
||||
$this->stmt = new MySQLiStatement($this->driver, $this->sql);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @runInSeparateProcess
|
||||
*/
|
||||
public function testFetchNoResult()
|
||||
{
|
||||
if (!defined('DEBUG')) {
|
||||
define('DEBUG', false);
|
||||
}
|
||||
$this->assertFalse($this->stmt->fetch());
|
||||
}
|
||||
|
||||
/**
|
||||
* @runInSeparateProcess
|
||||
* @expectedException Exception
|
||||
* @expectedExceptionMessage ERROR
|
||||
*/
|
||||
public function testDriverExecuteNoResult()
|
||||
{
|
||||
if (!defined('DEBUG')) {
|
||||
define('DEBUG', false);
|
||||
}
|
||||
$this->setDriverGetConnectionWrongResultMethod();
|
||||
$this->stmt->execute(array('place' => 'place_val', 'new' => 'new_val'));
|
||||
}
|
||||
|
||||
/**
|
||||
* @runInSeparateProcess
|
||||
*/
|
||||
public function testFetch()
|
||||
{
|
||||
if (!defined('DEBUG')) {
|
||||
define('DEBUG', false);
|
||||
}
|
||||
$this->setDriverGetConnectionMethod();
|
||||
$this->stmt->execute(array('place' => 'place_val', 'new' => 'new_val'));
|
||||
$this->assertEquals('OBJECT', $this->stmt->fetch());
|
||||
$this->assertEquals('ARRAY', $this->stmt->fetch(Db::FETCH_NUM));
|
||||
$this->assertEquals('ASSOC', $this->stmt->fetch(Db::FETCH_ASSOC));
|
||||
$this->assertEquals('ARRAY', $this->stmt->fetch(Db::FETCH_BOTH));
|
||||
}
|
||||
|
||||
/**
|
||||
* @runInSeparateProcess
|
||||
*/
|
||||
public function testFetchObject()
|
||||
{
|
||||
if (!defined('DEBUG')) {
|
||||
define('DEBUG', false);
|
||||
}
|
||||
$this->setDriverGetConnectionMethod();
|
||||
$this->stmt->execute(array('place' => 'place_val', 'new' => 'new_val'));
|
||||
$this->assertEquals('OBJECT', $this->stmt->fetchObject());
|
||||
}
|
||||
|
||||
/**
|
||||
* @runInSeparateProcess
|
||||
*/
|
||||
public function testFetchWithDebug()
|
||||
{
|
||||
if (!defined('DEBUG')) {
|
||||
define('DEBUG', true);
|
||||
}
|
||||
$this->setDriverGetConnectionMethod();
|
||||
$this->stmt->execute(array('place' => 'place_val', 'new' => 'new_val'));
|
||||
$this->assertEquals('OBJECT', $this->stmt->fetch());
|
||||
}
|
||||
|
||||
/**
|
||||
* @runInSeparateProcess
|
||||
*/
|
||||
public function testClose()
|
||||
{
|
||||
if (!defined('DEBUG')) {
|
||||
define('DEBUG', false);
|
||||
}
|
||||
$this->assertAttributeEquals(null, 'result', $this->stmt);
|
||||
$this->setDriverGetConnectionMethod();
|
||||
$this->stmt->execute(array('place' => 'place_val', 'new' => 'new_val'));
|
||||
$this->assertAttributeNotEquals(null, 'result', $this->stmt);
|
||||
$this->stmt->close();
|
||||
$this->assertAttributeEquals(null, 'result', $this->stmt);
|
||||
}
|
||||
|
||||
public function testAffectedRows()
|
||||
{
|
||||
$mysqliMock = $this->getMockBuilder('MysqliDrvr');
|
||||
$mysqliMock->affected_rows = 'AFFECTED_ROWS';
|
||||
|
||||
$this->driver
|
||||
->expects($this->any())
|
||||
->method('getConnection')
|
||||
->will($this->returnValue($mysqliMock));
|
||||
|
||||
$this->assertEquals('AFFECTED_ROWS', $this->stmt->affectedRows());
|
||||
}
|
||||
|
||||
public function testNumRowsNoResult()
|
||||
{
|
||||
$this->assertFalse($this->stmt->numRows());
|
||||
}
|
||||
|
||||
/**
|
||||
* @runInSeparateProcess
|
||||
* @expectedException Exception
|
||||
*/
|
||||
public function testNumRows()
|
||||
{
|
||||
if (!defined('DEBUG')) {
|
||||
define('DEBUG', false);
|
||||
}
|
||||
|
||||
$this->setDriverGetConnectionMethod();
|
||||
$this->stmt->execute(array('place' => 'place_val', 'new' => 'new_val'));
|
||||
$this->assertNull($this->stmt->numRows());
|
||||
}
|
||||
|
||||
/**
|
||||
* @expectedException Exception
|
||||
* @expectedExceptionMessage Invalid fetch mode
|
||||
* @runInSeparateProcess
|
||||
*/
|
||||
public function testFetchInvalidMode()
|
||||
{
|
||||
if (!defined('DEBUG')) {
|
||||
define('DEBUG', false);
|
||||
}
|
||||
$this->setDriverGetConnectionMethod();
|
||||
$this->stmt->execute(array('place' => 'place_val', 'new' => 'new_val'));
|
||||
$this->stmt->fetch(324);
|
||||
}
|
||||
|
||||
private function setDriverGetConnectionMethod()
|
||||
{
|
||||
$resultMock = $this->getMockBuilder('mysqli_result')
|
||||
->disableOriginalConstructor()
|
||||
->setMethods(array('fetch_object', 'fetch_array', 'fetch_assoc', 'close'))
|
||||
->setMockClassName('Mysqli_Result_Mock')
|
||||
->getMock();
|
||||
$resultMock
|
||||
->expects($this->any())
|
||||
->method('fetch_object')
|
||||
->will($this->returnValue('OBJECT'));
|
||||
$resultMock
|
||||
->expects($this->any())
|
||||
->method('fetch_array')
|
||||
->will($this->returnValue('ARRAY'));
|
||||
$resultMock
|
||||
->expects($this->any())
|
||||
->method('fetch_assoc')
|
||||
->will($this->returnValue('ASSOC'));
|
||||
$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));
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
private function setDriverGetConnectionWrongResultMethod()
|
||||
{
|
||||
$mysqliMock = $this->getMock('MysqliDrvr', array('query'));
|
||||
$mysqliMock
|
||||
->expects($this->any())
|
||||
->method('query')
|
||||
->with($this->anything())
|
||||
->will($this->returnValue(false));
|
||||
$mysqliMock->error = 'ERROR';
|
||||
$mysqliMock->errno = 0;
|
||||
|
||||
$this->driver
|
||||
->expects($this->any())
|
||||
->method('getConnection')
|
||||
->will($this->returnValue($mysqliMock));
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
}
|
Reference in New Issue
Block a user