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.

365 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-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. if (!defined('DEBUG')) {
  107. define('DEBUG', false);
  108. }
  109. $mongo = new MongoDriver($this->conf);
  110. $this->assertEquals(1, $mongo->find('items', array('name' => 'bread'))->numRows());
  111. $this->assertEquals(2, $mongo->find('items', array('name' => 'eggs'))->numRows());
  112. $eggs = $mongo->find('items', array('name' => 'eggs'));
  113. $egg = $eggs->fetch();
  114. $this->assertEquals(20, $egg->quantity);
  115. $egg = $eggs->fetchObject();
  116. $this->assertEquals('eggs', $egg->name);
  117. $this->assertFalse($eggs->fetchObject());
  118. $this->assertEquals(3, $mongo->find('items', array('price' => array('$lt' => 3.5)))->numRows());
  119. $data = $mongo->find('items', array('price' => array('$lt' => 3.5)));
  120. $count = 0;
  121. while ($row = $data->fetch(Db::FETCH_ASSOC)) {
  122. $count++;
  123. $this->assertLessThan(3.5, $row['price']);
  124. }
  125. $this->assertEquals(3, $count);
  126. $this->assertEquals(5, $mongo->find('items', array())->numRows());
  127. }
  128. /**
  129. * @runInSeparateProcess
  130. * @group Mongo
  131. */
  132. public function testOrderSkipLimit()
  133. {
  134. if (!defined('DEBUG')) {
  135. define('DEBUG', false);
  136. }
  137. $mongo = new MongoDriver($this->conf);
  138. $count = $mongo->find('items', array())->numRows();
  139. $this->assertEquals(1, $mongo->find('items', array('name' => 'bread'))->numRows());
  140. $this->assertEquals(2, $mongo->find('items', array('name' => 'eggs'))->numRows());
  141. $mongo->insert('items', array('name' => 'fdsbssc'));
  142. $mongo->insert('items', array('name' => 'boc'));
  143. $mongo->insert('items', array('name' => 'abc'));
  144. $mongo->insert('items', array('name' => 'vcxxc'));
  145. $mongo->insert('items', array('name' => 'abbc'));
  146. $mongo->insert('items', array('name' => 'dsbssc'));
  147. $mongo->insert('items', array('name' => 'bssc'));
  148. $data = $mongo->find('items', array());
  149. $this->assertEquals($count + 7, $data->numRows());
  150. $data->order(array('name' => 1));
  151. $this->assertEquals('abbc', $data->fetch()->name);
  152. $this->assertEquals('abc', $data->fetch()->name);
  153. $this->assertEquals('boc', $data->fetch()->name);
  154. $data = $mongo->find('items', array());
  155. $data->order(array('name' => -1));
  156. $data->skip(3);
  157. $data->limit(1);
  158. while ($row = $data->fetch()) {
  159. $this->assertEquals('fdsbssc', $row->name);
  160. }
  161. }
  162. /**
  163. * @runInSeparateProcess
  164. * @group Mongo
  165. */
  166. public function testCount()
  167. {
  168. if (!defined('DEBUG')) {
  169. define('DEBUG', false);
  170. }
  171. $mongo = new MongoDriver($this->conf);
  172. $this->assertEquals(5, $mongo->count('items'));
  173. $this->assertEquals(2, $mongo->count('items', array('name' => 'eggs')));
  174. $this->assertEquals(1, $mongo->count('items', array('name' => 'eggs'), 1));
  175. }
  176. /**
  177. * @runInSeparateProcess
  178. * @group Mongo
  179. */
  180. public function testGet()
  181. {
  182. if (!defined('DEBUG')) {
  183. define('DEBUG', false);
  184. }
  185. $mongo = new MongoDriver($this->conf);
  186. $eggs = $mongo->get('items', array('name' => 'eggs'))->fetchObject();
  187. $this->assertEquals(20, $eggs->quantity);
  188. $eggs = $mongo->get('items', array('name' => 'eggs'))->fetch();
  189. $this->assertEquals('eggs', $eggs->name);
  190. $this->assertInstanceOf('MongoId', $eggs->_id);
  191. }
  192. /**
  193. * @runInSeparateProcess
  194. * @group Mongo
  195. */
  196. public function testRemove()
  197. {
  198. if (!defined('DEBUG')) {
  199. define('DEBUG', false);
  200. }
  201. $mongo = new MongoDriver($this->conf);
  202. $this->assertEquals(1, $mongo->find('items', array('name' => 'bread'))->numRows());
  203. $this->assertEquals(0, $mongo->delete('items', array('name' => 'esggs')));
  204. $this->assertEquals(2, $mongo->delete('items', array('name' => 'eggs')));
  205. $this->assertEquals(1, $mongo->delete('items', array('name' => 'bread')));
  206. $this->assertEquals(0, $mongo->find('items', array('name' => 'bread'))->numRows());
  207. }
  208. /**
  209. * @runInSeparateProcess
  210. * @group Mongo
  211. */
  212. public function testInsert()
  213. {
  214. if (!defined('DEBUG')) {
  215. define('DEBUG', false);
  216. }
  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. if (!defined('DEBUG')) {
  232. define('DEBUG', false);
  233. }
  234. $data = array(
  235. array('name' => 'first object'),
  236. array('name' => 'second object'),
  237. array('name' => 'equal object'),
  238. array('name' => 'equal object')
  239. );
  240. $mongo = new MongoDriver($this->conf);
  241. $mongo->insert('items', $data, true);
  242. $this->assertEquals(1, $mongo->find('items', array('name' => 'first object'))->numRows());
  243. $this->assertEquals(1, $mongo->find('items', array('name' => 'second object'))->numRows());
  244. $this->assertEquals(2, $mongo->find('items', array('name' => 'equal object'))->numRows());
  245. }
  246. /**
  247. * @runInSeparateProcess
  248. * @group Mongo
  249. */
  250. public function testGetInsertId()
  251. {
  252. if (!defined('DEBUG')) {
  253. define('DEBUG', false);
  254. }
  255. $mongo = new MongoDriver($this->conf);
  256. $this->assertEquals(0, $mongo->getInsertId());
  257. $this->assertEquals(1, $mongo->find('items', array('name' => 'bread'))->numRows());
  258. $this->assertEquals(0, $mongo->insert('items', array('name' => 'bread')));
  259. $this->assertEquals(2, $mongo->find('items', array('name' => 'bread'))->numRows());
  260. $id1 = $mongo->getInsertId();
  261. $this->assertNotEmpty($id1);
  262. $this->assertEquals(0, $mongo->insert('items', array('name' => 'bread')));
  263. $id2 = $mongo->getInsertId();
  264. $this->assertNotEmpty($id2);
  265. $this->assertNotEquals($id1, $id2);
  266. $this->assertEquals(3, $mongo->find('items', array('name' => 'bread'))->numRows());
  267. }
  268. /**
  269. * @runInSeparateProcess
  270. * @group Mongo
  271. */
  272. public function testUpdate()
  273. {
  274. if (!defined('DEBUG')) {
  275. define('DEBUG', false);
  276. }
  277. $mongo = new MongoDriver($this->conf);
  278. $this->assertEquals(1, $mongo->find('items', array('name' => 'bread'))->numRows());
  279. $this->assertEquals(1, $mongo->update('items', array('$set' => array('price' => 200, 'date' => 'today')), array('name' => 'fish')));
  280. $this->assertEquals(2, $mongo->update('items', array('$set' => array('price' => 1)), array('name' => 'eggs')));
  281. $fish = $mongo->get('items', array('name' => 'fish'))->fetch();
  282. $this->assertEquals(200, $fish->price);
  283. $this->assertEquals('today', $fish->date);
  284. $this->assertEquals(0, $mongo->update('items', array('$set' => array('price' => 200, 'date' => 'today')), array('name' => 'ball')));
  285. }
  286. /**
  287. * @runInSeparateProcess
  288. * @group Mongo
  289. */
  290. public function testUpsert()
  291. {
  292. if (!defined('DEBUG')) {
  293. define('DEBUG', false);
  294. }
  295. $mongo = new MongoDriver($this->conf);
  296. $mongo->insert('items', array('name' => 'bread'));
  297. $this->assertEquals(1, $mongo->update('items', array('$set' => array('price' => 200, 'date' => 'today')), array('name' => 'ball'), true, true));
  298. $this->assertEquals('today', $mongo->get('items', array('name' => 'ball'))->fetch()->date);
  299. }
  300. /**
  301. * @runInSeparateProcess
  302. * @group Mongo
  303. */
  304. public function testFindAndModify()
  305. {
  306. if (!defined('DEBUG')) {
  307. define('DEBUG', false);
  308. }
  309. $mongo = new MongoDriver($this->conf);
  310. $this->assertEquals(10, $mongo->get('items', array('name' => 'bread'))->fetch()->quantity);
  311. $result = $mongo->findAndModify('items', array('name' => 'bread'), array('$set' => array('quantity' => 20)));
  312. $this->assertEquals(10, $result->fetch()->quantity);
  313. $this->assertEquals(20, $mongo->get('items', array('name' => 'bread'))->fetch()->quantity);
  314. }
  315. /**
  316. * @runInSeparateProcess
  317. * @group Mongo
  318. */
  319. public function testCommand()
  320. {
  321. if (!defined('DEBUG')) {
  322. define('DEBUG', false);
  323. }
  324. $mongo = new MongoDriver($this->conf);
  325. $result = $mongo->command('items', array('distinct' => 'items', 'key' => 'name'));
  326. $this->assertEquals(4, count($result->fetch(DB::FETCH_ASSOC)));
  327. }
  328. }