This repository was archived by the owner on Aug 19, 2022. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathReplaceTest.php
197 lines (163 loc) · 4.64 KB
/
ReplaceTest.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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
<?php
/**
* SQL database management to be used by several providers at the same time.
*
* @author Josantonius <[email protected]>
* @copyright 2017 - 2018 (c) Josantonius - PHP-Database
* @license https://opensource.org/licenses/MIT - The MIT License (MIT)
* @link https://github.com/Josantonius/PHP-Database
* @since 1.1.6
*/
namespace Josantonius\Database;
use PHPUnit\Framework\TestCase;
/**
* Test class for "REPLACE" query.
*/
final class ReplaceTest extends TestCase
{
/**
* Database instance.
*
* @since 1.1.7
*
* @var object
*/
private $db;
/**
* Setup.
*
* @since 1.1.7
*/
public function setUp()
{
parent::setUp();
$this->db = Database::getConnection(
'identifier',
'PDOprovider',
$GLOBALS['DB_HOST'],
$GLOBALS['DB_USER'],
$GLOBALS['DB_NAME'],
$GLOBALS['DB_PASSWORD'],
['charset' => 'utf8']
);
}
/**
* [METHOD] [ROWS AFFECTED NUMBER]
*/
public function testMethodReturnRows()
{
$data = [
'id' => 3008,
'name' => 'Manny',
'email' => '[email protected]',
];
$query = $this->db->replace($data)
->from('test_table');
$result = $query->execute();
$this->assertSame(1, $result);
}
/**
* [METHOD] [STATEMENTS] [WHERE ADVANCED] [LAST INSERT ID]
*/
public function testMethodStatementsAdvancedReturnID()
{
$data = [
'id' => 4889,
'name' => ':name',
'email' => ':email',
];
$statements[] = [':name', 'Manny'];
$statements[] = [':email', '[email protected]'];
$query = $this->db->replace($data, $statements)
->from('test_table');
$result = $query->execute('id');
$this->assertSame(4889, $result);
}
/**
* [METHOD] [STATEMENTS] [DATA-TYPE] [WHERE ADVANCED] [ROWS AFFECTED]
*/
public function testMethodStatementsDataTypeAvancedReturnRows()
{
$data = [
'id' => 1,
'name' => ':name',
'email' => ':email',
];
$statements[] = [':name', 'Manny', 'str'];
$statements[] = [':email', '[email protected]', 'str'];
$query = $this->db->replace($data, $statements)
->from('test_table');
$result = $query->execute();
$this->assertSame(1, $result);
}
/**
* [METHOD] [MARKS STATEMENTS] [WHERE ADVANCED] [ROWS AFFECTED NUMBER]
*/
public function testMethodMarksStatementsWhereAdvanceReturnRows()
{
$data = [
'id' => 2,
'name' => '?',
'email' => '?',
];
$statements[] = [1, 'Manny'];
$statements[] = [2, '[email protected]'];
$query = $this->db->replace($data, $statements)
->from('test_table');
$result = $query->execute();
$this->assertSame(1, $result);
}
/**
* [METHOD] [MARKS STATEMENTS] [DATATYPE] [WHEREADVANCED] [LAST INSERT ID]
*/
public function testMethodMarksStatementsDataTypeWhereReturnID()
{
$data = [
'id' => 4890,
'name' => '?',
'email' => '?',
];
$statements[] = [1, 'Manny', 'str'];
$statements[] = [2, '[email protected]', 'str'];
$query = $this->db->replace($data, $statements)
->from('test_table');
$result = $query->execute('id');
$this->assertSame(4890, $result);
}
/**
* [METHOD] [EXCEPTION]
*
* @expectedException \Josantonius\Database\Exception\DBException
*
* @expectedExceptionMessageRegExp (table|view|not|found|exist|Table)
*/
public function testMethodTableNameErrorException()
{
$data = [
'id' => 1,
'name' => 'Manny',
'email' => '[email protected]',
];
$query = $this->db->replace($data)
->from('xxxx');
$result = $query->execute();
}
/**
* [METHOD] [EXCEPTION]
*
* @expectedException \Josantonius\Database\Exception\DBException
*
* @expectedExceptionMessageRegExp (Column|not|found|Unknown|column)
*/
public function testMethodColumnNameErrorException()
{
$data = [
'id' => 1,
'xxxx' => 'Manny',
'email' => '[email protected]',
];
$query = $this->db->replace($data)
->from('test_table');
$result = $query->execute();
}
}