r69824 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r69823‎ | r69824 | r69825 >
Date:04:48, 24 July 2010
Author:aaron
Status:resolved (Comments)
Tags:
Comment:
Added some experimental code tests
Modified paths:
  • /trunk/extensions/FlaggedRevs/maintenance/tests (added) (history)
  • /trunk/extensions/FlaggedRevs/maintenance/tests/FRInclusionManagerTest.php (added) (history)
  • /trunk/extensions/FlaggedRevs/maintenance/tests/FlaggedRevsTestSuite.php (added) (history)

Diff [purge]

Index: trunk/extensions/FlaggedRevs/maintenance/tests/FlaggedRevsTestSuite.php
@@ -0,0 +1,20 @@
 2+<?php
 3+
 4+require_once 'PHPUnit\Framework\TestSuite.php';
 5+
 6+class FlaggedRevsTestSuite extends PHPUnit_Framework_TestSuite {
 7+ /**
 8+ * Constructs the test suite handler.
 9+ */
 10+ public function __construct() {
 11+ $this->setName( 'FlaggedRevs' );
 12+ $this->addTestSuite( 'FRInclusionManagerTest' );
 13+ }
 14+
 15+ /**
 16+ * Creates the suite.
 17+ */
 18+ public static function suite() {
 19+ return new self();
 20+ }
 21+}
