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.
 
 
 
 
 
 

292 lines
8.3 KiB

  1. <?php
  2. /**
  3. * PhpUnderControl_PhalApiDBNotORM_Test
  4. *
  5. * 针对 ../../PhalApi/DB/NotORM.php PhalApi_DB_NotORM 类的PHPUnit单元测试
  6. *
  7. * @author: dogstar 20141122
  8. */
  9. require_once dirname(__FILE__) . '/../test_env.php';
  10. if (!class_exists('PhalApi_DB_NotORM')) {
  11. require dirname(__FILE__) . '/../../PhalApi/DB/NotORM.php';
  12. }
  13. $_GET['__sql__'] = 1;
  14. class PhpUnderControl_PhalApiDBNotORM_Test extends PHPUnit_Framework_TestCase
  15. {
  16. public $notorm;
  17. protected function setUp()
  18. {
  19. parent::setUp();
  20. $this->notorm = new PhalApi_DB_NotORM(DI()->config->get('dbs')/** , true **/);
  21. }
  22. protected function tearDown()
  23. {
  24. }
  25. /**
  26. * @dataProvider provideTable
  27. */
  28. public function testHere($table)
  29. {
  30. $demo = $this->notorm->$table;
  31. $this->assertNotNull($demo);
  32. //var_dump($demo);
  33. $rs = $demo->fetchAll();
  34. //var_dump($rs);
  35. $this->assertNotEmpty($rs);
  36. }
  37. public function provideTable()
  38. {
  39. return array(
  40. array('demo'),
  41. array('demo_0'),
  42. array('demo_1'),
  43. array('demo_3'),
  44. );
  45. }
  46. /**
  47. * @expectedException PhalApi_Exception
  48. */
  49. public function testNoMap()
  50. {
  51. $notorm = new PhalApi_DB_NotORM(array());
  52. $rs = $notorm->demo->fetchAll();
  53. }
  54. public function testNoDbRouter()
  55. {
  56. $rs = $this->notorm->demo->fetchAll();
  57. $this->assertNotEmpty($rs);
  58. }
  59. public function testUseDefaultDbKey()
  60. {
  61. $rs = $this->notorm->demo_10->fetchAll();
  62. $this->assertNotEmpty($rs);
  63. }
  64. public function testMultiSet()
  65. {
  66. $this->notorm->debug = true;
  67. $this->notorm->debug = false;
  68. }
  69. public function testTransactionCommit()
  70. {
  71. //Step 1: 开启事务
  72. $this->notorm->beginTransaction('DB_A');
  73. //Step 2: 数据库操作
  74. $this->notorm->demo->insert(array('name' => 'commit at ' . $_SERVER['REQUEST_TIME']));
  75. $this->notorm->demo->insert(array('name' => 'commit again at ' . $_SERVER['REQUEST_TIME']));
  76. //Step 3: 提交事务
  77. $this->notorm->commit('DB_A');
  78. }
  79. public function testTransactionRollback()
  80. {
  81. //Step 1: 开启事务
  82. $this->notorm->beginTransaction('DB_A');
  83. //Step 2: 数据库操作
  84. $this->notorm->demo->insert(array('name' => 'test rollback'));
  85. //Step 3: 回滚事务
  86. $this->notorm->rollback('DB_A');
  87. $rs = $this->notorm->demo->where('name', 'test rollback')->fetchRow();
  88. $this->assertEmpty($rs);
  89. }
  90. /**
  91. * @expectedException PhalApi_Exception_InternalServerError
  92. */
  93. public function testTransactionException()
  94. {
  95. $this->notorm->beginTransaction('NO_THIS_DB');
  96. }
  97. public function testFetchPairs()
  98. {
  99. $rs = $this->notorm->demo->limit(3)->fetchPairs('id', 'name');
  100. //var_dump($rs);
  101. foreach ($rs as $key => $row) {
  102. $this->assertTrue(is_string($row));
  103. }
  104. $rs = $this->notorm->demo->select('name')->limit(3)->fetchPairs('id');
  105. //var_dump($rs);
  106. foreach ($rs as $key => $row) {
  107. $this->assertTrue(is_array($row));
  108. $this->assertArrayHasKey('name', $row);
  109. }
  110. }
  111. public function testAllAggreation()
  112. {
  113. $rs = $this->notorm->demo->where('id > 10')->count('id');
  114. //var_dump($rs);
  115. $this->assertTrue(is_numeric($rs));
  116. $rs = $this->notorm->demo->where('id > 10')->min('id');
  117. $this->assertTrue(is_numeric($rs));
  118. $rs = $this->notorm->demo->where('id > 10')->max('id');
  119. //var_dump($rs);
  120. $this->assertTrue(is_numeric($rs));
  121. $rs = $this->notorm->demo->where('id > 10')->sum('id');
  122. //var_dump($rs);
  123. $this->assertTrue(is_numeric($rs));
  124. }
  125. public function testLimit()
  126. {
  127. $rs1 = $this->notorm->demo->limit(1, 2)->fetchAll();
  128. $rs2 = $this->notorm->demo->limit('1', 2)->fetchAll();
  129. $rs3 = $this->notorm->demo->limit(1, '2')->fetchAll();
  130. $rs4 = $this->notorm->demo->limit('1', '2')->fetchAll();
  131. //var_dump($rs1);
  132. $this->assertEquals($rs1, $rs2);
  133. $this->assertEquals($rs2, $rs3);
  134. $this->assertEquals($rs3, $rs4);
  135. }
  136. public function testLimitInQueryRows()
  137. {
  138. //int
  139. $sql = 'SELECT * FROM tbl_demo LIMIT :start, :len';
  140. $params = array(':start' => 1, ':len' => 2);
  141. $rows = $this->notorm->demo->queryRows($sql, $params);
  142. //var_dump($rows);
  143. $this->assertNotEmpty($rows);
  144. //not support yet
  145. return;
  146. //string
  147. $params = array(':start' => '1', ':len' => '2');
  148. $rows = $this->notorm->demo->queryRows($sql, $params);
  149. //var_dump($rows);
  150. //int and string
  151. $params = array(':start' => '1', ':len' => 2);
  152. $rows = $this->notorm->demo->queryRows($sql, $params);
  153. //var_dump($rows);
  154. }
  155. public function testHereAgain()
  156. {
  157. $rs = $this->notorm->demo->select('name')->where('id', 1)->fetchRows();
  158. $this->assertNotEmpty($rs);
  159. $rs = $this->notorm->demo->select('name')->where('id = ?', 1)->fetchRows();
  160. $this->assertNotEmpty($rs);
  161. }
  162. public function testQueryRowsWithBoundValuesAndInputOnly()
  163. {
  164. $sql = 'SELECT * FROM tbl_demo WHERE id = ? OR id = ?';
  165. $params = array(1, 2);
  166. $rows1 = $this->notorm->demo->queryRows($sql, $params);
  167. //var_dump($rows1);
  168. $sql = 'SELECT * FROM tbl_demo WHERE id = :id1 OR id = :id2';
  169. $params = array(':id2' => 2, ':id1' => 1);
  170. $rows2 = $this->notorm->demo->queryRows($sql, $params);
  171. $this->assertEquals($rows1, $rows2);
  172. //兼容不连续的下标
  173. $sql = 'SELECT * FROM tbl_demo WHERE id = ? OR id = ?';
  174. $params = array(5 => 1, 9 => 2);
  175. $rows3 = $this->notorm->demo->queryRows($sql, $params);
  176. $this->assertEquals($rows1, $rows3);
  177. //should not use in this way
  178. $sql = 'SELECT * FROM tbl_demo WHERE id = ? OR id = :id';
  179. $params = array(1, ':id' => 2);
  180. //$rows = $this->notorm->demo->queryRows($sql, $params);
  181. }
  182. public function testParametersMixed()
  183. {
  184. $sql = "SELECT * FROM tbl_user WHERE name LIKE ? AND create_date >= '2015-10-1 10:00:00' AND create_date < '2015-12-31 10:00:00'";
  185. $params = array('%a%');
  186. $rows = $this->notorm->demo->queryRows($sql, $params);
  187. $this->assertNotEmpty($rows);
  188. }
  189. public function testNoKeyIndexAgain()
  190. {
  191. $notorm = new PhalApi_DB_NotORM(DI()->config->get('dbs')/** , true **/);
  192. $rs = $notorm->demo->order('id DESC')->limit(1, 2)->fetchAll();
  193. //var_dump($rs);
  194. //echo (json_encode($rs)), "\n\n";
  195. $keys = array_keys($rs);
  196. $this->assertEquals(0, $keys[0]);
  197. $notorm = new PhalApi_DB_NotORM(DI()->config->get('dbs')/** , true **/);
  198. $notorm->keepPrimaryKeyIndex();
  199. $rs = $notorm->demo->order('id DESC')->limit(1, 2)->fetchAll();
  200. //var_dump($rs);
  201. //echo (json_encode($rs)), "\n\n";
  202. $keys = array_keys($keys);
  203. $this->assertGreaterThan($keys[0], $keys[1]);
  204. $this->assertEquals(0, $keys[0]);
  205. $this->assertEquals(1, $keys[1]);
  206. }
  207. public function testInsertMulti()
  208. {
  209. $rows = array(
  210. array('name' => 'A君', 'age' => 12, 'note' => 'AA'),
  211. array('name' => 'B君', 'age' => 14, 'note' => 'BB'),
  212. array('name' => 'C君', 'age' => 16, 'note' => 'CC'),
  213. );
  214. $rs = $this->notorm->user->insert_multi($rows);
  215. }
  216. public function testFetchNothing()
  217. {
  218. $rs = $this->notorm->user->where('id', 4040404)->fetch();
  219. $this->assertFalse($rs);
  220. $rs = $this->notorm->user->where('id', 4040404)->fetchOne();
  221. $this->assertFalse($rs);
  222. $rs = $this->notorm->user->where('id', 4040404)->fetchAll();
  223. $this->assertEquals(array(), $rs);
  224. }
  225. public function testDisConnect()
  226. {
  227. // first, none to disconnect
  228. $this->notorm->disconnect();
  229. // second, disconnect after some query
  230. $rs = $this->notorm->user->where('id', 4040404)->fetch();
  231. $this->notorm->disconnect();
  232. // again
  233. $rs = $this->notorm->user->where('id', 4040404)->fetch();
  234. $this->notorm->disconnect();
  235. $this->notorm->disconnect();
  236. }
  237. }