You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

243 lines
7.3 KiB

<?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';
require_once dirname(__FILE__) . '/../../exception/GeneralException.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
*/
public function testDriverExecuteNoResult()
{
if (!defined('DEBUG')) {
define('DEBUG', false);
}
$this->setDriverGetConnectionWrongResultMethod();
$this->setExpectedException('GeneralException', 'ERROR');
$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
* @TODO: exception just for code coverage - could not mock mysqli properties
*/
public function testNumRows()
{
$this->markTestSkipped('Unable to execute a method or access a property of an incomplete object.');
if (!defined('DEBUG')) {
define('DEBUG', false);
}
$this->setDriverGetConnectionMethod();
$this->stmt->execute(array('place' => 'place_val', 'new' => 'new_val'));
$this->assertNull($this->stmt->numRows());
}
/**
* @runInSeparateProcess
*/
public function testFetchInvalidMode()
{
if (!defined('DEBUG')) {
define('DEBUG', false);
}
$this->setDriverGetConnectionMethod();
$this->stmt->execute(array('place' => 'place_val', 'new' => 'new_val'));
$this->setExpectedException('GeneralException');
$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;
}
}