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.

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