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.

306 lines
8.9 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. $this->assertEquals('field', $method->invoke($this->model, 'SELECT', array(), $key));
  98. }
  99. public function testFetchField()
  100. {
  101. $model = new ReflectionClass('Model');
  102. $method = $model->getMethod('fetchField');
  103. $method->setAccessible(true);
  104. $key = $this->getCacheKeyMockGetSet();
  105. $this->assertEquals('field', $method->invoke($this->model, 'SELECT', array(), 'field', $key));
  106. }
  107. public function testFetchAll()
  108. {
  109. $model = new ReflectionClass('Model');
  110. $method = $model->getMethod('fetchAll');
  111. $method->setAccessible(true);
  112. $key = $this->getCacheKeyMockGetSet();
  113. $this->assertEquals('field', $method->invoke($this->model, 'SELECT', array(), $key));
  114. }
  115. public function testGetCache()
  116. {
  117. $model = new ReflectionClass('MockModel');
  118. $method = $model->getMethod('getCache');
  119. $method->setAccessible(true);
  120. Config::set('Model', 'MockCache');
  121. if (!class_exists('MockCache')) {
  122. $this->getMock('Cache', array(), array(), 'MockCache');
  123. }
  124. $this->assertInstanceOf('MockCache', $method->invoke($this->model));
  125. }
  126. public function testCacheKey()
  127. {
  128. $model = new ReflectionClass('MockModel');
  129. $method = $model->getMethod('cacheKey');
  130. $method->setAccessible(true);
  131. Config::set('Model', 'MockCache');
  132. if (!class_exists('MockCache')) {
  133. $this->getMock('Cache', array(), array(), 'MockCache', false);
  134. }
  135. if (!class_exists('MockCacheKey')) {
  136. $this->getMock('CacheKey', array(), array(), 'MockCacheKey', false);
  137. }
  138. $this->assertInstanceOf('MockCacheKey', $method->invoke($this->model, 'name'));
  139. }
  140. public function testAddCleanCache()
  141. {
  142. $model = new ReflectionClass('Model');
  143. $method = $model->getMethod('addCleanCache');
  144. $method->setAccessible(true);
  145. $key = $this->getMock('Key', array('set', 'get'));
  146. $method->invoke($this->model, $key);
  147. $method->invoke($this->model, $key);
  148. $method->invoke($this->model, $key);
  149. $this->assertAttributeEquals(array($key, $key, $key), 'caches_clean', $this->model);
  150. }
  151. public function testCleanCaches()
  152. {
  153. $model = new ReflectionClass('Model');
  154. $method = $model->getMethod('addCleanCache');
  155. $method->setAccessible(true);
  156. $key = $this->getCacheKeyMockDel(3);
  157. $method->invoke($this->model, $key);
  158. $method->invoke($this->model, $key);
  159. $method->invoke($this->model, $key);
  160. $this->assertAttributeEquals(array($key, $key, $key), 'caches_clean', $this->model);
  161. $model = new ReflectionClass('Model');
  162. $method = $model->getMethod('cleanCaches');
  163. $method->setAccessible(true);
  164. $method->invoke($this->model, $key);
  165. $this->assertAttributeEquals(array(), 'caches_clean', $this->model);
  166. }
  167. protected function getCacheKeyMockGetSet()
  168. {
  169. $key = $this->getMock('Key', array('set', 'get'));
  170. $key
  171. ->expects($this->any())
  172. ->method('set')
  173. ->with($this->anything())
  174. ->will($this->returnValue(true));
  175. $key
  176. ->expects($this->any())
  177. ->method('get')
  178. ->will($this->returnValue(false));
  179. return $key;
  180. }
  181. protected function getCacheKeyMockDel($count)
  182. {
  183. $key = $this->getMock('Key', array('del'));
  184. $key
  185. ->expects($this->exactly($count))
  186. ->method('del')
  187. ->will($this->returnValue(true));
  188. return $key;
  189. }
  190. public function tearDown()
  191. {
  192. Config::getInstance()->offsetUnset('Db');
  193. $config = new ReflectionClass('Db');
  194. $registry = $config->getProperty('connections');
  195. $registry->setAccessible(true);
  196. $registry->setValue('Db', array());
  197. unset_new_overload();
  198. }
  199. protected function newCallback($className)
  200. {
  201. switch ($className) {
  202. case 'MockDbDriver':
  203. return 'MockDbDriver';
  204. case 'CacheKey':
  205. return 'MockCacheKey';
  206. default:
  207. return $className;
  208. }
  209. }
  210. }
  211. abstract class MyDbDriver extends DbDriver
  212. {
  213. public function getInsertId($table = null, $key = null)
  214. {
  215. return true;
  216. }
  217. public function quoteIdentifier($param)
  218. {
  219. return $param;
  220. }
  221. public function quote($param)
  222. {
  223. return $param;
  224. }
  225. public function insert($table, $bind, $on_duplicate = array())
  226. {
  227. return $table;
  228. }
  229. public function update($table, $bind, $where = '')
  230. {
  231. return $table;
  232. }
  233. public function delete($table, $where = '')
  234. {
  235. return $table;
  236. }
  237. public function query($sql, $params)
  238. {
  239. $conf = array('driver' => 'MockDbDriver', 'hostname' => 'localhost', 'database' => 'db', 'username' => 'test', 'password' => '1234');
  240. return new MockDbDriver($conf);
  241. }
  242. public function execute()
  243. {
  244. return true;
  245. }
  246. public function fetch()
  247. {
  248. return true;
  249. }
  250. public function fetchField($field)
  251. {
  252. return $field;
  253. }
  254. public function fetchAll()
  255. {
  256. return true;
  257. }
  258. }