PageRenderTime 44ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/system/libraries/Unit_test.php

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