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.

256 lines
7.5 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. * @group MySQL
  46. */
  47. public function testFetchNoResult()
  48. {
  49. if (!defined('DEBUG')) {
  50. define('DEBUG', false);
  51. }
  52. $this->assertFalse($this->stmt->fetch());
  53. }
  54. /**
  55. * @runInSeparateProcess
  56. * @group MySQL
  57. */
  58. public function testDriverExecuteNoResult()
  59. {
  60. if (!defined('DEBUG')) {
  61. define('DEBUG', false);
  62. }
  63. $this->setDriverGetConnectionWrongResultMethod();
  64. $this->setExpectedException('GeneralException', 'ERROR');
  65. $this->stmt->execute(array('place' => 'place_val', 'new' => 'new_val'));
  66. }
  67. /**
  68. * @runInSeparateProcess
  69. * @group MySQL
  70. */
  71. public function testFetch()
  72. {
  73. if (!defined('DEBUG')) {
  74. define('DEBUG', false);
  75. }
  76. $this->setDriverGetConnectionMethod();
  77. $this->stmt->execute(array('place' => 'place_val', 'new' => 'new_val'));
  78. $this->assertSame('OBJECT', $this->stmt->fetch());
  79. $this->assertSame('ARRAY', $this->stmt->fetch(Db::FETCH_NUM));
  80. $this->assertSame('ASSOC', $this->stmt->fetch(Db::FETCH_ASSOC));
  81. $this->assertSame('ARRAY', $this->stmt->fetch(Db::FETCH_BOTH));
  82. }
  83. /**
  84. * @runInSeparateProcess
  85. * @group MySQL
  86. */
  87. public function testFetchObject()
  88. {
  89. if (!defined('DEBUG')) {
  90. define('DEBUG', false);
  91. }
  92. $this->setDriverGetConnectionMethod();
  93. $this->stmt->execute(array('place' => 'place_val', 'new' => 'new_val'));
  94. $this->assertSame('OBJECT', $this->stmt->fetchObject());
  95. }
  96. /**
  97. * @runInSeparateProcess
  98. * @group MySQL
  99. */
  100. public function testFetchWithDebug()
  101. {
  102. if (!defined('DEBUG')) {
  103. define('DEBUG', true);
  104. }
  105. $this->setDriverGetConnectionMethod();
  106. $this->stmt->execute(array('place' => 'place_val', 'new' => 'new_val'));
  107. $this->assertSame('OBJECT', $this->stmt->fetch());
  108. }
  109. /**
  110. * @runInSeparateProcess
  111. * @group MySQL
  112. */
  113. public function testClose()
  114. {
  115. if (!defined('DEBUG')) {
  116. define('DEBUG', false);
  117. }
  118. $this->assertAttributeEquals(null, 'result', $this->stmt);
  119. $this->setDriverGetConnectionMethod();
  120. $this->stmt->execute(array('place' => 'place_val', 'new' => 'new_val'));
  121. $this->assertAttributeNotEquals(null, 'result', $this->stmt);
  122. $this->stmt->close();
  123. $this->assertAttributeEquals(null, 'result', $this->stmt);
  124. }
  125. /**
  126. * @group MySQL
  127. */
  128. public function testAffectedRows()
  129. {
  130. $mysqliMock = $this->getMockBuilder('MysqliDrvr');
  131. $mysqliMock->affected_rows = 'AFFECTED_ROWS';
  132. $this->driver
  133. ->expects($this->any())
  134. ->method('getConnection')
  135. ->will($this->returnValue($mysqliMock));
  136. $this->assertSame('AFFECTED_ROWS', $this->stmt->affectedRows());
  137. }
  138. /**
  139. * @group MySQL
  140. */
  141. public function testNumRowsNoResult()
  142. {
  143. $this->assertFalse($this->stmt->numRows());
  144. }
  145. /**
  146. * @runInSeparateProcess
  147. * @TODO: exception just for code coverage - could not mock mysqli properties
  148. * @group MySQL
  149. */
  150. public function testNumRows()
  151. {
  152. $this->markTestSkipped('Unable to execute a method or access a property of an incomplete object.');
  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->assertNull($this->stmt->numRows());
  159. }
  160. /**
  161. * @runInSeparateProcess
  162. * @group MySQL
  163. */
  164. public function testFetchInvalidMode()
  165. {
  166. if (!defined('DEBUG')) {
  167. define('DEBUG', false);
  168. }
  169. $this->setDriverGetConnectionMethod();
  170. $this->stmt->execute(array('place' => 'place_val', 'new' => 'new_val'));
  171. $this->setExpectedException('GeneralException');
  172. $this->stmt->fetch(324);
  173. }
  174. private function setDriverGetConnectionMethod()
  175. {
  176. $resultMock = $this->getMockBuilder('mysqli_result')
  177. ->disableOriginalConstructor()
  178. ->setMethods(array('fetch_object', 'fetch_array', 'fetch_assoc', 'close'))
  179. ->setMockClassName('Mysqli_Result_Mock')
  180. ->getMock();
  181. $resultMock
  182. ->expects($this->any())
  183. ->method('fetch_object')
  184. ->will($this->returnValue('OBJECT'));
  185. $resultMock
  186. ->expects($this->any())
  187. ->method('fetch_array')
  188. ->will($this->returnValue('ARRAY'));
  189. $resultMock
  190. ->expects($this->any())
  191. ->method('fetch_assoc')
  192. ->will($this->returnValue('ASSOC'));
  193. $resultMock
  194. ->expects($this->any())
  195. ->method('close')
  196. ->will($this->returnValue(TRUE));
  197. $mysqliMock = $this->getMock('MysqliDrvr', array('query'));
  198. $mysqliMock
  199. ->expects($this->any())
  200. ->method('query')
  201. ->with($this->anything())
  202. ->will($this->returnValue($resultMock));
  203. $this->driver
  204. ->expects($this->any())
  205. ->method('getConnection')
  206. ->will($this->returnValue($mysqliMock));
  207. return $this;
  208. }
  209. private function setDriverGetConnectionWrongResultMethod()
  210. {
  211. $mysqliMock = $this->getMock('MysqliDrvr', array('query'));
  212. $mysqliMock
  213. ->expects($this->any())
  214. ->method('query')
  215. ->with($this->anything())
  216. ->will($this->returnValue(false));
  217. $mysqliMock->error = 'ERROR';
  218. $mysqliMock->errno = 0;
  219. $this->driver
  220. ->expects($this->any())
  221. ->method('getConnection')
  222. ->will($this->returnValue($mysqliMock));
  223. return $this;
  224. }
  225. }