Property changes on: trunk/extensions/FlaggedRevs/maintenance/tests/FlaggedRevsTestSuite.php
___________________________________________________________________
Added: svn:eol-style
122 + native
Index: trunk/extensions/FlaggedRevs/maintenance/tests/FRInclusionManagerTest.php
@@ -0,0 +1,163 @@
 2+<?php
 3+
 4+require_once 'PHPUnit\Framework\TestCase.php';
 5+
 6+define( 'MEDIAWIKI', 1 ); // hack
 7+if ( getenv( 'MW_INSTALL_PATH' ) ) {
 8+ $IP = getenv( 'MW_INSTALL_PATH' );
 9+} else {
 10+ $IP = dirname(__FILE__).'/../../../..';
 11+}
 12+require_once "$IP/includes/defines.php"; // constants (NS_FILE ect...)
 13+require_once dirname(__FILE__).'/../../FRInclusionManager.php';
 14+
 15+##### Fake classes for dependencies ####
 16+class Title {
 17+ protected $ns;
 18+ protected $dbKey;
 19+ public static function makeTitleSafe( $ns, $dbKey ) {
 20+ $t = new self();
 21+ $t->ns = (int)$ns;
 22+ $t->dbKey = (string)$dbKey;
 23+ return $t;
 24+ }
 25+ public function getNamespace() {
 26+ return $this->ns;
 27+ }
 28+ public function getDBKey() {
 29+ return $this->dbKey;
 30+ }
 31+}
 32+class FlaggedRevision {
 33+ public static function newFromStable() {
 34+ return null; // not in DB
 35+ }
 36+}
 37+class MWException extends Exception { } // just in case
 38+##### End fake classes #####
 39+
 40+class FRInclusionManagerTest extends PHPUnit_Framework_TestCase {
 41+ /* starting input */
 42+ protected static $inputTmps = array(
 43+ 10 => array('XX' => '1242', 'YY' => '0'),
 44+ 4 => array('cite' => '30', 'moo' => 0),
 45+ 0 => array('ZZ' => 464)
 46+ );
 47+ protected static $inputFiles = array(
 48+ 'fXX' => array('ts' => '20100405192110', 'sha1' => 'abc1'),
 49+ 'fYY' => array('ts' => '20000403101300', 'sha1' => 'ffc2'),
 50+ 'fZZ' => array('ts' => '0', 'sha1' => ''),
 51+ 'fle' => array('ts' => 0, 'sha1' => '')
 52+ );
 53+ /* output to test against (test# => test) */
 54+ protected static $rOutputTmps = array(
 55+ 0 => array( 10, 'XX', 1242 ),
 56+ 1 => array( 10, 'YY', 0 ),
 57+ 2 => array( 4, 'cite', 30 ),
 58+ 3 => array( 4, 'moo', 0 ),
 59+ 4 => array( 0, 'ZZ', 464 ),
 60+ 5 => array( 0, 'notexists', null )
 61+ );
 62+ protected static $rOutputFiles = array(
 63+ 0 => array( 'fXX', '20100405192110', 'abc1'),
 64+ 1 => array( 'fYY', '20000403101300', 'ffc2'),
 65+ 2 => array( 'fZZ', '0', ''),
 66+ 3 => array( 'fle', '0', ''),
 67+ 4 => array( 'notgiven', null, null),
 68+ );
 69+ protected static $sOutputTmps = array(
 70+ 0 => array( 10, 'XX', 1242 ),
 71+ 1 => array( 10, 'YY', 0 ),
 72+ 2 => array( 4, 'cite', 30 ),
 73+ 3 => array( 4, 'moo', 0 ),
 74+ 4 => array( 0, 'ZZ', 464 ),
 75+ 5 => array( 0, 'notexists', 0 )
 76+ );
 77+ protected static $sOutputFiles = array(
 78+ 0 => array( 'fXX', '20100405192110', 'abc1'),
 79+ 1 => array( 'fYY', '20000403101300', 'ffc2'),
 80+ 2 => array( 'fZZ', '0', ''),
 81+ 3 => array( 'fle', '0', ''),
 82+ 4 => array( 'notgiven', '0', ''),
 83+ );
 84+
 85+ /**
 86+ * Prepares the environment before running a test.
 87+ */
 88+ protected function setUp() {
 89+ parent::setUp();
 90+ }
 91+
 92+ /**
 93+ * Cleans up the environment after running a test.
 94+ */
 95+ protected function tearDown() {
 96+ parent::tearDown();
 97+ FRInclusionManager::singleton()->clear();
 98+ }
 99+
 100+ /**
 101+ * Constructs the test case.
 102+ */
 103+ public function __construct() {}
 104+
 105+ public function testManagerInitial() {
 106+ $im = FRInclusionManager::singleton();
 107+ $this->assertEquals( false, $im->parserOutputIsStabilized(), "Starts off empty" );
 108+ }
 109+
 110+ public function testManagerClear() {
 111+ $im = FRInclusionManager::singleton();
 112+ $im->setReviewedVersions( self::$inputTmps, self::$inputFiles );
 113+ $im->clear();
 114+ $this->assertEquals( false, $im->parserOutputIsStabilized(), "Empty on clear()" );
 115+ }
 116+
 117+ public function testReviewedTemplateVersions() {
 118+ $im = FRInclusionManager::singleton();
 119+ $im->setReviewedVersions( self::$inputTmps, self::$inputFiles );
 120+ foreach ( self::$rOutputTmps as $x => $triple ) {
 121+ list($ns,$dbKey,$expId) = $triple;
 122+ $title = Title::makeTitleSafe( $ns, $dbKey );
 123+ $actual = $im->getReviewedTemplateVersion( $title );
 124+ $this->assertEquals( $expId, $actual, "Rev ID test case $x" );
 125+ }
 126+ }
 127+
 128+ public function testReviewedFileVersions() {
 129+ $im = FRInclusionManager::singleton();
 130+ $im->setReviewedVersions( self::$inputTmps, self::$inputFiles );
 131+ foreach ( self::$rOutputFiles as $x => $triple ) {
 132+ list($dbKey,$expTS,$expSha1) = $triple;
 133+ $title = Title::makeTitleSafe( NS_FILE, $dbKey );
 134+ list($actualTS,$actualSha1) = $im->getReviewedFileVersion( $title );
 135+ $this->assertEquals( $expTS, $actualTS, "Timestamp test case $x" );
 136+ $this->assertEquals( $expSha1, $actualSha1, "Sha1 test case $x" );
 137+ }
 138+ }
 139+
 140+ public function testStableTemplateVersions() {
 141+ $im = FRInclusionManager::singleton();
 142+ $im->setReviewedVersions( array(), array() );
 143+ $im->setStableVersionCache( self::$inputTmps, self::$inputFiles );
 144+ foreach ( self::$sOutputTmps as $x => $triple ) {
 145+ list($ns,$dbKey,$expId) = $triple;
 146+ $title = Title::makeTitleSafe( $ns, $dbKey );
 147+ $actual = $im->getStableTemplateVersion( $title );
 148+ $this->assertEquals( $expId, $actual, "Rev ID test case $x" );
 149+ }
 150+ }
 151+
 152+ public function testStableFileVersions() {
 153+ $im = FRInclusionManager::singleton();
 154+ $im->setReviewedVersions( array(), array() );
 155+ $im->setStableVersionCache( self::$inputTmps, self::$inputFiles );
 156+ foreach ( self::$sOutputFiles as $x => $triple ) {
 157+ list($dbKey,$expTS,$expSha1) = $triple;
 158+ $title = Title::makeTitleSafe( NS_FILE, $dbKey );
 159+ list($actualTS,$actualSha1) = $im->getStableFileVersion( $title );
 160+ $this->assertEquals( $expTS, $actualTS, "Timestamp test case $x" );
 161+ $this->assertEquals( $expSha1, $actualSha1, "Sha1 test case $x" );
 162+ }
 163+ }
 164+}
