MediaWiki  REL1_19
testHelpers.inc
Go to the documentation of this file.
00001 <?php
00002 
00003 class TestRecorder {
00004         var $parent;
00005         var $term;
00006 
00007         function __construct( $parent ) {
00008                 $this->parent = $parent;
00009                 $this->term = $parent->term;
00010         }
00011 
00012         function start() {
00013                 $this->total = 0;
00014                 $this->success = 0;
00015         }
00016 
00017         function record( $test, $result ) {
00018                 $this->total++;
00019                 $this->success += ( $result ? 1 : 0 );
00020         }
00021 
00022         function end() {
00023                 // dummy
00024         }
00025 
00026         function report() {
00027                 if ( $this->total > 0 ) {
00028                         $this->reportPercentage( $this->success, $this->total );
00029                 } else {
00030                         throw new MWException( "No tests found.\n" );
00031                 }
00032         }
00033 
00034         function reportPercentage( $success, $total ) {
00035                 $ratio = wfPercent( 100 * $success / $total );
00036                 print $this->term->color( 1 ) . "Passed $success of $total tests ($ratio)... ";
00037 
00038                 if ( $success == $total ) {
00039                         print $this->term->color( 32 ) . "ALL TESTS PASSED!";
00040                 } else {
00041                         $failed = $total - $success ;
00042                         print $this->term->color( 31 ) . "$failed tests failed!";
00043                 }
00044 
00045                 print $this->term->reset() . "\n";
00046 
00047                 return ( $success == $total );
00048         }
00049 }
00050 
00051 class DbTestPreviewer extends TestRecorder  {
00052         protected $lb;      // /< Database load balancer
00053         protected $db;      // /< Database connection to the main DB
00054         protected $curRun;  // /< run ID number for the current run
00055         protected $prevRun; // /< run ID number for the previous run, if any
00056         protected $results; // /< Result array
00057 
00061         function __construct( $parent ) {
00062                 parent::__construct( $parent );
00063 
00064                 $this->lb = wfGetLBFactory()->newMainLB();
00065                 // This connection will have the wiki's table prefix, not parsertest_
00066                 $this->db = $this->lb->getConnection( DB_MASTER );
00067         }
00068 
00073         function start() {
00074                 parent::start();
00075 
00076                 if ( ! $this->db->tableExists( 'testrun', __METHOD__ )
00077                         || ! $this->db->tableExists( 'testitem', __METHOD__ ) )
00078                 {
00079                         print "WARNING> `testrun` table not found in database.\n";
00080                         $this->prevRun = false;
00081                 } else {
00082                         // We'll make comparisons against the previous run later...
00083                         $this->prevRun = $this->db->selectField( 'testrun', 'MAX(tr_id)' );
00084                 }
00085 
00086                 $this->results = array();
00087         }
00088 
00089         function record( $test, $result ) {
00090                 parent::record( $test, $result );
00091                 $this->results[$test] = $result;
00092         }
00093 
00094         function report() {
00095                 if ( $this->prevRun ) {
00096                         // f = fail, p = pass, n = nonexistent
00097                         // codes show before then after
00098                         $table = array(
00099                                 'fp' => 'previously failing test(s) now PASSING! :)',
00100                                 'pn' => 'previously PASSING test(s) removed o_O',
00101                                 'np' => 'new PASSING test(s) :)',
00102 
00103                                 'pf' => 'previously passing test(s) now FAILING! :(',
00104                                 'fn' => 'previously FAILING test(s) removed O_o',
00105                                 'nf' => 'new FAILING test(s) :(',
00106                                 'ff' => 'still FAILING test(s) :(',
00107                         );
00108 
00109                         $prevResults = array();
00110 
00111                         $res = $this->db->select( 'testitem', array( 'ti_name', 'ti_success' ),
00112                                 array( 'ti_run' => $this->prevRun ), __METHOD__ );
00113 
00114                         foreach ( $res as $row ) {
00115                                 if ( !$this->parent->regex
00116                                         || preg_match( "/{$this->parent->regex}/i", $row->ti_name ) )
00117                                 {
00118                                         $prevResults[$row->ti_name] = $row->ti_success;
00119                                 }
00120                         }
00121 
00122                         $combined = array_keys( $this->results + $prevResults );
00123 
00124                         # Determine breakdown by change type
00125                         $breakdown = array();
00126                         foreach ( $combined as $test ) {
00127                                 if ( !isset( $prevResults[$test] ) ) {
00128                                         $before = 'n';
00129                                 } elseif ( $prevResults[$test] == 1 ) {
00130                                         $before = 'p';
00131                                 } else /* if ( $prevResults[$test] == 0 )*/ {
00132                                         $before = 'f';
00133                                 }
00134 
00135                                 if ( !isset( $this->results[$test] ) ) {
00136                                         $after = 'n';
00137                                 } elseif ( $this->results[$test] == 1 ) {
00138                                         $after = 'p';
00139                                 } else /*if ( $this->results[$test] == 0 ) */ {
00140                                         $after = 'f';
00141                                 }
00142 
00143                                 $code = $before . $after;
00144 
00145                                 if ( isset( $table[$code] ) ) {
00146                                         $breakdown[$code][$test] = $this->getTestStatusInfo( $test, $after );
00147                                 }
00148                         }
00149 
00150                         # Write out results
00151                         foreach ( $table as $code => $label ) {
00152                                 if ( !empty( $breakdown[$code] ) ) {
00153                                         $count = count( $breakdown[$code] );
00154                                         printf( "\n%4d %s\n", $count, $label );
00155 
00156                                         foreach ( $breakdown[$code] as $differing_test_name => $statusInfo ) {
00157                                                 print "      * $differing_test_name  [$statusInfo]\n";
00158                                         }
00159                                 }
00160                         }
00161                 } else {
00162                         print "No previous test runs to compare against.\n";
00163                 }
00164 
00165                 print "\n";
00166                 parent::report();
00167         }
00168 
00174         private function getTestStatusInfo( $testname, $after ) {
00175                 // If we're looking at a test that has just been removed, then say when it first appeared.
00176                 if ( $after == 'n' ) {
00177                         $changedRun = $this->db->selectField ( 'testitem',
00178                                 'MIN(ti_run)',
00179                                 array( 'ti_name' => $testname ),
00180                                 __METHOD__ );
00181                         $appear = $this->db->selectRow ( 'testrun',
00182                                 array( 'tr_date', 'tr_mw_version' ),
00183                                 array( 'tr_id' => $changedRun ),
00184                                 __METHOD__ );
00185 
00186                         return "First recorded appearance: "
00187                                    . date( "d-M-Y H:i:s",  strtotime ( $appear->tr_date ) )
00188                                    .  ", " . $appear->tr_mw_version;
00189                 }
00190 
00191                 // Otherwise, this test has previous recorded results.
00192                 // See when this test last had a different result to what we're seeing now.
00193                 $conds = array(
00194                         'ti_name'    => $testname,
00195                         'ti_success' => ( $after == 'f' ? "1" : "0" ) );
00196 
00197                 if ( $this->curRun ) {
00198                         $conds[] = "ti_run != " . $this->db->addQuotes ( $this->curRun );
00199                 }
00200 
00201                 $changedRun = $this->db->selectField ( 'testitem', 'MAX(ti_run)', $conds, __METHOD__ );
00202 
00203                 // If no record of ever having had a different result.
00204                 if ( is_null ( $changedRun ) ) {
00205                         if ( $after == "f" ) {
00206                                 return "Has never passed";
00207                         } else {
00208                                 return "Has never failed";
00209                         }
00210                 }
00211 
00212                 // Otherwise, we're looking at a test whose status has changed.
00213                 // (i.e. it used to work, but now doesn't; or used to fail, but is now fixed.)
00214                 // In this situation, give as much info as we can as to when it changed status.
00215                 $pre  = $this->db->selectRow ( 'testrun',
00216                         array( 'tr_date', 'tr_mw_version' ),
00217                         array( 'tr_id' => $changedRun ),
00218                         __METHOD__ );
00219                 $post = $this->db->selectRow ( 'testrun',
00220                         array( 'tr_date', 'tr_mw_version' ),
00221                         array( "tr_id > " . $this->db->addQuotes ( $changedRun ) ),
00222                         __METHOD__,
00223                         array( "LIMIT" => 1, "ORDER BY" => 'tr_id' )
00224                 );
00225 
00226                 if ( $post ) {
00227                         $postDate = date( "d-M-Y H:i:s",  strtotime ( $post->tr_date  ) ) . ", {$post->tr_mw_version}";
00228                 } else {
00229                         $postDate = 'now';
00230                 }
00231 
00232                 return ( $after == "f" ? "Introduced" : "Fixed" ) . " between "
00233                                 . date( "d-M-Y H:i:s",  strtotime ( $pre->tr_date ) ) .  ", " . $pre->tr_mw_version
00234                                 . " and $postDate";
00235 
00236         }
00237 
00241         function end() {
00242                 $this->lb->commitMasterChanges();
00243                 $this->lb->closeAll();
00244                 parent::end();
00245         }
00246 
00247 }
00248 
00249 class DbTestRecorder extends DbTestPreviewer  {
00250         var $version;
00251 
00256         function start() {
00257                 $this->db->begin();
00258 
00259                 if ( ! $this->db->tableExists( 'testrun' )
00260                         || ! $this->db->tableExists( 'testitem' ) )
00261                 {
00262                         print "WARNING> `testrun` table not found in database. Trying to create table.\n";
00263                         $this->db->sourceFile( $this->db->patchPath( 'patch-testrun.sql' ) );
00264                         echo "OK, resuming.\n";
00265                 }
00266 
00267                 parent::start();
00268 
00269                 $this->db->insert( 'testrun',
00270                         array(
00271                                 'tr_date'        => $this->db->timestamp(),
00272                                 'tr_mw_version'  => $this->version,
00273                                 'tr_php_version' => phpversion(),
00274                                 'tr_db_version'  => $this->db->getServerVersion(),
00275                                 'tr_uname'       => php_uname()
00276                         ),
00277                         __METHOD__ );
00278                         if ( $this->db->getType() === 'postgres' ) {
00279                                 $this->curRun = $this->db->currentSequenceValue( 'testrun_id_seq' );
00280                         } else {
00281                                 $this->curRun = $this->db->insertId();
00282                         }
00283         }
00284 
00291         function record( $test, $result ) {
00292                 parent::record( $test, $result );
00293 
00294                 $this->db->insert( 'testitem',
00295                         array(
00296                                 'ti_run'     => $this->curRun,
00297                                 'ti_name'    => $test,
00298                                 'ti_success' => $result ? 1 : 0,
00299                         ),
00300                         __METHOD__ );
00301         }
00302 }
00303 
00304 class TestFileIterator implements Iterator {
00305         private $file;
00306         private $fh;
00307         private $parserTest; /* An instance of ParserTest (parserTests.php) or MediaWikiParserTest (phpunit) */
00308         private $index = 0;
00309         private $test;
00310         private $section = null; 
00311         private $sectionData = array();
00312         private $lineNum;
00313         private $eof;
00314 
00315         function __construct( $file, $parserTest ) {
00316                 $this->file = $file;
00317                 $this->fh = fopen( $this->file, "rt" );
00318 
00319                 if ( !$this->fh ) {
00320                         throw new MWException( "Couldn't open file '$file'\n" );
00321                 }
00322 
00323                 $this->parserTest = $parserTest;
00324 
00325                 $this->lineNum = $this->index = 0;
00326         }
00327 
00328         function rewind() {
00329                 if ( fseek( $this->fh, 0 ) ) {
00330                         throw new MWException( "Couldn't fseek to the start of '$this->file'\n" );
00331                 }
00332 
00333                 $this->index = -1;
00334                 $this->lineNum = 0;
00335                 $this->eof = false;
00336                 $this->next();
00337 
00338                 return true;
00339         }
00340 
00341         function current() {
00342                 return $this->test;
00343         }
00344 
00345         function key() {
00346                 return $this->index;
00347         }
00348 
00349         function next() {
00350                 if ( $this->readNextTest() ) {
00351                         $this->index++;
00352                         return true;
00353                 } else {
00354                         $this->eof = true;
00355                 }
00356         }
00357 
00358         function valid() {
00359                 return $this->eof != true;
00360         }
00361 
00362         function readNextTest() {
00363                 $this->clearSection();
00364 
00365                 # Create a fake parser tests which never run anything unless
00366                 # asked to do so. This will avoid running hooks for a disabled test
00367                 $delayedParserTest = new DelayedParserTest();
00368 
00369                 while ( false !== ( $line = fgets( $this->fh ) ) ) {
00370                         $this->lineNum++;
00371                         $matches = array();
00372 
00373                         if ( preg_match( '/^!!\s*(\w+)/', $line, $matches ) ) {
00374                                 $this->section = strtolower( $matches[1] );
00375 
00376                                 if ( $this->section == 'endarticle' ) {
00377                                         $this->checkSection( 'text'    );
00378                                         $this->checkSection( 'article' );
00379 
00380                                         $this->parserTest->addArticle( ParserTest::chomp( $this->sectionData['article'] ), $this->sectionData['text'], $this->lineNum );
00381 
00382                                         $this->clearSection();
00383 
00384                                         continue;
00385                                 }
00386 
00387                                 if ( $this->section == 'endhooks' ) {
00388                                         $this->checkSection( 'hooks' );
00389 
00390                                         foreach ( explode( "\n", $this->sectionData['hooks'] ) as $line ) {
00391                                                 $line = trim( $line );
00392 
00393                                                 if ( $line ) {
00394                                                         $delayedParserTest->requireHook( $line );
00395                                                 }
00396                                         }
00397 
00398                                         $this->clearSection();
00399 
00400                                         continue;
00401                                 }
00402 
00403                                 if ( $this->section == 'endfunctionhooks' ) {
00404                                         $this->checkSection( 'functionhooks' );
00405 
00406                                         foreach ( explode( "\n", $this->sectionData['functionhooks'] ) as $line ) {
00407                                                 $line = trim( $line );
00408 
00409                                                 if ( $line ) {
00410                                                         $delayedParserTest->requireFunctionHook( $line );
00411                                                 }
00412                                         }
00413 
00414                                         $this->clearSection();
00415 
00416                                         continue;
00417                                 }
00418 
00419                                 if ( $this->section == 'end' ) {
00420                                         $this->checkSection( 'test'   );
00421                                         $this->checkSection( 'input'  );
00422                                         $this->checkSection( 'result' );
00423 
00424                                         if ( !isset( $this->sectionData['options'] ) ) {
00425                                                 $this->sectionData['options'] = '';
00426                                         }
00427 
00428                                         if ( !isset( $this->sectionData['config'] ) ) {
00429                                                 $this->sectionData['config'] = '';
00430                                         }
00431 
00432                                         if ( ( ( preg_match( '/\\bdisabled\\b/i', $this->sectionData['options'] ) && !$this->parserTest->runDisabled )
00433                                                          || !preg_match( "/" . $this->parserTest->regex . "/i", $this->sectionData['test'] ) )  ) {
00434                                                 # disabled test
00435                                                 $this->clearSection();
00436 
00437                                                 # Forget any pending hooks call since test is disabled
00438                                                 $delayedParserTest->reset();
00439 
00440                                                 continue;
00441                                         }
00442 
00443                                         # We are really going to run the test, run pending hooks and hooks function
00444                                         wfDebug( __METHOD__ . " unleashing delayed test for: {$this->sectionData['test']}" );
00445                                         $hooksResult = $delayedParserTest->unleash( $this->parserTest );
00446                                         if( !$hooksResult ) {
00447                                                 # Some hook reported an issue. Abort.
00448                                                 return false;
00449                                         }
00450 
00451                                         $this->test = array(
00452                                                 'test'    => ParserTest::chomp( $this->sectionData['test']    ),
00453                                                 'input'   => ParserTest::chomp( $this->sectionData['input']   ),
00454                                                 'result'  => ParserTest::chomp( $this->sectionData['result']  ),
00455                                                 'options' => ParserTest::chomp( $this->sectionData['options'] ),
00456                                                 'config'  => ParserTest::chomp( $this->sectionData['config']  ),
00457                                         );
00458 
00459                                         return true;
00460                                 }
00461 
00462                                 if ( isset ( $this->sectionData[$this->section] ) ) {
00463                                         throw new MWException( "duplicate section '$section' at line {$this->lineNum} of $this->file\n" );
00464                                 }
00465 
00466                                 $this->sectionData[$this->section] = '';
00467 
00468                                 continue;
00469                         }
00470 
00471                         if ( $this->section ) {
00472                                 $this->sectionData[$this->section] .= $line;
00473                         }
00474                 }
00475 
00476                 return false;
00477         }
00478 
00479 
00483         private function clearSection() {
00484                 $this->sectionData = array();
00485                 $this->section = null;
00486                 
00487         }
00488 
00497         private function checkSection( $token ) {
00498                 if( is_null( $this->section ) ) {
00499                         throw new MWException( __METHOD__ . " can not verify a null section!\n" );
00500                 }
00501 
00502                 if( !isset($this->sectionData[$token]) ) {
00503                         throw new MWException( sprintf(
00504                                 "'%s' without '%s' at line %s of %s\n",
00505                                 $this->section,
00506                                 $token,
00507                                 $this->lineNum,
00508                                 $this->file
00509                         ));
00510                 }
00511                 return true;
00512         }
00513 }
00514 
00518 class DelayedParserTest {
00519 
00521         private $hooks;
00522         private $fnHooks;
00523 
00524         public function __construct() {
00525                 $this->reset();
00526         }
00527 
00532         public function reset() {
00533                 $this->hooks   = array();
00534                 $this->fnHooks = array();
00535         }
00536 
00541         public function unleash( &$parserTest ) {
00542                 if( !($parserTest instanceof ParserTest || $parserTest instanceof NewParserTest
00543                 ) ) {
00544                         throw new MWException( __METHOD__ . " must be passed an instance of ParserTest or NewParserTest classes\n" );
00545                 }
00546 
00547                 # Trigger delayed hooks. Any failure will make us abort
00548                 foreach( $this->hooks as $hook ) {
00549                         $ret = $parserTest->requireHook( $hook );
00550                         if( !$ret ) {
00551                                 return false;
00552                         }
00553                 }
00554 
00555                 # Trigger delayed function hooks. Any failure will make us abort
00556                 foreach( $this->fnHooks as $fnHook ) {
00557                         $ret = $parserTest->requireFunctionHook( $fnHook );
00558                         if( !$ret ) {
00559                                 return false;
00560                         }
00561                 }
00562 
00563                 # Delayed execution was successful.
00564                 return true;
00565         }
00566 
00571         public function requireHook( $hook ) {
00572                 $this->hooks[] = $hook;
00573         }
00578         public function requireFunctionHook( $fnHook ) {
00579                 $this->fnHooks[] = $fnHook;
00580         }
00581 
00582 }