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