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.

225 lines
8.4 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. require_once dirname(__FILE__) . '/../../model/MongoDbCommand.php';
  15. require_once dirname(__FILE__) . '/../../exception/GeneralException.php';
  16. class MongoDbCommandTest extends PHPUnit_Framework_TestCase
  17. {
  18. private $conf = array();
  19. private $driver;
  20. private $collection;
  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. $this->driver = new MongoDriver($this->conf);
  31. $connection = $this->driver->getConnection();
  32. $db = $connection->selectDB($this->conf['database']);
  33. $db->authenticate($this->conf['username'], $this->conf['password']);
  34. $collection = 'items';
  35. $db->dropCollection($collection);
  36. $this->collection = $db->selectCollection($collection);
  37. }
  38. public function testCommandFactory()
  39. {
  40. $cmd = MongoCommandBuilder::factory(MongoCommandBuilder::FIND);
  41. $this->assertInstanceOf('MongoDbCommand', $cmd);
  42. $this->assertInstanceOf('FindMongoCommand', $cmd);
  43. $cmd = MongoCommandBuilder::factory(MongoCommandBuilder::INSERT);
  44. $this->assertInstanceOf('MongoDbCommand', $cmd);
  45. $this->assertInstanceOf('InsertMongoCommand', $cmd);
  46. $cmd = MongoCommandBuilder::factory(MongoCommandBuilder::UPDATE);
  47. $this->assertInstanceOf('MongoDbCommand', $cmd);
  48. $this->assertInstanceOf('UpdateMongoCommand', $cmd);
  49. $cmd = MongoCommandBuilder::factory(MongoCommandBuilder::REMOVE);
  50. $this->assertInstanceOf('MongoDbCommand', $cmd);
  51. $this->assertInstanceOf('RemoveMongoCommand', $cmd);
  52. }
  53. public function testFindCommand()
  54. {
  55. $cmd = MongoCommandBuilder::factory(MongoCommandBuilder::FIND, $this->collection);
  56. $cmd->bindParam('condition', array('name' => 'bread'))->bindParam('fields', array());
  57. $result = $cmd->execute();
  58. $this->assertEquals(0, $result->count());
  59. $cmd = MongoCommandBuilder::factory(MongoCommandBuilder::INSERT, $this->collection);
  60. $cmd
  61. ->bindParam('data', array('name' => 'insert'))
  62. ->bindParam('safe', true);
  63. $cmd->execute();
  64. $cmd = MongoCommandBuilder::factory(MongoCommandBuilder::INSERT, $this->collection);
  65. $cmd
  66. ->bindParam('data', array('name' => 'insert'))
  67. ->bindParam('safe', true);
  68. $cmd->execute();
  69. $cmd = MongoCommandBuilder::factory(MongoCommandBuilder::FIND, $this->collection);
  70. $cmd->bindParam('condition', array('name' => 'insert'))->bindParam('fields', array());
  71. $this->assertEquals(2, $cmd->execute()->count());
  72. $cmd
  73. ->bindParam('condition', array('name' => 'insert'))
  74. ->bindParam('fields', array())
  75. ->bindParam('multiple', false);
  76. $result = $cmd->execute();
  77. $this->assertEquals('insert', $result['name']);
  78. }
  79. /**
  80. * @expectedException GeneralException
  81. * @expectedExceptionMessage FindMongoCommand error. Bind all required params first.
  82. */
  83. public function testFindCommandNotAllParamsBinded()
  84. {
  85. $cmd = MongoCommandBuilder::factory(MongoCommandBuilder::FIND, $this->collection);
  86. $cmd->bindParam('condition', array('name' => 'bread'));
  87. $cmd->execute();
  88. }
  89. public function testInsertCommand()
  90. {
  91. $cmd = MongoCommandBuilder::factory(MongoCommandBuilder::INSERT, $this->collection);
  92. $cmd
  93. ->bindParam('data', array('name' => 'insert'))
  94. ->bindParam('safe', true);
  95. $this->assertFalse($cmd->getInsertId());
  96. $this->assertArrayHasKey('n', $cmd->execute());
  97. $this->assertNotEmpty($cmd->getInsertId());
  98. $cmd = MongoCommandBuilder::factory(MongoCommandBuilder::FIND, $this->collection);
  99. $cmd->bindParam('condition', array('name' => 'insert'))->bindParam('fields', array());
  100. $result = $cmd->execute();
  101. $this->assertEquals(1, $result->count());
  102. }
  103. /**
  104. * @expectedException GeneralException
  105. * @expectedExceptionMessage InsertMongoCommand error. Bind all required params first.
  106. */
  107. public function testInsertCommandNotAllParamsBinded()
  108. {
  109. $cmd = MongoCommandBuilder::factory(MongoCommandBuilder::INSERT, $this->collection);
  110. $cmd->execute();
  111. }
  112. public function testUpdateCommand()
  113. {
  114. $cmd = MongoCommandBuilder::factory(MongoCommandBuilder::INSERT, $this->collection);
  115. $cmd
  116. ->bindParam('data', array('name' => 'insert'))
  117. ->bindParam('safe', true);
  118. $this->assertArrayHasKey('n', $cmd->execute());
  119. $cmd = MongoCommandBuilder::factory(MongoCommandBuilder::UPDATE, $this->collection);
  120. $cmd
  121. ->bindParam('condition', array('name' => 'insert'))
  122. ->bindParam('data', array('$set' => array('name' => 'update')))
  123. ->bindParam('upsert', false)
  124. ->bindParam('safe', true);
  125. $this->assertArrayHasKey('n', $cmd->execute());
  126. $cmd = MongoCommandBuilder::factory(MongoCommandBuilder::FIND, $this->collection);
  127. $cmd->bindParam('condition', array('name' => 'insert'))->bindParam('fields', array());
  128. $result = $cmd->execute();
  129. $this->assertEquals(0, $result->count());
  130. $cmd->bindParam('condition', array('name' => 'update'))->bindParam('fields', array());
  131. $result = $cmd->execute();
  132. $this->assertEquals(1, $result->count());
  133. }
  134. /**
  135. * @expectedException GeneralException
  136. * @expectedExceptionMessage UpdateMongoCommand error. Bind all required params first.
  137. */
  138. public function testUpdateCommandNotAllParamsBinded()
  139. {
  140. $cmd = MongoCommandBuilder::factory(MongoCommandBuilder::UPDATE, $this->collection);
  141. $cmd->bindParam('data', array('name' => 'bread'));
  142. $cmd->execute();
  143. }
  144. public function testRemoveCommand()
  145. {
  146. $cmd = MongoCommandBuilder::factory(MongoCommandBuilder::INSERT, $this->collection);
  147. $cmd
  148. ->bindParam('data', array('name' => 'insert'))
  149. ->bindParam('safe', true);
  150. $this->assertArrayHasKey('n', $cmd->execute());
  151. $cmd = MongoCommandBuilder::factory(MongoCommandBuilder::FIND, $this->collection);
  152. $cmd->bindParam('condition', array('name' => 'insert'))->bindParam('fields', array());
  153. $result = $cmd->execute();
  154. $this->assertEquals(1, $result->count());
  155. $cmd = MongoCommandBuilder::factory(MongoCommandBuilder::REMOVE, $this->collection);
  156. $cmd
  157. ->bindParam('condition', array('name' => 'insert'))
  158. ->bindParam('safe', true);
  159. $this->assertArrayHasKey('n', $cmd->execute());
  160. $cmd = MongoCommandBuilder::factory(MongoCommandBuilder::FIND, $this->collection);
  161. $cmd->bindParam('condition', array('name' => 'insert'))->bindParam('fields', array());
  162. $result = $cmd->execute();
  163. $this->assertEquals(0, $result->count());
  164. }
  165. /**
  166. * @expectedException GeneralException
  167. * @expectedExceptionMessage RemoveMongoCommand error. Bind all required params first.
  168. */
  169. public function testRemoveCommandNotAllParamsBinded()
  170. {
  171. $cmd = MongoCommandBuilder::factory(MongoCommandBuilder::REMOVE, $this->collection);
  172. $cmd->execute();
  173. }
  174. /**
  175. * @expectedException GeneralException
  176. * @expectedExceptionMessage CommandMongoCommand error. Bind all required params first.
  177. */
  178. public function testCommandCommandNotAllParamsBinded()
  179. {
  180. $cmd = MongoCommandBuilder::factory(MongoCommandBuilder::COMMAND, $this->collection);
  181. $cmd->execute();
  182. }
  183. public function testCommandCommandNotMongoDb()
  184. {
  185. $cmd = MongoCommandBuilder::factory(MongoCommandBuilder::COMMAND, new CollectionMock());
  186. $cmd->bindParam('command', array());
  187. $this->assertFalse($cmd->execute());
  188. }
  189. }
  190. class CollectionMock
  191. {
  192. public $db = array();
  193. }