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.

319 lines
11 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. public function testGetConnectionNoHostname()
  69. {
  70. unset($this->conf['hostname']);
  71. $this->setExpectedException('GeneralException', 'Configuration must have a "hostname"');
  72. $mongo = new MongoDriver($this->conf);
  73. }
  74. public function testGetConnectionWrongPassword()
  75. {
  76. $this->conf['password'] = 'nopass';
  77. $mongo = new MongoDriver($this->conf);
  78. $this->setExpectedException('MongoConnectionException', 'Couldn\'t authenticate with database');
  79. $this->assertInstanceOf('MongoDB', $mongo->getConnection());
  80. }
  81. public function testGetConnection()
  82. {
  83. $mongo = new MongoDriver($this->conf);
  84. $this->assertFalse($mongo->isConnected());
  85. $this->assertInstanceOf('Mongo', $mongo->getConnection());
  86. $this->assertTrue($mongo->isConnected());
  87. $mongo->getConnection();
  88. $mongo->disconnect();
  89. $this->assertFalse($mongo->isConnected());
  90. }
  91. /**
  92. * @runInSeparateProcess
  93. */
  94. public function testFind()
  95. {
  96. if (!defined('DEBUG')) {
  97. define('DEBUG', false);
  98. }
  99. $mongo = new MongoDriver($this->conf);
  100. $this->assertEquals(1, $mongo->find('items', array('name' => 'bread'))->numRows());
  101. $this->assertEquals(2, $mongo->find('items', array('name' => 'eggs'))->numRows());
  102. $eggs = $mongo->find('items', array('name' => 'eggs'));
  103. $egg = $eggs->fetch();
  104. $this->assertEquals(20, $egg->quantity);
  105. $egg = $eggs->fetchObject();
  106. $this->assertEquals('eggs', $egg->name);
  107. $this->assertFalse($eggs->fetchObject());
  108. $this->assertEquals(3, $mongo->find('items', array('price' => array('$lt' => 3.5)))->numRows());
  109. $data = $mongo->find('items', array('price' => array('$lt' => 3.5)));
  110. $count = 0;
  111. while($row = $data->fetch(Db::FETCH_ASSOC)) {
  112. $count++;
  113. $this->assertLessThan(3.5, $row['price']);
  114. }
  115. $this->assertEquals(3, $count);
  116. $this->assertEquals(5, $mongo->find('items', array())->numRows());
  117. }
  118. /**
  119. * @runInSeparateProcess
  120. */
  121. public function testOrderSkipLimit()
  122. {
  123. if (!defined('DEBUG')) {
  124. define('DEBUG', false);
  125. }
  126. $mongo = new MongoDriver($this->conf);
  127. $count = $mongo->find('items', array())->numRows();
  128. $this->assertEquals(1, $mongo->find('items', array('name' => 'bread'))->numRows());
  129. $this->assertEquals(2, $mongo->find('items', array('name' => 'eggs'))->numRows());
  130. $mongo->insert('items', array('name' => 'fdsbssc'));
  131. $mongo->insert('items', array('name' => 'boc'));
  132. $mongo->insert('items', array('name' => 'abc'));
  133. $mongo->insert('items', array('name' => 'vcxxc'));
  134. $mongo->insert('items', array('name' => 'abbc'));
  135. $mongo->insert('items', array('name' => 'dsbssc'));
  136. $mongo->insert('items', array('name' => 'bssc'));
  137. $data = $mongo->find('items', array());
  138. $this->assertEquals($count + 7, $data->numRows());
  139. $data->order(array('name' => 1));
  140. $this->assertEquals('abbc', $data->fetch()->name);
  141. $this->assertEquals('abc', $data->fetch()->name);
  142. $this->assertEquals('boc', $data->fetch()->name);
  143. $data = $mongo->find('items', array());
  144. $data->order(array('name' => -1));
  145. $data->skip(3);
  146. $data->limit(1);
  147. while($row = $data->fetch()) {
  148. $this->assertEquals('fdsbssc', $row->name);
  149. }
  150. }
  151. /**
  152. * @runInSeparateProcess
  153. */
  154. public function testCount()
  155. {
  156. if (!defined('DEBUG')) {
  157. define('DEBUG', false);
  158. }
  159. $mongo = new MongoDriver($this->conf);
  160. $this->assertEquals(5, $mongo->count('items'));
  161. $this->assertEquals(2, $mongo->count('items', array('name' => 'eggs')));
  162. $this->assertEquals(1, $mongo->count('items', array('name' => 'eggs'), 1));
  163. }
  164. /**
  165. * @runInSeparateProcess
  166. */
  167. public function testGet()
  168. {
  169. if (!defined('DEBUG')) {
  170. define('DEBUG', false);
  171. }
  172. $mongo = new MongoDriver($this->conf);
  173. $eggs = $mongo->get('items', array('name' => 'eggs'))->fetchObject();
  174. $this->assertEquals(20, $eggs->quantity);
  175. $eggs = $mongo->get('items', array('name' => 'eggs'))->fetch();
  176. $this->assertEquals('eggs', $eggs->name);
  177. $this->assertInstanceOf('MongoId', $eggs->_id);
  178. }
  179. /**
  180. * @runInSeparateProcess
  181. */
  182. public function testRemove()
  183. {
  184. if (!defined('DEBUG')) {
  185. define('DEBUG', false);
  186. }
  187. $mongo = new MongoDriver($this->conf);
  188. $this->assertEquals(1, $mongo->find('items', array('name' => 'bread'))->numRows());
  189. $this->assertEquals(0, $mongo->delete('items', array('name' => 'esggs')));
  190. $this->assertEquals(2, $mongo->delete('items', array('name' => 'eggs')));
  191. $this->assertEquals(1, $mongo->delete('items', array('name' => 'bread')));
  192. $this->assertEquals(0, $mongo->find('items', array('name' => 'bread'))->numRows());
  193. }
  194. /**
  195. * @runInSeparateProcess
  196. */
  197. public function testInsert()
  198. {
  199. if (!defined('DEBUG')) {
  200. define('DEBUG', false);
  201. }
  202. $mongo = new MongoDriver($this->conf);
  203. $this->assertEquals(1, $mongo->find('items', array('name' => 'bread'))->numRows());
  204. $this->assertEquals(0, $mongo->insert('items', array('name' => 'bread')));
  205. $this->assertNotEmpty($mongo->getInsertId());
  206. $this->assertEquals(2, $mongo->find('items', array('name' => 'bread'))->numRows());
  207. $this->assertEquals(0, $mongo->insert('items', array('name' => 'meat', 'weight' => 230)));
  208. $this->assertEquals(230, $mongo->get('items', array('name' => 'meat'))->fetch()->weight);
  209. }
  210. public function testGetInsertId()
  211. {
  212. if (!defined('DEBUG')) {
  213. define('DEBUG', false);
  214. }
  215. $mongo = new MongoDriver($this->conf);
  216. $this->assertEquals(0, $mongo->getInsertId());
  217. $this->assertEquals(1, $mongo->find('items', array('name' => 'bread'))->numRows());
  218. $this->assertEquals(0, $mongo->insert('items', array('name' => 'bread')));
  219. $this->assertEquals(2, $mongo->find('items', array('name' => 'bread'))->numRows());
  220. $id1 = $mongo->getInsertId();
  221. $this->assertNotEmpty($id1);
  222. $this->assertEquals(0, $mongo->insert('items', array('name' => 'bread')));
  223. $id2 = $mongo->getInsertId();
  224. $this->assertNotEmpty($id2);
  225. $this->assertNotEquals($id1, $id2);
  226. $this->assertEquals(3, $mongo->find('items', array('name' => 'bread'))->numRows());
  227. }
  228. /**
  229. * @runInSeparateProcess
  230. */
  231. public function testUpdate()
  232. {
  233. if (!defined('DEBUG')) {
  234. define('DEBUG', false);
  235. }
  236. $mongo = new MongoDriver($this->conf);
  237. $this->assertEquals(1, $mongo->find('items', array('name' => 'bread'))->numRows());
  238. $this->assertEquals(1, $mongo->update('items', array('$set' => array('price' => 200, 'date' => 'today')), array('name' => 'fish')));
  239. $this->assertEquals(2, $mongo->update('items', array('$set' => array('price' => 1)), array('name' => 'eggs')));
  240. $fish = $mongo->get('items', array('name' => 'fish'))->fetch();
  241. $this->assertEquals(200, $fish->price);
  242. $this->assertEquals('today', $fish->date);
  243. $this->assertEquals(0, $mongo->update('items', array('$set' => array('price' => 200, 'date' => 'today')), array('name' => 'ball')));
  244. }
  245. /**
  246. * @runInSeparateProcess
  247. */
  248. public function testUpsert()
  249. {
  250. if (!defined('DEBUG')) {
  251. define('DEBUG', false);
  252. }
  253. $mongo = new MongoDriver($this->conf);
  254. $mongo->insert('items', array('name' => 'bread'));
  255. $this->assertEquals(1, $mongo->update('items', array('$set' => array('price' => 200, 'date' => 'today')), array('name' => 'ball'), true, true));
  256. $this->assertEquals('today', $mongo->get('items', array('name' => 'ball'))->fetch()->date);
  257. }
  258. /**
  259. * @runInSeparateProcess
  260. */
  261. public function testFindAndModify()
  262. {
  263. if (!defined('DEBUG')) {
  264. define('DEBUG', false);
  265. }
  266. $mongo = new MongoDriver($this->conf);
  267. $this->assertEquals(10, $mongo->get('items', array('name' => 'bread'))->fetch()->quantity);
  268. $result = $mongo->findAndModify('items', array('name' => 'bread'), array('$set' => array('quantity' => 20)));
  269. $this->assertEquals(10, $result->fetch()->quantity);
  270. $this->assertEquals(20, $mongo->get('items', array('name' => 'bread'))->fetch()->quantity);
  271. }
  272. /**
  273. * @runInSeparateProcess
  274. */
  275. public function testCommand()
  276. {
  277. if (!defined('DEBUG')) {
  278. define('DEBUG', false);
  279. }
  280. $mongo = new MongoDriver($this->conf);
  281. $result = $mongo->command('items', array('distinct' =>'items', 'key' => 'name'));
  282. $this->assertEquals(4, count($result->fetch(DB::FETCH_ASSOC)));
  283. }
  284. }