单元测试改进

This commit is contained in:
thinkphp
2017-01-31 16:20:37 +08:00
parent 1d23f67e11
commit 509905bbe4

View File

@@ -140,7 +140,7 @@ EOF;
public function testExecute() public function testExecute()
{ {
$config = $this->getConfig(); $config = $this->getConfig();
$sql = $this->getCreateTableSql(); $sql = $this->getCreateTableSql();
foreach ($sql as $one) { foreach ($sql as $one) {
Db::connect($config)->execute($one); Db::connect($config)->execute($one);
} }
@@ -151,7 +151,7 @@ EOF;
public function testQuery() public function testQuery()
{ {
$config = $this->getConfig(); $config = $this->getConfig();
$sql = $this->getCreateTableSql(); $sql = $this->getCreateTableSql();
Db::connect($config)->batchQuery($sql); Db::connect($config)->batchQuery($sql);
$tableQueryResult = Db::connect($config)->query("show tables;"); $tableQueryResult = Db::connect($config)->query("show tables;");
@@ -165,7 +165,7 @@ EOF;
public function testBatchQuery() public function testBatchQuery()
{ {
$config = $this->getConfig(); $config = $this->getConfig();
$sql = $this->getCreateTableSql(); $sql = $this->getCreateTableSql();
Db::connect($config)->batchQuery($sql); Db::connect($config)->batchQuery($sql);
$tableNum = Db::connect($config)->execute("show tables;"); $tableNum = Db::connect($config)->execute("show tables;");
@@ -174,28 +174,28 @@ EOF;
public function testTable() public function testTable()
{ {
$config = $this->getConfig(); $config = $this->getConfig();
$tableName = 'tp_user'; $tableName = 'tp_user';
$result = Db::connect($config)->table($tableName); $result = Db::connect($config)->table($tableName);
$this->assertEquals($tableName, $result->getOptions()['table']); $this->assertEquals($tableName, $result->getOptions()['table']);
} }
public function testName() public function testName()
{ {
$config = $this->getConfig(); $config = $this->getConfig();
$tableName = 'user'; $tableName = 'user';
$result = Db::connect($config)->name($tableName); $result = Db::connect($config)->name($tableName);
$this->assertEquals($config['prefix'] . $tableName, $result->getOptions()['table']); $this->assertEquals($config['prefix'] . $tableName, $result->getOptions()['table']);
} }
public function testInsert() public function testInsert()
{ {
$config = $this->getConfig(); $config = $this->getConfig();
$data = [ $data = [
'username' => 'chunice', 'username' => 'chunice',
'password' => md5('chunice'), 'password' => md5('chunice'),
'status' => 1, 'status' => 1,
'create_time' => time() 'create_time' => time(),
]; ];
$result = Db::connect($config)->name('user')->insert($data); $result = Db::connect($config)->name('user')->insert($data);
$this->assertEquals(1, $result); $this->assertEquals(1, $result);
@@ -204,11 +204,11 @@ EOF;
public function testUpdate() public function testUpdate()
{ {
$config = $this->getConfig(); $config = $this->getConfig();
$data = [ $data = [
'username' => 'chunice_update', 'username' => 'chunice_update',
'password' => md5('chunice'), 'password' => md5('chunice'),
'status' => 1, 'status' => 1,
'create_time' => time() 'create_time' => time(),
]; ];
$result = Db::connect($config)->name('user')->where('username', 'chunice')->update($data); $result = Db::connect($config)->name('user')->where('username', 'chunice')->update($data);
$this->assertEquals(1, $result); $this->assertEquals(1, $result);
@@ -216,7 +216,7 @@ EOF;
public function testFind() public function testFind()
{ {
$config = $this->getConfig(); $config = $this->getConfig();
$mustFind = Db::connect($config)->name('user')->where('username', 'chunice_update')->find(); $mustFind = Db::connect($config)->name('user')->where('username', 'chunice_update')->find();
$this->assertNotEmpty($mustFind); $this->assertNotEmpty($mustFind);
$mustNotFind = Db::connect($config)->name('user')->where('username', 'chunice')->find(); $mustNotFind = Db::connect($config)->name('user')->where('username', 'chunice')->find();
@@ -229,7 +229,7 @@ EOF;
$data = [ $data = [
['username' => 'foo', 'password' => md5('foo'), 'status' => 1, 'create_time' => time()], ['username' => 'foo', 'password' => md5('foo'), 'status' => 1, 'create_time' => time()],
['username' => 'bar', 'password' => md5('bar'), 'status' => 1, 'create_time' => time()] ['username' => 'bar', 'password' => md5('bar'), 'status' => 1, 'create_time' => time()],
]; ];
$insertNum = Db::connect($config)->name('user')->insertAll($data); $insertNum = Db::connect($config)->name('user')->insertAll($data);
@@ -238,7 +238,7 @@ EOF;
public function testSelect() public function testSelect()
{ {
$config = $this->getConfig(); $config = $this->getConfig();
$mustFound = Db::connect($config)->name('user')->where('status', 1)->select(); $mustFound = Db::connect($config)->name('user')->where('status', 1)->select();
$this->assertNotEmpty($mustFound); $this->assertNotEmpty($mustFound);
$mustNotFound = Db::connect($config)->name('user')->where('status', 0)->select(); $mustNotFound = Db::connect($config)->name('user')->where('status', 0)->select();
@@ -247,7 +247,7 @@ EOF;
public function testValue() public function testValue()
{ {
$config = $this->getConfig(); $config = $this->getConfig();
$username = Db::connect($config)->name('user')->where('id', 1)->value('username'); $username = Db::connect($config)->name('user')->where('id', 1)->value('username');
$this->assertEquals('chunice_update', $username); $this->assertEquals('chunice_update', $username);
$usernameNull = Db::connect($config)->name('user')->where('id', 0)->value('username'); $usernameNull = Db::connect($config)->name('user')->where('id', 0)->value('username');
@@ -256,7 +256,7 @@ EOF;
public function testColumn() public function testColumn()
{ {
$config = $this->getConfig(); $config = $this->getConfig();
$username = Db::connect($config)->name('user')->where('status', 1)->column('username'); $username = Db::connect($config)->name('user')->where('status', 1)->column('username');
$this->assertNotEmpty($username); $this->assertNotEmpty($username);
$usernameNull = Db::connect($config)->name('user')->where('status', 0)->column('username'); $usernameNull = Db::connect($config)->name('user')->where('status', 0)->column('username');
@@ -267,13 +267,13 @@ EOF;
public function testInsertGetId() public function testInsertGetId()
{ {
$config = $this->getConfig(); $config = $this->getConfig();
$id = Db::connect($config)->name('user')->order('id', 'desc')->value('id'); $id = Db::connect($config)->name('user')->order('id', 'desc')->value('id');
$data = [ $data = [
'username' => uniqid(), 'username' => uniqid(),
'password' => md5('chunice'), 'password' => md5('chunice'),
'status' => 1, 'status' => 1,
'create_time' => time() 'create_time' => time(),
]; ];
$lastId = Db::connect($config)->name('user')->insertGetId($data); $lastId = Db::connect($config)->name('user')->insertGetId($data);
$this->assertEquals($id + 1, $lastId); $this->assertEquals($id + 1, $lastId);
@@ -283,11 +283,11 @@ EOF;
public function testGetLastInsId() public function testGetLastInsId()
{ {
$config = $this->getConfig(); $config = $this->getConfig();
$data = [ $data = [
'username' => uniqid(), 'username' => uniqid(),
'password' => md5('chunice'), 'password' => md5('chunice'),
'status' => 1, 'status' => 1,
'create_time' => time() 'create_time' => time(),
]; ];
$lastId = Db::connect($config)->name('user')->insertGetId($data); $lastId = Db::connect($config)->name('user')->insertGetId($data);
@@ -308,7 +308,7 @@ EOF;
public function testSetInc() public function testSetInc()
{ {
$config = $this->getConfig(); $config = $this->getConfig();
$originCreateTime = Db::connect($config)->name('user')->where('id', 1)->value('create_time'); $originCreateTime = Db::connect($config)->name('user')->where('id', 1)->value('create_time');
Db::connect($config)->name('user')->where('id', 1)->setInc('create_time'); Db::connect($config)->name('user')->where('id', 1)->setInc('create_time');
$newCreateTime = Db::connect($config)->name('user')->where('id', 1)->value('create_time'); $newCreateTime = Db::connect($config)->name('user')->where('id', 1)->value('create_time');
@@ -318,7 +318,7 @@ EOF;
public function testSetDec() public function testSetDec()
{ {
$config = $this->getConfig(); $config = $this->getConfig();
$originCreateTime = Db::connect($config)->name('user')->where('id', 1)->value('create_time'); $originCreateTime = Db::connect($config)->name('user')->where('id', 1)->value('create_time');
Db::connect($config)->name('user')->where('id', 1)->setDec('create_time'); Db::connect($config)->name('user')->where('id', 1)->setDec('create_time');
$newCreateTime = Db::connect($config)->name('user')->where('id', 1)->value('create_time'); $newCreateTime = Db::connect($config)->name('user')->where('id', 1)->value('create_time');
@@ -341,12 +341,12 @@ EOF;
public function testCache() public function testCache()
{ {
$config = $this->getConfig(); $config = $this->getConfig();
$result = Db::connect($config)->name('user')->where('id', 1)->cache('key', 60)->select(); $result = Db::connect($config)->name('user')->where('id', 1)->cache('key', 60)->find();
$cache = \think\Cache::get('key'); $cache = \think\Cache::get('key');
$this->assertEquals($result, $cache); $this->assertEquals($result, $cache);
$updateCache = Db::connect($config)->name('user')->cache('key')->find(2); $updateCache = Db::connect($config)->name('user')->cache('key')->find(1);
$this->assertNotEquals($cache, $updateCache); $this->assertEquals($cache, $updateCache);
} }
} }