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