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.

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