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.

351 lines
11 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__) . '/../../Registry.php';
  12. require_once dirname(__FILE__) . '/../../Config.php';
  13. require_once dirname(__FILE__) . '/../../util/profiler/CommandProfiler.php';
  14. require_once dirname(__FILE__) . '/../../util/profiler/Profiler.php';
  15. require_once dirname(__FILE__) . '/../../model/Db.php';
  16. require_once dirname(__FILE__) . '/../../model/DbDriver.php';
  17. require_once dirname(__FILE__) . '/../../model/DbStatement.php';
  18. require_once dirname(__FILE__) . '/../../model/MySQLiStatement.php';
  19. require_once dirname(__FILE__) . '/../../exception/GeneralException.php';
  20. class MySQLiStatementTest extends PHPUnit_Framework_TestCase
  21. {
  22. private $driver;
  23. private $stmt;
  24. private $sql;
  25. private $testData = array(
  26. array('one' => 11, 'two' => 12),
  27. array('one' => 21, 'two' => 22),
  28. array('one' => 31, 'two' => 32),
  29. );
  30. public function run(PHPUnit_Framework_TestResult $result = NULL)
  31. {
  32. $this->setPreserveGlobalState(false);
  33. return parent::run($result);
  34. }
  35. public function setUp()
  36. {
  37. if (!isset($this->stmt)) {
  38. $this->driver = $this->getMockBuilder('DbDriverMock')
  39. ->disableOriginalConstructor()
  40. ->setMethods(array('quote', 'getConnection'))
  41. ->getMock();
  42. $this->sql = 'SELECT * :place FROM :place AND :new';
  43. $this->stmt = new MySQLiStatement($this->driver, $this->sql);
  44. }
  45. }
  46. public function testBindParam()
  47. {
  48. $val = $this->getMockBuilder('DbExpr')
  49. ->disableOriginalConstructor()
  50. ->getMock();
  51. $this->assertFalse($this->stmt->bindParam('var', $val));
  52. $this->assertTrue($this->stmt->bindParam('place', $val));
  53. }
  54. public function testBindParamExceptionParam()
  55. {
  56. $val = 1;
  57. $this->setExpectedException('GeneralException', 'Placeholder must be an integer or string');
  58. $this->stmt->bindParam(array(), $val);
  59. }
  60. public function testBindParamExceptionWrongObject()
  61. {
  62. $val = $this->getMock('NotDbExpr');
  63. $this->setExpectedException('GeneralException', 'Objects excepts DbExpr not allowed.');
  64. $this->stmt->bindParam('paa', $val);
  65. }
  66. /**
  67. * @runInSeparateProcess
  68. */
  69. public function testExecute()
  70. {
  71. Config::set('PROFILER_DETAILS', false);
  72. $this->driver
  73. ->expects($this->any())
  74. ->method('quote')
  75. ->with($this->anything())
  76. ->will($this->returnCallback(array($this, 'driverQuote')));
  77. $this->setDriverGetConnectionMethod();
  78. $result = $this->stmt->execute(array('place' => 'place_val', 'new' => 'new_val'));
  79. $this->assertTrue($result);
  80. }
  81. /**
  82. * @runInSeparateProcess
  83. */
  84. public function testExecuteNoPlaceholders()
  85. {
  86. Config::set('PROFILER_DETAILS', false);
  87. $this->setDriverGetConnectionMethod();
  88. $this->sql = 'PLAIN SQL';
  89. $result = $this->stmt->execute(array());
  90. $this->assertTrue($result);
  91. }
  92. /**
  93. * @runInSeparateProcess
  94. * @group MySQL
  95. */
  96. public function testFetchNoResult()
  97. {
  98. Config::set('PROFILER_DETAILS', false);
  99. $this->assertFalse($this->stmt->fetch());
  100. }
  101. /**
  102. * @runInSeparateProcess
  103. * @group MySQL
  104. */
  105. public function testDriverExecuteNoResult()
  106. {
  107. Config::set('PROFILER_DETAILS', false);
  108. $this->setDriverGetConnectionWrongResultMethod();
  109. $this->setExpectedException('GeneralException', 'ERROR');
  110. $this->stmt->execute(array('place' => 'place_val', 'new' => 'new_val'));
  111. }
  112. /**
  113. * @runInSeparateProcess
  114. * @group MySQL
  115. */
  116. public function testFetch()
  117. {
  118. Config::set('PROFILER_DETAILS', false);
  119. $this->setDriverGetConnectionMethod();
  120. $this->stmt->execute(array('place' => 'place_val', 'new' => 'new_val'));
  121. $this->assertSame('OBJECT', $this->stmt->fetch());
  122. $this->assertSame('ARRAY', $this->stmt->fetch(Db::FETCH_NUM));
  123. $this->assertSame('ASSOC', $this->stmt->fetch(Db::FETCH_ASSOC));
  124. $this->assertSame('ARRAY', $this->stmt->fetch(Db::FETCH_BOTH));
  125. }
  126. /**
  127. * @runInSeparateProcess
  128. * @group MySQL
  129. */
  130. public function testFetchObject()
  131. {
  132. Config::set('PROFILER_DETAILS', false);
  133. $this->setDriverGetConnectionMethod();
  134. $this->stmt->execute(array('place' => 'place_val', 'new' => 'new_val'));
  135. $this->assertSame('OBJECT', $this->stmt->fetchObject());
  136. }
  137. /**
  138. * @runInSeparateProcess
  139. * @group MySQL
  140. */
  141. public function testFetchPairs()
  142. {
  143. Config::set('PROFILER_DETAILS', false);
  144. $resultMock = $this->getMockBuilder('mysqli_result')
  145. ->disableOriginalConstructor()
  146. ->setMethods(array('fetch_array', 'close'))
  147. ->setMockClassName('Mysqli_Result_Mock')
  148. ->getMock();
  149. $resultMock
  150. ->expects($this->at(0))
  151. ->method('fetch_array')
  152. ->will($this->returnValue(array('pair', 'value')));
  153. $resultMock
  154. ->expects($this->at(1))
  155. ->method('fetch_array')
  156. ->will($this->returnValue(false));
  157. $resultMock
  158. ->expects($this->any())
  159. ->method('close')
  160. ->will($this->returnValue(TRUE));
  161. $mysqliMock = $this->getMock('MysqliDrvr', array('query'));
  162. $mysqliMock
  163. ->expects($this->any())
  164. ->method('query')
  165. ->with($this->anything())
  166. ->will($this->returnValue($resultMock));
  167. $this->driver
  168. ->expects($this->any())
  169. ->method('getConnection')
  170. ->will($this->returnValue($mysqliMock));
  171. $this->stmt->execute(array('place' => 'place_val', 'new' => 'new_val'));
  172. $this->assertSame(array('pair' => 'value'), $this->stmt->fetchPairs());
  173. }
  174. /**
  175. * @runInSeparateProcess
  176. */
  177. public function testFetchWithDebug()
  178. {
  179. Config::set('PROFILER', true);
  180. Config::set('PROFILER_DETAILS', true);
  181. $this->setDriverGetConnectionMethod();
  182. $this->stmt->execute(array('place' => 'place_val', 'new' => 'new_val'));
  183. $this->assertSame('OBJECT', $this->stmt->fetch());
  184. }
  185. /**
  186. * @runInSeparateProcess
  187. * @group MySQL
  188. */
  189. public function testClose()
  190. {
  191. Config::set('PROFILER_DETAILS', false);
  192. $this->assertAttributeEquals(null, 'result', $this->stmt);
  193. $this->setDriverGetConnectionMethod();
  194. $this->stmt->execute(array('place' => 'place_val', 'new' => 'new_val'));
  195. $this->assertAttributeNotEquals(null, 'result', $this->stmt);
  196. $this->stmt->close();
  197. $this->assertAttributeEquals(null, 'result', $this->stmt);
  198. }
  199. /**
  200. * @group MySQL
  201. */
  202. public function testAffectedRows()
  203. {
  204. $mysqliMock = $this->getMockBuilder('MysqliDrvr');
  205. $mysqliMock->affected_rows = 'AFFECTED_ROWS';
  206. $this->driver
  207. ->expects($this->any())
  208. ->method('getConnection')
  209. ->will($this->returnValue($mysqliMock));
  210. $this->assertSame('AFFECTED_ROWS', $this->stmt->affectedRows());
  211. }
  212. /**
  213. * @group MySQL
  214. */
  215. public function testNumRowsNoResult()
  216. {
  217. $this->assertFalse($this->stmt->numRows());
  218. }
  219. /**
  220. * @runInSeparateProcess
  221. * @TODO: exception just for code coverage - could not mock mysqli properties
  222. * @group MySQL
  223. */
  224. public function testNumRows()
  225. {
  226. $this->markTestSkipped('Unable to execute a method or access a property of an incomplete object.');
  227. Config::set('PROFILER_DETAILS', false);
  228. $this->setDriverGetConnectionMethod();
  229. $this->stmt->execute(array('place' => 'place_val', 'new' => 'new_val'));
  230. $this->assertNull($this->stmt->numRows());
  231. }
  232. /**
  233. * @runInSeparateProcess
  234. * @group MySQL
  235. */
  236. public function testFetchInvalidMode()
  237. {
  238. Config::set('PROFILER_DETAILS', false);
  239. $this->setDriverGetConnectionMethod();
  240. $this->stmt->execute(array('place' => 'place_val', 'new' => 'new_val'));
  241. $this->setExpectedException('GeneralException');
  242. $this->stmt->fetch(324);
  243. }
  244. private function setDriverGetConnectionMethod()
  245. {
  246. $resultMock = $this->getMockBuilder('mysqli_result')
  247. ->disableOriginalConstructor()
  248. ->setMethods(array('fetch_object', 'fetch_array', 'fetch_assoc', 'close'))
  249. ->setMockClassName('Mysqli_Result_Mock')
  250. ->getMock();
  251. $resultMock
  252. ->expects($this->any())
  253. ->method('fetch_object')
  254. ->will($this->returnValue('OBJECT'));
  255. $resultMock
  256. ->expects($this->any())
  257. ->method('fetch_array')
  258. ->will($this->returnValue('ARRAY'));
  259. $resultMock
  260. ->expects($this->any())
  261. ->method('fetch_assoc')
  262. ->will($this->returnValue('ASSOC'));
  263. $resultMock
  264. ->expects($this->any())
  265. ->method('close')
  266. ->will($this->returnValue(TRUE));
  267. $mysqliMock = $this->getMock('MysqliDrvr', array('query'));
  268. $mysqliMock
  269. ->expects($this->any())
  270. ->method('query')
  271. ->with($this->anything())
  272. ->will($this->returnValue($resultMock));
  273. $this->driver
  274. ->expects($this->any())
  275. ->method('getConnection')
  276. ->will($this->returnValue($mysqliMock));
  277. return $this;
  278. }
  279. private function setDriverGetConnectionWrongResultMethod()
  280. {
  281. $mysqliMock = $this->getMock('MysqliDrvr', array('query'));
  282. $mysqliMock
  283. ->expects($this->any())
  284. ->method('query')
  285. ->with($this->anything())
  286. ->will($this->returnValue(false));
  287. $mysqliMock->error = 'ERROR';
  288. $mysqliMock->errno = 0;
  289. $this->driver
  290. ->expects($this->any())
  291. ->method('getConnection')
  292. ->will($this->returnValue($mysqliMock));
  293. return $this;
  294. }
  295. public function driverQuote($val)
  296. {
  297. return $val;
  298. }
  299. public function dbStatementAssemble($val)
  300. {
  301. return $val;
  302. }
  303. }