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.

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