Make WatchedItemStore use MediaWikiServices
authoraddshore <addshorewiki@gmail.com>
Wed, 6 Apr 2016 10:46:50 +0000 (11:46 +0100)
committeraddshore <addshorewiki@gmail.com>
Mon, 11 Apr 2016 14:50:15 +0000 (15:50 +0100)
Change-Id: I5c8c1a652a35e055d8d65753a60c0d1684e1c37d

includes/MediaWikiServices.php
includes/ServiceWiring.php
includes/WatchedItemStore.php
tests/phpunit/includes/MediaWikiServicesTest.php
tests/phpunit/includes/WatchedItemStoreUnitTest.php
tests/phpunit/includes/WatchedItemUnitTest.php

index f5ae2d5..dd66967 100644 (file)
@@ -34,6 +34,8 @@ use SiteStore;
 use SpecialPageFactory;
 use Title;
 use User;
+use WatchedItemStore;
+use Wikimedia\Assert\Assert;
 
 /**
  * Service locator for MediaWiki core services.
@@ -448,6 +450,13 @@ class MediaWikiServices extends ServiceContainer {
                return $this->getService( 'DBLoadBalancer' );
        }
 
+       /**
+        * @return WatchedItemStore
+        */
+       public function getWatchedItemStore() {
+               return $this->getService( 'WatchedItemStore' );
+       }
+
        ///////////////////////////////////////////////////////////////////////////
        // NOTE: When adding a service getter here, don't forget to add a test
        // case for it in MediaWikiServicesTest::provideGetters() and in
index 5ec1d64..dc2c66b 100644 (file)
@@ -92,6 +92,15 @@ return [
                return $services->getConfigFactory()->makeConfig( 'main' );
        },
 
+       'WatchedItemStore' => function( MediaWikiServices $services ) {
+               $store = new WatchedItemStore(
+                       $services->getDBLoadBalancer(),
+                       new HashBagOStuff( [ 'maxKeys' => 100 ] )
+               );
+               $store->setStatsdDataFactory( RequestContext::getMain()->getStats() );
+               return $store;
+       }
+
        ///////////////////////////////////////////////////////////////////////////
        // NOTE: When adding a service here, don't forget to add a getter function
        // in the MediaWikiServices class. The convenience getter should just call
index 8ae7932..603bacd 100644 (file)
@@ -1,6 +1,7 @@
 <?php
 
 use Liuggio\StatsdClient\Factory\StatsdDataFactoryInterface;
+use MediaWiki\MediaWikiServices;
 use Wikimedia\Assert\Assert;
 
 /**
@@ -49,11 +50,6 @@ class WatchedItemStore implements StatsdAwareInterface {
         */
        private $stats;
 
-       /**
-        * @var self|null
-        */
-       private static $instance;
-
        /**
         * @param LoadBalancer $loadBalancer
         * @param HashBagOStuff $cache
@@ -125,43 +121,11 @@ class WatchedItemStore implements StatsdAwareInterface {
        }
 
        /**
-        * Overrides the default instance of this class
-        * This is intended for use while testing and will fail if MW_PHPUNIT_TEST is not defined.
-        *
-        * If this method is used it MUST also be called with null after a test to ensure a new
-        * default instance is created next time getDefaultInstance is called.
-        *
-        * @param WatchedItemStore|null $store
-        *
-        * @return ScopedCallback to reset the overridden value
-        * @throws MWException
-        */
-       public static function overrideDefaultInstance( WatchedItemStore $store = null ) {
-               if ( !defined( 'MW_PHPUNIT_TEST' ) ) {
-                       throw new MWException(
-                               'Cannot override ' . __CLASS__ . 'default instance in operation.'
-                       );
-               }
-
-               $previousValue = self::$instance;
-               self::$instance = $store;
-               return new ScopedCallback( function() use ( $previousValue ) {
-                       self::$instance = $previousValue;
-               } );
-       }
-
-       /**
+        * @deprecated use MediaWikiServices::getInstance()->getWatchedItemStore()
         * @return self
         */
        public static function getDefaultInstance() {
-               if ( !self::$instance ) {
-                       self::$instance = new self(
-                               wfGetLB(),
-                               new HashBagOStuff( [ 'maxKeys' => 100 ] )
-                       );
-                       self::$instance->setStatsdDataFactory( RequestContext::getMain()->getStats() );
-               }
-               return self::$instance;
+               return MediaWikiServices::getInstance()->getWatchedItemStore();
        }
 
        private function getCacheKey( User $user, LinkTarget $target ) {
index 53cd0b1..96abf97 100644 (file)
@@ -230,6 +230,7 @@ class MediaWikiServicesTest extends PHPUnit_Framework_TestCase {
                        'SiteLookup' => [ 'SiteLookup', SiteLookup::class ],
                        'DBLoadBalancerFactory' => [ 'DBLoadBalancerFactory', 'LBFactory' ],
                        'DBLoadBalancer' => [ 'DBLoadBalancer', 'LoadBalancer' ],
+                       'WatchedItemStore' => [ 'WatchedItemStore', WatchedItemStore::class ],
                ];
        }
 
index 9dd38df..78231c2 100644 (file)
@@ -1,12 +1,11 @@
 <?php
-use Liuggio\StatsdClient\Factory\StatsdDataFactory;
 
 /**
  * @author Addshore
  *
  * @covers WatchedItemStore
  */
