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.

224 lines
7.5 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 testGet()
  128. {
  129. if (!defined('DEBUG')) {
  130. define('DEBUG', false);
  131. }
  132. $mongo = new MongoDriver($this->conf);
  133. $eggs = $mongo->get('items', array('name' => 'eggs'))->fetchObject();
  134. $this->assertEquals(20, $eggs->quantity);
  135. $eggs = $mongo->get('items', array('name' => 'eggs'))->fetch();
  136. $this->assertEquals('eggs', $eggs->name);
  137. $this->assertInstanceOf('MongoId', $eggs->_id);
  138. }
  139. /**
  140. * @runInSeparateProcess
  141. */
  142. public function testRemove()
  143. {
  144. if (!defined('DEBUG')) {
  145. define('DEBUG', false);
  146. }
  147. $mongo = new MongoDriver($this->conf);
  148. $this->assertEquals(1, $mongo->find('items', array('name' => 'bread'))->numRows());
  149. $this->assertEquals(0, $mongo->delete('items', array('name' => 'esggs')));
  150. $this->assertEquals(2, $mongo->delete('items', array('name' => 'eggs')));
  151. $this->assertEquals(1, $mongo->delete('items', array('name' => 'bread')));
  152. $this->assertEquals(0, $mongo->find('items', array('name' => 'bread'))->numRows());
  153. }
  154. /**
  155. * @runInSeparateProcess
  156. */
  157. public function testInsert()
  158. {
  159. if (!defined('DEBUG')) {
  160. define('DEBUG', false);
  161. }
  162. $mongo = new MongoDriver($this->conf);
  163. $this->assertEquals(1, $mongo->find('items', array('name' => 'bread'))->numRows());
  164. $this->assertEquals(0, $mongo->insert('items', array('name' => 'bread')));
  165. //$this->assertNotEmpty($mongo->getInsertId());
  166. $this->assertEquals(2, $mongo->find('items', array('name' => 'bread'))->numRows());
  167. $this->assertEquals(0, $mongo->insert('items', array('name' => 'meat', 'weight' => 230)));
  168. $this->assertEquals(230, $mongo->get('items', array('name' => 'meat'))->fetch()->weight);
  169. }
  170. /**
  171. * @runInSeparateProcess
  172. */
  173. public function testUpdate()
  174. {
  175. if (!defined('DEBUG')) {
  176. define('DEBUG', false);
  177. }
  178. $mongo = new MongoDriver($this->conf);
  179. $this->assertEquals(1, $mongo->find('items', array('name' => 'bread'))->numRows());
  180. $this->assertEquals(1, $mongo->update('items', array('$set' => array('price' => 200, 'date' => 'today')), array('name' => 'fish')));
  181. $this->assertEquals(2, $mongo->update('items', array('$set' => array('price' => 1)), array('name' => 'eggs')));
  182. $fish = $mongo->get('items', array('name' => 'fish'))->fetch();
  183. $this->assertEquals(200, $fish->price);
  184. $this->assertEquals('today', $fish->date);
  185. $this->assertEquals(0, $mongo->update('items', array('$set' => array('price' => 200, 'date' => 'today')), array('name' => 'ball')));
  186. }
  187. /**
  188. * @runInSeparateProcess
  189. */
  190. public function testUpsert()
  191. {
  192. if (!defined('DEBUG')) {
  193. define('DEBUG', false);
  194. }
  195. $mongo = new MongoDriver($this->conf);
  196. $mongo->insert('items', array('name' => 'bread'));
  197. $this->assertEquals(1, $mongo->update('items', array('$set' => array('price' => 200, 'date' => 'today')), array('name' => 'ball'), true, true));
  198. $this->assertEquals('today', $mongo->get('items', array('name' => 'ball'))->fetch()->date);
  199. }
  200. }