PageRenderTime 55ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/core/snap/snap.php

http://github.com/Jakobo/snaptest
PHP | 208 lines | 105 code | 39 blank | 64 comment | 17 complexity | 4b1d73b5b9703c54c9052156fbe6677c MD5 | raw file
Possible License(s): BSD-3-Clause
  1. <?php
  2. // turn on all errors. everything. yes, everything
  3. //error_reporting(E_ERROR | E_WARNING | E_PARSE | E_NOTICE | E_CORE_ERROR | E_CORE_WARNING);
  4. /**
  5. * Snap error handling function. Takes care of PHP errors, and redirects to the current test
  6. * @param int $errno the error number
  7. * @param string $errstr the string of the error
  8. * @param string $errfile the file the error was in
  9. * @param int $errline the line triggered the error
  10. * @return TRUE
  11. */
  12. function SNAP_error_handler($errno, $errstr, $errfile, $errline) {
  13. global $SNAP_Current_Reporter_Running;
  14. global $SNAP_Current_Test_Running;
  15. if ($SNAP_Current_Test_Running->canError()) {
  16. return TRUE;
  17. }
  18. $trace = debug_backtrace();
  19. // call a php error on the snap test object running
  20. $SNAP_Current_Reporter_Running->recordPHPError($errstr, $errfile, $errline, $trace);
  21. // let it go through to php log
  22. return TRUE;
  23. }
  24. /**
  25. * Main tester class, used for putting together input and output handlers
  26. */
  27. class Snap_Tester {
  28. protected $output;
  29. protected $input;
  30. protected $tests;
  31. /**
  32. * Constructor, defines an output type
  33. * on creation, it sets the output handler and arranges
  34. * the array of tests to run
  35. * @param string $output the output handler name
  36. */
  37. public function __construct($output_type, $test_count = NULL) {
  38. $this->tests = array();
  39. $this->setOutput($output_type, $test_count);
  40. }
  41. /**
  42. * add tests via an input handler
  43. * @param string $input_handler the name of the input handler
  44. * @param array $params the list for the input handler, such as a list of files
  45. */
  46. public function addInput($input_handler, $params = array()) {
  47. if (is_scalar($params)) {
  48. $params = array($params);
  49. }
  50. $input_handler = strtolower($input_handler);
  51. if ($input_handler == 'local') {
  52. $this->addTests($params);
  53. return;
  54. }
  55. $c = $this->getTesterClass($input_handler, 'loader');
  56. $c = new $c();
  57. foreach($params as $item) {
  58. $c->add($item);
  59. }
  60. $this->addTests($c->getTests());
  61. }
  62. /**
  63. * Get the output class
  64. * @return Snap_UnitTestReporter
  65. **/
  66. public function getOutput() {
  67. return $this->output;
  68. }
  69. /**
  70. * run all loaded tests
  71. * the results of running all tests are then logged into the reporter
  72. * and the output is generated
  73. */
  74. public function runTests($match = NULL) {
  75. $this->tests = array_flip(array_flip($this->tests));
  76. foreach ($this->tests as $test_name) {
  77. $test = new $test_name();
  78. if ($match === NULL) {
  79. $test->runTests($this->getOutput());
  80. }
  81. else {
  82. $test->runTests($this->getOutput(), $match);
  83. }
  84. }
  85. $this->getOutput()->createReport();
  86. $this->getOutput()->generateFooter();
  87. return TRUE;
  88. }
  89. /**
  90. * set the output handler
  91. * @param string $output_type the name of an output handler
  92. */
  93. protected function setOutput($output_type, $test_count) {
  94. $output = $this->getTesterClass($output_type, 'reporter');
  95. $this->output = new $output($test_count);
  96. }
  97. /**
  98. * adds tests to the test stack
  99. * @param array $tests an array of tests to add
  100. */
  101. protected function addTests($tests) {
  102. $this->tests = array_merge($this->tests, $tests);
  103. }
  104. /**
  105. * resolves a tester to the proper name, serves as a factory method
  106. * @param string $name the name of the handler to load
  107. * @param string $type the type of handler, input or output
  108. * @return string a valid classname
  109. * @throws Snap_Exception
  110. */
  111. protected function getTesterClass($name, $type) {
  112. if ($type == 'reporter') {
  113. $suffix = 'UnitTestReporter';
  114. }
  115. else {
  116. $suffix = 'UnitTestLoader';
  117. }
  118. $class_name = 'Snap_'.ucwords(strtolower($name)).'_'.$suffix;
  119. // if class does not exist, include
  120. if (!class_exists($class_name)) {
  121. $path = SNAPTEST_CORE . $type . DIRECTORY_SEPARATOR . $type.'s'.DIRECTORY_SEPARATOR.strtolower($name).'.php';
  122. include $path;
  123. }
  124. // if class still does not exist, this is an error
  125. if (!class_exists($class_name)) {
  126. throw new Snap_Exception('Handler '.$class_name.' is not found, tried path '.$path);
  127. }
  128. return $class_name;
  129. }
  130. }
  131. // load all addons
  132. // we scan ./addons within the current directory, pulling out all .php files
  133. // and include_once on them. After each include, we scan $config
  134. // and report on what was loaded. This is then serialized and stored as a
  135. // constant, so that any module can read it.
  136. $handle = opendir(SNAPTEST_ROOT.'addons');
  137. $addon_report = array();
  138. while (FALSE !== ($file = readdir($handle))) {
  139. // skip files starting with .
  140. if (substr($file, 0, 1) == '.') {
  141. continue;
  142. }
  143. // skip directories
  144. if (is_dir($file)) {
  145. continue;
  146. }
  147. // skip things not ending in .php
  148. if (substr($file, -4) != '.php') {
  149. continue;
  150. }
  151. // valid addon, clear config and load
  152. $config = array();
  153. include_once SNAPTEST_ROOT.'addons'.DIRECTORY_SEPARATOR.$file;
  154. $addon_report[] = array(
  155. 'name' => $config['name'],
  156. 'version' => $config['version'],
  157. 'author' => $config['author'],
  158. 'description' => $config['description'],
  159. );
  160. // free mem in case of big config
  161. unset($config);
  162. }
  163. // do our define, then unset to clean up
  164. define('SNAP_ADDONS', serialize($addon_report));
  165. unset($handle);
  166. unset($addon_report);
  167. // ensure after init errors are proper
  168. error_reporting(E_ALL);
  169. ini_set('display_errors', TRUE);