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.

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