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.

183 lines
7.3 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. class MongoDbCommandTest extends PHPUnit_Framework_TestCase
  16. {
  17. private $conf = array();
  18. private $driver;
  19. public function setUp()
  20. {
  21. $this->conf = array(
  22. 'hostname' => 'localhost',
  23. 'database' => 'test',
  24. 'username' => 'test',
  25. 'password' => '1234',
  26. 'port' => 27017
  27. );
  28. $this->driver = new MongoDriver($this->conf);
  29. $connection = $this->driver->getConnection();
  30. $db = $connection->selectDB($this->conf['database']);
  31. $db->authenticate($this->conf['username'], $this->conf['password']);
  32. $collection = 'items';
  33. $db->dropCollection($collection);
  34. }
  35. public function testCommandFactory()
  36. {
  37. $cmd = MongoCommandBuilder::factory(MongoCommandBuilder::FIND);
  38. $this->assertInstanceOf('MongoDbCommand', $cmd);
  39. $this->assertInstanceOf('FindMongoCommand', $cmd);
  40. $cmd = MongoCommandBuilder::factory(MongoCommandBuilder::INSERT);
  41. $this->assertInstanceOf('MongoDbCommand', $cmd);
  42. $this->assertInstanceOf('InsertMongoCommand', $cmd);
  43. $cmd = MongoCommandBuilder::factory(MongoCommandBuilder::UPDATE);
  44. $this->assertInstanceOf('MongoDbCommand', $cmd);
  45. $this->assertInstanceOf('UpdateMongoCommand', $cmd);
  46. $cmd = MongoCommandBuilder::factory(MongoCommandBuilder::REMOVE);
  47. $this->assertInstanceOf('MongoDbCommand', $cmd);
  48. $this->assertInstanceOf('RemoveMongoCommand', $cmd);
  49. }
  50. public function testFindCommand()
  51. {
  52. $collection = $this->driver->getCollection('items');
  53. $cmd = MongoCommandBuilder::factory(MongoCommandBuilder::FIND);
  54. $cmd->bindParam('collection', $collection)->bindParam('condition', array('name' => 'bread'))->bindParam('fields', array());
  55. $result = $cmd->execute();
  56. $this->assertEquals(0, $result->count());
  57. }
  58. /**
  59. * @expectedException Exception
  60. * @expectedExceptionMessage FindMongoCommand error. Bind all required params first.
  61. */
  62. public function testFindCommandNotAllParamsBinded()
  63. {
  64. $collection = $this->driver->getCollection('items');
  65. $cmd = MongoCommandBuilder::factory(MongoCommandBuilder::FIND);
  66. $cmd->bindParam('collection', $collection)->bindParam('condition', array('name' => 'bread'));
  67. $cmd->execute();
  68. }
  69. public function testInsertCommand()
  70. {
  71. $collection = $this->driver->getCollection('items');
  72. $cmd = MongoCommandBuilder::factory(MongoCommandBuilder::INSERT);
  73. $cmd->bindParam('collection', $collection)
  74. ->bindParam('data', array('name' => 'insert'))
  75. ->bindParam('safe', true);
  76. $this->assertArrayHasKey('n', $cmd->execute());
  77. $cmd = MongoCommandBuilder::factory(MongoCommandBuilder::FIND);
  78. $cmd->bindParam('collection', $collection)->bindParam('condition', array('name' => 'insert'))->bindParam('fields', array());
  79. $result = $cmd->execute();
  80. $this->assertEquals(1, $result->count());
  81. }
  82. /**
  83. * @expectedException Exception
  84. * @expectedExceptionMessage InsertMongoCommand error. Bind all required params first.
  85. */
  86. public function testInsertCommandNotAllParamsBinded()
  87. {
  88. $collection = $this->driver->getCollection('items');
  89. $cmd = MongoCommandBuilder::factory(MongoCommandBuilder::INSERT);
  90. $cmd->bindParam('collection', $collection)->bindParam('data', array('name' => 'bread'));
  91. $cmd->execute();
  92. }
  93. public function testUpdateCommand()
  94. {
  95. $collection = $this->driver->getCollection('items');
  96. $cmd = MongoCommandBuilder::factory(MongoCommandBuilder::INSERT);
  97. $cmd->bindParam('collection', $collection)
  98. ->bindParam('data', array('name' => 'insert'))
  99. ->bindParam('safe', true);
  100. $this->assertArrayHasKey('n', $cmd->execute());
  101. $cmd = MongoCommandBuilder::factory(MongoCommandBuilder::UPDATE);
  102. $cmd->bindParam('collection', $collection)
  103. ->bindParam('condition', array('name' => 'insert'))
  104. ->bindParam('data', array('$set' => array('name' => 'update')))
  105. ->bindParam('upsert', false)
  106. ->bindParam('safe', true);
  107. $this->assertArrayHasKey('n', $cmd->execute());
  108. $cmd = MongoCommandBuilder::factory(MongoCommandBuilder::FIND);
  109. $cmd->bindParam('collection', $collection)->bindParam('condition', array('name' => 'insert'))->bindParam('fields', array());
  110. $result = $cmd->execute();
  111. $this->assertEquals(0, $result->count());
  112. $cmd->bindParam('collection', $collection)->bindParam('condition', array('name' => 'update'))->bindParam('fields', array());
  113. $result = $cmd->execute();
  114. $this->assertEquals(1, $result->count());
  115. }
  116. /**
  117. * @expectedException Exception
  118. * @expectedExceptionMessage UpdateMongoCommand error. Bind all required params first.
  119. */
  120. public function testUpdateCommandNotAllParamsBinded()
  121. {
  122. $collection = $this->driver->getCollection('items');
  123. $cmd = MongoCommandBuilder::factory(MongoCommandBuilder::UPDATE);
  124. $cmd->bindParam('collection', $collection)->bindParam('data', array('name' => 'bread'));
  125. $cmd->execute();
  126. }
  127. public function testRemoveCommand()
  128. {
  129. $collection = $this->driver->getCollection('items');
  130. $cmd = MongoCommandBuilder::factory(MongoCommandBuilder::INSERT);
  131. $cmd->bindParam('collection', $collection)
  132. ->bindParam('data', array('name' => 'insert'))
  133. ->bindParam('safe', true);
  134. $this->assertArrayHasKey('n', $cmd->execute());
  135. $cmd = MongoCommandBuilder::factory(MongoCommandBuilder::FIND);
  136. $cmd->bindParam('collection', $collection)->bindParam('condition', array('name' => 'insert'))->bindParam('fields', array());
  137. $result = $cmd->execute();
  138. $this->assertEquals(1, $result->count());
  139. $cmd = MongoCommandBuilder::factory(MongoCommandBuilder::REMOVE);
  140. $cmd->bindParam('collection', $collection)
  141. ->bindParam('condition', array('name' => 'insert'))
  142. ->bindParam('safe', true);
  143. $this->assertArrayHasKey('n', $cmd->execute());
  144. $cmd = MongoCommandBuilder::factory(MongoCommandBuilder::FIND);
  145. $cmd->bindParam('collection', $collection)->bindParam('condition', array('name' => 'insert'))->bindParam('fields', array());
  146. $result = $cmd->execute();
  147. $this->assertEquals(0, $result->count());
  148. }
  149. /**
  150. * @expectedException Exception
  151. * @expectedExceptionMessage RemoveMongoCommand error. Bind all required params first.
  152. */
  153. public function testRemoveCommandNotAllParamsBinded()
  154. {
  155. $collection = $this->driver->getCollection('items');
  156. $cmd = MongoCommandBuilder::factory(MongoCommandBuilder::REMOVE);
  157. $cmd->bindParam('collection', $collection);
  158. $cmd->execute();
  159. }
  160. }