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.

203 lines
6.4 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-7
  8. *
  9. * Unit tests for MongoModel class
  10. */
  11. require_once dirname(__FILE__) . '/../../Registry.php';
  12. require_once dirname(__FILE__) . '/../../Config.php';
  13. require_once dirname(__FILE__) . '/../../cache/Cacher.php';
  14. require_once dirname(__FILE__) . '/../../model/DbExpr.php';
  15. require_once dirname(__FILE__) . '/../../model/Db.php';
  16. require_once dirname(__FILE__) . '/../../model/MongoDbCommand.php';
  17. require_once dirname(__FILE__) . '/../../model/DbStatement.php';
  18. require_once dirname(__FILE__) . '/../../model/MongoStatement.php';
  19. require_once dirname(__FILE__) . '/../../model/DbDriver.php';
  20. require_once dirname(__FILE__) . '/../../model/NoSqlDbDriver.php';
  21. require_once dirname(__FILE__) . '/../../model/MongoDriver.php';
  22. require_once dirname(__FILE__) . '/../../model/Model.php';
  23. require_once dirname(__FILE__) . '/../../model/MongoModel.php';
  24. class MongoModelTest extends PHPUnit_Framework_TestCase
  25. {
  26. private $model;
  27. public function run(PHPUnit_Framework_TestResult $result = NULL)
  28. {
  29. $this->setPreserveGlobalState(false);
  30. return parent::run($result);
  31. }
  32. public function setUp()
  33. {
  34. $conf = array('default' => array('driver' => 'MongoDriver', 'hostname' => 'localhost', 'database' => 'test', 'username' => 'test', 'password' => '1234', 'port' => 27017));
  35. $this->dbSetUp($conf);
  36. Config::set('Db', $conf);
  37. if (!class_exists('MockModel')) {
  38. $this->model = $this->getMockForAbstractClass('MongoModel', array(), 'MongoMockModel');
  39. } else {
  40. $this->model = new MongoMockModel();
  41. }
  42. set_new_overload(array($this, 'newCallback'));
  43. }
  44. public function testModel()
  45. {
  46. $this->assertInstanceOf('MongoMockModel', $this->model);
  47. }
  48. /**
  49. * @runInSeparateProcess
  50. */
  51. public function testFind()
  52. {
  53. if (!defined('DEBUG')) {
  54. define('DEBUG', false);
  55. }
  56. $result = $this->model->find();
  57. $this->assertInstanceOf('MongoStatement', $result);
  58. $this->assertEquals('milk', $result->limit(2)->order(array('name' => -1))->fetch()->name);
  59. $this->assertEquals('fish', $result->fetch()->name);
  60. }
  61. /**
  62. * @runInSeparateProcess
  63. */
  64. public function testGet()
  65. {
  66. if (!defined('DEBUG')) {
  67. define('DEBUG', false);
  68. }
  69. $result = $this->model->find()->limit(1)->order(array('name' => 1));
  70. $result = $result->fetch();
  71. $this->assertEquals('bread', $result->name);
  72. $id = $result->_id;
  73. $this->assertEquals(10, $this->model->get($id)->quantity);
  74. }
  75. /**
  76. * @runInSeparateProcess
  77. */
  78. public function testDelete()
  79. {
  80. if (!defined('DEBUG')) {
  81. define('DEBUG', false);
  82. }
  83. $result = $this->model->find()->limit(1)->order(array('name' => 1));
  84. $id = $result->fetch()->_id;
  85. $this->assertEquals(1, $this->model->delete($id));
  86. $this->assertFalse($this->model->find(array('name' => 'bread'))->fetch());
  87. }
  88. /**
  89. * @runInSeparateProcess
  90. */
  91. public function testFetch()
  92. {
  93. if (!defined('DEBUG')) {
  94. define('DEBUG', false);
  95. }
  96. $mock = $this->getMock('CacheKey', array('set', 'get'));
  97. $mock->expects($this->exactly(3))
  98. ->method('set')
  99. ->will($this->returnValue(true));
  100. $mock->expects($this->exactly(3))
  101. ->method('get')
  102. ->will($this->returnValue(false));
  103. $model = new ReflectionClass('MongoModel');
  104. $method = $model->getMethod('fetchField');
  105. $method->setAccessible(true);
  106. $result = $method->invoke($this->model, array('name' => 'milk'), array(), 'quantity', $mock);
  107. $this->assertEquals(1, $result);
  108. $model = new ReflectionClass('MongoModel');
  109. $method = $model->getMethod('fetch');
  110. $method->setAccessible(true);
  111. $result = $method->invoke($this->model, array('name' => 'bread'), array(), $mock);
  112. $this->assertEquals('bread', $result->name);
  113. $model = new ReflectionClass('MongoModel');
  114. $method = $model->getMethod('fetchAll');
  115. $method->setAccessible(true);
  116. $result = $method->invoke($this->model, array('name' => 'eggs'), array(), $mock);
  117. $this->assertEquals(2, count($result));
  118. $this->assertEquals('eggs', $result[0]->name);
  119. }
  120. public function tearDown()
  121. {
  122. $conf = array('driver' => 'MongoDriver', 'hostname' => 'localhost', 'database' => 'test', 'username' => 'test', 'password' => '1234', 'port' => 27017);
  123. $connection = new Mongo('mongodb://' . $conf['hostname'] . ':' . $conf['port']);
  124. $db = $connection->selectDB($conf['database']);
  125. $db->authenticate($conf['username'], $conf['password']);
  126. $collection = 'mongomock';
  127. $db->dropCollection($collection);
  128. }
  129. protected function newCallback($className)
  130. {
  131. switch ($className) {
  132. case 'CacheKey':
  133. return 'MockCacheKey';
  134. default:
  135. return $className;
  136. }
  137. }
  138. public function dbSetUp($conf)
  139. {
  140. $data = array(
  141. array(
  142. 'name' => 'bread',
  143. 'price' => 3.2,
  144. 'quantity' => 10
  145. ),
  146. array(
  147. 'name' => 'eggs',
  148. 'price' => 2.1,
  149. 'quantity' => 20
  150. ),
  151. array(
  152. 'name' => 'fish',
  153. 'price' => 13.2,
  154. 'quantity' => 2
  155. ),
  156. array(
  157. 'name' => 'milk',
  158. 'price' => 3.8,
  159. 'quantity' => 1
  160. ),
  161. array(
  162. 'name' => 'eggs',
  163. 'price' => 2.3,
  164. 'quantity' => 5
  165. )
  166. );
  167. $connection = new Mongo('mongodb://' . $conf['default']['hostname'] . ':' . $conf['default']['port']);
  168. $db = $connection->selectDB($conf['default']['database']);
  169. $db->authenticate($conf['default']['username'], $conf['default']['password']);
  170. $collection = 'mongomock';
  171. $db->dropCollection($collection);
  172. $collection = $db->selectCollection($collection);
  173. foreach($data as $document) {
  174. $collection->insert($document);
  175. }
  176. }
  177. }