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.

229 lines
7.1 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 testDeleteAll()
  92. {
  93. if (!defined('DEBUG')) {
  94. define('DEBUG', false);
  95. }
  96. $this->assertEquals(2, $this->model->count(array('name' => 'eggs')));
  97. $this->assertEquals(0, $this->model->deleteAll(array('name' => 'eggs')));
  98. $this->assertFalse($this->model->find(array('name' => 'eggs'))->fetch());
  99. }
  100. /**
  101. * @runInSeparateProcess
  102. */
  103. public function testCount()
  104. {
  105. if (!defined('DEBUG')) {
  106. define('DEBUG', false);
  107. }
  108. $this->assertEquals(5, $this->model->count());
  109. $this->assertEquals(2, $this->model->count(array('name' => 'eggs')));
  110. }
  111. /**
  112. * @runInSeparateProcess
  113. */
  114. public function testFetch()
  115. {
  116. if (!defined('DEBUG')) {
  117. define('DEBUG', false);
  118. }
  119. $mock = $this->getMock('CacheKey', array('set', 'get'));
  120. $mock->expects($this->exactly(3))
  121. ->method('set')
  122. ->will($this->returnValue(true));
  123. $mock->expects($this->exactly(3))
  124. ->method('get')
  125. ->will($this->returnValue(false));
  126. $model = new ReflectionClass('MongoModel');
  127. $method = $model->getMethod('fetchField');
  128. $method->setAccessible(true);
  129. $result = $method->invoke($this->model, array('name' => 'milk'), array(), 'quantity', $mock);
  130. $this->assertEquals(1, $result);
  131. $model = new ReflectionClass('MongoModel');
  132. $method = $model->getMethod('fetch');
  133. $method->setAccessible(true);
  134. $result = $method->invoke($this->model, array('name' => 'bread'), array(), $mock);
  135. $this->assertEquals('bread', $result->name);
  136. $model = new ReflectionClass('MongoModel');
  137. $method = $model->getMethod('fetchAll');
  138. $method->setAccessible(true);
  139. $result = $method->invoke($this->model, array('name' => 'eggs'), array(), $mock);
  140. $this->assertEquals(2, count($result));
  141. $this->assertEquals('eggs', $result[0]->name);
  142. }
  143. public function tearDown()
  144. {
  145. $conf = array('driver' => 'MongoDriver', 'hostname' => 'localhost', 'database' => 'test', 'username' => 'test', 'password' => '1234', 'port' => 27017);
  146. $connection = new Mongo('mongodb://' . $conf['hostname'] . ':' . $conf['port']);
  147. $db = $connection->selectDB($conf['database']);
  148. $db->authenticate($conf['username'], $conf['password']);
  149. $collection = 'mongomock';
  150. $db->dropCollection($collection);
  151. }
  152. protected function newCallback($className)
  153. {
  154. switch ($className) {
  155. case 'CacheKey':
  156. return 'MockCacheKey';
  157. default:
  158. return $className;
  159. }
  160. }
  161. public function dbSetUp($conf)
  162. {
  163. $data = array(
  164. array(
  165. 'name' => 'bread',
  166. 'price' => 3.2,
  167. 'quantity' => 10
  168. ),
  169. array(
  170. 'name' => 'eggs',
  171. 'price' => 2.1,
  172. 'quantity' => 20
  173. ),
  174. array(
  175. 'name' => 'fish',
  176. 'price' => 13.2,
  177. 'quantity' => 2
  178. ),
  179. array(
  180. 'name' => 'milk',
  181. 'price' => 3.8,
  182. 'quantity' => 1
  183. ),
  184. array(
  185. 'name' => 'eggs',
  186. 'price' => 2.3,
  187. 'quantity' => 5
  188. )
  189. );
  190. $connection = new Mongo('mongodb://' . $conf['default']['hostname'] . ':' . $conf['default']['port']);
  191. $db = $connection->selectDB($conf['default']['database']);
  192. $db->authenticate($conf['default']['username'], $conf['default']['password']);
  193. $collection = 'mongomock';
  194. $db->dropCollection($collection);
  195. $collection = $db->selectCollection($collection);
  196. foreach($data as $document) {
  197. $collection->insert($document);
  198. }
  199. }
  200. }