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.

360 lines
13 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 testInsertCommandMultipleObjects()
  116. {
  117. $cmd = MongoCommandBuilder::factory(MongoCommandBuilder::INSERT, $this->collection);
  118. $data = array(
  119. array('name' => 'first object'),
  120. array('name' => 'second object'),
  121. array('name' => 'equal object'),
  122. array('name' => 'equal object')
  123. );
  124. $cmd
  125. ->bindParam('data', $data)
  126. ->bindParam('multiple', true)
  127. ->bindParam('safe', true);
  128. $this->assertFalse($cmd->getInsertId());
  129. $this->assertArrayHasKey('n', $cmd->execute());
  130. $cmd->bindParam('data', array());
  131. $cmd->execute();
  132. $cmd = MongoCommandBuilder::factory(MongoCommandBuilder::FIND, $this->collection);
  133. $cmd->bindParam('condition', array('name' => 'first object'))->bindParam('fields', array());
  134. $result = $cmd->execute();
  135. $this->assertEquals(1, $result->count());
  136. $cmd->bindParam('condition', array('name' => 'second object'))->bindParam('fields', array());
  137. $result = $cmd->execute();
  138. $this->assertEquals(1, $result->count());
  139. $cmd->bindParam('condition', array('name' => 'equal object'))->bindParam('fields', array());
  140. $result = $cmd->execute();
  141. $this->assertEquals(2, $result->count());
  142. }
  143. /**
  144. * @group Mongo
  145. */
  146. public function testInsertCommandNotAllParamsBinded()
  147. {
  148. $cmd = MongoCommandBuilder::factory(MongoCommandBuilder::INSERT, $this->collection);
  149. $this->setExpectedException('GeneralException', 'InsertMongoCommand error. Bind all required params first');
  150. $cmd->execute();
  151. }
  152. /**
  153. * @group Mongo
  154. */
  155. public function testUpdateCommand()
  156. {
  157. $cmd = MongoCommandBuilder::factory(MongoCommandBuilder::INSERT, $this->collection);
  158. $cmd
  159. ->bindParam('data', array('name' => 'insert'))
  160. ->bindParam('safe', true);
  161. $this->assertArrayHasKey('n', $cmd->execute());
  162. $cmd = MongoCommandBuilder::factory(MongoCommandBuilder::UPDATE, $this->collection);
  163. $cmd
  164. ->bindParam('condition', array('name' => 'insert'))
  165. ->bindParam('data', array('$set' => array('name' => 'update')))
  166. ->bindParam('upsert', false)
  167. ->bindParam('safe', true);
  168. $this->assertArrayHasKey('n', $cmd->execute());
  169. $cmd = MongoCommandBuilder::factory(MongoCommandBuilder::FIND, $this->collection);
  170. $cmd->bindParam('condition', array('name' => 'insert'))->bindParam('fields', array());
  171. $result = $cmd->execute();
  172. $this->assertEquals(0, $result->count());
  173. $cmd->bindParam('condition', array('name' => 'update'))->bindParam('fields', array());
  174. $result = $cmd->execute();
  175. $this->assertEquals(1, $result->count());
  176. }
  177. /**
  178. * @group Mongo
  179. */
  180. public function testUpdateCommandNotAllParamsBinded()
  181. {
  182. $cmd = MongoCommandBuilder::factory(MongoCommandBuilder::UPDATE, $this->collection);
  183. $cmd->bindParam('data', array('name' => 'bread'));
  184. $this->setExpectedException('GeneralException', 'UpdateMongoCommand error. Bind all required params first');
  185. $cmd->execute();
  186. }
  187. /**
  188. * @group Mongo
  189. */
  190. public function testRemoveCommand()
  191. {
  192. $cmd = MongoCommandBuilder::factory(MongoCommandBuilder::INSERT, $this->collection);
  193. $cmd
  194. ->bindParam('data', array('name' => 'insert'))
  195. ->bindParam('safe', true);
  196. $this->assertArrayHasKey('n', $cmd->execute());
  197. $cmd = MongoCommandBuilder::factory(MongoCommandBuilder::FIND, $this->collection);
  198. $cmd->bindParam('condition', array('name' => 'insert'))->bindParam('fields', array());
  199. $result = $cmd->execute();
  200. $this->assertEquals(1, $result->count());
  201. $cmd = MongoCommandBuilder::factory(MongoCommandBuilder::REMOVE, $this->collection);
  202. $cmd
  203. ->bindParam('condition', array('name' => 'insert'))
  204. ->bindParam('safe', true);
  205. $this->assertArrayHasKey('n', $cmd->execute());
  206. $cmd = MongoCommandBuilder::factory(MongoCommandBuilder::FIND, $this->collection);
  207. $cmd->bindParam('condition', array('name' => 'insert'))->bindParam('fields', array());
  208. $result = $cmd->execute();
  209. $this->assertEquals(0, $result->count());
  210. }
  211. /**
  212. * @group Mongo
  213. */
  214. public function testRemoveCommandNotAllParamsBinded()
  215. {
  216. $cmd = MongoCommandBuilder::factory(MongoCommandBuilder::REMOVE, $this->collection);
  217. $this->setExpectedException('GeneralException', 'RemoveMongoCommand error. Bind all required params first.');
  218. $cmd->execute();
  219. }
  220. /**
  221. * @group Mongo
  222. */
  223. public function testCommandCommandNotAllParamsBinded()
  224. {
  225. $cmd = MongoCommandBuilder::factory(MongoCommandBuilder::COMMAND, $this->collection);
  226. $this->setExpectedException('GeneralException', 'CommandMongoCommand error. Bind all required params first');
  227. $cmd->execute();
  228. }
  229. /**
  230. * @group Mongo
  231. */
  232. public function testCommandCommandNotMongoDb()
  233. {
  234. $cmd = MongoCommandBuilder::factory(MongoCommandBuilder::COMMAND, new CollectionMock());
  235. $cmd->bindParam('command', array());
  236. $this->assertFalse($cmd->execute());
  237. }
  238. /**
  239. * @group Mongo
  240. */
  241. public function testCommandCommand()
  242. {
  243. $col = new CollectionMock();
  244. $col->db = $this->getMock('MongoDb', array('command'), array(), 'SomeMongoMock', false);
  245. $col->db
  246. ->expects($this->once())
  247. ->method('command')
  248. ->will($this->returnValue(true));
  249. $cmd = MongoCommandBuilder::factory(MongoCommandBuilder::COMMAND, $col);
  250. $cmd->bindParam('command', array());
  251. $this->assertTrue($cmd->execute());
  252. }
  253. /**
  254. * @group Mongo
  255. */
  256. public function testToStringParamsNotSet()
  257. {
  258. $cmd = MongoCommandBuilder::factory(MongoCommandBuilder::COMMAND, new CollectionMock());
  259. $this->assertSame('Command properties not set', $cmd->__toString());
  260. }
  261. /**
  262. * @group Mongo
  263. */
  264. public function testToString()
  265. {
  266. $cmd = MongoCommandBuilder::factory(MongoCommandBuilder::COMMAND, new CollectionMock());
  267. $this->assertSame('Command properties not set', $cmd->__toString());
  268. $cmd->bindParam('command', array());
  269. $this->assertStringStartsWith('Collection: CollectionMock', $cmd->__toString());
  270. $cmd = MongoCommandBuilder::factory(MongoCommandBuilder::INSERT, $this->collection);
  271. $this->assertSame('Command properties not set', $cmd->__toString());
  272. $cmd
  273. ->bindParam('data', array('name' => 'insert'))
  274. ->bindParam('safe', true);
  275. $this->assertStringStartsWith('Collection: ', $cmd->__toString());
  276. $this->assertContains('Bulk insert: FALSE', $cmd->__toString());
  277. $cmd = MongoCommandBuilder::factory(MongoCommandBuilder::INSERT, $this->collection);
  278. $this->assertSame('Command properties not set', $cmd->__toString());
  279. $cmd
  280. ->bindParam('data', array('name' => 'insert'))
  281. ->bindParam('multiple', true)
  282. ->bindParam('safe', true);
  283. $this->assertContains('Bulk insert: TRUE', $cmd->__toString());
  284. $cmd = MongoCommandBuilder::factory(MongoCommandBuilder::FIND, $this->collection);
  285. $this->assertSame('Command properties not set', $cmd->__toString());
  286. $cmd->bindParam('condition', array('name' => 'insert'))->bindParam('fields', array());
  287. $this->assertStringStartsWith('Collection: ', $cmd->__toString());
  288. $cmd = MongoCommandBuilder::factory(MongoCommandBuilder::REMOVE, $this->collection);
  289. $this->assertSame('Command properties not set', $cmd->__toString());
  290. $cmd
  291. ->bindParam('condition', array('name' => 'insert'))
  292. ->bindParam('safe', true);
  293. $this->assertStringStartsWith('Collection: ', $cmd->__toString());
  294. $cmd = MongoCommandBuilder::factory(MongoCommandBuilder::UPDATE, $this->collection);
  295. $this->assertSame('Command properties not set', $cmd->__toString());
  296. $cmd
  297. ->bindParam('condition', array('name' => 'insert'))
  298. ->bindParam('data', array('$set' => array('name' => 'update')))
  299. ->bindParam('upsert', false)
  300. ->bindParam('safe', true);
  301. $this->assertStringStartsWith('Collection: ', $cmd->__toString());
  302. }
  303. }
  304. class CollectionMock
  305. {
  306. public $db = array();
  307. public function __toString()
  308. {
  309. return 'CollectionMock';
  310. }
  311. }