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.

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