Anton Grebnev
13 years ago
1 changed files with 335 additions and 0 deletions
@ -0,0 +1,335 @@ |
|||
<?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(); |
|||
$result = $this->stmt->fetch(222); |
|||
} |
|||
|
|||
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')) |
|||
->disableOriginalConstructor() |
|||
->getMock(); |
|||
|
|||
$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; |
|||
} |
|||
} |
Write
Preview
Loading…
Cancel
Save
Reference in new issue