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.

312 lines
9.0 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 Model 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/DbDriver.php';
  17. require_once dirname(__FILE__) . '/../../model/Model.php';
  18. class ModelTest extends PHPUnit_Framework_TestCase
  19. {
  20. private $model;
  21. public function setUp()
  22. {
  23. $conf = array('default' => array('driver' => 'MockDbDriver', 'hostname' => 'localhost', 'database' => 'db', 'username' => 'test', 'password' => '1234'));
  24. if (!class_exists('MockDbDriver')) {
  25. $this->getMockForAbstractClass('MyDbDriver', array($conf), 'MockDbDriver', false);
  26. }
  27. Config::set('Db', $conf);
  28. if (!class_exists('MockModel')) {
  29. $this->model = $this->getMockForAbstractClass('Model', array(), 'MockModel');
  30. } else {
  31. $this->model = new MockModel();
  32. }
  33. set_new_overload(array($this, 'newCallback'));
  34. }
  35. public function testModel()
  36. {
  37. $this->assertInstanceOf('Model', $this->model);
  38. }
  39. public function testGetInsertId()
  40. {
  41. $this->assertTrue($this->model->getInsertId());
  42. }
  43. public function testIdentify()
  44. {
  45. $param = 'param';
  46. $this->assertEquals($param, $this->model->identify($param));
  47. }
  48. public function testQuote()
  49. {
  50. $param = 'param';
  51. $this->assertEquals($param, $this->model->quote($param));
  52. }
  53. public function testGet()
  54. {
  55. $this->assertTrue($this->model->get(1));
  56. }
  57. public function testInsert()
  58. {
  59. $this->assertTrue($this->model->insert(array('data')));
  60. }
  61. public function testUpdate()
  62. {
  63. $this->assertEquals('mock', $this->model->update(array('var' => 'val'), 1));
  64. }
  65. public function testDelete()
  66. {
  67. $this->assertEquals('mock', $this->model->delete(1));
  68. }
  69. public function testOrder()
  70. {
  71. $model = new ReflectionClass('Model');
  72. $method = $model->getMethod('order');
  73. $method->setAccessible(true);
  74. $this->assertEquals(' ORDER BY id DESC', $method->invoke($this->model, array('sort' => 'id', 'order' => 'desc')));
  75. $this->assertEquals(' ORDER BY name ASC', $method->invoke($this->model, array('sort' => 'name'), array('id', 'name')));
  76. $this->assertEmpty($method->invoke($this->model, array()));
  77. /**
  78. * @TODO: Model::order - check DESC condition - make case insensitive
  79. */
  80. $this->assertEquals(' ORDER BY name ASC', $method->invoke($this->model, array('sort' => 'name', 'order' => 'DESC'), array('id', 'name')));
  81. }
  82. public function testSearch()
  83. {
  84. $model = new ReflectionClass('Model');
  85. $method = $model->getMethod('search');
  86. $method->setAccessible(true);
  87. $this->assertEmpty($method->invoke($this->model, array()));
  88. $this->assertEmpty($method->invoke($this->model, array('q' => 'in', 'qt' => 'name')));
  89. $this->assertEquals('test.name LIKE %on%', $method->invoke($this->model, array('qt' => 'name', 'q' => 'on'), array('name'), 'test'));
  90. }
  91. public function testFetch()
  92. {
  93. $model = new ReflectionClass('Model');
  94. $method = $model->getMethod('fetch');
  95. $method->setAccessible(true);
  96. $key = $this->getCacheKeyMockGetSet();
  97. //$debug = print_r($this->model, true);
  98. //throw new Exception($debug);
  99. $this->assertEquals('field', $method->invoke($this->model, 'SELECT', array(), $key));
  100. }
  101. public function testFetchField()
  102. {
  103. $model = new ReflectionClass('Model');
  104. $method = $model->getMethod('fetchField');
  105. $method->setAccessible(true);
  106. $key = $this->getCacheKeyMockGetSet();
  107. $this->assertEquals('field', $method->invoke($this->model, 'SELECT', array(), 'field', $key));
  108. }
  109. public function testFetchAll()
  110. {
  111. $model = new ReflectionClass('Model');
  112. $method = $model->getMethod('fetchAll');
  113. $method->setAccessible(true);
  114. $key = $this->getCacheKeyMockGetSet();
  115. $this->assertEquals('field', $method->invoke($this->model, 'SELECT', array(), $key));
  116. }
  117. public function testGetCache()
  118. {
  119. $model = new ReflectionClass('MockModel');
  120. $method = $model->getMethod('getCache');
  121. $method->setAccessible(true);
  122. Config::set('Model', 'MockCache');
  123. if (!class_exists('MockCache')) {
  124. $this->getMock('Cache', array(), array(), 'MockCache');
  125. }
  126. $this->assertInstanceOf('MockCache', $method->invoke($this->model));
  127. }
  128. public function testCacheKey()
  129. {
  130. $model = new ReflectionClass('MockModel');
  131. $method = $model->getMethod('cacheKey');
  132. $method->setAccessible(true);
  133. Config::set('Model', 'MockCache');
  134. if (!class_exists('MockCache')) {
  135. $this->getMock('Cache', array(), array(), 'MockCache', false);
  136. }
  137. if (!class_exists('MockCacheKey')) {
  138. $this->getMock('CacheKey', array(), array(), 'MockCacheKey', false);
  139. }
  140. $this->assertInstanceOf('MockCacheKey', $method->invoke($this->model, 'name'));
  141. }
  142. public function testAddCleanCache()
  143. {
  144. $model = new ReflectionClass('Model');
  145. $method = $model->getMethod('addCleanCache');
  146. $method->setAccessible(true);
  147. $key = $this->getMock('Key', array('set', 'get'));
  148. $method->invoke($this->model, $key);
  149. $method->invoke($this->model, $key);
  150. $method->invoke($this->model, $key);
  151. $this->assertAttributeEquals(array($key, $key, $key), 'caches_clean', $this->model);
  152. }
  153. public function testCleanCaches()
  154. {
  155. $model = new ReflectionClass('Model');
  156. $method = $model->getMethod('addCleanCache');
  157. $method->setAccessible(true);
  158. $key = $this->getCacheKeyMockDel(3);
  159. $method->invoke($this->model, $key);
  160. $method->invoke($this->model, $key);
  161. $method->invoke($this->model, $key);
  162. $this->assertAttributeEquals(array($key, $key, $key), 'caches_clean', $this->model);
  163. $model = new ReflectionClass('Model');
  164. $method = $model->getMethod('cleanCaches');
  165. $method->setAccessible(true);
  166. $method->invoke($this->model, $key);
  167. $this->assertAttributeEquals(array(), 'caches_clean', $this->model);
  168. }
  169. protected function getCacheKeyMockGetSet()
  170. {
  171. $key = $this->getMock('Key', array('set', 'get'));
  172. $key
  173. ->expects($this->any())
  174. ->method('set')
  175. ->with($this->anything())
  176. ->will($this->returnValue(true));
  177. $key
  178. ->expects($this->any())
  179. ->method('get')
  180. ->will($this->returnValue(false));
  181. return $key;
  182. }
  183. protected function getCacheKeyMockDel($count)
  184. {
  185. $key = $this->getMock('Key', array('del'));
  186. $key
  187. ->expects($this->exactly($count))
  188. ->method('del')
  189. ->will($this->returnValue(true));
  190. return $key;
  191. }
  192. public function tearDown()
  193. {
  194. Config::getInstance()->offsetUnset('Db');
  195. $config = new ReflectionClass('Db');
  196. $registry = $config->getProperty('connections');
  197. $registry->setAccessible(true);
  198. $registry->setValue('Db', array());
  199. unset_new_overload();
  200. }
  201. protected function newCallback($className)
  202. {
  203. switch ($className) {
  204. case 'MockDbDriver':
  205. return 'MockDbDriver';
  206. case 'CacheKey':
  207. return 'MockCacheKey';
  208. default:
  209. return $className;
  210. }
  211. }
  212. }
  213. abstract class MyDbDriver extends DbDriver
  214. {
  215. public function getInsertId($table = null, $key = null)
  216. {
  217. return true;
  218. }
  219. public function quoteIdentifier($param)
  220. {
  221. return $param;
  222. }
  223. public function quote($param)
  224. {
  225. return $param;
  226. }
  227. public function insert($table, $bind, $on_duplicate = array())
  228. {
  229. return $table;
  230. }
  231. public function update($table, $bind, $where = '')
  232. {
  233. return $table;
  234. }
  235. public function delete($table, $where = '')
  236. {
  237. return $table;
  238. }
  239. public function query($sql, $params = array())
  240. {
  241. $conf = array('driver' => 'MockDbDriver', 'hostname' => 'localhost', 'database' => 'db', 'username' => 'test', 'password' => '1234');
  242. return new MockDbDriver($conf);
  243. }
  244. public function execute()
  245. {
  246. return true;
  247. }
  248. public function fetch()
  249. {
  250. return true;
  251. }
  252. public function fetchField($field)
  253. {
  254. return $field;
  255. }
  256. public function fetchAll()
  257. {
  258. return true;
  259. }
  260. }