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.

316 lines
9.9 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. /**
  45. * @group Mongo
  46. */
  47. public function testModel()
  48. {
  49. $this->assertInstanceOf('MongoMockModel', $this->model);
  50. }
  51. /**
  52. * @runInSeparateProcess
  53. * @group Mongo
  54. */
  55. public function testFind()
  56. {
  57. if (!defined('DEBUG')) {
  58. define('DEBUG', false);
  59. }
  60. $result = $this->model->find();
  61. $this->assertInstanceOf('MongoStatement', $result);
  62. $this->assertEquals('milk', $result->limit(2)->order(array('name' => -1))->fetch()->name);
  63. $this->assertEquals('fish', $result->fetch()->name);
  64. $result = $this->model->find(array(), array('name'))->fetch();
  65. $this->assertSame('bread', $result->name);
  66. $this->setExpectedException('PHPUnit_Framework_Error');
  67. $this->assertNull($result->price);
  68. }
  69. /**
  70. * @runInSeparateProcess
  71. * @group Mongo
  72. */
  73. public function testGet()
  74. {
  75. if (!defined('DEBUG')) {
  76. define('DEBUG', false);
  77. }
  78. $result = $this->model->find()->limit(1)->order(array('name' => 1));
  79. $result = $result->fetch();
  80. $this->assertEquals('bread', $result->name);
  81. $id = $result->_id;
  82. $this->assertEquals(10, $this->model->get($id)->quantity);
  83. }
  84. /**
  85. * @runInSeparateProcess
  86. * @group Mongo
  87. */
  88. public function testDelete()
  89. {
  90. if (!defined('DEBUG')) {
  91. define('DEBUG', false);
  92. }
  93. $result = $this->model->find()->limit(1)->order(array('name' => 1));
  94. $id = $result->fetch()->_id;
  95. $this->assertEquals(1, $this->model->delete($id));
  96. $this->assertFalse($this->model->find(array('name' => 'bread'))->fetch());
  97. }
  98. /**
  99. * @runInSeparateProcess
  100. * @group Mongo
  101. */
  102. public function testBatchInsert()
  103. {
  104. if (!defined('DEBUG')) {
  105. define('DEBUG', false);
  106. }
  107. $data = array(
  108. array('name' => 'first object'),
  109. array('name' => 'second object'),
  110. array('name' => 'equal object'),
  111. array('name' => 'equal object')
  112. );
  113. $this->model->batchInsert($data);
  114. $this->assertEquals(1, $this->model->count(array('name' => 'first object')));
  115. $this->assertEquals(2, $this->model->count(array('name' => 'equal object')));
  116. }
  117. /**
  118. * @runInSeparateProcess
  119. * @group Mongo
  120. */
  121. public function testDeleteAll()
  122. {
  123. if (!defined('DEBUG')) {
  124. define('DEBUG', false);
  125. }
  126. $this->assertEquals(2, $this->model->count(array('name' => 'eggs')));
  127. $this->assertEquals(0, $this->model->deleteAll(array('name' => 'eggs')));
  128. $this->assertFalse($this->model->find(array('name' => 'eggs'))->fetch());
  129. }
  130. /**
  131. * @runInSeparateProcess
  132. * @group Mongo
  133. */
  134. public function testCount()
  135. {
  136. if (!defined('DEBUG')) {
  137. define('DEBUG', false);
  138. }
  139. $this->assertEquals(5, $this->model->count());
  140. $this->assertEquals(2, $this->model->count(array('name' => 'eggs')));
  141. }
  142. /**
  143. * @runInSeparateProcess
  144. * @group Mongo
  145. */
  146. public function testFetch()
  147. {
  148. if (!defined('DEBUG')) {
  149. define('DEBUG', false);
  150. }
  151. $mock = $this->getMock('CacheKey', array('set', 'get'));
  152. $mock->expects($this->exactly(3))
  153. ->method('set')
  154. ->will($this->returnValue(true));
  155. $mock->expects($this->exactly(3))
  156. ->method('get')
  157. ->will($this->returnValue(false));
  158. $model = new ReflectionClass('MongoModel');
  159. $method = $model->getMethod('fetchField');
  160. $method->setAccessible(true);
  161. $result = $method->invoke($this->model, array('name' => 'milk'), array(), 'quantity', $mock);
  162. $this->assertEquals(1, $result);
  163. $model = new ReflectionClass('MongoModel');
  164. $method = $model->getMethod('fetch');
  165. $method->setAccessible(true);
  166. $result = $method->invoke($this->model, array('name' => 'bread'), array(), $mock);
  167. $this->assertEquals('bread', $result->name);
  168. $model = new ReflectionClass('MongoModel');
  169. $method = $model->getMethod('fetchAll');
  170. $method->setAccessible(true);
  171. $result = $method->invoke($this->model, array('name' => 'eggs'), array(), $mock);
  172. $this->assertEquals(2, count($result));
  173. $this->assertEquals('eggs', $result[0]->name);
  174. }
  175. /**
  176. * @runInSeparateProcess
  177. * @group Mongo
  178. */
  179. public function testUseMongoId()
  180. {
  181. if (!defined('DEBUG')) {
  182. define('DEBUG', false);
  183. }
  184. $this->assertAttributeEquals(true, 'useMongoId', $this->model);
  185. }
  186. /**
  187. * @runInSeparateProcess
  188. * @group Mongo
  189. */
  190. public function testId()
  191. {
  192. if (!defined('DEBUG')) {
  193. define('DEBUG', false);
  194. }
  195. $class = new ReflectionClass('MongoModel');
  196. $prop = $class->getProperty('useMongoId');
  197. $prop->setAccessible(true);
  198. $this->model->insert(array('_id' => 1, 'name' => 'testbread', 'price' => 3.2, 'quantity' => 10));
  199. $this->model->insert(array('_id' => 2, 'name' => 'testbread', 'price' => 12, 'quantity' => 2));
  200. $this->assertSame(2, $this->model->count(array('name' => 'testbread')));
  201. $prop->setValue($this->model, false);
  202. $this->model->delete(1);
  203. $this->assertSame(1, $this->model->count(array('name' => 'testbread')));
  204. }
  205. /**
  206. * @runInSeparateProcess
  207. * @group Mongo
  208. */
  209. public function testIdToMongoId()
  210. {
  211. if (!defined('DEBUG')) {
  212. define('DEBUG', false);
  213. }
  214. $this->model->insert(array('name' => 'testbread', 'price' => 3.2, 'quantity' => 10));
  215. $this->model->insert(array('name' => 'testbread', 'price' => 12, 'quantity' => 2));
  216. $this->assertSame(2, $this->model->count(array('name' => 'testbread')));
  217. $id = $this->model->find(array('name' => 'testbread'))->limit(1)->fetch()->_id->__toString();
  218. $this->assertInternalType('string', $id);
  219. $item = $this->model->get($id);
  220. $this->assertSame('testbread', $item->name);
  221. $this->model->delete($id);
  222. $this->assertSame(1, $this->model->count(array('name' => 'testbread')));
  223. }
  224. public function tearDown()
  225. {
  226. $conf = array('driver' => 'MongoDriver', 'hostname' => 'localhost', 'database' => 'test', 'username' => 'test', 'password' => '1234', 'port' => 27017);
  227. $connection = new Mongo('mongodb://' . $conf['hostname'] . ':' . $conf['port']);
  228. $db = $connection->selectDB($conf['database']);
  229. $db->authenticate($conf['username'], $conf['password']);
  230. $collection = 'mongomock';
  231. $db->dropCollection($collection);
  232. }
  233. protected function newCallback($className)
  234. {
  235. switch ($className) {
  236. case 'CacheKey':
  237. return 'MockCacheKey';
  238. default:
  239. return $className;
  240. }
  241. }
  242. public function dbSetUp($conf)
  243. {
  244. $data = array(
  245. array(
  246. 'name' => 'bread',
  247. 'price' => 3.2,
  248. 'quantity' => 10
  249. ),
  250. array(
  251. 'name' => 'eggs',
  252. 'price' => 2.1,
  253. 'quantity' => 20
  254. ),
  255. array(
  256. 'name' => 'fish',
  257. 'price' => 13.2,
  258. 'quantity' => 2
  259. ),
  260. array(
  261. 'name' => 'milk',
  262. 'price' => 3.8,
  263. 'quantity' => 1
  264. ),
  265. array(
  266. 'name' => 'eggs',
  267. 'price' => 2.3,
  268. 'quantity' => 5
  269. )
  270. );
  271. $connection = new Mongo('mongodb://' . $conf['default']['hostname'] . ':' . $conf['default']['port']);
  272. $db = $connection->selectDB($conf['default']['database']);
  273. $db->authenticate($conf['default']['username'], $conf['default']['password']);
  274. $collection = 'mongomock';
  275. $db->dropCollection($collection);
  276. $collection = $db->selectCollection($collection);
  277. foreach ($data as $document) {
  278. $collection->insert($document);
  279. }
  280. }
  281. }