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.

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