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.

296 lines
9.3 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 testDeleteAll()
  103. {
  104. if (!defined('DEBUG')) {
  105. define('DEBUG', false);
  106. }
  107. $this->assertEquals(2, $this->model->count(array('name' => 'eggs')));
  108. $this->assertEquals(0, $this->model->deleteAll(array('name' => 'eggs')));
  109. $this->assertFalse($this->model->find(array('name' => 'eggs'))->fetch());
  110. }
  111. /**
  112. * @runInSeparateProcess
  113. * @group Mongo
  114. */
  115. public function testCount()
  116. {
  117. if (!defined('DEBUG')) {
  118. define('DEBUG', false);
  119. }
  120. $this->assertEquals(5, $this->model->count());
  121. $this->assertEquals(2, $this->model->count(array('name' => 'eggs')));
  122. }
  123. /**
  124. * @runInSeparateProcess
  125. * @group Mongo
  126. */
  127. public function testFetch()
  128. {
  129. if (!defined('DEBUG')) {
  130. define('DEBUG', false);
  131. }
  132. $mock = $this->getMock('CacheKey', array('set', 'get'));
  133. $mock->expects($this->exactly(3))
  134. ->method('set')
  135. ->will($this->returnValue(true));
  136. $mock->expects($this->exactly(3))
  137. ->method('get')
  138. ->will($this->returnValue(false));
  139. $model = new ReflectionClass('MongoModel');
  140. $method = $model->getMethod('fetchField');
  141. $method->setAccessible(true);
  142. $result = $method->invoke($this->model, array('name' => 'milk'), array(), 'quantity', $mock);
  143. $this->assertEquals(1, $result);
  144. $model = new ReflectionClass('MongoModel');
  145. $method = $model->getMethod('fetch');
  146. $method->setAccessible(true);
  147. $result = $method->invoke($this->model, array('name' => 'bread'), array(), $mock);
  148. $this->assertEquals('bread', $result->name);
  149. $model = new ReflectionClass('MongoModel');
  150. $method = $model->getMethod('fetchAll');
  151. $method->setAccessible(true);
  152. $result = $method->invoke($this->model, array('name' => 'eggs'), array(), $mock);
  153. $this->assertEquals(2, count($result));
  154. $this->assertEquals('eggs', $result[0]->name);
  155. }
  156. /**
  157. * @runInSeparateProcess
  158. * @group Mongo
  159. */
  160. public function testUseMongoId()
  161. {
  162. if (!defined('DEBUG')) {
  163. define('DEBUG', false);
  164. }
  165. $this->assertAttributeEquals(true, 'useMongoId', $this->model);
  166. }
  167. /**
  168. * @runInSeparateProcess
  169. * @group Mongo
  170. */
  171. public function testId()
  172. {
  173. if (!defined('DEBUG')) {
  174. define('DEBUG', false);
  175. }
  176. $class = new ReflectionClass('MongoModel');
  177. $prop = $class->getProperty('useMongoId');
  178. $prop->setAccessible(true);
  179. $this->model->insert(array('_id' => 1, 'name' => 'testbread', 'price' => 3.2, 'quantity' => 10));
  180. $this->model->insert(array('_id' => 2, 'name' => 'testbread', 'price' => 12, 'quantity' => 2));
  181. $this->assertSame(2, $this->model->count(array('name' => 'testbread')));
  182. $prop->setValue($this->model, false);
  183. $this->model->delete(1);
  184. $this->assertSame(1, $this->model->count(array('name' => 'testbread')));
  185. }
  186. /**
  187. * @runInSeparateProcess
  188. * @group Mongo
  189. */
  190. public function testIdToMongoId()
  191. {
  192. if (!defined('DEBUG')) {
  193. define('DEBUG', false);
  194. }
  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. }