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.

421 lines
12 KiB

<?php
/*
* @copyright NetMonsters <team@netmonsters.ru>
* @link http://netmonsters.ru
* @package Majestic
* @subpackage UnitTests
* @since 2011-11-15
*
* 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/MongoStatement.php';
class MongoStatementTest extends PHPUnit_Framework_TestCase
{
private $driver;
private $stmt;
private $request;
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('getConnection'))
->getMock();
$this->request = $this->getMockBuilder('MongoDbCommandMock')
->disableOriginalConstructor()
->setMethods(array('execute', 'bindParam'))
->getMock();
$this->stmt = new MongoStatement($this->driver, $this->request);
}
}
/**
* @runInSeparateProcess
*/
public function testAffectedNumRowsNoResult()
{
if (!defined('DEBUG')) {
define('DEBUG', false);
}
$this->assertFalse($this->stmt->affectedRows());
$this->assertFalse($this->stmt->numRows());
$this->setDriverGetConnectionMethod();
$this->request
->expects($this->any())
->method('execute')
->will($this->returnValue(array()));
$this->stmt->execute();
$this->assertFalse($this->stmt->affectedRows());
}
/**
* @runInSeparateProcess
*/
public function testAffectedNumRows()
{
if (!defined('DEBUG')) {
define('DEBUG', false);
}
$this->setDriverGetConnectionMethod();
$this->request
->expects($this->any())
->method('execute')
->will($this->returnValue(array('n' => 20, 'ok' => 1)));
$this->stmt->execute();
$this->assertEquals(20, $this->stmt->affectedRows());
}
/**
* @runInSeparateProcess
*/
public function testExecute()
{
if (!defined('DEBUG')) {
define('DEBUG', false);
}
$this->setDriverGetConnectionMethod()->setRequestExecuteMethod();
$this->assertTrue($this->stmt->execute());
}
/**
* @runInSeparateProcess
* @expectedException Exception
* @expectedExceptionMessage MongoDB request error.
*/
public function testExecuteNoResult()
{
if (!defined('DEBUG')) {
define('DEBUG', false);
}
$this->setDriverGetConnectionMethod();
$this->request
->expects($this->any())
->method('execute')
->will($this->returnValue(false));
$this->stmt->execute();
}
/**
* @runInSeparateProcess
* @expectedException Exception
* @expectedExceptionMessage No connection to MongoDB server.
*/
public function testExecuteNoConnection()
{
if (!defined('DEBUG')) {
define('DEBUG', false);
}
$this->driver
->expects($this->any())
->method('getConnection')
->will($this->returnValue(false));
$this->stmt->execute();
}
/**
* @runInSeparateProcess
*/
public function testExecuteWithDebug()
{
if (!defined('DEBUG')) {
define('DEBUG', true);
}
$this->setDriverGetConnectionMethod()->setRequestExecuteMethod();
$this->assertTrue($this->stmt->execute());
$this->assertEquals(10, $this->stmt->numRows());
}
/**
* @runInSeparateProcess
*/
public function testBindParam()
{
if (!defined('DEBUG')) {
define('DEBUG', true);
}
$this->request
->expects($this->once())
->method('bindParam')
->will($this->returnValue(true));
$this->setDriverGetConnectionMethod()->setRequestExecuteMethod();
$this->assertTrue($this->stmt->execute(array('one' => 'two')));
$this->assertAttributeNotEquals(null, 'result', $this->stmt);
$this->stmt->close();
$this->assertAttributeEquals(null, 'result', $this->stmt);
}
/**
* @runInSeparateProcess
*/
public function testFetch()
{
if (!defined('DEBUG')) {
define('DEBUG', true);
}
$this->assertFalse($this->stmt->fetch());
$this->setDriverGetConnectionMethod()->setRequestForFetch();
$this->stmt->execute();
$result = $this->stmt->fetch();
$this->assertEquals('prev', $result->next);
$this->assertFalse($this->stmt->fetch());
}
/**
* @runInSeparateProcess
*/
public function testFetchWithInitialArray()
{
if (!defined('DEBUG')) {
define('DEBUG', true);
}
$this->assertFalse($this->stmt->fetch());
$this->setDriverGetConnectionMethod();
$this->request
->expects($this->once())
->method('execute')
->will($this->returnValue(array('some' => 'val')));
$this->stmt->execute();
$this->assertFalse($this->stmt->fetch());
}
/**
* @runInSeparateProcess
*/
public function testFetchAssocFromCursor()
{
if (!defined('DEBUG')) {
define('DEBUG', true);
}
$this->assertFalse($this->stmt->fetch());
$this->setDriverGetConnectionMethod()->setRequestForFetch();
$this->stmt->execute();
$result = $this->stmt->fetch(Db::FETCH_ASSOC);
$this->assertEquals('prev', $result['next']);
$this->assertEquals(array(), $this->stmt->fetch(Db::FETCH_ASSOC));
}
/**
* @runInSeparateProcess
*/
public function testFetchAssocFromArray()
{
if (!defined('DEBUG')) {
define('DEBUG', true);
}
$this->assertFalse($this->stmt->fetch());
$this->setDriverGetConnectionMethod();
$this->request
->expects($this->once())
->method('execute')
->will($this->returnValue(array('some' => 'val')));
$this->stmt->execute();
$result = $this->stmt->fetch(Db::FETCH_ASSOC);
$this->assertEquals('val', $result['some']);
}
/**
* @runInSeparateProcess
* @expectedException Exception
* @expectedExceptionMessage Invalid fetch mode "222" specified
*/
public function testFetchWrongMode()
{
if (!defined('DEBUG')) {
define('DEBUG', true);
}
$this->assertFalse($this->stmt->fetch());
$this->setDriverGetConnectionMethod();
$this->request
->expects($this->once())
->method('execute')
->will($this->returnValue(array('some' => 'val')));
$this->stmt->execute();
$this->stmt->fetch(222);
}
/**
* @runInSeparateProcess
*/
public function testSkipOrderLimit()
{
if (!defined('DEBUG')) {
define('DEBUG', true);
}
$this->setDriverGetConnectionMethod()->setRequestForFetch();
$this->stmt->execute();
$this->assertInstanceOf('MongoStatement', $this->stmt->order(array('id' => 1)));
$this->stmt->fetch();
$this->stmt->fetch();
}
/**
* @runInSeparateProcess
* @expectedException Exception
* @expectedExceptionMessage MongoStatement error. Impossible order results of opened cursor
*/
public function testOrderException()
{
if (!defined('DEBUG')) {
define('DEBUG', true);
}
$this->setDriverGetConnectionMethod();
$this->request
->expects($this->once())
->method('execute')
->will($this->returnValue(array('some' => 'val')));
$this->stmt->execute();
$this->stmt->order(array('id' => 1));
}
/**
* @runInSeparateProcess
* @expectedException Exception
* @expectedExceptionMessage MongoStatement error. Impossible skip results of opened cursor
*/
public function testSkipException()
{
if (!defined('DEBUG')) {
define('DEBUG', true);
}
$this->setDriverGetConnectionMethod();
$this->request
->expects($this->once())
->method('execute')
->will($this->returnValue(array('some' => 'val')));
$this->stmt->execute();
$this->stmt->skip(array('id' => 1));
}
/**
* @runInSeparateProcess
* @expectedException Exception
* @expectedExceptionMessage MongoStatement error. Impossible limit results of opened cursor
*/
public function testLimitException()
{
if (!defined('DEBUG')) {
define('DEBUG', true);
}
$this->setDriverGetConnectionMethod();
$this->request
->expects($this->once())
->method('execute')
->will($this->returnValue(array('some' => 'val')));
$this->stmt->execute();
$this->stmt->limit(array('id' => 1));
}
private function setDriverGetConnectionMethod()
{
$mongoMock = $this->getMock('Mongo');
$this->driver
->expects($this->any())
->method('getConnection')
->will($this->returnValue($mongoMock));
return $this;
}
public function setRequestExecuteMethod()
{
$resultMock = $this->getMockBuilder('MongoCursor')
->setMethods(array('count'))
->disableOriginalConstructor()
->getMock();
$resultMock
->expects($this->any())
->method('count')
->will($this->returnValue(10));
$this->request
->expects($this->any())
->method('execute')
->will($this->returnValue($resultMock));
return $this;
}
public function setRequestForFetch()
{
$resultMock = $this->getMockBuilder('MongoCursor')
->setMethods(array('count', 'getNext', 'limit', 'sort', 'skip'))
->disableOriginalConstructor()
->getMock();
$resultMock
->expects($this->any())
->method('limit');
$resultMock
->expects($this->any())
->method('sort');
$resultMock
->expects($this->any())
->method('skip');
$resultMock
->expects($this->any())
->method('count')
->will($this->returnValue(10));
$resultMock
->expects($this->at(0))
->method('getNext')
->will($this->returnValue(array('next' => 'prev', '_id' => 10)));
$resultMock
->expects($this->at(1))
->method('getNext')
->will($this->returnValue(array()));
$this->request
->expects($this->any())
->method('execute')
->will($this->returnValue($resultMock));
return $this;
}
}