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.

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