X-Git-Url: https://git.heureux-cyclage.org/?a=blobdiff_plain;f=tests%2FtestHelpers.inc;h=d04e0fcb54fb6969cfae58fa6af22e64ab43e601;hb=36318e7e6b75686f300a77e1be66a5aa5addb21e;hp=d73496f070d3a2fe7bfed696cb10119ee75b6295;hpb=415b31766677e190c13322742b4e42da1157538c;p=lhc%2Fweb%2Fwiklou.git diff --git a/tests/testHelpers.inc b/tests/testHelpers.inc index d73496f070..d04e0fcb54 100644 --- a/tests/testHelpers.inc +++ b/tests/testHelpers.inc @@ -44,9 +44,10 @@ interface ITestRecorder { /** * Called after each test * @param string $test + * @param integer $subtest * @param bool $result */ - public function record( $test, $result ); + public function record( $test, $subtest, $result ); /** * Called before finishing the test run @@ -74,7 +75,7 @@ class TestRecorder implements ITestRecorder { $this->success = 0; } - function record( $test, $result ) { + function record( $test, $subtest, $result ) { $this->total++; $this->success += ( $result ? 1 : 0 ); } @@ -144,19 +145,27 @@ class DbTestPreviewer extends TestRecorder { $this->prevRun = $this->db->selectField( 'testrun', 'MAX(tr_id)' ); } - $this->results = array(); + $this->results = []; } - function record( $test, $result ) { - parent::record( $test, $result ); - $this->results[$test] = $result; + function getName( $test, $subtest ) { + if ( $subtest ) { + return "$test subtest #$subtest"; + } else { + return $test; + } + } + + function record( $test, $subtest, $result ) { + parent::record( $test, $subtest, $result ); + $this->results[ $this->getName( $test, $subtest ) ] = $result; } function report() { if ( $this->prevRun ) { // f = fail, p = pass, n = nonexistent // codes show before then after - $table = array( + $table = [ 'fp' => 'previously failing test(s) now PASSING! :)', 'pn' => 'previously PASSING test(s) removed o_O', 'np' => 'new PASSING test(s) :)', @@ -165,12 +174,12 @@ class DbTestPreviewer extends TestRecorder { 'fn' => 'previously FAILING test(s) removed O_o', 'nf' => 'new FAILING test(s) :(', 'ff' => 'still FAILING test(s) :(', - ); + ]; - $prevResults = array(); + $prevResults = []; - $res = $this->db->select( 'testitem', array( 'ti_name', 'ti_success' ), - array( 'ti_run' => $this->prevRun ), __METHOD__ ); + $res = $this->db->select( 'testitem', [ 'ti_name', 'ti_success' ], + [ 'ti_run' => $this->prevRun ], __METHOD__ ); foreach ( $res as $row ) { if ( !$this->parent->regex @@ -183,7 +192,7 @@ class DbTestPreviewer extends TestRecorder { $combined = array_keys( $this->results + $prevResults ); # Determine breakdown by change type - $breakdown = array(); + $breakdown = []; foreach ( $combined as $test ) { if ( !isset( $prevResults[$test] ) ) { $before = 'n'; @@ -240,11 +249,11 @@ class DbTestPreviewer extends TestRecorder { if ( $after == 'n' ) { $changedRun = $this->db->selectField( 'testitem', 'MIN(ti_run)', - array( 'ti_name' => $testname ), + [ 'ti_name' => $testname ], __METHOD__ ); $appear = $this->db->selectRow( 'testrun', - array( 'tr_date', 'tr_mw_version' ), - array( 'tr_id' => $changedRun ), + [ 'tr_date', 'tr_mw_version' ], + [ 'tr_id' => $changedRun ], __METHOD__ ); return "First recorded appearance: " @@ -254,9 +263,9 @@ class DbTestPreviewer extends TestRecorder { // Otherwise, this test has previous recorded results. // See when this test last had a different result to what we're seeing now. - $conds = array( + $conds = [ 'ti_name' => $testname, - 'ti_success' => ( $after == 'f' ? "1" : "0" ) ); + 'ti_success' => ( $after == 'f' ? "1" : "0" ) ]; if ( $this->curRun ) { $conds[] = "ti_run != " . $this->db->addQuotes( $this->curRun ); @@ -277,14 +286,14 @@ class DbTestPreviewer extends TestRecorder { // (i.e. it used to work, but now doesn't; or used to fail, but is now fixed.) // In this situation, give as much info as we can as to when it changed status. $pre = $this->db->selectRow( 'testrun', - array( 'tr_date', 'tr_mw_version' ), - array( 'tr_id' => $changedRun ), + [ 'tr_date', 'tr_mw_version' ], + [ 'tr_id' => $changedRun ], __METHOD__ ); $post = $this->db->selectRow( 'testrun', - array( 'tr_date', 'tr_mw_version' ), - array( "tr_id > " . $this->db->addQuotes( $changedRun ) ), + [ 'tr_date', 'tr_mw_version' ], + [ "tr_id > " . $this->db->addQuotes( $changedRun ) ], __METHOD__, - array( "LIMIT" => 1, "ORDER BY" => 'tr_id' ) + [ "LIMIT" => 1, "ORDER BY" => 'tr_id' ] ); if ( $post ) { @@ -299,10 +308,9 @@ class DbTestPreviewer extends TestRecorder { } /** - * Commit transaction and clean up for result recording + * Close the DB connection */ function end() { - $this->lb->commitMasterChanges(); $this->lb->closeAll(); parent::end(); } @@ -329,13 +337,13 @@ class DbTestRecorder extends DbTestPreviewer { parent::start(); $this->db->insert( 'testrun', - array( + [ 'tr_date' => $this->db->timestamp(), 'tr_mw_version' => $this->version, 'tr_php_version' => PHP_VERSION, 'tr_db_version' => $this->db->getServerVersion(), 'tr_uname' => php_uname() - ), + ], __METHOD__ ); if ( $this->db->getType() === 'postgres' ) { $this->curRun = $this->db->currentSequenceValue( 'testrun_id_seq' ); @@ -350,17 +358,25 @@ class DbTestRecorder extends DbTestPreviewer { * @param string $test * @param bool $result */ - function record( $test, $result ) { - parent::record( $test, $result ); + function record( $test, $subtest, $result ) { + parent::record( $test, $subtest, $result ); $this->db->insert( 'testitem', - array( + [ 'ti_run' => $this->curRun, - 'ti_name' => $test, + 'ti_name' => $this->getName( $test, $subtest ), 'ti_success' => $result ? 1 : 0, - ), + ], __METHOD__ ); } + + /** + * Commit transaction and clean up for result recording + */ + function end() { + $this->db->commit( __METHOD__ ); + parent::end(); + } } class TestFileIterator implements Iterator { @@ -375,7 +391,7 @@ class TestFileIterator implements Iterator { private $test; private $section = null; /** String|null: current test section being analyzed */ - private $sectionData = array(); + private $sectionData = []; private $lineNum; private $eof; # Create a fake parser tests which never run anything unless @@ -434,10 +450,10 @@ class TestFileIterator implements Iterator { function setupCurrentTest() { // "input" and "result" are old section names allowed // for backwards-compatibility. - $input = $this->checkSection( array( 'wikitext', 'input' ), false ); - $result = $this->checkSection( array( 'html/php', 'html/*', 'html', 'result' ), false ); + $input = $this->checkSection( [ 'wikitext', 'input' ], false ); + $result = $this->checkSection( [ 'html/php', 'html/*', 'html', 'result' ], false ); // some tests have "with tidy" and "without tidy" variants - $tidy = $this->checkSection( array( 'html/php+tidy', 'html+tidy' ), false ); + $tidy = $this->checkSection( [ 'html/php+tidy', 'html+tidy' ], false ); if ( $tidy != false ) { if ( $this->nextSubTest == 0 ) { if ( $result != false ) { @@ -477,13 +493,14 @@ class TestFileIterator implements Iterator { throw new MWException( "Problem running requested parser hook from the test file" ); } - $this->test = array( + $this->test = [ 'test' => ParserTest::chomp( $this->sectionData['test'] ), + 'subtest' => $this->nextSubTest, 'input' => ParserTest::chomp( $this->sectionData[$input] ), 'result' => ParserTest::chomp( $this->sectionData[$result] ), 'options' => ParserTest::chomp( $this->sectionData['options'] ), 'config' => ParserTest::chomp( $this->sectionData['config'] ), - ); + ]; if ( $tidy != false ) { $this->test['options'] .= " tidy"; } @@ -504,7 +521,7 @@ class TestFileIterator implements Iterator { while ( false !== ( $line = fgets( $this->fh ) ) ) { $this->lineNum++; - $matches = array(); + $matches = []; if ( preg_match( '/^!!\s*(\S+)/', $line, $matches ) ) { $this->section = strtolower( $matches[1] ); @@ -605,7 +622,7 @@ class TestFileIterator implements Iterator { * Clear section name and its data */ private function clearSection() { - $this->sectionData = array(); + $this->sectionData = []; $this->section = null; } @@ -628,7 +645,7 @@ class TestFileIterator implements Iterator { throw new MWException( __METHOD__ . " can not verify a null section!\n" ); } if ( !is_array( $tokens ) ) { - $tokens = array( $tokens ); + $tokens = [ $tokens ]; } if ( count( $tokens ) == 0 ) { throw new MWException( __METHOD__ . " can not verify zero sections!\n" ); @@ -661,8 +678,28 @@ class TestFileIterator implements Iterator { ) ); } - $tokens = array_values( $tokens ); - return $tokens[0]; + return array_values( $tokens )[0]; + } +} + +/** + * An iterator for use as a phpunit data provider. Provides the test arguments + * in the order expected by NewParserTest::testParserTest(). + */ +class TestFileDataProvider extends TestFileIterator { + function current() { + $test = parent::current(); + if ( $test ) { + return [ + $test['test'], + $test['input'], + $test['result'], + $test['options'], + $test['config'], + ]; + } else { + return $test; + } } } @@ -685,9 +722,9 @@ class DelayedParserTest { * Call to this will erase any hooks function that were pending. */ public function reset() { - $this->hooks = array(); - $this->fnHooks = array(); - $this->transparentHooks = array(); + $this->hooks = []; + $this->fnHooks = []; + $this->transparentHooks = []; } /** @@ -813,7 +850,7 @@ class TidySupport { class_exists( 'tidy' ) && !wfIsHHVM(); $this->externalTidy = is_executable( $wgTidyBin ) || - Installer::locateExecutableInDefaultPaths( array( $wgTidyBin ) ) + Installer::locateExecutableInDefaultPaths( [ $wgTidyBin ] ) !== false; }