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.

245 lines
7.8 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. $this->assertSame(' ORDER BY name ASC', $method->invoke($this->model, array('sort' => 'name', 'order' => 'DESC'), array('id', 'name')));
  80. }
  81. public function testSearch()
  82. {
  83. $model = new ReflectionClass('SqlModel');
  84. $method = $model->getMethod('search');
  85. $method->setAccessible(true);
  86. $this->assertEmpty($method->invoke($this->model, array()));
  87. $this->assertEmpty($method->invoke($this->model, array('q' => 'in', 'qt' => 'name')));
  88. $this->assertSame('test.name LIKE %on%', $method->invoke($this->model, array('qt' => 'name', 'q' => 'on'), array('name'), 'test'));
  89. }
  90. public function testFetch()
  91. {
  92. $model = new ReflectionClass('SqlModel');
  93. $method = $model->getMethod('fetch');
  94. $method->setAccessible(true);
  95. $key = $this->getCacheKeyMockGetSet();
  96. $this->assertTrue(true, $method->invoke($this->model, 'SELECT', array(), $key));
  97. }
  98. public function testFetchField()
  99. {
  100. $model = new ReflectionClass('SqlModel');
  101. $method = $model->getMethod('fetchField');
  102. $method->setAccessible(true);
  103. $key = $this->getCacheKeyMockGetSet();
  104. $this->assertSame('field', $method->invoke($this->model, 'SELECT', array(), 'field', $key));
  105. }
  106. public function testFetchAll()
  107. {
  108. $model = new ReflectionClass('SqlModel');
  109. $method = $model->getMethod('fetchAll');
  110. $method->setAccessible(true);
  111. $key = $this->getCacheKeyMockGetSet();
  112. $this->assertTrue(true, $method->invoke($this->model, 'SELECT', array(), $key));
  113. }
  114. public function testGetCache()
  115. {
  116. $model = new ReflectionClass('MockModel');
  117. $method = $model->getMethod('getCache');
  118. $method->setAccessible(true);
  119. Config::set('Model', 'MockCache');
  120. if (!class_exists('MockCache')) {
  121. $this->getMock('Cache', array(), array(), 'MockCache');
  122. }
  123. $this->assertInstanceOf('MockCache', $method->invoke($this->model));
  124. }
  125. public function testCacheKey()
  126. {
  127. $model = new ReflectionClass('MockModel');
  128. $method = $model->getMethod('cacheKey');
  129. $method->setAccessible(true);
  130. Config::set('Model', 'MockCache');
  131. if (!class_exists('MockCache')) {
  132. $this->getMock('Cache', array(), array(), 'MockCache', false);
  133. }
  134. if (!class_exists('MockCacheKey')) {
  135. $this->getMock('CacheKey', array(), array(), 'MockCacheKey', false);
  136. }
  137. $this->assertInstanceOf('MockCacheKey', $method->invoke($this->model, 'name'));
  138. }
  139. public function testAddCleanCache()
  140. {
  141. $model = new ReflectionClass('SqlModel');
  142. $method = $model->getMethod('addCleanCache');
  143. $method->setAccessible(true);
  144. $key = $this->getMock('Key', array('set', 'get'));
  145. $method->invoke($this->model, $key);
  146. $method->invoke($this->model, $key);
  147. $method->invoke($this->model, $key);
  148. $this->assertAttributeEquals(array($key, $key, $key), 'caches_clean', $this->model);
  149. }
  150. public function testCleanCaches()
  151. {
  152. $model = new ReflectionClass('SqlModel');
  153. $method = $model->getMethod('addCleanCache');
  154. $method->setAccessible(true);
  155. $key = $this->getCacheKeyMockDel(3);
  156. $method->invoke($this->model, $key);
  157. $method->invoke($this->model, $key);
  158. $method->invoke($this->model, $key);
  159. $this->assertAttributeEquals(array($key, $key, $key), 'caches_clean', $this->model);
  160. $model = new ReflectionClass('SqlModel');
  161. $method = $model->getMethod('cleanCaches');
  162. $method->setAccessible(true);
  163. $method->invoke($this->model, $key);
  164. $this->assertAttributeEquals(array(), 'caches_clean', $this->model);
  165. }
  166. protected function getCacheKeyMockGetSet()
  167. {
  168. $key = $this->getMock('Key', array('set', 'get'));
  169. $key
  170. ->expects($this->any())
  171. ->method('set')
  172. ->with($this->anything())
  173. ->will($this->returnValue(true));
  174. $key
  175. ->expects($this->any())
  176. ->method('get')
  177. ->will($this->returnValue(false));
  178. return $key;
  179. }
  180. protected function getCacheKeyMockDel($count)
  181. {
  182. $key = $this->getMock('Key', array('del'));
  183. $key
  184. ->expects($this->exactly($count))
  185. ->method('del')
  186. ->will($this->returnValue(true));
  187. return $key;
  188. }
  189. public function tearDown()
  190. {
  191. Config::getInstance()->offsetUnset('Db');
  192. $config = new ReflectionClass('Db');
  193. $registry = $config->getProperty('connections');
  194. $registry->setAccessible(true);
  195. $registry->setValue('Db', array());
  196. unset_new_overload();
  197. }
  198. protected function newCallback($className)
  199. {
  200. switch ($className) {
  201. case 'MockDbDriver':
  202. return 'MockDbDriver';
  203. case 'CacheKey':
  204. return 'MockCacheKey';
  205. default:
  206. return $className;
  207. }
  208. }
  209. }