PageRenderTime 40ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/sp_1/system/libraries/Unit_test.php

https://gitlab.com/rezaul007/Hospital-information-bank
PHP | 405 lines | 167 code | 53 blank | 185 comment | 13 complexity | 78b4439293aae467c9691a03fe63e22f MD5 | raw file
  1. <?php
  2. /**
  3. * CodeIgniter
  4. *
  5. * An open source application development framework for PHP
  6. *
  7. * This content is released under the MIT License (MIT)
  8. *
  9. * Copyright (c) 2014 - 2015, British Columbia Institute of Technology
  10. *
  11. * Permission is hereby granted, free of charge, to any person obtaining a copy
  12. * of this software and associated documentation files (the "Software"), to deal
  13. * in the Software without restriction, including without limitation the rights
  14. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  15. * copies of the Software, and to permit persons to whom the Software is
  16. * furnished to do so, subject to the following conditions:
  17. *
  18. * The above copyright notice and this permission notice shall be included in
  19. * all copies or substantial portions of the Software.
  20. *
  21. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  22. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  23. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  24. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  25. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  26. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  27. * THE SOFTWARE.
  28. *
  29. * @package CodeIgniter
  30. * @author EllisLab Dev Team
  31. * @copyright Copyright (c) 2008 - 2014, EllisLab, Inc. (http://ellislab.com/)
  32. * @copyright Copyright (c) 2014 - 2015, British Columbia Institute of Technology (http://bcit.ca/)
  33. * @license http://opensource.org/licenses/MIT MIT License
  34. * @link http://codeigniter.com
  35. * @since Version 1.3.1
  36. * @filesource
  37. */
  38. defined('BASEPATH') OR exit('No direct script access allowed');
  39. /**
  40. * Unit Testing Class
  41. *
  42. * Simple testing class
  43. *
  44. * @package CodeIgniter
  45. * @subpackage Libraries
  46. * @category UnitTesting
  47. * @author EllisLab Dev Team
  48. * @link http://codeigniter.com/user_guide/libraries/unit_testing.html
  49. */
  50. class CI_Unit_test {
  51. /**
  52. * Active flag
  53. *
  54. * @var bool
  55. */
  56. public $active = TRUE;
  57. /**
  58. * Test results
  59. *
  60. * @var array
  61. */
  62. public $results = array();
  63. /**
  64. * Strict comparison flag
  65. *
  66. * Whether to use === or == when comparing
  67. *
  68. * @var bool
  69. */
  70. public $strict = FALSE;
  71. /**
  72. * Template
  73. *
  74. * @var string
  75. */
  76. protected $_template = NULL;
  77. /**
  78. * Template rows
  79. *
  80. * @var string
  81. */
  82. protected $_template_rows = NULL;
  83. /**
  84. * List of visible test items
  85. *
  86. * @var array
  87. */
  88. protected $_test_items_visible = array(
  89. 'test_name',
  90. 'test_datatype',
  91. 'res_datatype',
  92. 'result',
  93. 'file',
  94. 'line',
  95. 'notes'
  96. );
  97. // --------------------------------------------------------------------
  98. /**
  99. * Constructor
  100. *
  101. * @return void
  102. */
  103. public function __construct()
  104. {
  105. log_message('info', 'Unit Testing Class Initialized');
  106. }
  107. // --------------------------------------------------------------------
  108. /**
  109. * Run the tests
  110. *
  111. * Runs the supplied tests
  112. *
  113. * @param array $items
  114. * @return void
  115. */
  116. public function set_test_items($items)
  117. {
  118. if ( ! empty($items) && is_array($items))
  119. {
  120. $this->_test_items_visible = $items;
  121. }
  122. }
  123. // --------------------------------------------------------------------
  124. /**
  125. * Run the tests
  126. *
  127. * Runs the supplied tests
  128. *
  129. * @param mixed $test
  130. * @param mixed $expected
  131. * @param string $test_name
  132. * @param string $notes
  133. * @return string
  134. */
  135. public function run($test, $expected = TRUE, $test_name = 'undefined', $notes = '')
  136. {
  137. if ($this->active === FALSE)
  138. {
  139. return FALSE;
  140. }
  141. 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))
  142. {
  143. $expected = str_replace('is_double', 'is_float', $expected);
  144. $result = $expected($test);
  145. $extype = str_replace(array('true', 'false'), 'bool', str_replace('is_', '', $expected));
  146. }
  147. else
  148. {
  149. $result = ($this->strict === TRUE) ? ($test === $expected) : ($test == $expected);
  150. $extype = gettype($expected);
  151. }
  152. $back = $this->_backtrace();
  153. $report = array (
  154. 'test_name' => $test_name,
  155. 'test_datatype' => gettype($test),
  156. 'res_datatype' => $extype,
  157. 'result' => ($result === TRUE) ? 'passed' : 'failed',
  158. 'file' => $back['file'],
  159. 'line' => $back['line'],
  160. 'notes' => $notes
  161. );
  162. $this->results[] = $report;
  163. return $this->report($this->result(array($report)));
  164. }
  165. // --------------------------------------------------------------------
  166. /**
  167. * Generate a report
  168. *
  169. * Displays a table with the test data
  170. *
  171. * @param array $result
  172. * @return string
  173. */
  174. public function report($result = array())
  175. {
  176. if (count($result) === 0)
  177. {
  178. $result = $this->result();
  179. }
  180. $CI =& get_instance();
  181. $CI->load->language('unit_test');
  182. $this->_parse_template();
  183. $r = '';
  184. foreach ($result as $res)
  185. {
  186. $table = '';
  187. foreach ($res as $key => $val)
  188. {
  189. if ($key === $CI->lang->line('ut_result'))
  190. {
  191. if ($val === $CI->lang->line('ut_passed'))
  192. {
  193. $val = '<span style="color: #0C0;">'.$val.'</span>';
  194. }
  195. elseif ($val === $CI->lang->line('ut_failed'))
  196. {
  197. $val = '<span style="color: #C00;">'.$val.'</span>';
  198. }
  199. }
  200. $table .= str_replace(array('{item}', '{result}'), array($key, $val), $this->_template_rows);
  201. }
  202. $r .= str_replace('{rows}', $table, $this->_template);
  203. }
  204. return $r;
  205. }
  206. // --------------------------------------------------------------------
  207. /**
  208. * Use strict comparison
  209. *
  210. * Causes the evaluation to use === rather than ==
  211. *
  212. * @param bool $state
  213. * @return void
  214. */
  215. public function use_strict($state = TRUE)
  216. {
  217. $this->strict = (bool) $state;
  218. }
  219. // --------------------------------------------------------------------
  220. /**
  221. * Make Unit testing active
  222. *
  223. * Enables/disables unit testing
  224. *
  225. * @param bool
  226. * @return void
  227. */
  228. public function active($state = TRUE)
  229. {
  230. $this->active = (bool) $state;
  231. }
  232. // --------------------------------------------------------------------
  233. /**
  234. * Result Array
  235. *
  236. * Returns the raw result data
  237. *
  238. * @param array $results
  239. * @return array
  240. */
  241. public function result($results = array())
  242. {
  243. $CI =& get_instance();
  244. $CI->load->language('unit_test');
  245. if (count($results) === 0)
  246. {
  247. $results = $this->results;
  248. }
  249. $retval = array();
  250. foreach ($results as $result)
  251. {
  252. $temp = array();
  253. foreach ($result as $key => $val)
  254. {
  255. if ( ! in_array($key, $this->_test_items_visible))
  256. {
  257. continue;
  258. }
  259. if (FALSE !== ($line = $CI->lang->line(strtolower('ut_'.$val), FALSE)))
  260. {
  261. $val = $line;
  262. }
  263. $temp[$CI->lang->line('ut_'.$key, FALSE)] = $val;
  264. }
  265. $retval[] = $temp;
  266. }
  267. return $retval;
  268. }
  269. // --------------------------------------------------------------------
  270. /**
  271. * Set the template
  272. *
  273. * This lets us set the template to be used to display results
  274. *
  275. * @param string
  276. * @return void
  277. */
  278. public function set_template($template)
  279. {
  280. $this->_template = $template;
  281. }
  282. // --------------------------------------------------------------------
  283. /**
  284. * Generate a backtrace
  285. *
  286. * This lets us show file names and line numbers
  287. *
  288. * @return array
  289. */
  290. protected function _backtrace()
  291. {
  292. $back = debug_backtrace();
  293. return array(
  294. 'file' => (isset($back[1]['file']) ? $back[1]['file'] : ''),
  295. 'line' => (isset($back[1]['line']) ? $back[1]['line'] : '')
  296. );
  297. }
  298. // --------------------------------------------------------------------
  299. /**
  300. * Get Default Template
  301. *
  302. * @return string
  303. */
  304. protected function _default_template()
  305. {
  306. $this->_template = "\n".'<table style="width:100%; font-size:small; margin:10px 0; border-collapse:collapse; border:1px solid #CCC;">{rows}'."\n</table>";
  307. $this->_template_rows = "\n\t<tr>\n\t\t".'<th style="text-align: left; border-bottom:1px solid #CCC;">{item}</th>'
  308. ."\n\t\t".'<td style="border-bottom:1px solid #CCC;">{result}</td>'."\n\t</tr>";
  309. }
  310. // --------------------------------------------------------------------
  311. /**
  312. * Parse Template
  313. *
  314. * Harvests the data within the template {pseudo-variables}
  315. *
  316. * @return void
  317. */
  318. protected function _parse_template()
  319. {
  320. if ($this->_template_rows !== NULL)
  321. {
  322. return;
  323. }
  324. if ($this->_template === NULL OR ! preg_match('/\{rows\}(.*?)\{\/rows\}/si', $this->_template, $match))
  325. {
  326. $this->_default_template();
  327. return;
  328. }
  329. $this->_template_rows = $match[1];
  330. $this->_template = str_replace($match[0], '{rows}', $this->_template);
  331. }
  332. }
  333. /**
  334. * Helper function to test boolean TRUE
  335. *
  336. * @param mixed $test
  337. * @return bool
  338. */
  339. function is_true($test)
  340. {
  341. return ($test === TRUE);
  342. }
  343. /**
  344. * Helper function to test boolean FALSE
  345. *
  346. * @param mixed $test
  347. * @return bool
  348. */
  349. function is_false($test)
  350. {
  351. return ($test === FALSE);
  352. }