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.

312 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. /**
  39. * @group Mongo
  40. */
  41. public function testCommandFactory()
  42. {
  43. $cmd = MongoCommandBuilder::factory(MongoCommandBuilder::FIND);
  44. $this->assertInstanceOf('MongoDbCommand', $cmd);
  45. $this->assertInstanceOf('FindMongoCommand', $cmd);
  46. $cmd = MongoCommandBuilder::factory(MongoCommandBuilder::INSERT);
  47. $this->assertInstanceOf('MongoDbCommand', $cmd);
  48. $this->assertInstanceOf('InsertMongoCommand', $cmd);
  49. $cmd = MongoCommandBuilder::factory(MongoCommandBuilder::UPDATE);
  50. $this->assertInstanceOf('MongoDbCommand', $cmd);
  51. $this->assertInstanceOf('UpdateMongoCommand', $cmd);
  52. $cmd = MongoCommandBuilder::factory(MongoCommandBuilder::REMOVE);
  53. $this->assertInstanceOf('MongoDbCommand', $cmd);
  54. $this->assertInstanceOf('RemoveMongoCommand', $cmd);
  55. }
  56. /**
  57. * @group Mongo
  58. */
  59. public function testFindCommand()
  60. {
  61. $cmd = MongoCommandBuilder::factory(MongoCommandBuilder::FIND, $this->collection);
  62. $cmd->bindParam('condition', array('name' => 'bread'))->bindParam('fields', array());
  63. $result = $cmd->execute();
  64. $this->assertEquals(0, $result->count());
  65. $cmd = MongoCommandBuilder::factory(MongoCommandBuilder::INSERT, $this->collection);
  66. $cmd
  67. ->bindParam('data', array('name' => 'insert'))
  68. ->bindParam('safe', true);
  69. $cmd->execute();
  70. $cmd = MongoCommandBuilder::factory(MongoCommandBuilder::INSERT, $this->collection);
  71. $cmd
  72. ->bindParam('data', array('name' => 'insert'))
  73. ->bindParam('safe', true);
  74. $cmd->execute();
  75. $cmd = MongoCommandBuilder::factory(MongoCommandBuilder::FIND, $this->collection);
  76. $cmd->bindParam('condition', array('name' => 'insert'))->bindParam('fields', array());
  77. $this->assertEquals(2, $cmd->execute()->count());
  78. $cmd
  79. ->bindParam('condition', array('name' => 'insert'))
  80. ->bindParam('fields', array())
  81. ->bindParam('multiple', false);
  82. $result = $cmd->execute();
  83. $this->assertEquals('insert', $result['name']);
  84. }
  85. /**
  86. * @group Mongo
  87. */
  88. public function testFindCommandNotAllParamsBinded()
  89. {
  90. $cmd = MongoCommandBuilder::factory(MongoCommandBuilder::FIND, $this->collection);
  91. $cmd->bindParam('condition', array('name' => 'bread'));
  92. $this->setExpectedException('GeneralException', 'FindMongoCommand error. Bind all required params first');
  93. $cmd->execute();
  94. }
  95. /**
  96. * @group Mongo
  97. */
  98. public function testInsertCommand()
  99. {
  100. $cmd = MongoCommandBuilder::factory(MongoCommandBuilder::INSERT, $this->collection);
  101. $cmd
  102. ->bindParam('data', array('name' => 'insert'))
  103. ->bindParam('safe', true);
  104. $this->assertFalse($cmd->getInsertId());
  105. $this->assertArrayHasKey('n', $cmd->execute());
  106. $this->assertNotEmpty($cmd->getInsertId());
  107. $cmd = MongoCommandBuilder::factory(MongoCommandBuilder::FIND, $this->collection);
  108. $cmd->bindParam('condition', array('name' => 'insert'))->bindParam('fields', array());
  109. $result = $cmd->execute();
  110. $this->assertEquals(1, $result->count());
  111. }
  112. /**
  113. * @group Mongo
  114. */
  115. public function testInsertCommandNotAllParamsBinded()
  116. {
  117. $cmd = MongoCommandBuilder::factory(MongoCommandBuilder::INSERT, $this->collection);
  118. $this->setExpectedException('GeneralException', 'InsertMongoCommand error. Bind all required params first');
  119. $cmd->execute();
  120. }
  121. /**
  122. * @group Mongo
  123. */
  124. public function testUpdateCommand()
  125. {
  126. $cmd = MongoCommandBuilder::factory(MongoCommandBuilder::INSERT, $this->collection);
  127. $cmd
  128. ->bindParam('data', array('name' => 'insert'))
  129. ->bindParam('safe', true);
  130. $this->assertArrayHasKey('n', $cmd->execute());
  131. $cmd = MongoCommandBuilder::factory(MongoCommandBuilder::UPDATE, $this->collection);
  132. $cmd
  133. ->bindParam('condition', array('name' => 'insert'))
  134. ->bindParam('data', array('$set' => array('name' => 'update')))
  135. ->bindParam('upsert', false)
  136. ->bindParam('safe', true);
  137. $this->assertArrayHasKey('n', $cmd->execute());
  138. $cmd = MongoCommandBuilder::factory(MongoCommandBuilder::FIND, $this->collection);
  139. $cmd->bindParam('condition', array('name' => 'insert'))->bindParam('fields', array());
  140. $result = $cmd->execute();
  141. $this->assertEquals(0, $result->count());
  142. $cmd->bindParam('condition', array('name' => 'update'))->bindParam('fields', array());
  143. $result = $cmd->execute();
  144. $this->assertEquals(1, $result->count());
  145. }
  146. /**
  147. * @group Mongo
  148. */
  149. public function testUpdateCommandNotAllParamsBinded()
  150. {
  151. $cmd = MongoCommandBuilder::factory(MongoCommandBuilder::UPDATE, $this->collection);
  152. $cmd->bindParam('data', array('name' => 'bread'));
  153. $this->setExpectedException('GeneralException', 'UpdateMongoCommand error. Bind all required params first');
  154. $cmd->execute();
  155. }
  156. /**
  157. * @group Mongo
  158. */
  159. public function testRemoveCommand()
  160. {
  161. $cmd = MongoCommandBuilder::factory(MongoCommandBuilder::INSERT, $this->collection);
  162. $cmd
  163. ->bindParam('data', array('name' => 'insert'))
  164. ->bindParam('safe', true);
  165. $this->assertArrayHasKey('n', $cmd->execute());
  166. $cmd = MongoCommandBuilder::factory(MongoCommandBuilder::FIND, $this->collection);
  167. $cmd->bindParam('condition', array('name' => 'insert'))->bindParam('fields', array());
  168. $result = $cmd->execute();
  169. $this->assertEquals(1, $result->count());
  170. $cmd = MongoCommandBuilder::factory(MongoCommandBuilder::REMOVE, $this->collection);
  171. $cmd
  172. ->bindParam('condition', array('name' => 'insert'))
  173. ->bindParam('safe', true);
  174. $this->assertArrayHasKey('n', $cmd->execute());
  175. $cmd = MongoCommandBuilder::factory(MongoCommandBuilder::FIND, $this->collection);
  176. $cmd->bindParam('condition', array('name' => 'insert'))->bindParam('fields', array());
  177. $result = $cmd->execute();
  178. $this->assertEquals(0, $result->count());
  179. }
  180. /**
  181. * @group Mongo
  182. */
  183. public function testRemoveCommandNotAllParamsBinded()
  184. {
  185. $cmd = MongoCommandBuilder::factory(MongoCommandBuilder::REMOVE, $this->collection);
  186. $this->setExpectedException('GeneralException', 'RemoveMongoCommand error. Bind all required params first.');
  187. $cmd->execute();
  188. }
  189. /**
  190. * @group Mongo
  191. */
  192. public function testCommandCommandNotAllParamsBinded()
  193. {
  194. $cmd = MongoCommandBuilder::factory(MongoCommandBuilder::COMMAND, $this->collection);
  195. $this->setExpectedException('GeneralException', 'CommandMongoCommand error. Bind all required params first');
  196. $cmd->execute();
  197. }
  198. /**
  199. * @group Mongo
  200. */
  201. public function testCommandCommandNotMongoDb()
  202. {
  203. $cmd = MongoCommandBuilder::factory(MongoCommandBuilder::COMMAND, new CollectionMock());
  204. $cmd->bindParam('command', array());
  205. $this->assertFalse($cmd->execute());
  206. }
  207. /**
  208. * @group Mongo
  209. */
  210. public function testCommandCommand()
  211. {
  212. $col = new CollectionMock();
  213. $col->db = $this->getMock('MongoDb', array('command'), array(), 'SomeMongoMock', false);
  214. $col->db
  215. ->expects($this->once())
  216. ->method('command')
  217. ->will($this->returnValue(true));
  218. $cmd = MongoCommandBuilder::factory(MongoCommandBuilder::COMMAND, $col);
  219. $cmd->bindParam('command', array());
  220. $this->assertTrue($cmd->execute());
  221. }
  222. /**
  223. * @group Mongo
  224. */
  225. public function testToStringParamsNotSet()
  226. {
  227. $cmd = MongoCommandBuilder::factory(MongoCommandBuilder::COMMAND, new CollectionMock());
  228. $this->assertSame('Command properties not set', $cmd->__toString());
  229. }
  230. /**
  231. * @group Mongo
  232. */
  233. public function testToString()
  234. {
  235. $cmd = MongoCommandBuilder::factory(MongoCommandBuilder::COMMAND, new CollectionMock());
  236. $this->assertSame('Command properties not set', $cmd->__toString());
  237. $cmd->bindParam('command', array());
  238. $this->assertStringStartsWith('Collection: CollectionMock', $cmd->__toString());
  239. $cmd = MongoCommandBuilder::factory(MongoCommandBuilder::INSERT, $this->collection);
  240. $this->assertSame('Command properties not set', $cmd->__toString());
  241. $cmd
  242. ->bindParam('data', array('name' => 'insert'))
  243. ->bindParam('safe', true);
  244. $this->assertStringStartsWith('Collection: ', $cmd->__toString());
  245. $cmd = MongoCommandBuilder::factory(MongoCommandBuilder::FIND, $this->collection);
  246. $this->assertSame('Command properties not set', $cmd->__toString());
  247. $cmd->bindParam('condition', array('name' => 'insert'))->bindParam('fields', array());
  248. $this->assertStringStartsWith('Collection: ', $cmd->__toString());
  249. $cmd = MongoCommandBuilder::factory(MongoCommandBuilder::REMOVE, $this->collection);
  250. $this->assertSame('Command properties not set', $cmd->__toString());
  251. $cmd
  252. ->bindParam('condition', array('name' => 'insert'))
  253. ->bindParam('safe', true);
  254. $this->assertStringStartsWith('Collection: ', $cmd->__toString());
  255. $cmd = MongoCommandBuilder::factory(MongoCommandBuilder::UPDATE, $this->collection);
  256. $this->assertSame('Command properties not set', $cmd->__toString());
  257. $cmd
  258. ->bindParam('condition', array('name' => 'insert'))
  259. ->bindParam('data', array('$set' => array('name' => 'update')))
  260. ->bindParam('upsert', false)
  261. ->bindParam('safe', true);
  262. $this->assertStringStartsWith('Collection: ', $cmd->__toString());
  263. }
  264. }
  265. class CollectionMock
  266. {
  267. public $db = array();
  268. public function __toString()
  269. {
  270. return 'CollectionMock';
  271. }
  272. }