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.

386 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 testRemove()
  187. {
  188. Config::set('DEBUG', false);
  189. $mongo = new MongoDriver($this->conf);
  190. $this->assertEquals(1, $mongo->find('items', array('name' => 'bread'))->numRows());
  191. $this->assertEquals(0, $mongo->delete('items', array('name' => 'esggs')));
  192. $this->assertEquals(2, $mongo->delete('items', array('name' => 'eggs')));
  193. $this->assertEquals(1, $mongo->delete('items', array('name' => 'bread')));
  194. $this->assertEquals(0, $mongo->find('items', array('name' => 'bread'))->numRows());
  195. }
  196. /**
  197. * @runInSeparateProcess
  198. * @group Mongo
  199. */
  200. public function testInsert()
  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->insert('items', array('name' => 'bread')));
  206. $this->assertNotEmpty($mongo->getInsertId());
  207. $this->assertEquals(2, $mongo->find('items', array('name' => 'bread'))->numRows());
  208. $this->assertEquals(0, $mongo->insert('items', array('name' => 'meat', 'weight' => 230)));
  209. $this->assertEquals(230, $mongo->find('items', array('name' => 'meat'))->fetch()->weight);
  210. }
  211. /**
  212. * @runInSeparateProcess
  213. * @group Mongo
  214. */
  215. public function testBatchInsert()
  216. {
  217. Config::set('DEBUG', false);
  218. $data = array(
  219. array('name' => 'first object'),
  220. array('name' => 'second object'),
  221. array('name' => 'equal object'),
  222. array('name' => 'equal object')
  223. );
  224. $mongo = new MongoDriver($this->conf);
  225. $mongo->insert('items', $data, true);
  226. $this->assertEquals(1, $mongo->find('items', array('name' => 'first object'))->numRows());
  227. $this->assertEquals(1, $mongo->find('items', array('name' => 'second object'))->numRows());
  228. $this->assertEquals(2, $mongo->find('items', array('name' => 'equal object'))->numRows());
  229. }
  230. /**
  231. * @runInSeparateProcess
  232. * @group Mongo
  233. */
  234. public function testGetInsertId()
  235. {
  236. Config::set('DEBUG', false);
  237. $mongo = new MongoDriver($this->conf);
  238. $this->assertEquals(0, $mongo->getInsertId());
  239. $this->assertEquals(1, $mongo->find('items', array('name' => 'bread'))->numRows());
  240. $this->assertEquals(0, $mongo->insert('items', array('name' => 'bread')));
  241. $this->assertEquals(2, $mongo->find('items', array('name' => 'bread'))->numRows());
  242. $id1 = $mongo->getInsertId();
  243. $this->assertNotEmpty($id1);
  244. $this->assertEquals(0, $mongo->insert('items', array('name' => 'bread')));
  245. $id2 = $mongo->getInsertId();
  246. $this->assertNotEmpty($id2);
  247. $this->assertNotEquals($id1, $id2);
  248. $this->assertEquals(3, $mongo->find('items', array('name' => 'bread'))->numRows());
  249. }
  250. /**
  251. * @runInSeparateProcess
  252. * @group Mongo
  253. */
  254. public function testUpdate()
  255. {
  256. Config::set('DEBUG', false);
  257. $mongo = new MongoDriver($this->conf);
  258. $this->assertEquals(1, $mongo->find('items', array('name' => 'bread'))->numRows());
  259. $this->assertEquals(1, $mongo->update('items', array('$set' => array('price' => 200, 'date' => 'today')), array('name' => 'fish')));
  260. $this->assertEquals(2, $mongo->update('items', array('$set' => array('price' => 1)), array('name' => 'eggs')));
  261. $fish = $mongo->find('items', array('name' => 'fish'))->fetch();
  262. $this->assertEquals(200, $fish->price);
  263. $this->assertEquals('today', $fish->date);
  264. $this->assertEquals(0, $mongo->update('items', array('$set' => array('price' => 200, 'date' => 'today')), array('name' => 'ball')));
  265. }
  266. /**
  267. * @runInSeparateProcess
  268. * @group Mongo
  269. */
  270. public function testUpsert()
  271. {
  272. Config::set('DEBUG', false);
  273. $mongo = new MongoDriver($this->conf);
  274. $mongo->insert('items', array('name' => 'bread'));
  275. $this->assertInstanceOf('MongoId', $mongo->update('items', array('$set' => array('price' => 200, 'date' => 'today')), array('name' => 'ball'), true, true));
  276. $this->assertEquals('today', $mongo->find('items', array('name' => 'ball'))->fetch()->date);
  277. $this->assertEquals(2, $mongo->update('items', array('$set' => array('price' => 200, 'date' => 'today')), array('name' => 'eggs'), true, true));
  278. }
  279. /**
  280. * @runInSeparateProcess
  281. * @group Mongo
  282. */
  283. public function testFindAndModify()
  284. {
  285. Config::set('DEBUG', false);
  286. $mongo = new MongoDriver($this->conf);
  287. $this->assertEquals(10, $mongo->find('items', array('name' => 'bread'))->fetch()->quantity);
  288. $result = $mongo->findAndModify('items', array('name' => 'bread'), array('$set' => array('quantity' => 20)));
  289. $this->assertEquals(10, $result->fetch()->quantity);
  290. $this->assertEquals(20, $mongo->find('items', array('name' => 'bread'))->fetch()->quantity);
  291. }
  292. /**
  293. * @runInSeparateProcess
  294. * @group Mongo
  295. */
  296. public function testFindAndModifyNoItem()
  297. {
  298. Config::set('DEBUG', false);
  299. $mongo = new MongoDriver($this->conf);
  300. $this->assertEquals(10, $mongo->find('items', array('name' => 'bread'))->fetch()->quantity);
  301. $result = $mongo->findAndModify('items', array('name' => 'breading'), array('$set' => array('quantity' => 20)))->fetch();
  302. $this->assertFalse($result);
  303. $this->assertEquals(10, $mongo->find('items', array('name' => 'bread'))->fetch()->quantity);
  304. }
  305. /**
  306. * @runInSeparateProcess
  307. * @group Mongo
  308. */
  309. public function testEvalCommand()
  310. {
  311. Config::set('DEBUG', false);
  312. $mongo = new MongoDriver($this->conf);
  313. $result = $mongo->command('items', array('$eval' => 'function() { return db.items.count();}'));
  314. $this->assertEquals(5, $result->fetch());
  315. $this->assertEquals(5, $mongo->count('items'));
  316. $result = $mongo->command('items', array('$eval' => 'function() { return "HELLO!";}'));
  317. $this->assertEquals("HELLO!", $result->fetch());
  318. }
  319. /**
  320. * @runInSeparateProcess
  321. * @group Mongo
  322. */
  323. public function testEval()
  324. {
  325. Config::set('DEBUG', false);
  326. $mongo = new MongoDriver($this->conf);
  327. $result = $mongo->command('items', array('$eval' => 'function() {return true; }'));
  328. $this->assertTrue($result->fetch());
  329. $result = $mongo->command('items', array('$eval' => 'function() {return "Hello"; }'));
  330. $this->assertSame('Hello', $result->fetch());
  331. $result = $mongo->command('items', array('$eval' => 'function() {return db.items.count(); }'));
  332. $this->assertEquals(5, $result->fetch());
  333. }
  334. /**
  335. * @runInSeparateProcess
  336. * @group Mongo
  337. */
  338. public function testCommand()
  339. {
  340. Config::set('DEBUG', false);
  341. $mongo = new MongoDriver($this->conf);
  342. $result = $mongo->command('items', array('distinct' => 'items', 'key' => 'name'));
  343. $this->assertEquals(4, count($result->fetch(DB::FETCH_ASSOC)));
  344. }
  345. }