jenkins-bot has submitted this change and it was merged.

Change subject: Name singleton overriding methods more clearly
......................................................................


Name singleton overriding methods more clearly

MobileContext::setInstance is only used in PHPUnit test suites.
Typically it's used to destroy the singleton instance in order to reset
its state.

Changes:
* Rename MobileContext::setInstance to ::setInstanceForTesting to make
  it clear when the method should be used
* Add MobileContext::resetInstanceForTesting, which handles the common
  use case above

Bug: T143875
Change-Id: If959a53c57235de1bc74a406b1b42c3ea463897e
---
M includes/MobileContext.php
M tests/phpunit/MobileContextTest.php
M tests/phpunit/MobileFrontend.hooksTest.php
M tests/phpunit/specials/SpecialMobileDiffTest.php
4 files changed, 23 insertions(+), 15 deletions(-)

Approvals:
  Jdlrobson: Looks good to me, approved
  jenkins-bot: Verified



diff --git a/includes/MobileContext.php b/includes/MobileContext.php
index d88da4b..bd54b1f 100644
--- a/includes/MobileContext.php
+++ b/includes/MobileContext.php
@@ -121,15 +121,25 @@
        }
 
        /**
-        * Set $this->instance to the given instance of MobileContext or null
-        * @param MobileContext|null $instance MobileContext instance or null 
to set
-        * @return MobileContext|null
+        * Overrides the singleton instance.
+        *
+        * @warning This method should only accept instances of 
`MobileContext`. The `BogusMobileContext`
+        *  class is used in the `MobileContextTest` test suite and will be 
removed.
+        *
+        * @param MobileContext|BogusContext $instance
         */
-       public static function setInstance( /* MobileContext|null */ $instance 
) {
+       public static function setInstanceForTesting( $instance ) {
                self::$instance = $instance;
        }
 
        /**
+        * Resets the singleton instance.
+        */
+       public static function resetInstanceForTesting() {
+               self::$instance = null;
+       }
+
+       /**
         * Set the IontextSource Object
         * @param IContextSource $context The IContextSource Object has to set
         */
diff --git a/tests/phpunit/MobileContextTest.php 
b/tests/phpunit/MobileContextTest.php
index 5f6a657..dfb7287 100644
--- a/tests/phpunit/MobileContextTest.php
+++ b/tests/phpunit/MobileContextTest.php
@@ -22,12 +22,13 @@
        protected function setUp() {
                parent::setUp();
                // Permit no access to the singleton
-               MobileContext::setInstance( new BogusMobileContext() );
+               MobileContext::setInstanceForTesting( new BogusMobileContext() 
);
        }
 
        protected function tearDown() {
-               MobileContext::setInstance( null ); // refresh it
                parent::tearDown();
+
+               MobileContext::resetInstanceForTesting();
        }
 
        /**
@@ -653,7 +654,7 @@
                );
                $req->setRequestURL( 
'/w/index.php?title=Special:Search&mobileaction=toggle_view_mobile' );
                RequestContext::getMain()->setRequest( $req );
-               MobileContext::setInstance( null );
+               MobileContext::resetInstanceForTesting();
                $this->setMwGlobals( 'wgTitle', null );
                SpecialPage::getTitleFor( 'Search' );
                $this->assertTrue( true, 'In case of failure this test just 
crashes' );
diff --git a/tests/phpunit/MobileFrontend.hooksTest.php 
b/tests/phpunit/MobileFrontend.hooksTest.php
index b5b5d8a..4525f62 100644
--- a/tests/phpunit/MobileFrontend.hooksTest.php
+++ b/tests/phpunit/MobileFrontend.hooksTest.php
@@ -108,8 +108,7 @@
         * SkinTemplate (sk) and OutputPage (out)
         */
        protected function getContextSetup( $mode, $mfXAnalyticsItems, $title = 
null ) {
-               // Create a new MobileContext object for this test
-               MobileContext::setInstance( null );
+               MobileContext::resetInstanceForTesting();
                // create a new instance of MobileContext
                $context = MobileContext::singleton();
                // create a DerivativeContext to use in MobileContext later
@@ -141,8 +140,6 @@
                foreach ( $mfXAnalyticsItems as $key => $val ) {
                        $context->addAnalyticsLogItem( $key, $val );
                }
-               // set the newly created MobileContext object as the current 
instance to use
-               MobileContext::setInstance( $context );
 
                // return the stuff
                return [
@@ -183,7 +180,7 @@
                        'wgScriptPath' => '/w',
                        'wgScript' => '/w/index.php',
                ] );
-               MobileContext::setInstance( null );
+               MobileContext::resetInstanceForTesting();
 
                $title = Title::newFromText( 'PurgeTest' );
 
diff --git a/tests/phpunit/specials/SpecialMobileDiffTest.php 
b/tests/phpunit/specials/SpecialMobileDiffTest.php
index bea2133..179419d 100644
--- a/tests/phpunit/specials/SpecialMobileDiffTest.php
+++ b/tests/phpunit/specials/SpecialMobileDiffTest.php
@@ -8,11 +8,12 @@
        private $unsetReqVals = [];
 
        public function tearDown() {
+               parent::tearDown();
+
                foreach ( $this->unsetReqVals as $v ) {
                        MobileContext::singleton()->getRequest()->unsetVal( $v 
);
                }
-               MobileContext::setInstance( null ); // refresh MobileContext 
instance
-               parent::tearDown();
+               MobileContext::resetInstanceForTesting();
        }
        /**
         * @dataProvider providerTestNames
@@ -142,4 +143,3 @@
                return '';
        }
 }
-

-- 
To view, visit https://gerrit.wikimedia.org/r/312243
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: merged
Gerrit-Change-Id: If959a53c57235de1bc74a406b1b42c3ea463897e
Gerrit-PatchSet: 2
Gerrit-Project: mediawiki/extensions/MobileFrontend
Gerrit-Branch: master
Gerrit-Owner: Phuedx <g...@samsmith.io>
Gerrit-Reviewer: Jdlrobson <jrob...@wikimedia.org>
Gerrit-Reviewer: jenkins-bot <>

_______________________________________________
MediaWiki-commits mailing list
MediaWiki-commits@lists.wikimedia.org
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to