From 5b9f76aef7ce60846a3820bd170aa09a3a6c12e6 Mon Sep 17 00:00:00 2001 From: Antoine Musso Date: Tue, 23 Oct 2012 01:53:51 +0200 Subject: [PATCH] test: detects parent setUp not being called Our test classes often overload MediaWikiTestCase::setUp() but forget to call their parent. This patch makes MediaWikiTestCase to flag whenever its own setUp() is called and then simply assert it got called. Any class failing the assertion is missing a call to its parent setup which is easily fixed by adding: parent::setUp(). It would be nice to find a similar trick for tearDown(). Change-Id: Ia2afed6052eb3863d6c8e68c551cf03b33bb4be9 --- tests/phpunit/MediaWikiTestCase.php | 25 +++++++++++++++++++ .../phpunit/includes/parser/NewParserTest.php | 4 +++ 2 files changed, 29 insertions(+) diff --git a/tests/phpunit/MediaWikiTestCase.php b/tests/phpunit/MediaWikiTestCase.php index a594202106..a414962188 100644 --- a/tests/phpunit/MediaWikiTestCase.php +++ b/tests/phpunit/MediaWikiTestCase.php @@ -5,6 +5,20 @@ abstract class MediaWikiTestCase extends PHPUnit_Framework_TestCase { public $regex = ''; public $runDisabled = false; + /** + * $called tracks whether the setUp and tearDown method has been called. + * class extending MediaWikiTestCase usually override setUp and tearDown + * but forget to call the parent. + * + * The array format takes a method name as key and anything as a value. + * By asserting the key exist, we know the child class has called the + * parent. + * + * This property must be private, we do not want child to override it, + * they should call the appropriate parent method instead. + */ + private $called = array(); + /** * @var Array of TestUser */ @@ -150,6 +164,7 @@ abstract class MediaWikiTestCase extends PHPUnit_Framework_TestCase { protected function setUp() { wfProfileIn( __METHOD__ ); parent::setUp(); + $this->called['setUp'] = 1; /* //@todo: global variables to restore for *every* test @@ -210,6 +225,16 @@ abstract class MediaWikiTestCase extends PHPUnit_Framework_TestCase { wfProfileOut( __METHOD__ ); } + /** + * Make sure MediaWikiTestCase extending classes have called their + * parent setUp method + */ + final public function testMediaWikiTestCaseParentSetupCalled() { + $this->assertArrayHasKey( 'setUp', $this->called, + get_called_class() . "::setUp() must call parent::setUp()" + ); + } + /** * Individual test functions may override globals (either directly or through this * setMwGlobals() function), however one must call this method at least once for diff --git a/tests/phpunit/includes/parser/NewParserTest.php b/tests/phpunit/includes/parser/NewParserTest.php index 3476ce39ee..3e3b141fcd 100644 --- a/tests/phpunit/includes/parser/NewParserTest.php +++ b/tests/phpunit/includes/parser/NewParserTest.php @@ -36,6 +36,8 @@ class NewParserTest extends MediaWikiTestCase { global $wgNamespaceProtection, $wgNamespaceAliases; global $wgHooks, $IP; + parent::setUp(); + $wgLanguageCode = 'en'; $wgContLang = Language::factory( 'en' ); @@ -117,6 +119,8 @@ class NewParserTest extends MediaWikiTestCase { // Restore backends RepoGroup::destroySingleton(); FileBackendGroup::destroySingleton(); + + parent::tearDown(); } function addDBData() { -- 2.20.1