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.

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