]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - tests/unit/test.php
postpone all errors, fixed assertion, added memory usage for _DEBUG_TRACE
[SourceForge/phpwiki.git] / tests / unit / test.php
1 #!/usr/local/bin/php -Cq
2 <?php  
3 /* Copyright (C) 2004 Dan Frankowski <dfrankow@cs.umn.edu>
4  * Copyright (C) 2004 Reini Urban <rurban@x-ray.at>
5  *
6  * This file is part of PhpWiki.
7  * 
8  * PhpWiki is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  * 
13  * PhpWiki is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  * 
18  * You should have received a copy of the GNU General Public License
19  * along with PhpWiki; if not, write to the Free Software
20  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
21  */
22
23 /**
24  * Unit tests for PhpWiki. 
25  *
26  * You must have PEAR's PHPUnit package <http://pear.php.net/package/PHPUnit>. 
27  * These tests are unrelated to test/maketest.pl, which do not use PHPUnit.
28  * These tests run from the command-line as well as from the browser.
29  * Use the argv (from cli) or tests (from browser) params to run only certain tests.
30  */
31
32 ####################################################################
33 #
34 # Preamble needed to get the tests to run.
35 #
36 ####################################################################
37
38
39 $cur_dir = getcwd();
40 # Add root dir to the path
41 if (substr(PHP_OS,0,3) == 'WIN')
42     $cur_dir = str_replace("\\","/",$cur_dir);
43 $rootdir = $cur_dir . '/../../';
44 $ini_sep = substr(PHP_OS,0,3) == 'WIN' ? ';' : ':';
45 ini_set('include_path', ini_get('include_path') . $ini_sep . $rootdir);
46
47 # This quiets a warning in config.php
48 $HTTP_SERVER_VARS['REMOTE_ADDR'] = '127.0.0.1';
49 $HTTP_SERVER_VARS['HTTP_USER_AGENT'] = "PHPUnit";
50 define('GROUP_METHOD', 'NONE');
51 define('RATING_STORAGE','WIKIPAGE');
52 define('PHPWIKI_NOMAIN',true);
53
54 # Other needed files
55 define('DEBUG', 9); //_DEBUG_VERBOSE | _DEBUG_TRACE
56 require_once $rootdir.'index.php';
57 require_once $rootdir.'lib/main.php';
58
59 function printSimpleTrace($bt) {
60     //print_r($bt);
61     echo "Traceback:\n";
62     foreach ($bt as $i => $elem) {
63         if (!array_key_exists('file', $elem)) {
64             continue;
65         }
66         print "  " . $elem['file'] . ':' . $elem['line'] . "\n";
67     }
68 }
69
70 # Show lots of detail when an assert() in the code fails
71 function assert_callback( $script, $line, $message ) {
72    echo "assert failed: script ", $script," line ", $line," :";
73    echo "$message";
74    echo "Traceback:\n";
75    printSimpleTrace(debug_backtrace());
76    exit;
77 }
78 $foo = assert_options( ASSERT_CALLBACK, 'assert_callback');
79
80 #
81 # Get error reporting to call back, too
82 #
83 // set the error reporting level for this script
84 error_reporting(E_ALL);
85 // This is too strict, fails on every notice and warning. 
86 /*
87 function myErrorHandler$errno, $errstr, $errfile, $errline) {
88    echo "$errfile: $errline: error# $errno: $errstr\n";
89    echo "Traceback:\n";
90    printSimpleTrace(debug_backtrace());
91 }
92 // The ErrorManager version
93 function _ErrorHandler_CB(&$error) {
94    echo "Traceback:\n";
95    printSimpleTrace(debug_backtrace());
96    if ($error->isFatal()) {
97         $error->errno = E_USER_WARNING;
98         return true; // ignore error
99    }
100    return true;
101 }
102 // set to the user defined error handler
103 // $old_error_handler = set_error_handler("myErrorHandler");
104 // This is already done via _DEBUG_TRACE
105 //$ErrorManager->pushErrorHandler(new WikiFunctionCb('_ErrorHandler_CB'));
106 */
107
108 # This is the test DB backend
109 $db_params                         = array();
110 $db_params['directory']            = $cur_dir . '/.testbox';
111 $db_params['dbtype']               = 'file';
112
113 if (ENABLE_USER_NEW) {
114     class MockUser extends _WikiUser {
115         function MockUser($name, $isSignedIn) {
116             $this->_userid = $name;
117             $this->_isSignedIn = $isSignedIn;
118         }
119         function isSignedIn() {
120             return $this->_isSignedIn;
121         }
122     }
123 } else {
124     class MockUser extends WikiUser {
125         function MockUser($name, $isSignedIn) {
126             $this->_userid = $name;
127             $this->_isSignedIn = $isSignedIn;
128         }
129         function isSignedIn() {
130             return $this->_isSignedIn;
131         }
132     }
133 }
134
135 //FIXME: ignore cached requests (if-modified-since) from cli
136 class MockRequest extends WikiRequest {
137     function MockRequest(&$dbparams) {
138         $this->_dbi = WikiDB::open($dbparams);
139         $this->_user = new MockUser("a_user", true);
140         $this->_group = WikiGroup::getGroup();
141         $this->_args = array('pagename' => 'HomePage', 'action' => 'browse');
142         $this->Request();
143     }
144     function getGroup() {
145         if (is_object($this->_group))
146             return $this->_group;
147         else // FIXME: this is set to "/f:" somewhere.
148             return WikiGroup::getGroup();
149     }
150 }
151
152 function purge_dir($dir) {
153     static $finder;
154     if (!isset($finder)) {
155         $finder = new FileFinder;
156     }
157     $fileSet = new fileSet($dir);
158     assert(!empty($dir));
159     foreach ($fileSet->getFiles() as $f) {
160         unlink("$dir/$f");
161     }
162 }
163
164 function purge_testbox() {
165     global $db_params;  
166     $dir = $db_params['directory'];
167     assert(!empty($dir));
168     foreach (array('latest_ver','links','page_data','ver_data') as $d) {
169         purge_dir("$dir/$d");
170     }
171     if (isset($GLOBALS['request'])) {
172         $dbi = $GLOBALS['request']->getDbh();
173         $dbi->_cache->close();
174         $dbi->_backend->_latest_versions = array();
175     }
176 }
177
178 global $ErrorManager;
179 $ErrorManager->setPostponedErrorMask(EM_FATAL_ERRORS|EM_WARNING_ERRORS|EM_NOTICE_ERRORS);
180 $request = new MockRequest($db_params);
181
182 /*
183 if (ENABLE_USER_NEW)
184     $request->_user = WikiUser('AnonUser');
185 else {
186     $request->_user = new WikiUser($request, 'AnonUser');
187     $request->_prefs = $request->_user->getPreferences();
188 }
189 */
190 include_once("themes/" . THEME . "/themeinfo.php");
191
192 ####################################################################
193 #
194 # End of preamble, run the test suite ..
195 #
196 ####################################################################
197
198 # Test files
199 require_once 'PHPUnit.php';
200 # lib/config.php might do a cwd()
201
202 if (isset($HTTP_SERVER_VARS['REQUEST_METHOD']))
203     echo "<pre>\n";
204 // purge the testbox
205     
206 print "Run tests .. ";
207
208 $suite  = new PHPUnit_TestSuite("phpwiki");
209
210 // save and restore all args for each test.
211 class phpwiki_TestCase extends PHPUnit_TestCase {
212     function setUp() { 
213         global $request;
214         $this->_savedargs = $request->_args;
215         $request->_args = array();
216     }
217     function tearDown() {
218         global $request;
219         $request->_args = $this->_savedargs;
220         if (DEBUG & _DEBUG_TRACE) {
221             echo "-- MEMORY USAGE: ";
222             if (function_exists('memory_get_usage')) {
223                 echo memory_get_usage(),"\n";
224             } elseif (function_exists('getrusage')) {
225                 $u = getrusage();
226                 echo $u['ru_maxrss'],"\n";
227             } elseif (!isWindows()) { // only on unix, not on cygwin:
228                 $pid = getmypid();
229                 echo `ps -eo%mem,rss,pid | grep $pid`,"\n";
230             } else { // requires a newer cygwin
231                 echo `cat /proc/meminfo | grep Mem:|perl -ane"print \$F[2];"`,"\n";
232             }
233             flush();
234         }
235     }
236 }
237
238 // use argv (from cli) or tests (from browser) params to run only certain tests
239 $alltests = array('InlineParserTest','HtmlParserTest','PageListTest','ListPagesTest',
240                   'SetupWiki','DumpHtml','AllPagesTest','AllUsersTest','OrphanedPagesTest');
241 if (isset($HTTP_SERVER_VARS['REQUEST_METHOD']) and !empty($HTTP_GET_VARS['tests']))
242     $argv = explode(',',$HTTP_GET_VARS['tests']);
243 if (!empty($argv) and preg_match("/test\.php$/", $argv[0]))
244     array_shift($argv);
245 if (!empty($argv)) {
246     $runtests = array();
247     foreach ($argv as $test) {
248         if (in_array($test,$alltests))
249             $runtests[] = $test;
250     }
251     $alltests = $runtests;
252     print_r($runtests);
253     flush();
254 }
255
256 foreach ($alltests as $test) {
257     if (file_exists(dirname(__FILE__).'/lib/'.$test.'.php'))
258         require_once dirname(__FILE__).'/lib/'.$test.'.php';
259     else    
260         require_once dirname(__FILE__).'/lib/plugin/'.$test.'.php';
261     $suite->addTest( new PHPUnit_TestSuite($test) );
262 }
263 $result = PHPUnit::run($suite); 
264
265 echo "ran " . $result->runCount() . " tests, " . $result->failureCount() . " failures.\n";
266 flush();
267
268 if ($result->failureCount() > 0) {
269     echo "More detail:\n";
270     echo $result->toString();
271 }
272
273 if (isset($HTTP_SERVER_VARS['REQUEST_METHOD']))
274     echo "</pre>\n";
275
276 // (c-file-style: "gnu")
277 // Local Variables:
278 // mode: php
279 // tab-width: 8
280 // c-basic-offset: 4
281 // c-hanging-comment-ender-p: nil
282 // indent-tabs-mode: nil
283 // End:   
284 ?>