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.

420 lines
12 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-15
  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/MongoStatement.php';
  17. class MongoStatementTest extends PHPUnit_Framework_TestCase
  18. {
  19. private $driver;
  20. private $stmt;
  21. private $request;
  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('getConnection'))
  38. ->getMock();
  39. $this->request = $this->getMockBuilder('MongoDbCommandMock')
  40. ->disableOriginalConstructor()
  41. ->setMethods(array('execute', 'bindParam'))
  42. ->getMock();
  43. $this->stmt = new MongoStatement($this->driver, $this->request);
  44. }
  45. }
  46. /**
  47. * @runInSeparateProcess
  48. */
  49. public function testAffectedNumRowsNoResult()
  50. {
  51. if (!defined('DEBUG')) {
  52. define('DEBUG', false);
  53. }
  54. $this->assertFalse($this->stmt->affectedRows());
  55. $this->assertFalse($this->stmt->numRows());
  56. $this->setDriverGetConnectionMethod();
  57. $this->request
  58. ->expects($this->any())
  59. ->method('execute')
  60. ->will($this->returnValue(array()));
  61. $this->stmt->execute();
  62. $this->assertFalse($this->stmt->affectedRows());
  63. }
  64. /**
  65. * @runInSeparateProcess
  66. */
  67. public function testAffectedNumRows()
  68. {
  69. if (!defined('DEBUG')) {
  70. define('DEBUG', false);
  71. }
  72. $this->setDriverGetConnectionMethod();
  73. $this->request
  74. ->expects($this->any())
  75. ->method('execute')
  76. ->will($this->returnValue(array('n' => 20, 'ok' => 1)));
  77. $this->stmt->execute();
  78. $this->assertEquals(20, $this->stmt->affectedRows());
  79. }
  80. /**
  81. * @runInSeparateProcess
  82. */
  83. public function testExecute()
  84. {
  85. if (!defined('DEBUG')) {
  86. define('DEBUG', false);
  87. }
  88. $this->setDriverGetConnectionMethod()->setRequestExecuteMethod();
  89. $this->assertTrue($this->stmt->execute());
  90. }
  91. /**
  92. * @runInSeparateProcess
  93. * @expectedException Exception
  94. * @expectedExceptionMessage MongoDB request error.
  95. */
  96. public function testExecuteNoResult()
  97. {
  98. if (!defined('DEBUG')) {
  99. define('DEBUG', false);
  100. }
  101. $this->setDriverGetConnectionMethod();
  102. $this->request
  103. ->expects($this->any())
  104. ->method('execute')
  105. ->will($this->returnValue(false));
  106. $this->stmt->execute();
  107. }
  108. /**
  109. * @runInSeparateProcess
  110. * @expectedException Exception
  111. * @expectedExceptionMessage No connection to MongoDB server.
  112. */
  113. public function testExecuteNoConnection()
  114. {
  115. if (!defined('DEBUG')) {
  116. define('DEBUG', false);
  117. }
  118. $this->driver
  119. ->expects($this->any())
  120. ->method('getConnection')
  121. ->will($this->returnValue(false));
  122. $this->stmt->execute();
  123. }
  124. /**
  125. * @runInSeparateProcess
  126. */
  127. public function testExecuteWithDebug()
  128. {
  129. if (!defined('DEBUG')) {
  130. define('DEBUG', true);
  131. }
  132. $this->setDriverGetConnectionMethod()->setRequestExecuteMethod();
  133. $this->assertTrue($this->stmt->execute());
  134. $this->assertEquals(10, $this->stmt->numRows());
  135. }
  136. /**
  137. * @runInSeparateProcess
  138. */
  139. public function testBindParam()
  140. {
  141. if (!defined('DEBUG')) {
  142. define('DEBUG', true);
  143. }
  144. $this->request
  145. ->expects($this->once())
  146. ->method('bindParam')
  147. ->will($this->returnValue(true));
  148. $this->setDriverGetConnectionMethod()->setRequestExecuteMethod();
  149. $this->assertTrue($this->stmt->execute(array('one' => 'two')));
  150. $this->assertAttributeNotEquals(null, 'result', $this->stmt);
  151. $this->stmt->close();
  152. $this->assertAttributeEquals(null, 'result', $this->stmt);
  153. }
  154. /**
  155. * @runInSeparateProcess
  156. */
  157. public function testFetch()
  158. {
  159. if (!defined('DEBUG')) {
  160. define('DEBUG', true);
  161. }
  162. $this->assertFalse($this->stmt->fetch());
  163. $this->setDriverGetConnectionMethod()->setRequestForFetch();
  164. $this->stmt->execute();
  165. $result = $this->stmt->fetch();
  166. $this->assertEquals('prev', $result->next);
  167. $this->assertFalse($this->stmt->fetch());
  168. }
  169. /**
  170. * @runInSeparateProcess
  171. */
  172. public function testFetchWithInitialArray()
  173. {
  174. if (!defined('DEBUG')) {
  175. define('DEBUG', true);
  176. }
  177. $this->assertFalse($this->stmt->fetch());
  178. $this->setDriverGetConnectionMethod();
  179. $this->request
  180. ->expects($this->once())
  181. ->method('execute')
  182. ->will($this->returnValue(array('some' => 'val')));
  183. $this->stmt->execute();
  184. $this->assertFalse($this->stmt->fetch());
  185. }
  186. /**
  187. * @runInSeparateProcess
  188. */
  189. public function testFetchAssocFromCursor()
  190. {
  191. if (!defined('DEBUG')) {
  192. define('DEBUG', true);
  193. }
  194. $this->assertFalse($this->stmt->fetch());
  195. $this->setDriverGetConnectionMethod()->setRequestForFetch();
  196. $this->stmt->execute();
  197. $result = $this->stmt->fetch(Db::FETCH_ASSOC);
  198. $this->assertEquals('prev', $result['next']);
  199. $this->assertEquals(array(), $this->stmt->fetch(Db::FETCH_ASSOC));
  200. }
  201. /**
  202. * @runInSeparateProcess
  203. */
  204. public function testFetchAssocFromArray()
  205. {
  206. if (!defined('DEBUG')) {
  207. define('DEBUG', true);
  208. }
  209. $this->assertFalse($this->stmt->fetch());
  210. $this->setDriverGetConnectionMethod();
  211. $this->request
  212. ->expects($this->once())
  213. ->method('execute')
  214. ->will($this->returnValue(array('some' => 'val')));
  215. $this->stmt->execute();
  216. $result = $this->stmt->fetch(Db::FETCH_ASSOC);
  217. $this->assertEquals('val', $result['some']);
  218. }
  219. /**
  220. * @runInSeparateProcess
  221. * @expectedException Exception
  222. * @expectedExceptionMessage Invalid fetch mode "222" specified
  223. */
  224. public function testFetchWrongMode()
  225. {
  226. if (!defined('DEBUG')) {
  227. define('DEBUG', true);
  228. }
  229. $this->assertFalse($this->stmt->fetch());
  230. $this->setDriverGetConnectionMethod();
  231. $this->request
  232. ->expects($this->once())
  233. ->method('execute')
  234. ->will($this->returnValue(array('some' => 'val')));
  235. $this->stmt->execute();
  236. $this->stmt->fetch(222);
  237. }
  238. /**
  239. * @runInSeparateProcess
  240. */
  241. public function testSkipOrderLimit()
  242. {
  243. if (!defined('DEBUG')) {
  244. define('DEBUG', true);
  245. }
  246. $this->setDriverGetConnectionMethod()->setRequestForFetch();
  247. $this->stmt->execute();
  248. $this->assertInstanceOf('MongoStatement', $this->stmt->order(array('id' => 1)));
  249. $this->stmt->fetch();
  250. $this->stmt->fetch();
  251. }
  252. /**
  253. * @runInSeparateProcess
  254. * @expectedException Exception
  255. * @expectedExceptionMessage MongoStatement error. Impossible order results of opened cursor
  256. */
  257. public function testOrderException()
  258. {
  259. if (!defined('DEBUG')) {
  260. define('DEBUG', true);
  261. }
  262. $this->setDriverGetConnectionMethod();
  263. $this->request
  264. ->expects($this->once())
  265. ->method('execute')
  266. ->will($this->returnValue(array('some' => 'val')));
  267. $this->stmt->execute();
  268. $this->stmt->order(array('id' => 1));
  269. }
  270. /**
  271. * @runInSeparateProcess
  272. * @expectedException Exception
  273. * @expectedExceptionMessage MongoStatement error. Impossible skip results of opened cursor
  274. */
  275. public function testSkipException()
  276. {
  277. if (!defined('DEBUG')) {
  278. define('DEBUG', true);
  279. }
  280. $this->setDriverGetConnectionMethod();
  281. $this->request
  282. ->expects($this->once())
  283. ->method('execute')
  284. ->will($this->returnValue(array('some' => 'val')));
  285. $this->stmt->execute();
  286. $this->stmt->skip(array('id' => 1));
  287. }
  288. /**
  289. * @runInSeparateProcess
  290. * @expectedException Exception
  291. * @expectedExceptionMessage MongoStatement error. Impossible limit results of opened cursor
  292. */
  293. public function testLimitException()
  294. {
  295. if (!defined('DEBUG')) {
  296. define('DEBUG', true);
  297. }
  298. $this->setDriverGetConnectionMethod();
  299. $this->request
  300. ->expects($this->once())
  301. ->method('execute')
  302. ->will($this->returnValue(array('some' => 'val')));
  303. $this->stmt->execute();
  304. $this->stmt->limit(array('id' => 1));
  305. }
  306. private function setDriverGetConnectionMethod()
  307. {
  308. $mongoMock = $this->getMock('Mongo');
  309. $this->driver
  310. ->expects($this->any())
  311. ->method('getConnection')
  312. ->will($this->returnValue($mongoMock));
  313. return $this;
  314. }
  315. public function setRequestExecuteMethod()
  316. {
  317. $resultMock = $this->getMockBuilder('MongoCursor')
  318. ->setMethods(array('count'))
  319. ->disableOriginalConstructor()
  320. ->getMock();
  321. $resultMock
  322. ->expects($this->any())
  323. ->method('count')
  324. ->will($this->returnValue(10));
  325. $this->request
  326. ->expects($this->any())
  327. ->method('execute')
  328. ->will($this->returnValue($resultMock));
  329. return $this;
  330. }
  331. public function setRequestForFetch()
  332. {
  333. $resultMock = $this->getMockBuilder('MongoCursor')
  334. ->setMethods(array('count', 'getNext', 'limit', 'sort', 'skip'))
  335. ->disableOriginalConstructor()
  336. ->getMock();
  337. $resultMock
  338. ->expects($this->any())
  339. ->method('limit');
  340. $resultMock
  341. ->expects($this->any())
  342. ->method('sort');
  343. $resultMock
  344. ->expects($this->any())
  345. ->method('skip');
  346. $resultMock
  347. ->expects($this->any())
  348. ->method('count')
  349. ->will($this->returnValue(10));
  350. $resultMock
  351. ->expects($this->at(0))
  352. ->method('getNext')
  353. ->will($this->returnValue(array('next' => 'prev', '_id' => 10)));
  354. $resultMock
  355. ->expects($this->at(1))
  356. ->method('getNext')
  357. ->will($this->returnValue(array()));
  358. $this->request
  359. ->expects($this->any())
  360. ->method('execute')
  361. ->will($this->returnValue($resultMock));
  362. return $this;
  363. }
  364. }