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.

328 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. class MongoDriverTest extends PHPUnit_Framework_TestCase
  19. {
  20. private $conf = array();
  21. public function setUp()
  22. {
  23. $this->conf = array(
  24. 'hostname' => 'localhost',
  25. 'database' => 'test',
  26. 'username' => 'test',
  27. 'password' => '1234',
  28. 'port' => 27017
  29. );
  30. $data = array(
  31. array(
  32. 'name' => 'bread',
  33. 'price' => 3.2,
  34. 'quantity' => 10
  35. ),
  36. array(
  37. 'name' => 'eggs',
  38. 'price' => 2.1,
  39. 'quantity' => 20
  40. ),
  41. array(
  42. 'name' => 'fish',
  43. 'price' => 13.2,
  44. 'quantity' => 2
  45. ),
  46. array(
  47. 'name' => 'milk',
  48. 'price' => 3.8,
  49. 'quantity' => 1
  50. ),
  51. array(
  52. 'name' => 'eggs',
  53. 'price' => 2.3,
  54. 'quantity' => 5
  55. )
  56. );
  57. $connection = new Mongo('mongodb://' . $this->conf['hostname'] . ':' . $this->conf['port']);
  58. $db = $connection->selectDB($this->conf['database']);
  59. $db->authenticate($this->conf['username'], $this->conf['password']);
  60. $collection = 'items';
  61. $db->dropCollection($collection);
  62. $collection = $db->selectCollection($collection);
  63. foreach($data as $document) {
  64. $collection->insert($document);
  65. }
  66. }
  67. /**
  68. * @expectedException Exception
  69. * @expectedExceptionMessage Configuration must have a "hostname".
  70. */
  71. public function testGetConnectionNoHostname()
  72. {
  73. unset($this->conf['hostname']);
  74. $mongo = new MongoDriver($this->conf);
  75. $mongo->getConnection();
  76. }
  77. /**
  78. * @expectedException MongoConnectionException
  79. * @expectedExceptionMessage Couldn't authenticate with database
  80. */
  81. public function testGetConnectionWrongPassword()
  82. {
  83. $this->conf['password'] = 'nopass';
  84. $mongo = new MongoDriver($this->conf);
  85. $this->assertInstanceOf('MongoDB', $mongo->getConnection());
  86. }
  87. public function testGetConnection()
  88. {
  89. $mongo = new MongoDriver($this->conf);
  90. $this->assertFalse($mongo->isConnected());
  91. $this->assertInstanceOf('Mongo', $mongo->getConnection());
  92. $this->assertTrue($mongo->isConnected());
  93. $mongo->getConnection();
  94. $mongo->disconnect();
  95. $this->assertFalse($mongo->isConnected());
  96. }
  97. /**
  98. * @runInSeparateProcess
  99. */
  100. public function testFind()
  101. {
  102. if (!defined('DEBUG')) {
  103. define('DEBUG', false);
  104. }
  105. $mongo = new MongoDriver($this->conf);
  106. $this->assertEquals(1, $mongo->find('items', array('name' => 'bread'))->numRows());
  107. $this->assertEquals(2, $mongo->find('items', array('name' => 'eggs'))->numRows());
  108. $eggs = $mongo->find('items', array('name' => 'eggs'));
  109. $egg = $eggs->fetch();
  110. $this->assertEquals(20, $egg->quantity);
  111. $egg = $eggs->fetchObject();
  112. $this->assertEquals('eggs', $egg->name);
  113. $this->assertFalse($eggs->fetchObject());
  114. $this->assertEquals(3, $mongo->find('items', array('price' => array('$lt' => 3.5)))->numRows());
  115. $data = $mongo->find('items', array('price' => array('$lt' => 3.5)));
  116. $count = 0;
  117. while($row = $data->fetch(Db::FETCH_ASSOC)) {
  118. $count++;
  119. $this->assertLessThan(3.5, $row['price']);
  120. }
  121. $this->assertEquals(3, $count);
  122. $this->assertEquals(5, $mongo->find('items', array())->numRows());
  123. }
  124. /**
  125. * @runInSeparateProcess
  126. */
  127. public function testOrderSkipLimit()
  128. {
  129. if (!defined('DEBUG')) {
  130. define('DEBUG', false);
  131. }
  132. $mongo = new MongoDriver($this->conf);
  133. $count = $mongo->find('items', array())->numRows();
  134. $this->assertEquals(1, $mongo->find('items', array('name' => 'bread'))->numRows());
  135. $this->assertEquals(2, $mongo->find('items', array('name' => 'eggs'))->numRows());
  136. $mongo->insert('items', array('name' => 'fdsbssc'));
  137. $mongo->insert('items', array('name' => 'boc'));
  138. $mongo->insert('items', array('name' => 'abc'));
  139. $mongo->insert('items', array('name' => 'vcxxc'));
  140. $mongo->insert('items', array('name' => 'abbc'));
  141. $mongo->insert('items', array('name' => 'dsbssc'));
  142. $mongo->insert('items', array('name' => 'bssc'));
  143. $data = $mongo->find('items', array());
  144. $this->assertEquals($count + 7, $data->numRows());
  145. $data->order(array('name' => 1));
  146. $this->assertEquals('abbc', $data->fetch()->name);
  147. $this->assertEquals('abc', $data->fetch()->name);
  148. $this->assertEquals('boc', $data->fetch()->name);
  149. $data = $mongo->find('items', array());
  150. $data->order(array('name' => -1));
  151. $data->skip(3);
  152. $data->limit(1);
  153. while($row = $data->fetch()) {
  154. $this->assertEquals('fdsbssc', $row->name);
  155. }
  156. }
  157. /**
  158. * @runInSeparateProcess
  159. */
  160. public function testCount()
  161. {
  162. if (!defined('DEBUG')) {
  163. define('DEBUG', false);
  164. }
  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. */
  173. public function testGet()
  174. {
  175. if (!defined('DEBUG')) {
  176. define('DEBUG', false);
  177. }
  178. $mongo = new MongoDriver($this->conf);
  179. $eggs = $mongo->get('items', array('name' => 'eggs'))->fetchObject();
  180. $this->assertEquals(20, $eggs->quantity);
  181. $eggs = $mongo->get('items', array('name' => 'eggs'))->fetch();
  182. $this->assertEquals('eggs', $eggs->name);
  183. $this->assertInstanceOf('MongoId', $eggs->_id);
  184. }
  185. /**
  186. * @runInSeparateProcess
  187. */
  188. public function testRemove()
  189. {
  190. if (!defined('DEBUG')) {
  191. define('DEBUG', false);
  192. }
  193. $mongo = new MongoDriver($this->conf);
  194. $this->assertEquals(1, $mongo->find('items', array('name' => 'bread'))->numRows());
  195. $this->assertEquals(0, $mongo->delete('items', array('name' => 'esggs')));
  196. $this->assertEquals(2, $mongo->delete('items', array('name' => 'eggs')));
  197. $this->assertEquals(1, $mongo->delete('items', array('name' => 'bread')));
  198. $this->assertEquals(0, $mongo->find('items', array('name' => 'bread'))->numRows());
  199. }
  200. /**
  201. * @runInSeparateProcess
  202. */
  203. public function testInsert()
  204. {
  205. if (!defined('DEBUG')) {
  206. define('DEBUG', false);
  207. }
  208. $mongo = new MongoDriver($this->conf);
  209. $this->assertEquals(1, $mongo->find('items', array('name' => 'bread'))->numRows());
  210. $this->assertEquals(0, $mongo->insert('items', array('name' => 'bread')));
  211. $this->assertNotEmpty($mongo->getInsertId());
  212. $this->assertEquals(2, $mongo->find('items', array('name' => 'bread'))->numRows());
  213. $this->assertEquals(0, $mongo->insert('items', array('name' => 'meat', 'weight' => 230)));
  214. $this->assertEquals(230, $mongo->get('items', array('name' => 'meat'))->fetch()->weight);
  215. }
  216. public function testGetInsertId()
  217. {
  218. if (!defined('DEBUG')) {
  219. define('DEBUG', false);
  220. }
  221. $mongo = new MongoDriver($this->conf);
  222. $this->assertEquals(0, $mongo->getInsertId());
  223. $this->assertEquals(1, $mongo->find('items', array('name' => 'bread'))->numRows());
  224. $this->assertEquals(0, $mongo->insert('items', array('name' => 'bread')));
  225. $this->assertEquals(2, $mongo->find('items', array('name' => 'bread'))->numRows());
  226. $id1 = $mongo->getInsertId();
  227. $this->assertNotEmpty($id1);
  228. $this->assertEquals(0, $mongo->insert('items', array('name' => 'bread')));
  229. $id2 = $mongo->getInsertId();
  230. $this->assertNotEmpty($id2);
  231. $this->assertNotEquals($id1, $id2);
  232. $this->assertEquals(3, $mongo->find('items', array('name' => 'bread'))->numRows());
  233. }
  234. /**
  235. * @runInSeparateProcess
  236. */
  237. public function testUpdate()
  238. {
  239. if (!defined('DEBUG')) {
  240. define('DEBUG', false);
  241. }
  242. $mongo = new MongoDriver($this->conf);
  243. $this->assertEquals(1, $mongo->find('items', array('name' => 'bread'))->numRows());
  244. $this->assertEquals(1, $mongo->update('items', array('$set' => array('price' => 200, 'date' => 'today')), array('name' => 'fish')));
  245. $this->assertEquals(2, $mongo->update('items', array('$set' => array('price' => 1)), array('name' => 'eggs')));
  246. $fish = $mongo->get('items', array('name' => 'fish'))->fetch();
  247. $this->assertEquals(200, $fish->price);
  248. $this->assertEquals('today', $fish->date);
  249. $this->assertEquals(0, $mongo->update('items', array('$set' => array('price' => 200, 'date' => 'today')), array('name' => 'ball')));
  250. }
  251. /**
  252. * @runInSeparateProcess
  253. */
  254. public function testUpsert()
  255. {
  256. if (!defined('DEBUG')) {
  257. define('DEBUG', false);
  258. }
  259. $mongo = new MongoDriver($this->conf);
  260. $mongo->insert('items', array('name' => 'bread'));
  261. $this->assertEquals(1, $mongo->update('items', array('$set' => array('price' => 200, 'date' => 'today')), array('name' => 'ball'), true, true));
  262. $this->assertEquals('today', $mongo->get('items', array('name' => 'ball'))->fetch()->date);
  263. }
  264. /**
  265. * @runInSeparateProcess
  266. */
  267. public function testFindAndModify()
  268. {
  269. if (!defined('DEBUG')) {
  270. define('DEBUG', false);
  271. }
  272. $mongo = new MongoDriver($this->conf);
  273. $this->assertEquals(10, $mongo->get('items', array('name' => 'bread'))->fetch()->quantity);
  274. $result = $mongo->findAndModify('items', array('name' => 'bread'), array('$set' => array('quantity' => 20)));
  275. $this->assertEquals(10, $result->fetch()->quantity);
  276. $this->assertEquals(20, $mongo->get('items', array('name' => 'bread'))->fetch()->quantity);
  277. }
  278. /**
  279. * @runInSeparateProcess
  280. */
  281. public function testCommand()
  282. {
  283. if (!defined('DEBUG')) {
  284. define('DEBUG', false);
  285. }
  286. $mongo = new MongoDriver($this->conf);
  287. $result = $mongo->command('items', array('distinct' =>'items', 'key' => 'name'));
  288. $this->assertEquals(4, count($result->fetch(DB::FETCH_ASSOC)));
  289. }
  290. }