test: detects parent setUp not being called
authorAntoine Musso <hashar@free.fr>
Mon, 22 Oct 2012 23:53:51 +0000 (01:53 +0200)
committerAntoine Musso <hashar@free.fr>
Wed, 5 Dec 2012 15:44:41 +0000 (16:44 +0100)
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
tests/phpunit/includes/parser/NewParserTest.php

index a594202..a414962 100644 (file)
@@ -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
index 3476ce3..3e3b141 100644 (file)
@@ -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() {