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.

242 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. */
  56. public function testDriverExecuteNoResult()
  57. {
  58. if (!defined('DEBUG')) {
  59. define('DEBUG', false);
  60. }
  61. $this->setDriverGetConnectionWrongResultMethod();
  62. $this->setExpectedException('GeneralException', 'ERROR');
  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. * @TODO: exception just for code coverage - could not mock mysqli properties
  136. */
  137. public function testNumRows()
  138. {
  139. $this->markTestSkipped('Unable to execute a method or access a property of an incomplete object.');
  140. if (!defined('DEBUG')) {
  141. define('DEBUG', false);
  142. }
  143. $this->setDriverGetConnectionMethod();
  144. $this->stmt->execute(array('place' => 'place_val', 'new' => 'new_val'));
  145. $this->assertNull($this->stmt->numRows());
  146. }
  147. /**
  148. * @runInSeparateProcess
  149. */
  150. public function testFetchInvalidMode()
  151. {
  152. if (!defined('DEBUG')) {
  153. define('DEBUG', false);
  154. }
  155. $this->setDriverGetConnectionMethod();
  156. $this->stmt->execute(array('place' => 'place_val', 'new' => 'new_val'));
  157. $this->setExpectedException('GeneralException');
  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. }