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.

241 lines
7.1 KiB

  1. <?php
  2. /*
  3. * @copyright NetMonsters <team@netmonsters.ru>
  4. * @link http://netmonsters.ru
  5. * @package Majestic
  6. * @subpackage UnitTests
  7. * @since 2011-11-4
  8. *
  9. * Unit tests for MySQLiStatement class
  10. */
  11. require_once dirname(__FILE__) . '/../../util/profiler/CommandProfiler.php';
  12. require_once dirname(__FILE__) . '/../../util/profiler/Profiler.php';
  13. require_once dirname(__FILE__) . '/../../model/Db.php';
  14. require_once dirname(__FILE__) . '/../../model/DbDriver.php';
  15. require_once dirname(__FILE__) . '/../../model/DbStatement.php';
  16. require_once dirname(__FILE__) . '/../../model/MySQLiStatement.php';
  17. class MySQLiStatementTest extends PHPUnit_Framework_TestCase
  18. {
  19. private $driver;
  20. private $stmt;
  21. private $testData = array(
  22. array('one' => 11, 'two' => 12),
  23. array('one' => 21, 'two' => 22),
  24. array('one' => 31, 'two' => 32),
  25. );
  26. public function run(PHPUnit_Framework_TestResult $result = NULL)
  27. {
  28. $this->setPreserveGlobalState(false);
  29. return parent::run($result);
  30. }
  31. public function setUp()
  32. {
  33. if (!isset($this->stmt)) {
  34. $this->driver = $this->getMockBuilder('DbDriverMock')
  35. ->disableOriginalConstructor()
  36. ->setMethods(array('quote', 'getConnection'))
  37. ->getMock();
  38. $this->sql = 'SELECT * :place FROM :place AND :new';
  39. $this->stmt = new MySQLiStatement($this->driver, $this->sql);
  40. }
  41. }
  42. /**
  43. * @runInSeparateProcess
  44. */
  45. public function testFetchNoResult()
  46. {
  47. if (!defined('DEBUG')) {
  48. define('DEBUG', false);
  49. }
  50. $this->assertFalse($this->stmt->fetch());
  51. }
  52. /**
  53. * @runInSeparateProcess
  54. * @expectedException Exception
  55. * @expectedExceptionMessage ERROR
  56. */
  57. public function testDriverExecuteNoResult()
  58. {
  59. if (!defined('DEBUG')) {
  60. define('DEBUG', false);
  61. }
  62. $this->setDriverGetConnectionWrongResultMethod();
  63. $this->stmt->execute(array('place' => 'place_val', 'new' => 'new_val'));
  64. }
  65. /**
  66. * @runInSeparateProcess
  67. */
  68. public function testFetch()
  69. {
  70. if (!defined('DEBUG')) {
  71. define('DEBUG', false);
  72. }
  73. $this->setDriverGetConnectionMethod();
  74. $this->stmt->execute(array('place' => 'place_val', 'new' => 'new_val'));
  75. $this->assertEquals('OBJECT', $this->stmt->fetch());
  76. $this->assertEquals('ARRAY', $this->stmt->fetch(Db::FETCH_NUM));
  77. $this->assertEquals('ASSOC', $this->stmt->fetch(Db::FETCH_ASSOC));
  78. $this->assertEquals('ARRAY', $this->stmt->fetch(Db::FETCH_BOTH));
  79. }
  80. /**
  81. * @runInSeparateProcess
  82. */
  83. public function testFetchObject()
  84. {
  85. if (!defined('DEBUG')) {
  86. define('DEBUG', false);
  87. }
  88. $this->setDriverGetConnectionMethod();
  89. $this->stmt->execute(array('place' => 'place_val', 'new' => 'new_val'));
  90. $this->assertEquals('OBJECT', $this->stmt->fetchObject());
  91. }
  92. /**
  93. * @runInSeparateProcess
  94. */
  95. public function testFetchWithDebug()
  96. {
  97. if (!defined('DEBUG')) {
  98. define('DEBUG', true);
  99. }
  100. $this->setDriverGetConnectionMethod();
  101. $this->stmt->execute(array('place' => 'place_val', 'new' => 'new_val'));
  102. $this->assertEquals('OBJECT', $this->stmt->fetch());
  103. }
  104. /**
  105. * @runInSeparateProcess
  106. */
  107. public function testClose()
  108. {
  109. if (!defined('DEBUG')) {
  110. define('DEBUG', false);
  111. }
  112. $this->assertAttributeEquals(null, 'result', $this->stmt);
  113. $this->setDriverGetConnectionMethod();
  114. $this->stmt->execute(array('place' => 'place_val', 'new' => 'new_val'));
  115. $this->assertAttributeNotEquals(null, 'result', $this->stmt);
  116. $this->stmt->close();
  117. $this->assertAttributeEquals(null, 'result', $this->stmt);
  118. }
  119. public function testAffectedRows()
  120. {
  121. $mysqliMock = $this->getMockBuilder('MysqliDrvr');
  122. $mysqliMock->affected_rows = 'AFFECTED_ROWS';
  123. $this->driver
  124. ->expects($this->any())
  125. ->method('getConnection')
  126. ->will($this->returnValue($mysqliMock));
  127. $this->assertEquals('AFFECTED_ROWS', $this->stmt->affectedRows());
  128. }
  129. public function testNumRowsNoResult()
  130. {
  131. $this->assertFalse($this->stmt->numRows());
  132. }
  133. /**
  134. * @runInSeparateProcess
  135. * @expectedException Exception
  136. */
  137. public function testNumRows()
  138. {
  139. if (!defined('DEBUG')) {
  140. define('DEBUG', false);
  141. }
  142. $this->setDriverGetConnectionMethod();
  143. $this->stmt->execute(array('place' => 'place_val', 'new' => 'new_val'));
  144. $this->assertNull($this->stmt->numRows());
  145. }
  146. /**
  147. * @expectedException Exception
  148. * @expectedExceptionMessage Invalid fetch mode
  149. * @runInSeparateProcess
  150. */
  151. public function testFetchInvalidMode()
  152. {
  153. if (!defined('DEBUG')) {
  154. define('DEBUG', false);
  155. }
  156. $this->setDriverGetConnectionMethod();
  157. $this->stmt->execute(array('place' => 'place_val', 'new' => 'new_val'));
  158. $this->stmt->fetch(324);
  159. }
  160. private function setDriverGetConnectionMethod()
  161. {
  162. $resultMock = $this->getMockBuilder('mysqli_result')
  163. ->disableOriginalConstructor()
  164. ->setMethods(array('fetch_object', 'fetch_array', 'fetch_assoc', 'close'))
  165. ->setMockClassName('Mysqli_Result_Mock')
  166. ->getMock();
  167. $resultMock
  168. ->expects($this->any())
  169. ->method('fetch_object')
  170. ->will($this->returnValue('OBJECT'));
  171. $resultMock
  172. ->expects($this->any())
  173. ->method('fetch_array')
  174. ->will($this->returnValue('ARRAY'));
  175. $resultMock
  176. ->expects($this->any())
  177. ->method('fetch_assoc')
  178. ->will($this->returnValue('ASSOC'));
  179. $resultMock
  180. ->expects($this->any())
  181. ->method('close')
  182. ->will($this->returnValue(TRUE));
  183. $mysqliMock = $this->getMock('MysqliDrvr', array('query'));
  184. $mysqliMock
  185. ->expects($this->any())
  186. ->method('query')
  187. ->with($this->anything())
  188. ->will($this->returnValue($resultMock));
  189. $this->driver
  190. ->expects($this->any())
  191. ->method('getConnection')
  192. ->will($this->returnValue($mysqliMock));
  193. return $this;
  194. }
  195. private function setDriverGetConnectionWrongResultMethod()
  196. {
  197. $mysqliMock = $this->getMock('MysqliDrvr', array('query'));
  198. $mysqliMock
  199. ->expects($this->any())
  200. ->method('query')
  201. ->with($this->anything())
  202. ->will($this->returnValue(false));
  203. $mysqliMock->error = 'ERROR';
  204. $mysqliMock->errno = 0;
  205. $this->driver
  206. ->expects($this->any())
  207. ->method('getConnection')
  208. ->will($this->returnValue($mysqliMock));
  209. return $this;
  210. }
  211. }