Take two:
[www-register-wizard.git] / libraries / Unit_test.php
1 <?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');\r
2 /**\r
3  * CodeIgniter\r
4  *\r
5  * An open source application development framework for PHP 4.3.2 or newer\r
6  *\r
7  * @package             CodeIgniter\r
8  * @author              ExpressionEngine Dev Team\r
9  * @copyright   Copyright (c) 2008, EllisLab, Inc.\r
10  * @license             http://codeigniter.com/user_guide/license.html\r
11  * @link                http://codeigniter.com\r
12  * @since               Version 1.3.1\r
13  * @filesource\r
14  */\r
15 \r
16 // ------------------------------------------------------------------------\r
17 \r
18 /**\r
19  * Unit Testing Class\r
20  *\r
21  * Simple testing class\r
22  *\r
23  * @package             CodeIgniter\r
24  * @subpackage  Libraries\r
25  * @category    UnitTesting\r
26  * @author              ExpressionEngine Dev Team\r
27  * @link                http://codeigniter.com/user_guide/libraries/uri.html\r
28  */\r
29 class CI_Unit_test {\r
30 \r
31         var $active                     = TRUE;\r
32         var $results            = array();\r
33         var $strict                     = FALSE;\r
34         var $_template          = NULL;\r
35         var $_template_rows     = NULL;\r
36 \r
37         function CI_Unit_test()\r
38         {\r
39                 log_message('debug', "Unit Testing Class Initialized");\r
40         }       \r
41 \r
42         // --------------------------------------------------------------------\r
43         \r
44         /**\r
45          * Run the tests\r
46          *\r
47          * Runs the supplied tests\r
48          *\r
49          * @access      public\r
50          * @param       mixed\r
51          * @param       mixed\r
52          * @param       string\r
53          * @return      string\r
54          */     \r
55         function run($test, $expected = TRUE, $test_name = 'undefined')\r
56         {\r
57                 if ($this->active == FALSE)\r
58                 {\r
59                         return FALSE;\r
60                 }\r
61         \r
62                 if (in_array($expected, array('is_string', 'is_bool', 'is_true', 'is_false', 'is_int', 'is_numeric', 'is_float', 'is_double', 'is_array', 'is_null'), TRUE))\r
63                 {\r
64                         $expected = str_replace('is_float', 'is_double', $expected);\r
65                         $result = ($expected($test)) ? TRUE : FALSE;    \r
66                         $extype = str_replace(array('true', 'false'), 'bool', str_replace('is_', '', $expected));\r
67                 }\r
68                 else\r
69                 {\r
70                         if ($this->strict == TRUE)\r
71                                 $result = ($test === $expected) ? TRUE : FALSE; \r
72                         else\r
73                                 $result = ($test == $expected) ? TRUE : FALSE;  \r
74                         \r
75                         $extype = gettype($expected);\r
76                 }\r
77                                 \r
78                 $back = $this->_backtrace();\r
79         \r
80                 $report[] = array (\r
81                                                         'test_name'                     => $test_name,\r
82                                                         'test_datatype'         => gettype($test),\r
83                                                         'res_datatype'          => $extype,\r
84                                                         'result'                        => ($result === TRUE) ? 'passed' : 'failed',\r
85                                                         'file'                          => $back['file'],\r
86                                                         'line'                          => $back['line']\r
87                                                 );\r
88 \r
89                 $this->results[] = $report;             \r
90                                 \r
91                 return($this->report($this->result($report)));\r
92         }\r
93 \r
94         // --------------------------------------------------------------------\r
95         \r
96         /**\r
97          * Generate a report\r
98          *\r
99          * Displays a table with the test data\r
100          *\r
101          * @access      public\r
102          * @return      string\r
103          */\r
104         function report($result = array())\r
105         {\r
106                 if (count($result) == 0)\r
107                 {\r
108                         $result = $this->result();\r
109                 }\r
110 \r
111                 $CI =& get_instance();\r
112                 $CI->load->language('unit_test');\r
113 \r
114                 $this->_parse_template();\r
115 \r
116                 $r = '';\r
117                 foreach ($result as $res)\r
118                 {\r
119                         $table = '';\r
120 \r
121                         foreach ($res as $key => $val)\r
122                         {\r
123 \r
124                                 if ($key == $CI->lang->line('ut_result'))\r
125                                 {\r
126                                         if ($val == $CI->lang->line('ut_passed'))\r
127                                         {\r
128                                                 $val = '<span style="color: #0C0;">'.$val.'</span>';\r
129                                         }\r
130                                         elseif ($val == $CI->lang->line('ut_failed'))\r
131                                         {\r
132                                                 $val = '<span style="color: #C00;">'.$val.'</span>';\r
133                                         }\r
134                                 }\r
135 \r
136                                 $temp = $this->_template_rows;\r
137                                 $temp = str_replace('{item}', $key, $temp);\r
138                                 $temp = str_replace('{result}', $val, $temp);\r
139                                 $table .= $temp;\r
140                         }\r
141 \r
142                         $r .= str_replace('{rows}', $table, $this->_template);\r
143                 }\r
144 \r
145                 return $r;\r
146         }\r
147         \r
148         // --------------------------------------------------------------------\r
149         \r
150         /**\r
151          * Use strict comparison\r
152          *\r
153          * Causes the evaluation to use === rather than ==\r
154          *\r
155          * @access      public\r
156          * @param       bool\r
157          * @return      null\r
158          */\r
159         function use_strict($state = TRUE)\r
160         {\r
161                 $this->strict = ($state == FALSE) ? FALSE : TRUE;\r
162         }\r
163         \r
164         // --------------------------------------------------------------------\r
165         \r
166         /**\r
167          * Make Unit testing active\r
168          *\r
169          * Enables/disables unit testing\r
170          *\r
171          * @access      public\r
172          * @param       bool\r
173          * @return      null\r
174          */\r
175         function active($state = TRUE)\r
176         {\r
177                 $this->active = ($state == FALSE) ? FALSE : TRUE;\r
178         }\r
179         \r
180         // --------------------------------------------------------------------\r
181         \r
182         /**\r
183          * Result Array\r
184          *\r
185          * Returns the raw result data\r
186          *\r
187          * @access      public\r
188          * @return      array\r
189          */\r
190         function result($results = array())\r
191         {       \r
192                 $CI =& get_instance();\r
193                 $CI->load->language('unit_test');\r
194                 \r
195                 if (count($results) == 0)\r
196                 {\r
197                         $results = $this->results;\r
198                 }\r
199                 \r
200                 $retval = array();\r
201                 foreach ($results as $result)\r
202                 {\r
203                         $temp = array();\r
204                         foreach ($result as $key => $val)\r
205                         {\r
206                                 if (is_array($val))\r
207                                 {\r
208                                         foreach ($val as $k => $v)\r
209                                         {\r
210                                                 if (FALSE !== ($line = $CI->lang->line(strtolower('ut_'.$v))))\r
211                                                 {\r
212                                                         $v = $line;\r
213                                                 }                               \r
214                                                 $temp[$CI->lang->line('ut_'.$k)] = $v;                                  \r
215                                         }\r
216                                 }\r
217                                 else\r
218                                 {\r
219                                         if (FALSE !== ($line = $CI->lang->line(strtolower('ut_'.$val))))\r
220                                         {\r
221                                                 $val = $line;\r
222                                         }                               \r
223                                         $temp[$CI->lang->line('ut_'.$key)] = $val;\r
224                                 }\r
225                         }\r
226                         \r
227                         $retval[] = $temp;\r
228                 }\r
229         \r
230                 return $retval;\r
231         }\r
232         \r
233         // --------------------------------------------------------------------\r
234         \r
235         /**\r
236          * Set the template\r
237          *\r
238          * This lets us set the template to be used to display results\r
239          *\r
240          * @access      public\r
241          * @param       string\r
242          * @return      void\r
243          */     \r
244         function set_template($template)\r
245         {\r
246                 $this->_template = $template;\r
247         }\r
248         \r
249         // --------------------------------------------------------------------\r
250         \r
251         /**\r
252          * Generate a backtrace\r
253          *\r
254          * This lets us show file names and line numbers\r
255          *\r
256          * @access      private\r
257          * @return      array\r
258          */\r
259         function _backtrace()\r
260         {\r
261                 if (function_exists('debug_backtrace'))\r
262                 {\r
263                         $back = debug_backtrace();\r
264                         \r
265                         $file = ( ! isset($back['1']['file'])) ? '' : $back['1']['file'];\r
266                         $line = ( ! isset($back['1']['line'])) ? '' : $back['1']['line'];\r
267                                                 \r
268                         return array('file' => $file, 'line' => $line);\r
269                 }\r
270                 return array('file' => 'Unknown', 'line' => 'Unknown');\r
271         }\r
272 \r
273         // --------------------------------------------------------------------\r
274         \r
275         /**\r
276          * Get Default Template\r
277          *\r
278          * @access      private\r
279          * @return      string\r
280          */\r
281         function _default_template()\r
282         {       \r
283                 $this->_template = "\n".'<table style="width:100%; font-size:small; margin:10px 0; border-collapse:collapse; border:1px solid #CCC;">';\r
284                 $this->_template .= '{rows}';\r
285                 $this->_template .= "\n".'</table>';\r
286                 \r
287                 $this->_template_rows = "\n\t".'<tr>';\r
288                 $this->_template_rows .= "\n\t\t".'<th style="text-align: left; border-bottom:1px solid #CCC;">{item}</th>';\r
289                 $this->_template_rows .= "\n\t\t".'<td style="border-bottom:1px solid #CCC;">{result}</td>';\r
290                 $this->_template_rows .= "\n\t".'</tr>';        \r
291         }\r
292         \r
293         // --------------------------------------------------------------------\r
294 \r
295         /**\r
296          * Parse Template\r
297          *\r
298          * Harvests the data within the template {pseudo-variables}\r
299          *\r
300          * @access      private\r
301          * @return      void\r
302          */\r
303         function _parse_template()\r
304         {\r
305                 if ( ! is_null($this->_template_rows))\r
306                 {\r
307                         return;\r
308                 }\r
309                 \r
310                 if (is_null($this->_template))\r
311                 {\r
312                         $this->_default_template();\r
313                         return;\r
314                 }\r
315                 \r
316                 if ( ! preg_match("/\{rows\}(.*?)\{\/rows\}/si", $this->_template, $match))\r
317                 {\r
318                         $this->_default_template();\r
319                         return;\r
320                 }\r
321 \r
322                 $this->_template_rows = $match['1'];\r
323                 $this->_template = str_replace($match['0'], '{rows}', $this->_template);        \r
324         }\r
325         \r
326 }\r
327 // END Unit_test Class\r
328 \r
329 /**\r
330  * Helper functions to test boolean true/false\r
331  *\r
332  *\r
333  * @access      private\r
334  * @return      bool\r
335  */\r
336 function is_true($test)\r
337 {\r
338         return (is_bool($test) AND $test === TRUE) ? TRUE : FALSE;\r
339 }\r
340 function is_false($test)\r
341 {\r
342         return (is_bool($test) AND $test === FALSE) ? TRUE : FALSE;\r
343 }\r
344 \r
345 \r
346 /* End of file Unit_test.php */\r
347 /* Location: ./system/libraries/Unit_test.php */