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

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