PageRenderTime 51ms CodeModel.GetById 5ms RepoModel.GetById 0ms app.codeStats 1ms

/libraries/Unit_test.php

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