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.

165 lines
5.7 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/DbDriver.php';
  12. require_once dirname(__FILE__) . '/../../model/NoSqlDbDriver.php';
  13. require_once dirname(__FILE__) . '/../../model/MongoDriver.php';
  14. class MongoDriverTest extends PHPUnit_Framework_TestCase
  15. {
  16. private $conf = array();
  17. public function setUp()
  18. {
  19. $this->conf = array(
  20. 'hostname' => 'localhost',
  21. 'database' => 'test',
  22. 'username' => 'test',
  23. 'password' => '1234',
  24. 'port' => 27017
  25. );
  26. $data = array(
  27. array(
  28. 'name' => 'bread',
  29. 'price' => 3.2,
  30. 'quantity' => 10
  31. ),
  32. array(
  33. 'name' => 'eggs',
  34. 'price' => 2.1,
  35. 'quantity' => 20
  36. ),
  37. array(
  38. 'name' => 'fish',
  39. 'price' => 13.2,
  40. 'quantity' => 2
  41. ),
  42. array(
  43. 'name' => 'milk',
  44. 'price' => 3.8,
  45. 'quantity' => 1
  46. ),
  47. array(
  48. 'name' => 'eggs',
  49. 'price' => 2.3,
  50. 'quantity' => 5
  51. )
  52. );
  53. $connection = new Mongo('mongodb://' . $this->conf['hostname'] . ':' . $this->conf['port']);
  54. $db = $connection->selectDB($this->conf['database']);
  55. $db->authenticate($this->conf['username'], $this->conf['password']);
  56. $collection = 'items';
  57. $db->dropCollection($collection);
  58. $collection = $db->selectCollection($collection);
  59. foreach($data as $document) {
  60. $collection->insert($document);
  61. }
  62. }
  63. /**
  64. * @expectedException Exception
  65. * @expectedExceptionMessage Configuration must have a "hostname".
  66. */
  67. public function testGetConnectionNoHostname()
  68. {
  69. unset($this->conf['hostname']);
  70. $mongo = new MongoDriver($this->conf);
  71. $mongo->getConnection();
  72. }
  73. /**
  74. * @expectedException MongoConnectionException
  75. * @expectedExceptionMessage Couldn't authenticate with database
  76. */
  77. public function testGetConnectionWrongPassword()
  78. {
  79. $this->conf['password'] = 'nopass';
  80. $mongo = new MongoDriver($this->conf);
  81. $this->assertInstanceOf('MongoDB', $mongo->getConnection());
  82. }
  83. public function testGetConnection()
  84. {
  85. $mongo = new MongoDriver($this->conf);
  86. $this->assertFalse($mongo->isConnected());
  87. $this->assertInstanceOf('Mongo', $mongo->getConnection());
  88. $this->assertTrue($mongo->isConnected());
  89. $mongo->getConnection();
  90. $mongo->disconnect();
  91. $this->assertFalse($mongo->isConnected());
  92. }
  93. public function testFind()
  94. {
  95. $mongo = new MongoDriver($this->conf);
  96. $mongo->getConnection();
  97. $this->assertEquals(1, $mongo->find('items', array('name' => 'bread'))->count());
  98. $this->assertEquals(2, $mongo->find('items', array('name' => 'eggs'))->count());
  99. $this->assertEquals(3, $mongo->find('items', array('price' => array('$lt' => 3.5)))->count());
  100. $this->assertEquals(5, $mongo->find('items', array())->count());
  101. }
  102. public function testGet()
  103. {
  104. $mongo = new MongoDriver($this->conf);
  105. $mongo->getConnection();
  106. $bread = $mongo->get('items', array('name' => 'bread'));
  107. $this->assertEquals(3.2, $bread['price']);
  108. }
  109. public function testRemove()
  110. {
  111. $mongo = new MongoDriver($this->conf);
  112. $mongo->getConnection();
  113. $this->assertEquals(1, $mongo->find('items', array('name' => 'bread'))->count());
  114. $this->assertEquals(0, $mongo->delete('items', array('name' => 'esggs')));
  115. $this->assertEquals(2, $mongo->delete('items', array('name' => 'eggs')));
  116. $this->assertEquals(1, $mongo->delete('items', array('name' => 'bread')));
  117. $this->assertEquals(0, $mongo->find('items', array('name' => 'bread'))->count());
  118. }
  119. public function testInsert()
  120. {
  121. $mongo = new MongoDriver($this->conf);
  122. $mongo->getConnection();
  123. $this->assertEquals(1, $mongo->find('items', array('name' => 'bread'))->count());
  124. $this->assertTrue($mongo->insert('items', array('name' => 'bread')));
  125. $this->assertNotEmpty($mongo->getInsertId());
  126. $this->assertEquals(2, $mongo->find('items', array('name' => 'bread'))->count());
  127. }
  128. public function testUpdate()
  129. {
  130. $mongo = new MongoDriver($this->conf);
  131. $mongo->getConnection();
  132. $this->assertEquals(1, $mongo->find('items', array('name' => 'bread'))->count());
  133. $mongo->update('items', array('$set' => array('price' => 200, 'date' => 'today')), array('name' => 'fish'));
  134. $fish = $mongo->get('items', array('name' => 'fish'));
  135. $this->assertEquals(200, $fish['price']);
  136. $this->assertEquals('today', $fish['date']);
  137. $this->assertEquals(0, $mongo->update('items', array('$set' => array('price' => 200, 'date' => 'today')), array('name' => 'ball')));
  138. }
  139. public function testUpsert()
  140. {
  141. $mongo = new MongoDriver($this->conf);
  142. $mongo->getConnection();
  143. $this->assertTrue($mongo->insert('items', array('name' => 'bread')));
  144. $id = $mongo->getInsertId();
  145. $this->assertNotEmpty($id);
  146. $this->assertEquals(1, $mongo->update('items', array('$set' => array('price' => 200, 'date' => 'today')), array('name' => 'ball'), true));
  147. $this->assertNotEquals($id, $mongo->getInsertId());
  148. }
  149. }