<?php
use Liuggio\StatsdClient\Factory\StatsdDataFactoryInterface;
+use MediaWiki\MediaWikiServices;
use Wikimedia\Assert\Assert;
/**
*/
private $stats;
- /**
- * @var self|null
- */
- private static $instance;
-
/**
* @param LoadBalancer $loadBalancer
* @param HashBagOStuff $cache
}
/**
- * 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 ) {
<?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
$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 );
*
* @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' ],
];
}
->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 );
}
/**
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() {
$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'
);
$itemTwo->getTitle()->getTalkPage(),
]
);
- $scopedOverride = WatchedItemStore::overrideDefaultInstance( $store );
+ $this->setService( 'WatchedItemStore', $store );
WatchedItem::batchAddWatch( [ $itemOne, $itemTwo ] );
-
- ScopedCallback::consume( $scopedOverride );
}
}