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.

261 lines
8.9 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 testGet()
  161. {
  162. if (!defined('DEBUG')) {
  163. define('DEBUG', false);
  164. }
  165. $mongo = new MongoDriver($this->conf);
  166. $eggs = $mongo->get('items', array('name' => 'eggs'))->fetchObject();
  167. $this->assertEquals(20, $eggs->quantity);
  168. $eggs = $mongo->get('items', array('name' => 'eggs'))->fetch();
  169. $this->assertEquals('eggs', $eggs->name);
  170. $this->assertInstanceOf('MongoId', $eggs->_id);
  171. }
  172. /**
  173. * @runInSeparateProcess
  174. */
  175. public function testRemove()
  176. {
  177. if (!defined('DEBUG')) {
  178. define('DEBUG', false);
  179. }
  180. $mongo = new MongoDriver($this->conf);
  181. $this->assertEquals(1, $mongo->find('items', array('name' => 'bread'))->numRows());
  182. $this->assertEquals(0, $mongo->delete('items', array('name' => 'esggs')));
  183. $this->assertEquals(2, $mongo->delete('items', array('name' => 'eggs')));
  184. $this->assertEquals(1, $mongo->delete('items', array('name' => 'bread')));
  185. $this->assertEquals(0, $mongo->find('items', array('name' => 'bread'))->numRows());
  186. }
  187. /**
  188. * @runInSeparateProcess
  189. */
  190. public function testInsert()
  191. {
  192. if (!defined('DEBUG')) {
  193. define('DEBUG', false);
  194. }
  195. $mongo = new MongoDriver($this->conf);
  196. $this->assertEquals(1, $mongo->find('items', array('name' => 'bread'))->numRows());
  197. $this->assertEquals(0, $mongo->insert('items', array('name' => 'bread')));
  198. //$this->assertNotEmpty($mongo->getInsertId());
  199. $this->assertEquals(2, $mongo->find('items', array('name' => 'bread'))->numRows());
  200. $this->assertEquals(0, $mongo->insert('items', array('name' => 'meat', 'weight' => 230)));
  201. $this->assertEquals(230, $mongo->get('items', array('name' => 'meat'))->fetch()->weight);
  202. }
  203. /**
  204. * @runInSeparateProcess
  205. */
  206. public function testUpdate()
  207. {
  208. if (!defined('DEBUG')) {
  209. define('DEBUG', false);
  210. }
  211. $mongo = new MongoDriver($this->conf);
  212. $this->assertEquals(1, $mongo->find('items', array('name' => 'bread'))->numRows());
  213. $this->assertEquals(1, $mongo->update('items', array('$set' => array('price' => 200, 'date' => 'today')), array('name' => 'fish')));
  214. $this->assertEquals(2, $mongo->update('items', array('$set' => array('price' => 1)), array('name' => 'eggs')));
  215. $fish = $mongo->get('items', array('name' => 'fish'))->fetch();
  216. $this->assertEquals(200, $fish->price);
  217. $this->assertEquals('today', $fish->date);
  218. $this->assertEquals(0, $mongo->update('items', array('$set' => array('price' => 200, 'date' => 'today')), array('name' => 'ball')));
  219. }
  220. /**
  221. * @runInSeparateProcess
  222. */
  223. public function testUpsert()
  224. {
  225. if (!defined('DEBUG')) {
  226. define('DEBUG', false);
  227. }
  228. $mongo = new MongoDriver($this->conf);
  229. $mongo->insert('items', array('name' => 'bread'));
  230. $this->assertEquals(1, $mongo->update('items', array('$set' => array('price' => 200, 'date' => 'today')), array('name' => 'ball'), true, true));
  231. $this->assertEquals('today', $mongo->get('items', array('name' => 'ball'))->fetch()->date);
  232. }
  233. }