-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathtest.php
174 lines (171 loc) · 5.35 KB
/
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
<?php
/*
SimpleTest + CodeIgniter
test.php
the test runner - loads all needed files,
integrates with CodeIgniter and runs the tests
by Jamie Rumbelow
http://jamierumbelow.net/
*/
//Configure and load files
define('ROOT', dirname(__file__).'/');
define('APPLICATION',ROOT.'application/');
define('APPINDEX',ROOT.'index.php');
define('SIMPLETEST',ROOT.'unit_test/simpletest/');
define('TESTS',ROOT.'unit_test/tests/');
$global_array = array("ROOT" => ROOT, "SIMPLETEST" => SIMPLETEST, "APPLICATION" =>
APPLICATION, "TESTS" => TESTS, "APPINDEX" => APPINDEX);
foreach ($global_array as $global_name => $dir_check):
if (!file_exists($dir_check))
{
echo "Cannot Find ".$global_name." File / Directory: ".$dir_check;
exit;
}
endforeach;
require_once SIMPLETEST.'unit_tester.php';
require_once SIMPLETEST.'web_tester.php';
require_once SIMPLETEST.'reporter.php';
function get_loader_id($file, $type)
{
$loader_url = ROOT.'application/tests/'.$type.'/';
$loader_start = strpos($file, $loader_url) + strlen($loader_url) + 1;
$loader_length = strpos($file, '_'.$type.'_test.php', $loader_start) - $loader_start;
$loader_id = substr($file, $loader_start, $loader_length);
return $loader_id;
}
function add_test($file, &$test)
{
$implementation = '';
if (preg_match('/_controller/', $file))
{
$controller = get_loader_id($file, 'controller');
$implementation = APPLICATION.'controllers/'.$controller.'.php';
} elseif (preg_match('/_model/', $file))
{
$model = get_loader_id($file, 'model');
$implementation = APPLICATION.'models/'.$model.'_model.php';
} elseif (preg_match('/_view/', $file))
{
$view = get_loader_id($file, 'views');
$implementation = APPLICATION.'views/'.$view.'.php';
} elseif (preg_match('/_helper/', $file))
{
$helper = get_loader_id($file, 'helpers');
$implementation = APPLICATION.'helpers/'.$helper.'.php';
} elseif (preg_match('/_library/', $file))
{
$library = get_loader_id($file, 'libraries');
$implementation = APPLICATION.'libraries/'.$library.'.php';
}
if (file_exists($implementation))
{
require_once ($implementation);
}
$test->addFile($file);
}
class CodeIgniterUnitTestCase extends UnitTestCase
{
protected $ci;
public function __construct()
{
parent::UnitTestCase();
$this->ci = &get_instance();
}
}
class CodeIgniterWebTestCase extends WebTestCase
{
protected $ci;
public function __construct()
{
parent::WebTestCase();
$this->ci = &get_instance();
}
}
//Capture CodeIgniter output, discard and load system into $CI variable
ob_start();
include (APPINDEX);
$CI = &get_instance();
ob_end_clean();
//Setup the test suite
$test = new TestSuite();
$test->_label = 'CodeIgniter Application Test Suite';
if ((isset($_POST['test_name'])) && ($_POST['test_name'] <> ''))
{
add_test(TESTS.$_POST['test_name'].'.php', $test);
} elseif (isset($_POST['test_type']))
{
//What are we testing?
$files = array();
if ($_POST['test_type'] == "controllers")
{
$files = @scandir(TESTS.'/controllers');
} elseif ($_POST['test_type'] == "models")
{
$files = @scandir(TESTS.'/views');
} elseif ($_POST['test_type'] == "views")
{
$files = @scandir(TESTS.'/models');
} elseif ($_POST['test_type'] == "helpers")
{
$files = array_merge($files, @scandir(TESTS.'helpers'));
} elseif ($_POST['test_type'] == "libraries")
{
$files = array_merge($files, @scandir(TESTS.'libraries'));
} elseif ($_POST['test_type'] == "all")
{
$files = @scandir(TESTS.'/controllers');
$files = array_merge($files, @scandir(TESTS.'models'));
$files = array_merge($files, @scandir(TESTS.'views'));
$files = array_merge($files, @scandir(TESTS.'helpers'));
$files = array_merge($files, @scandir(TESTS.'libraries'));
}
else
{
//Use all by default
$files = @scandir(TESTS.'/controllers');
$files = array_merge($files, @scandir(TESTS.'models'));
$files = array_merge($files, @scandir(TESTS.'views'));
$files = array_merge($files, @scandir(TESTS.'helpers'));
$files = array_merge($files, @scandir(TESTS.'libraries'));
}
//Remove ., .. and any .whatever files, and add the full path
function prepare_array($value, $key)
{
global $files;
if (preg_match('/^\./', $value))
{
unset($files[$key]);
}
if (preg_match('/_model/', $value))
{
$files[$key] = TESTS.'models/'.$value;
}
if (preg_match('/_controller/', $value))
{
$files[$key] = TESTS.'controllers/'.$value;
}
if (preg_match('/_view/', $value))
{
$files[$key] = TESTS.'views/'.$value;
}
if (preg_match('/_helper/', $value))
{
$files[$key] = TESTS.'helpers/'.$value;
}
if (preg_match('/_library/', $value))
{
$files[$key] = TESTS.'libraries/'.$value;
}
}
array_walk($files, 'prepare_array');
//Add each file to the test suite
foreach ($files as $file)
{
add_test($file, $test);
}
}
//just above the $test->run() call
include (SIMPLETEST.'custom_test_gui.php');
//Run tests!
$test->run(new HtmlReporter());
/* End of file */