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.

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