Merge "test: detects parent setUp not being called"
authorAnomie <bjorsch@wikimedia.org>
Wed, 5 Dec 2012 22:42:03 +0000 (22:42 +0000)
committerGerrit Code Review <gerrit@wikimedia.org>
Wed, 5 Dec 2012 22:42:03 +0000 (22:42 +0000)
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() {