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