dbTest.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | ThinkPHP [ WE CAN DO IT JUST THINK ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2006~2016 http://thinkphp.cn All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
  8. // +----------------------------------------------------------------------
  9. // | Author: liu21st <liu21st@gmail.com>
  10. // +----------------------------------------------------------------------
  11. /**
  12. * Db类测试
  13. * @author: 刘志淳 <chun@engineer.com>
  14. */
  15. namespace tests\thinkphp\library\think;
  16. use think\Db;
  17. class dbTest extends \PHPUnit_Framework_TestCase
  18. {
  19. // 获取测试数据库配置
  20. private function getConfig()
  21. {
  22. return [
  23. // 数据库类型
  24. 'type' => 'mysql',
  25. // 服务器地址
  26. 'hostname' => '127.0.0.1',
  27. // 数据库名
  28. 'database' => 'test',
  29. // 用户名
  30. 'username' => 'root',
  31. // 密码
  32. 'password' => '',
  33. // 端口
  34. 'hostport' => '',
  35. // 连接dsn
  36. 'dsn' => '',
  37. // 数据库连接参数
  38. 'params' => [],
  39. // 数据库编码默认采用utf8
  40. 'charset' => 'utf8',
  41. // 数据库表前缀
  42. 'prefix' => 'tp_',
  43. // 数据库调试模式
  44. 'debug' => true,
  45. // 数据库部署方式:0 集中式(单一服务器),1 分布式(主从服务器)
  46. 'deploy' => 0,
  47. // 数据库读写是否分离 主从式有效
  48. 'rw_separate' => false,
  49. // 读写分离后 主服务器数量
  50. 'master_num' => 1,
  51. // 指定从服务器序号
  52. 'slave_no' => '',
  53. // 是否严格检查字段是否存在
  54. 'fields_strict' => true,
  55. // 数据集返回类型 array 数组 collection Collection对象
  56. 'resultset_type' => 'array',
  57. // 是否自动写入时间戳字段
  58. 'auto_timestamp' => false,
  59. // 是否需要进行SQL性能分析
  60. 'sql_explain' => false,
  61. ];
  62. }
  63. // 获取创建数据库 SQL
  64. private function getCreateTableSql()
  65. {
  66. $sql[] = <<<EOF
  67. DROP TABLE IF EXISTS `tp_user`;
  68. EOF;
  69. $sql[] = <<<EOF
  70. CREATE TABLE `tp_user` (
  71. `id` int(10) unsigned NOT NULL PRIMARY KEY AUTO_INCREMENT,
  72. `username` char(40) NOT NULL DEFAULT '' COMMENT '用户名',
  73. `password` char(40) NOT NULL DEFAULT '' COMMENT '密码',
  74. `status` tinyint(1) NOT NULL DEFAULT '0' COMMENT '状态',
  75. `create_time` int(10) unsigned NOT NULL DEFAULT '0' COMMENT '创建时间'
  76. ) ENGINE=MyISAM AUTO_INCREMENT=1 DEFAULT CHARSET=utf8 COMMENT='会员表';
  77. EOF;
  78. $sql[] = <<<EOF
  79. ALTER TABLE `tp_user` ADD INDEX(`create_time`);
  80. EOF;
  81. $sql[] = <<<EOF
  82. DROP TABLE IF EXISTS `tp_order`;
  83. EOF;
  84. $sql[] = <<<EOF
  85. CREATE TABLE `tp_order` (
  86. `id` int(10) unsigned NOT NULL PRIMARY KEY AUTO_INCREMENT,
  87. `user_id` int(10) unsigned NOT NULL DEFAULT '0' COMMENT '用户id',
  88. `sn` char(20) NOT NULL DEFAULT '' COMMENT '订单号',
  89. `amount` decimal(10,2) unsigned NOT NULL DEFAULT '0' COMMENT '金额',
  90. `freight_fee` decimal(10,2) unsigned NOT NULL DEFAULT '0' COMMENT '运费',
  91. `address_id` int(10) unsigned NOT NULL DEFAULT '0' COMMENT '地址id',
  92. `status` tinyint(1) NOT NULL DEFAULT '0' COMMENT '状态',
  93. `create_time` int(10) unsigned NOT NULL DEFAULT '0' COMMENT '创建时间'
  94. ) ENGINE=MyISAM AUTO_INCREMENT=1 DEFAULT CHARSET=utf8 COMMENT='订单表';
  95. EOF;
  96. $sql[] = <<<EOF
  97. DROP TABLE IF EXISTS `tp_user_address`;
  98. EOF;
  99. $sql[] = <<<EOF
  100. CREATE TABLE `tp_user_address` (
  101. `id` int(10) unsigned NOT NULL PRIMARY KEY AUTO_INCREMENT,
  102. `user_id` int(10) unsigned NOT NULL DEFAULT '0' COMMENT '用户id',
  103. `consignee` varchar(60) NOT NULL DEFAULT '' COMMENT '收货人',
  104. `area_info` varchar(50) NOT NULL DEFAULT '' COMMENT '地区信息',
  105. `city_id` smallint(5) unsigned NOT NULL DEFAULT '0' COMMENT '城市id',
  106. `area_id` smallint(5) unsigned NOT NULL DEFAULT '0' COMMENT '地区id',
  107. `address` varchar(120) NOT NULL DEFAULT '' COMMENT '地址',
  108. `tel` varchar(60) NOT NULL DEFAULT '' COMMENT '电话',
  109. `mobile` varchar(60) NOT NULL DEFAULT '' COMMENT '手机',
  110. `isdefault` tinyint(1) unsigned NOT NULL DEFAULT '0' COMMENT '是否默认'
  111. ) ENGINE=MyISAM AUTO_INCREMENT=1 DEFAULT CHARSET=utf8 COMMENT='地址表';
  112. EOF;
  113. $sql[] = <<<EOF
  114. DROP TABLE IF EXISTS `tp_role_user`;
  115. EOF;
  116. $sql[] = <<<EOF
  117. CREATE TABLE `tp_role_user` (
  118. `role_id` smallint(5) unsigned NOT NULL,
  119. `user_id` int(10) unsigned NOT NULL,
  120. `remark` varchar(250) NOT NULL DEFAULT '',
  121. PRIMARY KEY (`role_id`,`user_id`)
  122. ) ENGINE=MyISAM DEFAULT CHARSET=utf8;
  123. EOF;
  124. return $sql;
  125. }
  126. public function testConnect()
  127. {
  128. $config = $this->getConfig();
  129. $result = Db::connect($config)->execute('show databases');
  130. $this->assertNotEmpty($result);
  131. }
  132. public function testExecute()
  133. {
  134. $config = $this->getConfig();
  135. $sql = $this->getCreateTableSql();
  136. foreach ($sql as $one) {
  137. Db::connect($config)->execute($one);
  138. }
  139. $tableNum = Db::connect($config)->execute("show tables;");
  140. $this->assertEquals(4, $tableNum);
  141. }
  142. public function testQuery()
  143. {
  144. $config = $this->getConfig();
  145. $sql = $this->getCreateTableSql();
  146. Db::connect($config)->batchQuery($sql);
  147. $tableQueryResult = Db::connect($config)->query("show tables;");
  148. $this->assertTrue(is_array($tableQueryResult));
  149. $tableNum = count($tableQueryResult);
  150. $this->assertEquals(4, $tableNum);
  151. }
  152. public function testBatchQuery()
  153. {
  154. $config = $this->getConfig();
  155. $sql = $this->getCreateTableSql();
  156. Db::connect($config)->batchQuery($sql);
  157. $tableNum = Db::connect($config)->execute("show tables;");
  158. $this->assertEquals(4, $tableNum);
  159. }
  160. public function testTable()
  161. {
  162. $config = $this->getConfig();
  163. $tableName = 'tp_user';
  164. $result = Db::connect($config)->table($tableName);
  165. $this->assertEquals($tableName, $result->getOptions()['table']);
  166. }
  167. public function testName()
  168. {
  169. $config = $this->getConfig();
  170. $tableName = 'user';
  171. $result = Db::connect($config)->name($tableName);
  172. $this->assertEquals($config['prefix'] . $tableName, $result->getTable());
  173. }
  174. public function testInsert()
  175. {
  176. $config = $this->getConfig();
  177. $data = [
  178. 'username' => 'chunice',
  179. 'password' => md5('chunice'),
  180. 'status' => 1,
  181. 'create_time' => time(),
  182. ];
  183. $result = Db::connect($config)->name('user')->insert($data);
  184. $this->assertEquals(1, $result);
  185. }
  186. public function testUpdate()
  187. {
  188. $config = $this->getConfig();
  189. $data = [
  190. 'username' => 'chunice_update',
  191. 'password' => md5('chunice'),
  192. 'status' => 1,
  193. 'create_time' => time(),
  194. ];
  195. $result = Db::connect($config)->name('user')->where('username', 'chunice')->update($data);
  196. $this->assertEquals(1, $result);
  197. }
  198. public function testFind()
  199. {
  200. $config = $this->getConfig();
  201. $mustFind = Db::connect($config)->name('user')->where('username', 'chunice_update')->find();
  202. $this->assertNotEmpty($mustFind);
  203. $mustNotFind = Db::connect($config)->name('user')->where('username', 'chunice')->find();
  204. $this->assertEmpty($mustNotFind);
  205. }
  206. public function testInsertAll()
  207. {
  208. $config = $this->getConfig();
  209. $data = [
  210. ['username' => 'foo', 'password' => md5('foo'), 'status' => 1, 'create_time' => time()],
  211. ['username' => 'bar', 'password' => md5('bar'), 'status' => 1, 'create_time' => time()],
  212. ];
  213. $insertNum = Db::connect($config)->name('user')->insertAll($data);
  214. $this->assertEquals(count($data), $insertNum);
  215. }
  216. public function testSelect()
  217. {
  218. $config = $this->getConfig();
  219. $mustFound = Db::connect($config)->name('user')->where('status', 1)->select();
  220. $this->assertNotEmpty($mustFound);
  221. $mustNotFound = Db::connect($config)->name('user')->where('status', 0)->select();
  222. $this->assertEmpty($mustNotFound);
  223. }
  224. public function testValue()
  225. {
  226. $config = $this->getConfig();
  227. $username = Db::connect($config)->name('user')->where('id', 1)->value('username');
  228. $this->assertEquals('chunice_update', $username);
  229. $usernameNull = Db::connect($config)->name('user')->where('id', 0)->value('username');
  230. $this->assertEmpty($usernameNull);
  231. }
  232. public function testColumn()
  233. {
  234. $config = $this->getConfig();
  235. $username = Db::connect($config)->name('user')->where('status', 1)->column('username');
  236. $this->assertNotEmpty($username);
  237. $usernameNull = Db::connect($config)->name('user')->where('status', 0)->column('username');
  238. $this->assertEmpty($usernameNull);
  239. }
  240. public function testInsertGetId()
  241. {
  242. $config = $this->getConfig();
  243. $id = Db::connect($config)->name('user')->order('id', 'desc')->value('id');
  244. $data = [
  245. 'username' => uniqid(),
  246. 'password' => md5('chunice'),
  247. 'status' => 1,
  248. 'create_time' => time(),
  249. ];
  250. $lastId = Db::connect($config)->name('user')->insertGetId($data);
  251. $this->assertEquals($id + 1, $lastId);
  252. }
  253. public function testGetLastInsId()
  254. {
  255. $config = $this->getConfig();
  256. $data = [
  257. 'username' => uniqid(),
  258. 'password' => md5('chunice'),
  259. 'status' => 1,
  260. 'create_time' => time(),
  261. ];
  262. $lastId = Db::connect($config)->name('user')->insertGetId($data);
  263. $lastInsId = Db::connect($config)->name('user')->getLastInsID();
  264. $this->assertEquals($lastId, $lastInsId);
  265. }
  266. public function testSetField()
  267. {
  268. $config = $this->getConfig();
  269. $setFieldNum = Db::connect($config)->name('user')->where('id', 1)->setField('username', 'chunice_setField');
  270. $this->assertEquals(1, $setFieldNum);
  271. $setFieldNum = Db::connect($config)->name('user')->where('id', 1)->setField('username', 'chunice_setField');
  272. $this->assertEquals(0, $setFieldNum);
  273. }
  274. public function testSetInc()
  275. {
  276. $config = $this->getConfig();
  277. $originCreateTime = Db::connect($config)->name('user')->where('id', 1)->value('create_time');
  278. Db::connect($config)->name('user')->where('id', 1)->setInc('create_time');
  279. $newCreateTime = Db::connect($config)->name('user')->where('id', 1)->value('create_time');
  280. $this->assertEquals($originCreateTime + 1, $newCreateTime);
  281. }
  282. public function testSetDec()
  283. {
  284. $config = $this->getConfig();
  285. $originCreateTime = Db::connect($config)->name('user')->where('id', 1)->value('create_time');
  286. Db::connect($config)->name('user')->where('id', 1)->setDec('create_time');
  287. $newCreateTime = Db::connect($config)->name('user')->where('id', 1)->value('create_time');
  288. $this->assertEquals($originCreateTime - 1, $newCreateTime);
  289. }
  290. public function testDelete()
  291. {
  292. $config = $this->getConfig();
  293. Db::connect($config)->name('user')->where('id', 1)->delete();
  294. $result = Db::connect($config)->name('user')->where('id', 1)->find();
  295. $this->assertEmpty($result);
  296. }
  297. public function testChunk()
  298. {
  299. // todo 暂未想到测试方法
  300. }
  301. public function testCache()
  302. {
  303. $config = $this->getConfig();
  304. $result = Db::connect($config)->name('user')->where('id', 1)->cache('key', 60)->find();
  305. $cache = \think\Cache::get('key');
  306. $this->assertEquals($result, $cache);
  307. $updateCache = Db::connect($config)->name('user')->cache('key')->find(1);
  308. $this->assertEquals($cache, $updateCache);
  309. }
  310. }