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.2 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. * @TODO: exception just for code coverage - could not mock mysqli properties
  137. */
  138. public function testNumRows()
  139. {
  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. * @expectedException Exception
  149. * @expectedExceptionMessage Invalid fetch mode
  150. * @runInSeparateProcess
  151. */
  152. public function testFetchInvalidMode()
  153. {
  154. if (!defined('DEBUG')) {
  155. define('DEBUG', false);
  156. }
  157. $this->setDriverGetConnectionMethod();
  158. $this->stmt->execute(array('place' => 'place_val', 'new' => 'new_val'));
  159. $this->stmt->fetch(324);
  160. }
  161. private function setDriverGetConnectionMethod()
  162. {
  163. $resultMock = $this->getMockBuilder('mysqli_result')
  164. ->disableOriginalConstructor()
  165. ->setMethods(array('fetch_object', 'fetch_array', 'fetch_assoc', 'close'))
  166. ->setMockClassName('Mysqli_Result_Mock')
  167. ->getMock();
  168. $resultMock
  169. ->expects($this->any())
  170. ->method('fetch_object')
  171. ->will($this->returnValue('OBJECT'));
  172. $resultMock
  173. ->expects($this->any())
  174. ->method('fetch_array')
  175. ->will($this->returnValue('ARRAY'));
  176. $resultMock
  177. ->expects($this->any())
  178. ->method('fetch_assoc')
  179. ->will($this->returnValue('ASSOC'));
  180. $resultMock
  181. ->expects($this->any())
  182. ->method('close')
  183. ->will($this->returnValue(TRUE));
  184. $mysqliMock = $this->getMock('MysqliDrvr', array('query'));
  185. $mysqliMock
  186. ->expects($this->any())
  187. ->method('query')
  188. ->with($this->anything())
  189. ->will($this->returnValue($resultMock));
  190. $this->driver
  191. ->expects($this->any())
  192. ->method('getConnection')
  193. ->will($this->returnValue($mysqliMock));
  194. return $this;
  195. }
  196. private function setDriverGetConnectionWrongResultMethod()
  197. {
  198. $mysqliMock = $this->getMock('MysqliDrvr', array('query'));
  199. $mysqliMock
  200. ->expects($this->any())
  201. ->method('query')
  202. ->with($this->anything())
  203. ->will($this->returnValue(false));
  204. $mysqliMock->error = 'ERROR';
  205. $mysqliMock->errno = 0;
  206. $this->driver
  207. ->expects($this->any())
  208. ->method('getConnection')
  209. ->will($this->returnValue($mysqliMock));
  210. return $this;
  211. }
  212. }