diff options
author | Chad Horohoe <demon@users.mediawiki.org> | 2010-12-14 16:26:35 +0000 |
---|---|---|
committer | Chad Horohoe <demon@users.mediawiki.org> | 2010-12-14 16:26:35 +0000 |
commit | 23f69f10ed07c7fbe7d752882a88d55351ce2e3d (patch) | |
tree | 43054aea852645def63951fcbf45eb2cf2551adb /tests/phpunit/includes/parser | |
parent | 5903e492a5c60f65182d6339f63693aa2dca92f0 (diff) | |
download | mediawikicore-23f69f10ed07c7fbe7d752882a88d55351ce2e3d.tar.gz mediawikicore-23f69f10ed07c7fbe7d752882a88d55351ce2e3d.zip |
Per wikitech-l discussion: Move tests from maintenance/tests/ to tests/. They're not strictly maintenance scripts, and some people want to do a selective checkout that doesn't include the tests. There's still debate on whether we should include these in the release downloads, but we had a pretty firm consensus to move this.
Notes
Notes:
http://mediawiki.org/wiki/Special:Code/MediaWiki/78383
Diffstat (limited to 'tests/phpunit/includes/parser')
-rw-r--r-- | tests/phpunit/includes/parser/MediaWikiParserTest.php | 73 | ||||
-rw-r--r-- | tests/phpunit/includes/parser/ParserHelpers.php | 132 |
2 files changed, 205 insertions, 0 deletions
diff --git a/tests/phpunit/includes/parser/MediaWikiParserTest.php b/tests/phpunit/includes/parser/MediaWikiParserTest.php new file mode 100644 index 000000000000..3825a8fabf50 --- /dev/null +++ b/tests/phpunit/includes/parser/MediaWikiParserTest.php @@ -0,0 +1,73 @@ +<?php + +require_once( dirname( __FILE__ ) . '/ParserHelpers.php' ); +require_once( dirname(dirname(dirname( __FILE__ ))) . '/bootstrap.php' ); + +class MediaWikiParserTest extends MediaWikiTestSetup { + public $count; // Number of tests in the suite. + public $backend; // ParserTestSuiteBackend instance + public $articles = array(); // Array of test articles defined by the tests + + public function __construct() { + $suite = new PHPUnit_Framework_TestSuite('Parser Tests'); + parent::__construct($suite); + $this->backend = new ParserTestSuiteBackend; + $this->setName( 'Parser tests' ); + } + + public static function suite() { + global $IP; + + $tester = new self; + + $iter = new TestFileIterator( "$IP/maintenance/tests/parser/parserTests.txt", $tester ); + $tester->count = 0; + + foreach ( $iter as $test ) { + $tester->suite->addTest( new ParserUnitTest( $tester, $test ), array( 'Parser', 'Destructive', 'Database', 'Broken' ) ); + $tester->count++; + } + + return $tester->suite; + } + + public function count() { + return $this->count; + } + + public function toString() { + return "MediaWiki Parser Tests"; + } + + public function getBackend() { + return $this->backend; + } + + public function getIterator() { + return $this->iterator; + } + + public function publishTestArticles() { + if ( empty( $this->articles ) ) { + return; + } + + foreach ( $this->articles as $name => $text ) { + $title = Title::newFromText( $name ); + + if ( $title->getArticleID( Title::GAID_FOR_UPDATE ) == 0 ) { + ParserTest::addArticle( $name, $text ); + } + } + $this->articles = array(); + } + + public function addArticle( $name, $text, $line ) { + $this->articles[$name] = $text; + } + + public function showRunFile( $path ) { + /* Nothing shown when run from phpunit */ + } +} + diff --git a/tests/phpunit/includes/parser/ParserHelpers.php b/tests/phpunit/includes/parser/ParserHelpers.php new file mode 100644 index 000000000000..43da34218b09 --- /dev/null +++ b/tests/phpunit/includes/parser/ParserHelpers.php @@ -0,0 +1,132 @@ +<?php + +class PHPUnitParserTest extends ParserTest { + function showTesting( $desc ) { + /* Do nothing since we don't want to show info during PHPUnit testing. */ + } + + public function showSuccess( $desc ) { + PHPUnit_Framework_Assert::assertTrue( true, $desc ); + return true; + } + + public function showFailure( $desc, $expected, $got ) { + PHPUnit_Framework_Assert::assertEquals( $expected, $got, $desc ); + return false; + } +} + +class ParserUnitTest extends PHPUnit_Framework_TestCase { + private $test = ""; + private $suite; + + public function __construct( $suite, $test = null ) { + $this->suite = $suite; + $this->test = $test; + } + + function count() { return 1; } + + public function run( PHPUnit_Framework_TestResult $result = null ) { + PHPUnit_Framework_Assert::resetCount(); + if ( $result === NULL ) { + $result = new PHPUnit_Framework_TestResult; + } + + $this->suite->publishTestArticles(); // Add articles needed by the tests. + $backend = new ParserTestSuiteBackend; + $result->startTest( $this ); + + // Support the transition to PHPUnit 3.5 where PHPUnit_Util_Timer is replaced with PHP_Timer + if ( class_exists( 'PHP_Timer' ) ) { + PHP_Timer::start(); + } else { + PHPUnit_Util_Timer::start(); + } + + $r = false; + try { + # Run the test. + # On failure, the subclassed backend will throw an exception with + # the details. + $pt = new PHPUnitParserTest; + $r = $pt->runTest( $this->test['test'], $this->test['input'], + $this->test['result'], $this->test['options'], $this->test['config'] + ); + } + catch ( PHPUnit_Framework_AssertionFailedError $e ) { + + // PHPUnit_Util_Timer -> PHP_Timer support (see above) + if ( class_exists( 'PHP_Timer' ) ) { + $result->addFailure( $this, $e, PHP_Timer::stop() ); + } else { + $result->addFailure( $this, $e, PHPUnit_Util_Timer::stop() ); + } + } + catch ( Exception $e ) { + // PHPUnit_Util_Timer -> PHP_Timer support (see above) + if ( class_exists( 'PHP_Timer' ) ) { + $result->addFailure( $this, $e, PHP_Timer::stop() ); + } else { + $result->addFailure( $this, $e, PHPUnit_Util_Timer::stop() ); + } + } + + // PHPUnit_Util_Timer -> PHP_Timer support (see above) + if ( class_exists( 'PHP_Timer' ) ) { + $result->endTest( $this, PHP_Timer::stop() ); + } else { + $result->endTest( $this, PHPUnit_Util_Timer::stop() ); + } + + $backend->recorder->record( $this->test['test'], $r ); + $this->addToAssertionCount( PHPUnit_Framework_Assert::getCount() ); + + return $result; + } + + public function toString() { + return $this->test['test']; + } + +} + +class ParserTestSuiteBackend extends PHPUnit_FrameWork_TestSuite { + public $recorder; + public $term; + static $usePHPUnit = false; + + function __construct() { + parent::__construct(); + $this->setupRecorder(null); + self::$usePHPUnit = method_exists('PHPUnit_Framework_Assert', 'assertEquals'); + } + + function showTesting( $desc ) { + } + + function showRunFile( $path ) { + } + + function showTestResult( $desc, $result, $out ) { + if ( $result === $out ) { + return self::showSuccess( $desc, $result, $out ); + } else { + return self::showFailure( $desc, $result, $out ); + } + } + + public function setupRecorder( $options ) { + $this->recorder = new PHPUnitTestRecorder( $this ); + } +} + +class PHPUnitTestRecorder extends TestRecorder { + function record( $test, $result ) { + $this->total++; + $this->success += $result; + + } + + function reportPercentage( $success, $total ) { } +} |