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.

270 lines
11 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. public function testFindCommandNotAllParamsBinded()
  80. {
  81. $cmd = MongoCommandBuilder::factory(MongoCommandBuilder::FIND, $this->collection);
  82. $cmd->bindParam('condition', array('name' => 'bread'));
  83. $this->setExpectedException('GeneralException', 'FindMongoCommand error. Bind all required params first');
  84. $cmd->execute();
  85. }
  86. public function testInsertCommand()
  87. {
  88. $cmd = MongoCommandBuilder::factory(MongoCommandBuilder::INSERT, $this->collection);
  89. $cmd
  90. ->bindParam('data', array('name' => 'insert'))
  91. ->bindParam('safe', true);
  92. $this->assertFalse($cmd->getInsertId());
  93. $this->assertArrayHasKey('n', $cmd->execute());
  94. $this->assertNotEmpty($cmd->getInsertId());
  95. $cmd = MongoCommandBuilder::factory(MongoCommandBuilder::FIND, $this->collection);
  96. $cmd->bindParam('condition', array('name' => 'insert'))->bindParam('fields', array());
  97. $result = $cmd->execute();
  98. $this->assertEquals(1, $result->count());
  99. }
  100. public function testInsertCommandNotAllParamsBinded()
  101. {
  102. $cmd = MongoCommandBuilder::factory(MongoCommandBuilder::INSERT, $this->collection);
  103. $this->setExpectedException('GeneralException', 'InsertMongoCommand error. Bind all required params first');
  104. $cmd->execute();
  105. }
  106. public function testUpdateCommand()
  107. {
  108. $cmd = MongoCommandBuilder::factory(MongoCommandBuilder::INSERT, $this->collection);
  109. $cmd
  110. ->bindParam('data', array('name' => 'insert'))
  111. ->bindParam('safe', true);
  112. $this->assertArrayHasKey('n', $cmd->execute());
  113. $cmd = MongoCommandBuilder::factory(MongoCommandBuilder::UPDATE, $this->collection);
  114. $cmd
  115. ->bindParam('condition', array('name' => 'insert'))
  116. ->bindParam('data', array('$set' => array('name' => 'update')))
  117. ->bindParam('upsert', false)
  118. ->bindParam('safe', true);
  119. $this->assertArrayHasKey('n', $cmd->execute());
  120. $cmd = MongoCommandBuilder::factory(MongoCommandBuilder::FIND, $this->collection);
  121. $cmd->bindParam('condition', array('name' => 'insert'))->bindParam('fields', array());
  122. $result = $cmd->execute();
  123. $this->assertEquals(0, $result->count());
  124. $cmd->bindParam('condition', array('name' => 'update'))->bindParam('fields', array());
  125. $result = $cmd->execute();
  126. $this->assertEquals(1, $result->count());
  127. }
  128. public function testUpdateCommandNotAllParamsBinded()
  129. {
  130. $cmd = MongoCommandBuilder::factory(MongoCommandBuilder::UPDATE, $this->collection);
  131. $cmd->bindParam('data', array('name' => 'bread'));
  132. $this->setExpectedException('GeneralException', 'UpdateMongoCommand error. Bind all required params first');
  133. $cmd->execute();
  134. }
  135. public function testRemoveCommand()
  136. {
  137. $cmd = MongoCommandBuilder::factory(MongoCommandBuilder::INSERT, $this->collection);
  138. $cmd
  139. ->bindParam('data', array('name' => 'insert'))
  140. ->bindParam('safe', true);
  141. $this->assertArrayHasKey('n', $cmd->execute());
  142. $cmd = MongoCommandBuilder::factory(MongoCommandBuilder::FIND, $this->collection);
  143. $cmd->bindParam('condition', array('name' => 'insert'))->bindParam('fields', array());
  144. $result = $cmd->execute();
  145. $this->assertEquals(1, $result->count());
  146. $cmd = MongoCommandBuilder::factory(MongoCommandBuilder::REMOVE, $this->collection);
  147. $cmd
  148. ->bindParam('condition', 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(0, $result->count());
  155. }
  156. public function testRemoveCommandNotAllParamsBinded()
  157. {
  158. $cmd = MongoCommandBuilder::factory(MongoCommandBuilder::REMOVE, $this->collection);
  159. $this->setExpectedException('GeneralException', 'RemoveMongoCommand error. Bind all required params first.');
  160. $cmd->execute();
  161. }
  162. public function testCommandCommandNotAllParamsBinded()
  163. {
  164. $cmd = MongoCommandBuilder::factory(MongoCommandBuilder::COMMAND, $this->collection);
  165. $this->setExpectedException('GeneralException', 'CommandMongoCommand error. Bind all required params first');
  166. $cmd->execute();
  167. }
  168. public function testCommandCommandNotMongoDb()
  169. {
  170. $cmd = MongoCommandBuilder::factory(MongoCommandBuilder::COMMAND, new CollectionMock());
  171. $cmd->bindParam('command', array());
  172. $this->assertFalse($cmd->execute());
  173. }
  174. public function testCommandCommand()
  175. {
  176. $col = new CollectionMock();
  177. $col->db = $this->getMock('MongoDb', array('command'), array(), 'SomeMongoMock', false);
  178. $col->db
  179. ->expects($this->once())
  180. ->method('command')
  181. ->will($this->returnValue(true));
  182. $cmd = MongoCommandBuilder::factory(MongoCommandBuilder::COMMAND, $col);
  183. $cmd->bindParam('command', array());
  184. $this->assertTrue($cmd->execute());
  185. }
  186. public function testToStringParamsNotSet()
  187. {
  188. $cmd = MongoCommandBuilder::factory(MongoCommandBuilder::COMMAND, new CollectionMock());
  189. $this->assertSame('Command properties not set', $cmd->__toString());
  190. }
  191. public function testToString()
  192. {
  193. $cmd = MongoCommandBuilder::factory(MongoCommandBuilder::COMMAND, new CollectionMock());
  194. $this->assertSame('Command properties not set', $cmd->__toString());
  195. $cmd->bindParam('command', array());
  196. $this->assertStringStartsWith('Collection: CollectionMock', $cmd->__toString());
  197. $cmd = MongoCommandBuilder::factory(MongoCommandBuilder::INSERT, $this->collection);
  198. $this->assertSame('Command properties not set', $cmd->__toString());
  199. $cmd
  200. ->bindParam('data', array('name' => 'insert'))
  201. ->bindParam('safe', true);
  202. $this->assertStringStartsWith('Collection: ', $cmd->__toString());
  203. $cmd = MongoCommandBuilder::factory(MongoCommandBuilder::FIND, $this->collection);
  204. $this->assertSame('Command properties not set', $cmd->__toString());
  205. $cmd->bindParam('condition', array('name' => 'insert'))->bindParam('fields', array());
  206. $this->assertStringStartsWith('Collection: ', $cmd->__toString());
  207. $cmd = MongoCommandBuilder::factory(MongoCommandBuilder::REMOVE, $this->collection);
  208. $this->assertSame('Command properties not set', $cmd->__toString());
  209. $cmd
  210. ->bindParam('condition', array('name' => 'insert'))
  211. ->bindParam('safe', true);
  212. $this->assertStringStartsWith('Collection: ', $cmd->__toString());
  213. $cmd = MongoCommandBuilder::factory(MongoCommandBuilder::UPDATE, $this->collection);
  214. $this->assertSame('Command properties not set', $cmd->__toString());
  215. $cmd
  216. ->bindParam('condition', array('name' => 'insert'))
  217. ->bindParam('data', array('$set' => array('name' => 'update')))
  218. ->bindParam('upsert', false)
  219. ->bindParam('safe', true);
  220. $this->assertStringStartsWith('Collection: ', $cmd->__toString());
  221. }
  222. }
  223. class CollectionMock
  224. {
  225. public $db = array();
  226. public function __toString()
  227. {
  228. return 'CollectionMock';
  229. }
  230. }