1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Part of ci-phpunit-test |
4
|
|
|
* |
5
|
|
|
* @author Kenji Suzuki <https://github.com/kenjis> |
6
|
|
|
* @license MIT License |
7
|
|
|
* @copyright 2016 Kenji Suzuki |
8
|
|
|
* @link https://github.com/kenjis/ci-phpunit-test |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Copyright for Original Code |
13
|
|
|
* |
14
|
|
|
* @author CodeIgniter Dev Team |
15
|
|
|
* @copyright Copyright (c) 2014 - 2016, British Columbia Institute of Technology (http://bcit.ca/) |
16
|
|
|
* @license http://opensource.org/licenses/MIT MIT License |
17
|
|
|
* @link http://codeigniter.com |
18
|
|
|
* |
19
|
|
|
* @see https://github.com/bcit-ci/CodeIgniter4/blob/59e1587a9875141586f8333ff9cc64cdae2173c4/system/Test/CIDatabaseTestCase.php |
20
|
|
|
*/ |
21
|
|
|
|
22
|
|
|
class CIPHPUnitTestDbTestCase extends CIPHPUnitTestCase |
23
|
|
|
{ |
24
|
|
|
protected $db; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Stores information needed to remove any |
28
|
|
|
* rows inserted via $this->hasInDatabase(); |
29
|
|
|
* |
30
|
|
|
* @var array |
31
|
|
|
*/ |
32
|
|
|
protected $insertCache = []; |
33
|
|
|
|
34
|
|
|
protected function loadDependencies() |
35
|
|
|
{ |
36
|
|
|
if ($this->db === null) |
37
|
|
|
{ |
38
|
|
|
$CI =& get_instance(); |
39
|
|
|
$CI->load->database(); |
40
|
|
|
$this->db = $CI->db; |
41
|
|
|
} |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
protected function setUp() |
45
|
|
|
{ |
46
|
|
|
$this->loadDependencies(); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
//-------------------------------------------------------------------- |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Takes care of any required cleanup after the test, like |
53
|
|
|
* removing any rows inserted via $this->hasInDatabase() |
54
|
|
|
*/ |
55
|
|
|
protected function tearDown() |
56
|
|
|
{ |
57
|
|
|
if (! empty($this->insertCache)) |
58
|
|
|
{ |
59
|
|
|
foreach ($this->insertCache as $row) |
60
|
|
|
{ |
61
|
|
|
$this->db->delete($row[0], $row[1]); |
|
|
|
|
62
|
|
|
} |
63
|
|
|
} |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Workaround for the following error |
68
|
|
|
* |
69
|
|
|
* Error: Call to a member function quote() on boolean |
70
|
|
|
* vendor/codeigniter/framework/system/database/drivers/pdo/pdo_driver.php:234 |
71
|
|
|
* |
72
|
|
|
* I don't know why, but when I call $this->seeInDatabase() after $this->request(), |
73
|
|
|
* I got it |
74
|
|
|
*/ |
75
|
|
|
private function checkDbConnId() |
76
|
|
|
{ |
77
|
|
|
if (is_object($this->db->conn_id)) { |
78
|
|
|
return; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
$this->db->close(); |
82
|
|
|
$this->db = null; |
83
|
|
|
|
84
|
|
|
$CI =& get_instance(); |
85
|
|
|
$CI->load->database(); |
86
|
|
|
$this->db = $this->CI->db; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
//-------------------------------------------------------------------- |
90
|
|
|
// Database Test Helpers |
91
|
|
|
//-------------------------------------------------------------------- |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Asserts that records that match the conditions in $where do |
95
|
|
|
* not exist in the database. |
96
|
|
|
* |
97
|
|
|
* @param string $table |
98
|
|
|
* @param array $where |
99
|
|
|
* |
100
|
|
|
* @return bool |
101
|
|
|
*/ |
102
|
|
View Code Duplication |
public function dontSeeInDatabase($table, array $where) |
|
|
|
|
103
|
|
|
{ |
104
|
|
|
$this->checkDbConnId(); |
105
|
|
|
|
106
|
|
|
$this->db->from($table); |
|
|
|
|
107
|
|
|
$this->db->where($where); |
|
|
|
|
108
|
|
|
$count = $this->db->count_all_results(); |
|
|
|
|
109
|
|
|
|
110
|
|
|
$this->assertTrue($count == 0, 'Row was found in database'); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
//-------------------------------------------------------------------- |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* Asserts that records that match the conditions in $where DO |
117
|
|
|
* exist in the database. |
118
|
|
|
* |
119
|
|
|
* @param string $table |
120
|
|
|
* @param array $where |
121
|
|
|
* |
122
|
|
|
* @return bool |
123
|
|
|
*/ |
124
|
|
View Code Duplication |
public function seeInDatabase($table, array $where) |
|
|
|
|
125
|
|
|
{ |
126
|
|
|
$this->checkDbConnId(); |
127
|
|
|
|
128
|
|
|
$this->db->from($table); |
|
|
|
|
129
|
|
|
$this->db->where($where); |
|
|
|
|
130
|
|
|
$count = $this->db->count_all_results(); |
|
|
|
|
131
|
|
|
|
132
|
|
|
$this->assertTrue($count > 0, 'Row not found in database'); |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
//-------------------------------------------------------------------- |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* Fetches a single column from a database row with criteria |
139
|
|
|
* matching $where. |
140
|
|
|
* |
141
|
|
|
* @param string $table |
142
|
|
|
* @param string $column |
143
|
|
|
* @param array $where |
144
|
|
|
* |
145
|
|
|
* @return bool |
146
|
|
|
*/ |
147
|
|
|
public function grabFromDatabase($table, $column, array $where) |
148
|
|
|
{ |
149
|
|
|
$this->checkDbConnId(); |
150
|
|
|
|
151
|
|
|
$this->db->select($column); |
|
|
|
|
152
|
|
|
$this->db->where($where); |
|
|
|
|
153
|
|
|
$query = $this->db->get($table); |
|
|
|
|
154
|
|
|
$row = $query->row(); |
155
|
|
|
|
156
|
|
|
return isset($row->$column) ? $row->$column : false; |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
//-------------------------------------------------------------------- |
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* Inserts a row into to the database. This row will be removed |
163
|
|
|
* after the test has run. |
164
|
|
|
* |
165
|
|
|
* @param string $table |
166
|
|
|
* @param array $data |
167
|
|
|
* |
168
|
|
|
*/ |
169
|
|
|
public function hasInDatabase($table, array $data) |
170
|
|
|
{ |
171
|
|
|
$this->checkDbConnId(); |
172
|
|
|
|
173
|
|
|
$this->insertCache[] = [ |
174
|
|
|
$table, $data |
175
|
|
|
]; |
176
|
|
|
|
177
|
|
|
$this->db->insert($table, $data); |
|
|
|
|
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
//-------------------------------------------------------------------- |
181
|
|
|
|
182
|
|
|
/** |
183
|
|
|
* Asserts that the number of rows in the database that match $where |
184
|
|
|
* is equal to $expected. |
185
|
|
|
* |
186
|
|
|
* @param int $expected |
187
|
|
|
* @param string $table |
188
|
|
|
* @param array $where |
189
|
|
|
* |
190
|
|
|
* @return bool |
191
|
|
|
*/ |
192
|
|
View Code Duplication |
public function seeNumRecords($expected, $table, array $where = []) |
|
|
|
|
193
|
|
|
{ |
194
|
|
|
$this->checkDbConnId(); |
195
|
|
|
|
196
|
|
|
$this->db->from($table); |
|
|
|
|
197
|
|
|
$this->db->where($where); |
|
|
|
|
198
|
|
|
$count = $this->db->count_all_results(); |
|
|
|
|
199
|
|
|
|
200
|
|
|
$this->assertEquals($expected, $count, 'Wrong number of matching rows in database.'); |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
//-------------------------------------------------------------------- |
204
|
|
|
|
205
|
|
|
} |
206
|
|
|
|
Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.