-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.php
More file actions
297 lines (260 loc) · 7.91 KB
/
test.php
File metadata and controls
297 lines (260 loc) · 7.91 KB
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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
/**
* Test class
*
* @author Istvan Pusztai
* @version 1.0.2 $Id: test.php 7 2009-09-29 06:23:41Z Istvan $
* @copyright Copyright (C) 2010 Istvan Pusztai (twitter.com/istvanp)
**/
class Test extends CI_Controller {
private $timings = array();
private $tests = array();
/**
* Constructor function
*
* @return void
**/
public function __construct()
{
parent::__construct();
// Set time marker for the start of the test suite
$this->benchmark->mark('first');
log_message('debug', 'Test Controller Initialized');
// Load the unit test library
$this->load->library('unit_test');
// Load syntax highlighting helper
$this->load->helper('text');
// Set mode to strict
$this->unit->use_strict(TRUE);
// Disable database debugging so we can test all units without stopping
// at the first SQL error
// $this->db->db_debug = FALSE;
// Create list of tests
$this->_map_tests();
}
/**
* *SAMPLE* test function
* Here we are testing a model called User Model which has some basic
* methods like create(username, password, email, name) and
* update(id, array(key => value[, key => value])
*
* @return void
**/
function user_model()
{
$model_name = 'user_model';
$this->load->model($model_name);
// Insert
$this->benchmark->mark('start');
$test = $this->user_model->create('admin', 'admin', 'admin@somesite.com', 'Administrator');
$this->unit->run($test, TRUE, $model_name . '->create()');
$id = $this->db->insert_id(); // save insert id
$this->benchmark->mark('end');
$this->timings[] = $this->benchmark->elapsed_time('start', 'end');
// ID exists
$this->benchmark->mark('start');
$test = $this->user_model->id_exists($id);
$this->unit->run($test, TRUE, $model_name . '->id_exists()');
$this->benchmark->mark('end');
$this->timings[] = $this->benchmark->elapsed_time('start', 'end');
// ID exists
$this->benchmark->mark('start');
$test = $this->user_model->id_exists($id + 1);
$this->unit->run($test, FALSE, $model_name . '->id_exists()');
$this->benchmark->mark('end');
$this->timings[] = $this->benchmark->elapsed_time('start', 'end');
// Username exists
$this->benchmark->mark('start');
$test = $this->user_model->username_exists('admin');
$this->unit->run($test, TRUE, $model_name . '->username_exists()');
$this->benchmark->mark('end');
$this->timings[] = $this->benchmark->elapsed_time('start', 'end');
// Username exists
$this->benchmark->mark('start');
$test = $this->user_model->username_exists('admin123');
$this->unit->run($test, FALSE, $model_name . '->username_exists()');
$this->benchmark->mark('end');
$this->timings[] = $this->benchmark->elapsed_time('start', 'end');
// Email exists
$this->benchmark->mark('start');
$test = $this->user_model->email_exists('admin@somesite.com');
$this->unit->run($test, TRUE, $model_name . '->email_exists()');
$this->benchmark->mark('end');
$this->timings[] = $this->benchmark->elapsed_time('start', 'end');
// Email exists
$this->benchmark->mark('start');
$test = $this->user_model->email_exists('test@somesite.com');
$this->unit->run($test, FALSE, $model_name . '->email_exists()');
$this->benchmark->mark('end');
$this->timings[] = $this->benchmark->elapsed_time('start', 'end');
// Update
$this->benchmark->mark('start');
$test = $this->user_model->update($id, array('username' => 'admin2'));
$this->unit->run($test, TRUE, $model_name . '->update()');
$this->benchmark->mark('end');
$this->timings[] = $this->benchmark->elapsed_time('start', 'end');
// Delete
$this->benchmark->mark('start');
$test = $this->user_model->delete($id);
$this->unit->run($test, TRUE, $model_name . '->delete()');
$this->benchmark->mark('end');
$this->timings[] = $this->benchmark->elapsed_time('start', 'end');
}
/**
* Remap function
* Maps the requested action to a method and performs the tests.
* Do not modify/delete this function.
*
* @author Istvan Pusztai
* @return void
**/
public function _remap()
{
$view_data = array();
$action = $this->uri->rsegment(2);
$view_data['headings'] = array("types" => array(), "tests" => array());
switch (strtolower($action))
{
case 'index':
$view_data['msg'] = "Please pick a test suite";
break;
case 'all':
$i = 0;
foreach ($this->tests as $key => $type)
{
$view_data['headings']['types'][count($this->timings)] = ucfirst($key);
foreach($type as $key2 => $method)
{
$view_data['headings']['tests'][count($this->timings)] = $method;
call_user_func(array($this, $method));
}
}
break;
case 'models':
case 'views':
case 'libraries':
case 'helpers':
if (array_key_exists($action, $this->tests) && count($this->tests[$action]) > 0)
{
foreach ($this->tests[$action] as $method)
{
$view_data['headings']['tests'][count($this->timings)] = $method;
call_user_func(array($this, $method));
}
}
else
{
$view_data['msg'] = "There are no test suites for $action";
}
break;
default:
if (array_search_recursive($action, $this->tests))
{
call_user_func(array($this, $action));
}
else
{
$view_data['msg'] = "<em>$action</em> is an invalid test suite";
}
}
// Prepare report
$report = $this->unit->result();
// Prepare totals
$view_data['totals']['all'] = count($report);
$view_data['totals']['failed'] = 0;
// Count failures
foreach($report as $key => $test)
{
if ($test['Result'] == 'Failed')
{
++$view_data['totals']['failed'];
}
}
// Count passes
$view_data['totals']['passed'] = $view_data['totals']['all'] - $view_data['totals']['failed'];
// Calculate the total time taken for the test suite
$view_data['total_time'] = $this->benchmark->elapsed_time('first', 'end');
// Other useful data
$view_data['tests'] = $this->tests;
$view_data['type'] = $action;
$view_data['report'] = $report;
$view_data['timings'] = $this->timings;
$this->load->view('unit_test', $view_data);
}
/**
* Map Tests
* Creates a list of tests from the functions defined in this class.
* Do not modify/delete this function.
*
* @author Istvan Pusztai
* @return void
**/
public function _map_tests()
{
$methods = get_class_methods($this);
natsort($methods);
foreach ($methods as $method)
{
if (strpos($method, '_') !== 0
AND $method != __CLASS__
AND $method != "CI_Base"
AND $method != "Controller"
AND $method != "get_instance"
)
{
$length = strlen($method);
if (strripos($method, 'model') === $length - 5)
{
$this->tests['models'][] = $method;
}
else if (strripos($method, 'view') === $length - 4)
{
$this->tests['views'][] = $method;
}
else if (strripos($method, 'library') === $length - 7)
{
$this->tests['libraries'][] = $method;
}
else if (strripos($method, 'helper') === $length - 6)
{
$this->tests['helpers'][] = $method;
}
}
}
return $this->tests;
}
}
/**
* Array Search (Rescursive)
* Searches through an array for a value recursively
* >>> Place this code in a helper if you use it elsewhere <<<
*
* @author Istvan Pusztai
* @since 1.0.2
* @param string $needle The value to look for
* @param array $haystack The array to search
* @param bool $strict Use strict comparison
* @return bool
**/
function array_search_recursive($needle, $haystack, $strict = FALSE, $path = array())
{
if ( ! is_array($haystack))
{
return FALSE;
}
foreach ($haystack as $key => $val)
{
if (is_array($val) && $subPath = array_search_recursive($needle, $val, $strict, $path))
{
$path = array_merge($path, array($key), $subPath);
return $path;
}
else if (( ! $strict && $val == $needle) || ($strict && $val === $needle))
{
$path[] = $key;
return $path;
}
}
return FALSE;
}
/* End of file test.php */