-class WatchedItemStoreUnitTest extends PHPUnit_Framework_TestCase {
+class WatchedItemStoreUnitTest extends MediaWikiTestCase {
 
        /**
         * @return PHPUnit_Framework_MockObject_MockObject|IDatabase
@@ -96,17 +95,6 @@ class WatchedItemStoreUnitTest extends PHPUnit_Framework_TestCase {
                $this->assertSame( $instanceOne, $instanceTwo );
        }
 
-       public function testOverrideDefaultInstance() {
-               $instance = WatchedItemStore::getDefaultInstance();
-               $scopedOverride = $instance->overrideDefaultInstance( null );
-
-               $this->assertNotSame( $instance, WatchedItemStore::getDefaultInstance() );
-
-               unset( $scopedOverride );
-
-               $this->assertSame( $instance, WatchedItemStore::getDefaultInstance() );
-       }
-
        public function testCountWatchedItems() {
                $user = $this->getMockNonAnonUserWithId( 1 );
 
index b4eaa76..db7f16c 100644 (file)
@@ -5,13 +5,30 @@
  *
  * @covers WatchedItem
  */
-class WatchedItemUnitTest extends PHPUnit_Framework_TestCase {
+class WatchedItemUnitTest extends MediaWikiTestCase {
+
+       /**
+        * @param int $id
+        *
+        * @return PHPUnit_Framework_MockObject_MockObject|User
+        */
+       private function getMockUser( $id ) {
+               $user = $this->getMock( User::class );
+               $user->expects( $this->any() )
+                       ->method( 'getId' )
+                       ->will( $this->returnValue( $id ) );
+               $user->expects( $this->any() )
+                       ->method( 'isAllowed' )
+                       ->will( $this->returnValue( true ) );
+               return $user;
+       }
 
        public function provideUserTitleTimestamp() {
+               $user = $this->getMockUser( 111 );
                return [
-                       [ User::newFromId( 111 ), Title::newFromText( 'SomeTitle' ), null ],
-                       [ User::newFromId( 111 ), Title::newFromText( 'SomeTitle' ), '20150101010101' ],
-                       [ User::newFromId( 111 ), new TitleValue( 0, 'TVTitle', 'frag' ), '20150101010101' ],
+                       [ $user, Title::newFromText( 'SomeTitle' ), null ],
+                       [ $user, Title::newFromText( 'SomeTitle' ), '20150101010101' ],
+                       [ $user, new TitleValue( 0, 'TVTitle', 'frag' ), '20150101010101' ],
                ];
        }
 
@@ -51,15 +68,13 @@ class WatchedItemUnitTest extends PHPUnit_Framework_TestCase {
                        ->method( 'loadWatchedItem' )
                        ->with( $user, $linkTarget )
                        ->will( $this->returnValue( new WatchedItem( $user, $linkTarget, $timestamp ) ) );
-               $scopedOverride = WatchedItemStore::overrideDefaultInstance( $store );
+               $this->setService( 'WatchedItemStore', $store );
 
                $item = WatchedItem::fromUserTitle( $user, $linkTarget, User::IGNORE_USER_RIGHTS );
 
                $this->assertEquals( $user, $item->getUser() );
                $this->assertEquals( $linkTarget, $item->getLinkTarget() );
                $this->assertEquals( $timestamp, $item->getNotificationTimestamp() );
-
-               ScopedCallback::consume( $scopedOverride );
        }
 
        /**
@@ -85,12 +100,10 @@ class WatchedItemUnitTest extends PHPUnit_Framework_TestCase {
                                        return true;
                                }
                        ) );
-               $scopedOverride = WatchedItemStore::overrideDefaultInstance( $store );
+               $this->setService( 'WatchedItemStore', $store );
 
                $item = new WatchedItem( $user, $linkTarget, $timestamp );
                $item->resetNotificationTimestamp( $force, $oldid );
-
-               ScopedCallback::consume( $scopedOverride );
        }
 
        public function testAddWatch() {
@@ -157,17 +170,15 @@ class WatchedItemUnitTest extends PHPUnit_Framework_TestCase {
                $store->expects( $this->once() )
                        ->method( 'duplicateAllAssociatedEntries' )
                        ->with( $oldTitle, $newTitle );
-               $scopedOverride = WatchedItemStore::overrideDefaultInstance( $store );
+               $this->setService( 'WatchedItemStore', $store );
 
                WatchedItem::duplicateEntries( $oldTitle, $newTitle );
-
-               ScopedCallback::consume( $scopedOverride );
        }
 
        public function testBatchAddWatch() {
-               $itemOne = new WatchedItem( User::newFromId( 1 ), new TitleValue( 0, 'Title1' ), null );
+               $itemOne = new WatchedItem( $this->getMockUser( 1 ), new TitleValue( 0, 'Title1' ), null );
                $itemTwo = new WatchedItem(
-                       User::newFromId( 3 ),
+                       $this->getMockUser( 3 ),
                        Title::newFromText( 'Title2' ),
                        '20150101010101'
                );
@@ -193,11 +204,9 @@ class WatchedItemUnitTest extends PHPUnit_Framework_TestCase {
                                        $itemTwo->getTitle()->getTalkPage(),
                                ]
                        );
-               $scopedOverride = WatchedItemStore::overrideDefaultInstance( $store );
+               $this->setService( 'WatchedItemStore', $store );
 
                WatchedItem::batchAddWatch( [ $itemOne, $itemTwo ] );
-
-               ScopedCallback::consume( $scopedOverride );
        }
 
 }