|
@@ -0,0 +1,350 @@
|
|
1
|
+<?php
|
|
2
|
+/**
|
|
3
|
+ * @package tests
|
|
4
|
+ */
|
|
5
|
+
|
|
6
|
+# ============================================================================ #
|
|
7
|
+# TESTS #
|
|
8
|
+# ============================================================================ #
|
|
9
|
+
|
|
10
|
+/**
|
|
11
|
+ * load assertions
|
|
12
|
+ */
|
|
13
|
+require_once dirname(__FILE__)."/assertions.php";
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+/**
|
|
18
|
+ * Constants and globals
|
|
19
|
+ */
|
|
20
|
+if(!defined('DS')) define("DS", DIRECTORY_SEPARATOR);
|
|
21
|
+
|
|
22
|
+if(!array_key_exists("limonade", $GLOBALS))
|
|
23
|
+ $GLOBALS["limonade"] = array();
|
|
24
|
+if(!array_key_exists("test_cases", $GLOBALS["limonade"]))
|
|
25
|
+ $GLOBALS["limonade"]["test_cases"] = array();
|
|
26
|
+if(!array_key_exists("test_errors", $GLOBALS["limonade"]))
|
|
27
|
+ $GLOBALS["limonade"]["test_errors"] = array();
|
|
28
|
+if(!array_key_exists("test_case_current", $GLOBALS["limonade"]))
|
|
29
|
+ $GLOBALS["limonade"]["test_case_current"] = NULL;
|
|
30
|
+if(!array_key_exists("test_suites", $GLOBALS["limonade"]))
|
|
31
|
+ $GLOBALS["limonade"]["test_suites"] = NULL;
|
|
32
|
+
|
|
33
|
+ini_set("display_errors", true);
|
|
34
|
+error_reporting(E_ALL ^ (E_USER_WARNING | E_NOTICE | E_USER_NOTICE));
|
|
35
|
+// error_reporting(E_ALL | E_STRICT);
|
|
36
|
+assert_options(ASSERT_ACTIVE, 1);
|
|
37
|
+assert_options(ASSERT_WARNING, 0);
|
|
38
|
+assert_options(ASSERT_BAIL, 0);
|
|
39
|
+assert_options(ASSERT_QUIET_EVAL, 0);
|
|
40
|
+assert_options(ASSERT_CALLBACK, 'test_assert_failure');
|
|
41
|
+
|
|
42
|
+# TODO: separate display from logic
|
|
43
|
+# TODO: clean results output
|
|
44
|
+# TODO: add all tests results
|
|
45
|
+
|
|
46
|
+/**
|
|
47
|
+ * Starts a test suite
|
|
48
|
+ *
|
|
49
|
+ * @param string $name
|
|
50
|
+ * @return void
|
|
51
|
+ */
|
|
52
|
+function test_suite($name)
|
|
53
|
+{
|
|
54
|
+ $GLOBALS["limonade"]["test_suites"] = $name;
|
|
55
|
+ echo test_cli_format("===========================================================\n", 'white');
|
|
56
|
+ echo test_cli_format(">>>> START $name tests suites\n", 'white');
|
|
57
|
+ echo test_cli_format("-----------------------------------------------------------\n", 'white');
|
|
58
|
+}
|
|
59
|
+
|
|
60
|
+/**
|
|
61
|
+ * Ends the last group of test suites
|
|
62
|
+ *
|
|
63
|
+ * @return void
|
|
64
|
+ */
|
|
65
|
+function end_test_suite()
|
|
66
|
+{
|
|
67
|
+ $name = $GLOBALS["limonade"]["test_suites"];
|
|
68
|
+ $failures = 0;
|
|
69
|
+ $tests = 0;
|
|
70
|
+ $passed_tests = 0;
|
|
71
|
+ $assertions = 0;
|
|
72
|
+
|
|
73
|
+ foreach($GLOBALS["limonade"]["test_cases"] as $test)
|
|
74
|
+ {
|
|
75
|
+ $failures += $test['failures'];
|
|
76
|
+ $assertions += $test['assertions'];
|
|
77
|
+ if(empty($test['failures'])) $passed_tests++;
|
|
78
|
+ $tests++;
|
|
79
|
+ }
|
|
80
|
+ echo ">> ENDING $name tests suites\n ";
|
|
81
|
+ echo $failures > 0 ? test_cli_format("|FAILED!|", "red") : test_cli_format("|PASSED|", "green");;
|
|
82
|
+ echo " Passes ".$passed_tests."/".$tests.", ";
|
|
83
|
+ echo " {$failures} failures for {$assertions} assertions.\n";
|
|
84
|
+ echo test_cli_format("===========================================================\n", 'white');
|
|
85
|
+}
|
|
86
|
+
|
|
87
|
+/**
|
|
88
|
+ * Starting a new test case
|
|
89
|
+ *
|
|
90
|
+ * @param string $name
|
|
91
|
+ * @return void
|
|
92
|
+ */
|
|
93
|
+function test_case($name)
|
|
94
|
+{
|
|
95
|
+ $name = strtolower($name); // TODO: normalize name
|
|
96
|
+
|
|
97
|
+ if(!array_key_exists($name, $GLOBALS["limonade"]["test_cases"]))
|
|
98
|
+ {
|
|
99
|
+ $GLOBALS["limonade"]["test_cases"][$name] = array(
|
|
100
|
+ "name" => $name,
|
|
101
|
+ "assertions" => 0,
|
|
102
|
+ "failures" => 0,
|
|
103
|
+ "description" => NULL
|
|
104
|
+ );
|
|
105
|
+ $GLOBALS["limonade"]["test_case_current"] = $name;
|
|
106
|
+ }
|
|
107
|
+ else
|
|
108
|
+ {
|
|
109
|
+
|
|
110
|
+ }
|
|
111
|
+}
|
|
112
|
+
|
|
113
|
+/**
|
|
114
|
+ * Displays and ending the current tests suite
|
|
115
|
+ *
|
|
116
|
+ * @return void
|
|
117
|
+ */
|
|
118
|
+function end_test_case()
|
|
119
|
+{
|
|
120
|
+ $name = $GLOBALS["limonade"]["test_case_current"];
|
|
121
|
+ echo "## ".strtoupper($name)."\n";
|
|
122
|
+
|
|
123
|
+ $desc = test_case_describe();
|
|
124
|
+ if(!is_null($desc)) echo $desc."\n";
|
|
125
|
+
|
|
126
|
+ echo "- - - - - - - - - - - - - - - - - - - - - - - - - - - - - -\n";
|
|
127
|
+
|
|
128
|
+ test_case_execute_current();
|
|
129
|
+
|
|
130
|
+
|
|
131
|
+ if(!is_null($name))
|
|
132
|
+ {
|
|
133
|
+ $test = $GLOBALS["limonade"]["test_cases"][$name];
|
|
134
|
+ // closing previous test
|
|
135
|
+ echo "\n- - - - - - - - - - - - - - - - - - - - - - - - - - - - - -\n";
|
|
136
|
+ echo $test['failures'] > 0 ? test_cli_format("|FAILED!|", "red") : test_cli_format("|PASSED|", "green");
|
|
137
|
+ echo " Test case '$name' finished: ";
|
|
138
|
+ echo count(test_case_all_func())." tests, ";
|
|
139
|
+ echo " {$test['failures']} failures for {$test['assertions']} assertions.\n";
|
|
140
|
+
|
|
141
|
+ echo "-----------------------------------------------------------\n";
|
|
142
|
+ }
|
|
143
|
+ $GLOBALS["limonade"]["test_case_current"] = null;
|
|
144
|
+}
|
|
145
|
+
|
|
146
|
+/**
|
|
147
|
+ * Describes the current tests suite
|
|
148
|
+ *
|
|
149
|
+ * @param string $msg
|
|
150
|
+ * @return string tests description
|
|
151
|
+ */
|
|
152
|
+function test_case_describe($msg = NULL)
|
|
153
|
+{
|
|
154
|
+ $test =& test_case_current();
|
|
155
|
+ if(!is_null($msg))
|
|
156
|
+ {
|
|
157
|
+ $test["description"] = $msg;
|
|
158
|
+ }
|
|
159
|
+ //var_dump($test["description"]);
|
|
160
|
+ return $test["description"];
|
|
161
|
+}
|
|
162
|
+
|
|
163
|
+/**
|
|
164
|
+ * Returns all user test case functions
|
|
165
|
+ *
|
|
166
|
+ * @access private
|
|
167
|
+ * @return void
|
|
168
|
+ */
|
|
169
|
+function test_case_all_func()
|
|
170
|
+{
|
|
171
|
+ $functions = get_defined_functions();
|
|
172
|
+ $functions = $functions['user'];
|
|
173
|
+ $tests = array();
|
|
174
|
+ $name = $GLOBALS["limonade"]["test_case_current"];
|
|
175
|
+ while ($func = array_shift($functions)) {
|
|
176
|
+ $regexp = "/^test_{$name}_(.*)$/";
|
|
177
|
+ if(!preg_match($regexp, $func)) continue;
|
|
178
|
+ if($func == test_before_func_name()) continue;
|
|
179
|
+ // TODO: adding break for all test api methods
|
|
180
|
+
|
|
181
|
+ $tests[] = $func;
|
|
182
|
+ }
|
|
183
|
+ return $tests;
|
|
184
|
+}
|
|
185
|
+
|
|
186
|
+/**
|
|
187
|
+ * Execute current test case
|
|
188
|
+ *
|
|
189
|
+ * @access private
|
|
190
|
+ * @return void
|
|
191
|
+ */
|
|
192
|
+function test_case_execute_current()
|
|
193
|
+{
|
|
194
|
+ $tests = test_case_all_func();
|
|
195
|
+ while($func = array_shift($tests))
|
|
196
|
+ {
|
|
197
|
+ test_call_func(test_before_func_name());
|
|
198
|
+ call_user_func($func);
|
|
199
|
+ }
|
|
200
|
+}
|
|
201
|
+
|
|
202
|
+
|
|
203
|
+function &test_case_current()
|
|
204
|
+{
|
|
205
|
+ $name = $GLOBALS["limonade"]["test_case_current"];
|
|
206
|
+ return $GLOBALS["limonade"]["test_cases"][$name];
|
|
207
|
+}
|
|
208
|
+
|
|
209
|
+
|
|
210
|
+
|
|
211
|
+function test_before_func_name()
|
|
212
|
+{
|
|
213
|
+ $test = test_case_current();
|
|
214
|
+ $func = "before_each_test_in_".$test["name"];
|
|
215
|
+ return $func;
|
|
216
|
+}
|
|
217
|
+
|
|
218
|
+function test_before_assert_func_name()
|
|
219
|
+{
|
|
220
|
+ $test = test_case_current();
|
|
221
|
+ $func = "before_each_assert_in_$name".$test["name"];
|
|
222
|
+ return $func;
|
|
223
|
+}
|
|
224
|
+
|
|
225
|
+function test_run_assertion()
|
|
226
|
+{
|
|
227
|
+ $name = $GLOBALS["limonade"]["test_case_current"];
|
|
228
|
+ $GLOBALS["limonade"]["test_cases"][$name]['assertions']++;
|
|
229
|
+ test_call_func(test_before_assert_func_name());
|
|
230
|
+}
|
|
231
|
+
|
|
232
|
+/**
|
|
233
|
+ * Calls a function if exists
|
|
234
|
+ *
|
|
235
|
+ * @param string $func the function name
|
|
236
|
+ * @param mixed $arg,.. (optional)
|
|
237
|
+ * @return mixed
|
|
238
|
+ */
|
|
239
|
+function test_call_func($func)
|
|
240
|
+{
|
|
241
|
+ if(empty($func)) return;
|
|
242
|
+ $args = func_get_args();
|
|
243
|
+ $func = array_shift($args);
|
|
244
|
+ if(function_exists($func)) return call_user_func_array($func, $args);
|
|
245
|
+ return;
|
|
246
|
+}
|
|
247
|
+
|
|
248
|
+/**
|
|
249
|
+ * Error handler
|
|
250
|
+ *
|
|
251
|
+ * @access private
|
|
252
|
+ * @return boolean true
|
|
253
|
+ */
|
|
254
|
+function test_error_handler($errno, $errstr, $errfile, $errline)
|
|
255
|
+{
|
|
256
|
+ if($errno < E_USER_ERROR || $errno > E_USER_NOTICE)
|
|
257
|
+ echo test_cli_format("!!! ERROR", "red") . " [$errno], $errstr in $errfile at line $errline\n";
|
|
258
|
+ $GLOBALS["limonade"]["test_errors"][] = array($errno, $errstr, $errfile, $errline);
|
|
259
|
+ return true;
|
|
260
|
+}
|
|
261
|
+
|
|
262
|
+/**
|
|
263
|
+ * Assert callback
|
|
264
|
+ *
|
|
265
|
+ * @access private
|
|
266
|
+ * @param string $script
|
|
267
|
+ * @param string $line
|
|
268
|
+ * @param string $message
|
|
269
|
+ * @return void
|
|
270
|
+ */
|
|
271
|
+function test_assert_failure($script, $line, $message)
|
|
272
|
+{
|
|
273
|
+ // Using the stack trace, find the outermost assert*() call
|
|
274
|
+ $stacktrace = array_slice(debug_backtrace(), 1); // skip self
|
|
275
|
+ $assertion = reset($stacktrace);
|
|
276
|
+ while ($stackframe = array_shift($stacktrace)) {
|
|
277
|
+ if (!preg_match('/^assert/', $stackframe['function']))
|
|
278
|
+ break;
|
|
279
|
+ $assertion = $stackframe;
|
|
280
|
+ }
|
|
281
|
+
|
|
282
|
+ extract($assertion, EXTR_PREFIX_ALL, 'assert');
|
|
283
|
+ $code = explode("\n", file_get_contents($assert_file));
|
|
284
|
+ $code = trim($code[$assert_line - 1]);
|
|
285
|
+
|
|
286
|
+ list($assert_code, $message) = explode("//", $message);
|
|
287
|
+ echo test_cli_format("Assertion failed", "yellow");
|
|
288
|
+ echo " in script *{$assert_file}* (line {$assert_line}):\n";
|
|
289
|
+ echo " * assertion: $code\n";
|
|
290
|
+ echo " * message: $message\n";
|
|
291
|
+ $name = $GLOBALS["limonade"]["test_case_current"];
|
|
292
|
+ $GLOBALS["limonade"]["test_cases"][$name]['failures']++;
|
|
293
|
+}
|
|
294
|
+
|
|
295
|
+function test_cli_format($text, $format) {
|
|
296
|
+ $formats = array(
|
|
297
|
+ "blue" => 34,
|
|
298
|
+ "bold" => 1,
|
|
299
|
+ "green" => 32,
|
|
300
|
+ "highlight" => 7,
|
|
301
|
+ "light_blue" => 36,
|
|
302
|
+ "purple" => 35,
|
|
303
|
+ "red" => 31,
|
|
304
|
+ "underline" => 4,
|
|
305
|
+ "white" => 37,
|
|
306
|
+ "yellow" => 33
|
|
307
|
+ );
|
|
308
|
+
|
|
309
|
+ if (array_key_exists($format, $formats)) $format = $formats[$format];
|
|
310
|
+ return chr(27) . "[01;{$format} m{$text}" . chr(27) . "[00m";
|
|
311
|
+}
|
|
312
|
+
|
|
313
|
+/**
|
|
314
|
+ * Do HTTP request and return the response content.
|
|
315
|
+ *
|
|
316
|
+ * @param string $url
|
|
317
|
+ * @param string $method
|
|
318
|
+ * @param bool $include_header
|
|
319
|
+ * @return string
|
|
320
|
+ * @author Nando Vieira
|
|
321
|
+ */
|
|
322
|
+function test_request($url, $method="GET", $include_header=false, $post_data=array(), $http_header=array()) {
|
|
323
|
+ $method = strtoupper($method);
|
|
324
|
+ $allowed_methods = array("GET", "PUT", "POST", "DELETE", "HEAD");
|
|
325
|
+ if(!in_array($method, $allowed_methods))
|
|
326
|
+ {
|
|
327
|
+ $message = "The requested method '$method' is not allowed";
|
|
328
|
+ return assert('false; //'.$message);
|
|
329
|
+ }
|
|
330
|
+
|
|
331
|
+ $curl = curl_init($url);
|
|
332
|
+ curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
|
|
333
|
+ curl_setopt($curl, CURLOPT_HEADER, $include_header);
|
|
334
|
+ curl_setopt($curl, CURLOPT_HTTPHEADER, $http_header);
|
|
335
|
+ curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $method);
|
|
336
|
+ curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
|
|
337
|
+ if($method == 'POST' || $method == 'PUT')
|
|
338
|
+ {
|
|
339
|
+ curl_setopt($curl, CURLOPT_POST, 1);
|
|
340
|
+ curl_setopt($curl, CURLOPT_POSTFIELDS, $post_data);
|
|
341
|
+ }
|
|
342
|
+ if($method == 'HEAD')
|
|
343
|
+ {
|
|
344
|
+ curl_setopt($curl, CURLOPT_NOBODY, true);
|
|
345
|
+ }
|
|
346
|
+ $response = curl_exec($curl);
|
|
347
|
+ curl_close($curl);
|
|
348
|
+
|
|
349
|
+ return $response;
|
|
350
|
+}
|