Property changes on: trunk/extensions/FlaggedRevs/maintenance/tests/FRInclusionManagerTest.php
___________________________________________________________________
Added: svn:eol-style
1165 + native

Follow-up revisions

RevisionCommit summaryAuthorDate
r70254* r69824: Removed require() that is not needed anymore...aaron04:37, 1 August 2010

Comments

#Comment by MaxSem (talk | contribs)   20:15, 24 July 2010

Instead of making these tests standalone, use UnitTestsList hook to integrate them into core.

#Comment by Aaron Schulz (talk | contribs)   21:09, 24 July 2010

I already looked at that and it was too much of a pain setting up. Someday I'll look at that again. It's also not really documented enough to know what's going on.

#Comment by MaxSem (talk | contribs)   21:16, 24 July 2010

From CodeReview:

$wgHooks['UnitTestsList'][] = 'efCodeReviewUnitTests';

function efCodeReviewUnitTests( &$files ) {
	$files[] = dirname( __FILE__ ) . '/tests/CodeReviewTest.php';
	return true;
}

That's all:) And you don't need to include manually anything neither from PHPUnit nor MW.

#Comment by Aaron Schulz (talk | contribs)   21:25, 24 July 2010

Doesn't this run from Selenium tests? I wouldn't be able to tell if I was breaking anything there.

#Comment by MaxSem (talk | contribs)   06:35, 25 July 2010

No, just ordinary PHPUnit. Here's an example of how it could be done: http://mediawiki.pastebin.com/CtLgKj7q Tests fail for me this way, but it's just a demonstration how you can:

  • run your tests when core tests are run with maintenance/tests/phpunit
  • get rid of useless cruft of including pieces of MW manually and making mistakes in the process - for example, these tests will fail on *nix because you wrote defines.php with a wrong case
#Comment by Aaron Schulz (talk | contribs)   08:03, 25 July 2010

That's a relief. I never *wanted* to manually include stuff.

globalArticleInstance() fails since wgTitle/wgArticle are null. I'm using the .bat file (which I guess I use?). Is there a documentation page that explains this kind of stuff somewhere?

#Comment by Platonides (talk | contribs)   18:51, 31 July 2010

+require_once 'PHPUnit\Framework\TestCase.php'; Windows paths?

I don't know what's the right way to run this, but I'm pretty sure it's not doing that.

#Comment by 😂 (talk | contribs)   15:53, 7 September 2010

Fixed with r69863

Status & tagging log