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.

401 lines
14 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-10
  8. *
  9. * Unit tests for MongoDriver class
  10. */
  11. require_once dirname(__FILE__) . '/../../model/Db.php';
  12. require_once dirname(__FILE__) . '/../../model/DbDriver.php';
  13. require_once dirname(__FILE__) . '/../../model/NoSqlDbDriver.php';
  14. require_once dirname(__FILE__) . '/../../model/MongoDbCommand.php';
  15. require_once dirname(__FILE__) . '/../../model/DbStatement.php';
  16. require_once dirname(__FILE__) . '/../../model/MongoStatement.php';
  17. require_once dirname(__FILE__) . '/../../model/MongoDriver.php';
  18. require_once dirname(__FILE__) . '/../../exception/GeneralException.php';
  19. class MongoDriverTest extends PHPUnit_Framework_TestCase
  20. {
  21. private $conf = array();
  22. public function setUp()
  23. {
  24. $this->conf = array(
  25. 'hostname' => 'localhost',
  26. 'database' => 'test',
  27. 'username' => 'test',
  28. 'password' => '1234',
  29. 'port' => 27017
  30. );
  31. $data = array(
  32. array(
  33. 'name' => 'bread',
  34. 'price' => 3.2,
  35. 'quantity' => 10
  36. ),
  37. array(
  38. 'name' => 'eggs',
  39. 'price' => 2.1,
  40. 'quantity' => 20
  41. ),
  42. array(
  43. 'name' => 'fish',
  44. 'price' => 13.2,
  45. 'quantity' => 2
  46. ),
  47. array(
  48. 'name' => 'milk',
  49. 'price' => 3.8,
  50. 'quantity' => 1
  51. ),
  52. array(
  53. 'name' => 'eggs',
  54. 'price' => 2.3,
  55. 'quantity' => 5
  56. )
  57. );
  58. $connection = new Mongo('mongodb://' . $this->conf['hostname'] . ':' . $this->conf['port']);
  59. $db = $connection->selectDB($this->conf['database']);
  60. $db->authenticate($this->conf['username'], $this->conf['password']);
  61. $collection = 'items';
  62. $db->dropCollection($collection);
  63. $collection = $db->selectCollection($collection);
  64. foreach ($data as $document) {
  65. $collection->insert($document);
  66. }
  67. }
  68. /**
  69. * @group Mongo
  70. */
  71. public function testGetConnectionNoHostname()
  72. {
  73. unset($this->conf['hostname']);
  74. $this->setExpectedException('GeneralException', 'Configuration must have a "hostname"');
  75. $mongo = new MongoDriver($this->conf);
  76. }
  77. /**
  78. * @group Mongo
  79. */
  80. public function testGetConnectionWrongPassword()
  81. {
  82. $this->conf['password'] = 'nopass';
  83. $mongo = new MongoDriver($this->conf);
  84. $this->setExpectedException('MongoConnectionException', 'Couldn\'t authenticate with database');
  85. $this->assertInstanceOf('MongoDB', $mongo->getConnection());
  86. }
  87. /**
  88. * @group Mongo
  89. */
  90. public function testGetConnection()
  91. {
  92. $mongo = new MongoDriver($this->conf);
  93. $this->assertFalse($mongo->isConnected());
  94. $this->assertInstanceOf('Mongo', $mongo->getConnection());
  95. $this->assertTrue($mongo->isConnected());
  96. $mongo->getConnection();
  97. $mongo->disconnect();
  98. $this->assertFalse($mongo->isConnected());
  99. }
  100. /**
  101. * @runInSeparateProcess
  102. * @group Mongo
  103. */
  104. public function testFind()
  105. {
  106. Config::set('DEBUG', false);
  107. $mongo = new MongoDriver($this->conf);
  108. $this->assertEquals(1, $mongo->find('items', array('name' => 'bread'))->numRows());
  109. $this->assertEquals(2, $mongo->find('items', array('name' => 'eggs'))->numRows());
  110. $eggs = $mongo->find('items', array('name' => 'eggs'));
  111. $egg = $eggs->fetch();
  112. $this->assertEquals(20, $egg->quantity);
  113. $egg = $eggs->fetchObject();
  114. $this->assertEquals('eggs', $egg->name);
  115. $this->assertFalse($eggs->fetchObject());
  116. $this->assertEquals(3, $mongo->find('items', array('price' => array('$lt' => 3.5)))->numRows());
  117. $data = $mongo->find('items', array('price' => array('$lt' => 3.5)));
  118. $count = 0;
  119. while ($row = $data->fetch(Db::FETCH_ASSOC)) {
  120. $count++;
  121. $this->assertLessThan(3.5, $row['price']);
  122. }
  123. $this->assertEquals(3, $count);
  124. $this->assertEquals(5, $mongo->find('items', array())->numRows());
  125. }
  126. /**
  127. * @runInSeparateProcess
  128. * @group Mongo
  129. */
  130. public function testOrderSkipLimit()
  131. {
  132. Config::set('DEBUG', false);
  133. $mongo = new MongoDriver($this->conf);
  134. $count = $mongo->find('items', array())->numRows();
  135. $this->assertEquals(1, $mongo->find('items', array('name' => 'bread'))->numRows());
  136. $this->assertEquals(2, $mongo->find('items', array('name' => 'eggs'))->numRows());
  137. $mongo->insert('items', array('name' => 'fdsbssc'));
  138. $mongo->insert('items', array('name' => 'boc'));
  139. $mongo->insert('items', array('name' => 'abc'));
  140. $mongo->insert('items', array('name' => 'vcxxc'));
  141. $mongo->insert('items', array('name' => 'abbc'));
  142. $mongo->insert('items', array('name' => 'dsbssc'));
  143. $mongo->insert('items', array('name' => 'bssc'));
  144. $data = $mongo->find('items', array());
  145. $this->assertEquals($count + 7, $data->numRows());
  146. $data->order(array('name' => 1));
  147. $this->assertEquals('abbc', $data->fetch()->name);
  148. $this->assertEquals('abc', $data->fetch()->name);
  149. $this->assertEquals('boc', $data->fetch()->name);
  150. $data = $mongo->find('items', array());
  151. $data->order(array('name' => -1));
  152. $data->skip(3);
  153. $data->limit(1);
  154. while ($row = $data->fetch()) {
  155. $this->assertEquals('fdsbssc', $row->name);
  156. }
  157. }
  158. /**
  159. * @runInSeparateProcess
  160. * @group Mongo
  161. */
  162. public function testCount()
  163. {
  164. Config::set('DEBUG', false);
  165. $mongo = new MongoDriver($this->conf);
  166. $this->assertEquals(5, $mongo->count('items'));
  167. $this->assertEquals(2, $mongo->count('items', array('name' => 'eggs')));
  168. $this->assertEquals(1, $mongo->count('items', array('name' => 'eggs'), 1));
  169. }
  170. /**
  171. * @runInSeparateProcess
  172. * @group Mongo
  173. */
  174. public function testCursorCount()
  175. {
  176. Config::set('DEBUG', false);
  177. $mongo = new MongoDriver($this->conf);
  178. $this->assertEquals(5, $mongo->find('items')->count(5));
  179. $this->assertCount(3, $mongo->find('items')->limit(3)->fetchAll());
  180. $this->assertEquals(5, $mongo->count('items'));
  181. }
  182. /**
  183. * @runInSeparateProcess
  184. * @group Mongo
  185. */
  186. public function testGet()
  187. {
  188. Config::set('DEBUG', false);
  189. $mongo = new MongoDriver($this->conf);
  190. $eggs = $mongo->get('items', array('name' => 'eggs'))->fetchObject();
  191. $this->assertEquals(20, $eggs->quantity);
  192. $eggs = $mongo->get('items', array('name' => 'eggs'))->fetch();
  193. $this->assertEquals('eggs', $eggs->name);
  194. $this->assertInstanceOf('MongoId', $eggs->_id);
  195. }
  196. /**
  197. * @runInSeparateProcess
  198. * @group Mongo
  199. */
  200. public function testRemove()
  201. {
  202. Config::set('DEBUG', false);
  203. $mongo = new MongoDriver($this->conf);
  204. $this->assertEquals(1, $mongo->find('items', array('name' => 'bread'))->numRows());
  205. $this->assertEquals(0, $mongo->delete('items', array('name' => 'esggs')));
  206. $this->assertEquals(2, $mongo->delete('items', array('name' => 'eggs')));
  207. $this->assertEquals(1, $mongo->delete('items', array('name' => 'bread')));
  208. $this->assertEquals(0, $mongo->find('items', array('name' => 'bread'))->numRows());
  209. }
  210. /**
  211. * @runInSeparateProcess
  212. * @group Mongo
  213. */
  214. public function testInsert()
  215. {
  216. Config::set('DEBUG', false);
  217. $mongo = new MongoDriver($this->conf);
  218. $this->assertEquals(1, $mongo->find('items', array('name' => 'bread'))->numRows());
  219. $this->assertEquals(0, $mongo->insert('items', array('name' => 'bread')));
  220. $this->assertNotEmpty($mongo->getInsertId());
  221. $this->assertEquals(2, $mongo->find('items', array('name' => 'bread'))->numRows());
  222. $this->assertEquals(0, $mongo->insert('items', array('name' => 'meat', 'weight' => 230)));
  223. $this->assertEquals(230, $mongo->get('items', array('name' => 'meat'))->fetch()->weight);
  224. }
  225. /**
  226. * @runInSeparateProcess
  227. * @group Mongo
  228. */
  229. public function testBatchInsert()
  230. {
  231. Config::set('DEBUG', false);
  232. $data = array(
  233. array('name' => 'first object'),
  234. array('name' => 'second object'),
  235. array('name' => 'equal object'),
  236. array('name' => 'equal object')
  237. );
  238. $mongo = new MongoDriver($this->conf);
  239. $mongo->insert('items', $data, true);
  240. $this->assertEquals(1, $mongo->find('items', array('name' => 'first object'))->numRows());
  241. $this->assertEquals(1, $mongo->find('items', array('name' => 'second object'))->numRows());
  242. $this->assertEquals(2, $mongo->find('items', array('name' => 'equal object'))->numRows());
  243. }
  244. /**
  245. * @runInSeparateProcess
  246. * @group Mongo
  247. */
  248. public function testGetInsertId()
  249. {
  250. Config::set('DEBUG', false);
  251. $mongo = new MongoDriver($this->conf);
  252. $this->assertEquals(0, $mongo->getInsertId());
  253. $this->assertEquals(1, $mongo->find('items', array('name' => 'bread'))->numRows());
  254. $this->assertEquals(0, $mongo->insert('items', array('name' => 'bread')));
  255. $this->assertEquals(2, $mongo->find('items', array('name' => 'bread'))->numRows());
  256. $id1 = $mongo->getInsertId();
  257. $this->assertNotEmpty($id1);
  258. $this->assertEquals(0, $mongo->insert('items', array('name' => 'bread')));
  259. $id2 = $mongo->getInsertId();
  260. $this->assertNotEmpty($id2);
  261. $this->assertNotEquals($id1, $id2);
  262. $this->assertEquals(3, $mongo->find('items', array('name' => 'bread'))->numRows());
  263. }
  264. /**
  265. * @runInSeparateProcess
  266. * @group Mongo
  267. */
  268. public function testUpdate()
  269. {
  270. Config::set('DEBUG', false);
  271. $mongo = new MongoDriver($this->conf);
  272. $this->assertEquals(1, $mongo->find('items', array('name' => 'bread'))->numRows());
  273. $this->assertEquals(1, $mongo->update('items', array('$set' => array('price' => 200, 'date' => 'today')), array('name' => 'fish')));
  274. $this->assertEquals(2, $mongo->update('items', array('$set' => array('price' => 1)), array('name' => 'eggs')));
  275. $fish = $mongo->get('items', array('name' => 'fish'))->fetch();
  276. $this->assertEquals(200, $fish->price);
  277. $this->assertEquals('today', $fish->date);
  278. $this->assertEquals(0, $mongo->update('items', array('$set' => array('price' => 200, 'date' => 'today')), array('name' => 'ball')));
  279. }
  280. /**
  281. * @runInSeparateProcess
  282. * @group Mongo
  283. */
  284. public function testUpsert()
  285. {
  286. Config::set('DEBUG', false);
  287. $mongo = new MongoDriver($this->conf);
  288. $mongo->insert('items', array('name' => 'bread'));
  289. $this->assertEquals(1, $mongo->update('items', array('$set' => array('price' => 200, 'date' => 'today')), array('name' => 'ball'), true, true));
  290. $this->assertEquals('today', $mongo->get('items', array('name' => 'ball'))->fetch()->date);
  291. }
  292. /**
  293. * @runInSeparateProcess
  294. * @group Mongo
  295. */
  296. public function testFindAndModify()
  297. {
  298. Config::set('DEBUG', false);
  299. $mongo = new MongoDriver($this->conf);
  300. $this->assertEquals(10, $mongo->get('items', array('name' => 'bread'))->fetch()->quantity);
  301. $result = $mongo->findAndModify('items', array('name' => 'bread'), array('$set' => array('quantity' => 20)));
  302. $this->assertEquals(10, $result->fetch()->quantity);
  303. $this->assertEquals(20, $mongo->get('items', array('name' => 'bread'))->fetch()->quantity);
  304. }
  305. /**
  306. * @runInSeparateProcess
  307. * @group Mongo
  308. */
  309. public function testFindAndModifyNoItem()
  310. {
  311. Config::set('DEBUG', false);
  312. $mongo = new MongoDriver($this->conf);
  313. $this->assertEquals(10, $mongo->get('items', array('name' => 'bread'))->fetch()->quantity);
  314. $result = $mongo->findAndModify('items', array('name' => 'breading'), array('$set' => array('quantity' => 20)))->fetch();
  315. $this->assertFalse($result);
  316. $this->assertEquals(10, $mongo->get('items', array('name' => 'bread'))->fetch()->quantity);
  317. }
  318. /**
  319. * @runInSeparateProcess
  320. * @group Mongo
  321. */
  322. public function testEvalCommand()
  323. {
  324. Config::set('DEBUG', false);
  325. $mongo = new MongoDriver($this->conf);
  326. $result = $mongo->command('items', array('$eval' => 'function() { return db.items.count();}'));
  327. $this->assertEquals(5, $result->fetch());
  328. $this->assertEquals(5, $mongo->count('items'));
  329. $result = $mongo->command('items', array('$eval' => 'function() { return "HELLO!";}'));
  330. $this->assertEquals("HELLO!", $result->fetch());
  331. }
  332. /**
  333. * @runInSeparateProcess
  334. * @group Mongo
  335. */
  336. public function testEval()
  337. {
  338. Config::set('DEBUG', false);
  339. $mongo = new MongoDriver($this->conf);
  340. $result = $mongo->command('items', array('$eval' => 'function() {return true; }'));
  341. $this->assertTrue($result->fetch());
  342. $result = $mongo->command('items', array('$eval' => 'function() {return "Hello"; }'));
  343. $this->assertSame('Hello', $result->fetch());
  344. $result = $mongo->command('items', array('$eval' => 'function() {return db.items.count(); }'));
  345. $this->assertEquals(5, $result->fetch());
  346. }
  347. /**
  348. * @runInSeparateProcess
  349. * @group Mongo
  350. */
  351. public function testCommand()
  352. {
  353. Config::set('DEBUG', false);
  354. $mongo = new MongoDriver($this->conf);
  355. $result = $mongo->command('items', array('distinct' => 'items', 'key' => 'name'));
  356. $this->assertEquals(4, count($result->fetch(DB::FETCH_ASSOC)));
  357. }
  358. }