PageRenderTime 59ms CodeModel.GetById 33ms RepoModel.GetById 0ms app.codeStats 0ms

/snap.php

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