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.

248 lines
7.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__) . '/MyDbDriver.php';
  18. require_once dirname(__FILE__) . '/../../model/Model.php';
  19. require_once dirname(__FILE__) . '/../../model/SqlModel.php';
  20. class SqlModelTest extends PHPUnit_Framework_TestCase
  21. {
  22. private $model;
  23. public function setUp()
  24. {
  25. $conf = array('default' => array('driver' => 'MockDbDriver', 'hostname' => 'localhost', 'database' => 'db', 'username' => 'test', 'password' => '1234'));
  26. if (!class_exists('MockDbDriver')) {
  27. $this->getMockForAbstractClass('MyDbDriver', array($conf), 'MockDbDriver', false);
  28. }
  29. Config::set('Db', $conf);
  30. if (!class_exists('MockModel')) {
  31. $this->model = $this->getMockForAbstractClass('SqlModel', array(), 'MockModel');
  32. } else {
  33. $this->model = new MockModel();
  34. }
  35. set_new_overload(array($this, 'newCallback'));
  36. }
  37. public function testModel()
  38. {
  39. $this->assertInstanceOf('SqlModel', $this->model);
  40. }
  41. public function testGetInsertId()
  42. {
  43. $this->assertTrue($this->model->getInsertId());
  44. }
  45. public function testIdentify()
  46. {
  47. $param = 'param';
  48. $this->assertSame($param, $this->model->identify($param));
  49. }
  50. public function testQuote()
  51. {
  52. $param = 'param';
  53. $this->assertSame($param, $this->model->quote($param));
  54. }
  55. public function testGet()
  56. {
  57. $this->assertTrue($this->model->get(1));
  58. }
  59. public function testInsert()
  60. {
  61. $this->assertTrue($this->model->insert(array('data')));
  62. }
  63. public function testUpdate()
  64. {
  65. $this->assertSame('mock', $this->model->update(array('var' => 'val'), 1));
  66. }
  67. public function testDelete()
  68. {
  69. $this->assertSame('mock', $this->model->delete(1));
  70. }
  71. public function testOrder()
  72. {
  73. $model = new ReflectionClass('SqlModel');
  74. $method = $model->getMethod('order');
  75. $method->setAccessible(true);
  76. $this->assertSame(' ORDER BY id DESC', $method->invoke($this->model, array('sort' => 'id', 'order' => 'desc')));
  77. $this->assertSame(' ORDER BY name ASC', $method->invoke($this->model, array('sort' => 'name'), array('id', 'name')));
  78. $this->assertEmpty($method->invoke($this->model, array()));
  79. /**
  80. * @TODO: Model::order - check DESC condition - make case insensitive
  81. */
  82. $this->assertSame(' ORDER BY name ASC', $method->invoke($this->model, array('sort' => 'name', 'order' => 'DESC'), array('id', 'name')));
  83. }
  84. public function testSearch()
  85. {
  86. $model = new ReflectionClass('SqlModel');
  87. $method = $model->getMethod('search');
  88. $method->setAccessible(true);
  89. $this->assertEmpty($method->invoke($this->model, array()));
  90. $this->assertEmpty($method->invoke($this->model, array('q' => 'in', 'qt' => 'name')));
  91. $this->assertSame('test.name LIKE %on%', $method->invoke($this->model, array('qt' => 'name', 'q' => 'on'), array('name'), 'test'));
  92. }
  93. public function testFetch()
  94. {
  95. $model = new ReflectionClass('SqlModel');
  96. $method = $model->getMethod('fetch');
  97. $method->setAccessible(true);
  98. $key = $this->getCacheKeyMockGetSet();
  99. $this->assertTrue(true, $method->invoke($this->model, 'SELECT', array(), $key));
  100. }
  101. public function testFetchField()
  102. {
  103. $model = new ReflectionClass('SqlModel');
  104. $method = $model->getMethod('fetchField');
  105. $method->setAccessible(true);
  106. $key = $this->getCacheKeyMockGetSet();
  107. $this->assertSame('field', $method->invoke($this->model, 'SELECT', array(), 'field', $key));
  108. }
  109. public function testFetchAll()
  110. {
  111. $model = new ReflectionClass('SqlModel');
  112. $method = $model->getMethod('fetchAll');
  113. $method->setAccessible(true);
  114. $key = $this->getCacheKeyMockGetSet();
  115. $this->assertTrue(true, $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('SqlModel');
  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('SqlModel');
  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('SqlModel');
  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. }