]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - tests/unit/test.php
run only requested tests
[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  */
30
31 ####################################################################
32 #
33 # Preamble needed to get the tests to run.
34 #
35 ####################################################################
36
37
38 $cur_dir = getcwd();
39 # Add root dir to the path
40 if (substr(PHP_OS,0,3) == 'WIN')
41     $cur_dir = str_replace("\\","/",$cur_dir);
42 $rootdir = $cur_dir . '/../../';
43 $ini_sep = substr(PHP_OS,0,3) == 'WIN' ? ';' : ':';
44 ini_set('include_path', ini_get('include_path') . $ini_sep . $rootdir);
45
46 # This quiets a warning in config.php
47 $HTTP_SERVER_VARS['REMOTE_ADDR'] = '127.0.0.1';
48 $HTTP_SERVER_VARS['HTTP_USER_AGENT'] = "PHPUnit";
49 define('GROUP_METHOD', 'NONE');
50 define('RATING_STORAGE','WIKIPAGE');
51 define('PHPWIKI_NOMAIN',true);
52
53 # Other needed files
54 require_once $rootdir.'index.php';
55 require_once $rootdir.'lib/main.php';
56
57 define('DEBUG', _DEBUG_TRACE);
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 /*
86 // This is too strict, fails on every notice and warning. 
87 // TODO: push an errorhandler with printSimpleTrace
88 function myErrorHandler($errno, $errstr, $errfile, $errline)
89 {
90    echo "$errfile: $errline: error# $errno: $errstr\n";
91    // Back trace
92    echo "Traceback:\n";
93    printSimpleTrace(debug_backtrace());
94    exit;
95 }
96 // set to the user defined error handler
97 $old_error_handler = set_error_handler("myErrorHandler");
98 */
99
100 # This is the test DB backend
101 $db_params                         = array();
102 $db_params['directory']            = $cur_dir . '/.testbox';
103 $db_params['dbtype']               = 'file';
104
105 if (ENABLE_USER_NEW) {
106     class MockUser extends _WikiUser {
107         function MockUser($name, $isSignedIn) {
108             $this->_userid = $name;
109             $this->_isSignedIn = $isSignedIn;
110         }
111         function isSignedIn() {
112             return $this->_isSignedIn;
113         }
114     }
115 } else {
116     class MockUser extends WikiUser {
117         function MockUser($name, $isSignedIn) {
118             $this->_userid = $name;
119             $this->_isSignedIn = $isSignedIn;
120         }
121         function isSignedIn() {
122             return $this->_isSignedIn;
123         }
124     }
125 }
126
127 class MockRequest extends WikiRequest {
128     function MockRequest(&$dbparams) {
129         $this->_dbi = WikiDB::open($dbparams);
130         $this->_user = new MockUser("a_user", true);
131         $this->_group = WikiGroup::getGroup();
132         $this->_args = array('pagename' => 'HomePage', 'action' => 'browse');
133         $this->Request();
134     }
135     function getGroup() {
136         if (is_object($this->_group))
137             return $this->_group;
138         else     
139             return WikiGroup::getGroup();
140     }
141 }
142
143 function purge_dir($dir) {
144     static $finder;
145     if (!isset($finder)) {
146         $finder = new FileFinder;
147     }
148         $fileSet = new fileSet($dir);
149         assert($dir);
150     foreach ($fileSet->getFiles() as $f) {
151         unlink("$dir/$f");
152     }
153 }
154
155 function purge_testbox() {
156     global $db_params;  
157     $dir = $db_params['directory'];
158     assert($dir);
159     foreach (array('latest_ver','links','page_data', 'ver_data') as $d) {
160         purge_dir("$dir/$d");
161     }
162 }
163
164 global $ErrorManager;
165 $ErrorManager->setPostponedErrorMask(E_NOTICE|E_USER_NOTICE|E_USER_WARNING|E_WARNING);
166 $request = new MockRequest($db_params);
167
168 /*
169 if (ENABLE_USER_NEW)
170     $request->_user = WikiUser('AnonUser');
171 else {
172     $request->_user = new WikiUser($request, 'AnonUser');
173     $request->_prefs = $request->_user->getPreferences();
174 }
175 */
176 include_once("themes/" . THEME . "/themeinfo.php");
177
178 ####################################################################
179 #
180 # End of preamble, run the test suite ..
181 #
182 ####################################################################
183
184 # Test files
185 require_once 'PHPUnit.php';
186 # lib/config.php might do a cwd()
187
188 if (isset($HTTP_SERVER_VARS['REQUEST_METHOD']))
189     echo "<pre>\n";
190 // purge the testbox
191     
192 print "Run tests .. ";
193
194 $suite  = new PHPUnit_TestSuite("phpwiki");
195 // use argv (from cli) or tests (from browser) params to run only certain tests
196 $alltests = array('InlineParserTest','HtmlParserTest','PageListTest','ListPagesTest',
197                               'SetupWiki','DumpHtml','AllPagesTest','AllUsersTest','OrphanedPagesTest');
198 if (isset($HTTP_SERVER_VARS['REQUEST_METHOD']) and !empty($HTTP_GET_VARS['tests']))
199     $argv = explode(',',$HTTP_GET_VARS['tests']);
200 if (!empty($argv)) {
201         $runtests = array();
202         foreach ($argv as $test) {
203             if (in_array($test,$alltests))
204             $runtests[] = $test;
205         }        
206         $alltests = $runtests;
207 }
208
209 foreach ($alltests as $test) {
210         if (file_exists(dirname(__FILE__).'/lib/'.$test.'.php'))
211             require_once dirname(__FILE__).'/lib/'.$test.'.php';
212         else    
213             require_once dirname(__FILE__).'/lib/plugin/'.$test.'.php';
214     $suite->addTest( new PHPUnit_TestSuite($test) );
215 }
216 $result = PHPUnit::run($suite); 
217
218 echo "ran " . $result->runCount() . " tests, " . $result->failureCount() . " failures.\n";
219
220 if ($result->failureCount() > 0) {
221     echo "More detail:\n";
222     echo $result->toString();
223 }
224
225 if (isset($HTTP_SERVER_VARS['REQUEST_METHOD']))
226     echo "</pre>\n";
227
228 // (c-file-style: "gnu")
229 // Local Variables:
230 // mode: php
231 // tab-width: 8
232 // c-basic-offset: 4
233 // c-hanging-comment-ender-p: nil
234 // indent-tabs-mode: nil
235 // End:   
236 ?>