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
|
|
|
public function resetInstance() |
35
|
|
|
{ |
36
|
|
|
parent::resetInstance(); |
37
|
|
|
$this->loadDependencies(); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
protected function loadDependencies() |
41
|
|
|
{ |
42
|
|
|
if ($this->db === null || $this->db->conn_id === false) |
43
|
|
|
{ |
44
|
|
|
$CI =& get_instance(); |
45
|
|
|
$CI->load->database(); |
46
|
|
|
$this->db = $CI->db; |
47
|
|
|
} |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
protected function setUp() |
51
|
|
|
{ |
52
|
|
|
$this->loadDependencies(); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
//-------------------------------------------------------------------- |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Takes care of any required cleanup after the test, like |
59
|
|
|
* removing any rows inserted via $this->hasInDatabase() |
60
|
|
|
*/ |
61
|
|
|
protected function tearDown() |
62
|
|
|
{ |
63
|
|
|
if (! empty($this->insertCache)) |
64
|
|
|
{ |
65
|
|
|
foreach ($this->insertCache as $row) |
66
|
|
|
{ |
67
|
|
|
$this->db->delete($row[0], $row[1]); |
68
|
|
|
} |
69
|
|
|
} |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
//-------------------------------------------------------------------- |
73
|
|
|
// Database Test Helpers |
74
|
|
|
//-------------------------------------------------------------------- |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Asserts that records that match the conditions in $where do |
78
|
|
|
* not exist in the database. |
79
|
|
|
* |
80
|
|
|
* @param string $table |
81
|
|
|
* @param array $where |
82
|
|
|
* |
83
|
|
|
* @return bool |
84
|
|
|
*/ |
85
|
|
View Code Duplication |
public function dontSeeInDatabase($table, array $where) |
|
|
|
|
86
|
|
|
{ |
87
|
|
|
$this->db->from($table); |
88
|
|
|
$this->db->where($where); |
89
|
|
|
$count = $this->db->count_all_results(); |
90
|
|
|
|
91
|
|
|
$this->assertTrue($count == 0, 'Row was found in database'); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
//-------------------------------------------------------------------- |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Asserts that records that match the conditions in $where DO |
98
|
|
|
* exist in the database. |
99
|
|
|
* |
100
|
|
|
* @param string $table |
101
|
|
|
* @param array $where |
102
|
|
|
* |
103
|
|
|
* @return bool |
104
|
|
|
*/ |
105
|
|
View Code Duplication |
public function seeInDatabase($table, array $where) |
|
|
|
|
106
|
|
|
{ |
107
|
|
|
$this->db->from($table); |
108
|
|
|
$this->db->where($where); |
109
|
|
|
$count = $this->db->count_all_results(); |
110
|
|
|
|
111
|
|
|
$this->assertTrue($count > 0, 'Row not found in database'); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
//-------------------------------------------------------------------- |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* Fetches a single column from a database row with criteria |
118
|
|
|
* matching $where. |
119
|
|
|
* |
120
|
|
|
* @param string $table |
121
|
|
|
* @param string $column |
122
|
|
|
* @param array $where |
123
|
|
|
* |
124
|
|
|
* @return bool |
125
|
|
|
*/ |
126
|
|
|
public function grabFromDatabase($table, $column, array $where) |
127
|
|
|
{ |
128
|
|
|
$this->db->select($column); |
129
|
|
|
$this->db->where($where); |
130
|
|
|
$query = $this->db->get($table); |
131
|
|
|
$row = $query->row(); |
132
|
|
|
|
133
|
|
|
return isset($row->$column) ? $row->$column : false; |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
//-------------------------------------------------------------------- |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* Inserts a row into to the database. This row will be removed |
140
|
|
|
* after the test has run. |
141
|
|
|
* |
142
|
|
|
* @param string $table |
143
|
|
|
* @param array $data |
144
|
|
|
* |
145
|
|
|
*/ |
146
|
|
|
public function hasInDatabase($table, array $data) |
147
|
|
|
{ |
148
|
|
|
$this->insertCache[] = [ |
149
|
|
|
$table, $data |
150
|
|
|
]; |
151
|
|
|
|
152
|
|
|
$this->db->insert($table, $data); |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
//-------------------------------------------------------------------- |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* Asserts that the number of rows in the database that match $where |
159
|
|
|
* is equal to $expected. |
160
|
|
|
* |
161
|
|
|
* @param int $expected |
162
|
|
|
* @param string $table |
163
|
|
|
* @param array $where |
164
|
|
|
* |
165
|
|
|
* @return bool |
166
|
|
|
*/ |
167
|
|
|
public function seeNumRecords($expected, $table, array $where = []) |
168
|
|
|
{ |
169
|
|
|
$this->db->from($table); |
170
|
|
|
$this->db->where($where); |
171
|
|
|
$count = $this->db->count_all_results(); |
172
|
|
|
|
173
|
|
|
$this->assertEquals($expected, $count, 'Wrong number of matching rows in database.'); |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
//-------------------------------------------------------------------- |
177
|
|
|
|
178
|
|
|
} |
179
|
|
|
|
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.