forked from lonnieezell/Bonfire
-
Notifications
You must be signed in to change notification settings - Fork 0
/
unit_test.php
executable file
·204 lines (173 loc) · 4.29 KB
/
unit_test.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
198
199
200
201
202
203
204
<?php
/**
* Please note this file shouldn't be exposed on a live server,
* there is no filtering of $_POST!!!!
*/
error_reporting(0);
$cli_mode = setup_cli($argv); // Determines if running in cli mode
/**
* Configure your paths here:
*/
define('MAIN_PATH', realpath(dirname(__FILE__)).'/');
define('SIMPLETEST', MAIN_PATH .'tests/simpletest/'); // Directory of simpletest
define('ROOT', MAIN_PATH); // Directory of codeigniter index.php
define('TESTS_DIR', MAIN_PATH . 'tests/'); // Directory of your tests.
define('APP_DIR', MAIN_PATH . 'bonfire/application/'); // CodeIgniter Application directory
//do not use autorun as it output ugly report upon no test run
require_once SIMPLETEST . 'unit_tester.php';
require_once SIMPLETEST . 'mock_objects.php';
require_once SIMPLETEST . 'collector.php';
require_once SIMPLETEST . 'web_tester.php';
require_once SIMPLETEST . 'extensions/my_reporter.php';
$test_suite = new TestSuite();
$test_suite->_label = 'Bonfire Test Suite';
class CodeIgniterUnitTestCase extends UnitTestCase {
protected $ci;
public function __construct()
{
parent::__construct();
$this->ci =& get_instance();
}
public function __get($var)
{
return $this->ci->$var;
}
}
class CodeIgniterWebTestCase extends WebTestCase {
protected $ci;
public function __construct()
{
parent::WebTestCase();
$this->ci =& get_instance();
}
public function __get($var)
{
return $this->ci->$var;
}
}
// Because get is removed in ci we pull it out here.
$run_all = FALSE;
if (isset($_GET['all']) || isset($_POST['all']))
{
$run_all = TRUE;
}
//Capture CodeIgniter output, discard and load system into $CI variable
ob_start();
include(ROOT . 'index.php');
$CI =& get_instance();
ob_end_clean();
$CI->load->library('session');
$CI->session->sess_destroy();
$CI->load->helper('directory');
$test_start = microtime();
// Get all main tests
if ($run_all OR ( ! empty($_POST) && ! isset($_POST['test'])))
{
$test_objs = array('controllers','models','views','libraries','bugs','helpers');
foreach ($test_objs as $obj)
{
if (isset($_POST[$obj]) OR $run_all)
{
$dir = TESTS_DIR . $obj;
$dir_files = directory_map($dir);
foreach ($dir_files as $file)
{
if ($file != 'index.html')
{
$test_suite->addTestFile($dir . '/' . $file);
}
}
}
}
}
elseif (isset($_POST['test'])) //single test
{
$file = $_POST['test'];
if (file_exists(TESTS_DIR . $file))
{
$test_suite->addTestFile(TESTS_DIR . $file);
}
}
// ------------------------------------------------------------------------
/**
* Function to determine if in cli mode and if so set up variables to make it work
*
* @param Array of commandline args
* @return Boolean true or false if commandline mode setup
*
*/
function setup_cli($argv)
{
if (php_sapi_name() == 'cli')
{
if(isset($argv[1]))
{
if(stripos($argv[1],'.php') !== false)
{
$_POST['test'] = $argv[1];
}
else
{
$_POST[$argv[1]] = $argv[1];
}
}
else
{
$_POST['all'] = 'all';
}
$_SERVER['HTTP_HOST'] = '';
$_SERVER['REQUEST_URI'] = '';
return true;
}
return false;
}
/**
* Function to map tests and strip .html files.
*
*
* @param string
* @return array
*/
function map_tests($location = '')
{
if (empty($location))
{
return FALSE;
}
$files = directory_map($location);
$return = array();
foreach ($files as $file)
{
if ($file != 'index.html')
{
$return[] = $file;
}
}
return $return;
}
function memory_usage()
{
$size = memory_get_usage(true);
$unit=array('B','KB','MB','GB','TB','PB');
return @round($size/pow(1024,($i=floor(log($size,1024)))),2).' '.$unit[$i];
}
//variables for report
$controllers = map_tests(TESTS_DIR . 'controllers');
$models = map_tests(TESTS_DIR . 'models');
$views = map_tests(TESTS_DIR . 'views');
$libraries = map_tests(TESTS_DIR . 'libraries');
$bugs = map_tests(TESTS_DIR . 'bugs');
$helpers = map_tests(TESTS_DIR . 'helpers');
$form_url = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
$test_end = microtime();
/* Benchmark */
list($sm, $ss) = explode(' ', $test_start);
list($em, $es) = explode(' ', $test_end);
$elapse_time = number_format(($em + $es) - ($sm + $ss), 4);
//display the form
if ($cli_mode) {
exit ($test_suite->run(new TextReporter()) ? 0 : 1);
}
else {
include(TESTS_DIR . 'test_gui.php');
}