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.

342 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 testGetInsertId()
  230. {
  231. if (!defined('DEBUG')) {
  232. define('DEBUG', false);
  233. }
  234. $mongo = new MongoDriver($this->conf);
  235. $this->assertEquals(0, $mongo->getInsertId());
  236. $this->assertEquals(1, $mongo->find('items', array('name' => 'bread'))->numRows());
  237. $this->assertEquals(0, $mongo->insert('items', array('name' => 'bread')));
  238. $this->assertEquals(2, $mongo->find('items', array('name' => 'bread'))->numRows());
  239. $id1 = $mongo->getInsertId();
  240. $this->assertNotEmpty($id1);
  241. $this->assertEquals(0, $mongo->insert('items', array('name' => 'bread')));
  242. $id2 = $mongo->getInsertId();
  243. $this->assertNotEmpty($id2);
  244. $this->assertNotEquals($id1, $id2);
  245. $this->assertEquals(3, $mongo->find('items', array('name' => 'bread'))->numRows());
  246. }
  247. /**
  248. * @runInSeparateProcess
  249. * @group Mongo
  250. */
  251. public function testUpdate()
  252. {
  253. if (!defined('DEBUG')) {
  254. define('DEBUG', false);
  255. }
  256. $mongo = new MongoDriver($this->conf);
  257. $this->assertEquals(1, $mongo->find('items', array('name' => 'bread'))->numRows());
  258. $this->assertEquals(1, $mongo->update('items', array('$set' => array('price' => 200, 'date' => 'today')), array('name' => 'fish')));
  259. $this->assertEquals(2, $mongo->update('items', array('$set' => array('price' => 1)), array('name' => 'eggs')));
  260. $fish = $mongo->get('items', array('name' => 'fish'))->fetch();
  261. $this->assertEquals(200, $fish->price);
  262. $this->assertEquals('today', $fish->date);
  263. $this->assertEquals(0, $mongo->update('items', array('$set' => array('price' => 200, 'date' => 'today')), array('name' => 'ball')));
  264. }
  265. /**
  266. * @runInSeparateProcess
  267. * @group Mongo
  268. */
  269. public function testUpsert()
  270. {
  271. if (!defined('DEBUG')) {
  272. define('DEBUG', false);
  273. }
  274. $mongo = new MongoDriver($this->conf);
  275. $mongo->insert('items', array('name' => 'bread'));
  276. $this->assertEquals(1, $mongo->update('items', array('$set' => array('price' => 200, 'date' => 'today')), array('name' => 'ball'), true, true));
  277. $this->assertEquals('today', $mongo->get('items', array('name' => 'ball'))->fetch()->date);
  278. }
  279. /**
  280. * @runInSeparateProcess
  281. * @group Mongo
  282. */
  283. public function testFindAndModify()
  284. {
  285. if (!defined('DEBUG')) {
  286. define('DEBUG', false);
  287. }
  288. $mongo = new MongoDriver($this->conf);
  289. $this->assertEquals(10, $mongo->get('items', array('name' => 'bread'))->fetch()->quantity);
  290. $result = $mongo->findAndModify('items', array('name' => 'bread'), array('$set' => array('quantity' => 20)));
  291. $this->assertEquals(10, $result->fetch()->quantity);
  292. $this->assertEquals(20, $mongo->get('items', array('name' => 'bread'))->fetch()->quantity);
  293. }
  294. /**
  295. * @runInSeparateProcess
  296. * @group Mongo
  297. */
  298. public function testCommand()
  299. {
  300. if (!defined('DEBUG')) {
  301. define('DEBUG', false);
  302. }
  303. $mongo = new MongoDriver($this->conf);
  304. $result = $mongo->command('items', array('distinct' =>'items', 'key' => 'name'));
  305. $this->assertEquals(4, count($result->fetch(DB::FETCH_ASSOC)));
  306. }
  307. }