-
Notifications
You must be signed in to change notification settings - Fork 0
/
DatabaseTest.php
125 lines (105 loc) · 3.78 KB
/
DatabaseTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
<?php
/**
* Created by PhpStorm.
* User: seven
* Date: 2019/2/3
* Time: 20:31
*/
namespace Tests\KickPeach\DataBase;
use PHPUnit\Framework\TestCase;
use KickPeach\DataBase\MysqlConnection;
class DatabaseTest extends TestCase
{
/**
* @var MySqlConnection
*/
private $conn;
public function setUp()
{
parent::setUp();
$dsn = sprintf('mysql:host=%s;port=%s;dbname=%s;charset=%s', '192.168.10.10', 3306, 'geermall_psbildr', 'utf8mb4');
$this->conn = new MysqlConnection($dsn, 'homestead', 'secret');
}
private function getLast()
{
return $this->conn->select('select * from users order by id desc limit 1');
}
private function insertGetId()
{
return $this->conn->table('users')->insertGetId([
'name' => 'seven',
'email' => 'shisiying@xhzyxed.cn',
'password' =>'$10$wGP.Jj.2CK2xaplIem31geUDdIHaCn1CKNuUvJ4IZdcZcxbJdJrrO',
'phone' =>'15626832124',
'profile_color' =>'#7c5cc4',
'role_id' =>1,
'is_active' =>1,
'is_deleted' =>0,
'expire_date' =>'2018-11-19',
'language' =>'zh-CN',
]);
}
public function testInsertGetId()
{
$lastRecord = $this->getLast();
$lastId = empty($lastRecord) ? 0 : $lastRecord[0]['id'];
$id = $this->insertGetId();
$this->assertEquals($lastId + 1, (int) $id);
}
public function testInsert()
{
$affectNum = $this->conn->table('users')->insert([
[
'name' => 'seven1',
'email' => 'shisiying@xhzyxed.cn',
'password' =>'$10$wGP.Jj.2CK2xaplIem31geUDdIHaCn1CKNuUvJ4IZdcZcxbJdJrrO',
'phone' =>'15626832124',
'profile_color' =>'#7c5cc4',
'role_id' =>1,
'is_active' =>1,
'is_deleted' =>0,
'expire_date' =>'2018-11-19',
'language' =>'zh-CN',
],
[
'name' => 'seven2',
'email' => 'shisiying@xhzyxed.cn',
'password' =>'$10$wGP.Jj.2CK2xaplIem31geUDdIHaCn1CKNuUvJ4IZdcZcxbJdJrrO',
'phone' =>'15626832124',
'profile_color' =>'#7c5cc4',
'role_id' =>1,
'is_active' =>1,
'is_deleted' =>0,
'expire_date' =>'2018-11-19',
'language' =>'zh-CN',
],
]);
$this->assertEquals(2, $affectNum);
}
public function testSelect()
{
$id = $this->insertGetId();
$lastRecord = $this->getLast();
$record = $this->conn->select('select * from users where id = :id', [
':id' => $id
]);
$this->assertEquals($lastRecord[0]['name'], $record[0]['name']);
}
public function testUpdate()
{
$id = $this->insertGetId();
$affectNum = $this->conn->update('update users set name = :name where id = :id', [
':name' => 'sevenupdate',
':id' => $id,
]);
$this->assertEquals(1, $affectNum);
}
public function testDelete()
{
$id = $this->insertGetId();
$affectNum = $this->conn->delete('delete from users where id = :id', [
':id' => $id,
]);
$this->assertEquals(1, $affectNum);
}
}