Thiemo Mättig (WMDE) has uploaded a new change for review. ( https://gerrit.wikimedia.org/r/330194 )
Change subject: Greatly reduce the complexity of InfoActionHookHandlerTest ...................................................................... Greatly reduce the complexity of InfoActionHookHandlerTest * I found this because this was mocking specific implementations instead of interfaces. * InfoActionHookHandler was also expecting a specific implementation, but should not. * There was an entirely unused $enabled parameter. * I turned the $message parameter into actual names for the test cases. * A method was mocked twice. This never triggered an error because the return value of this method was never used anyway. I fixed this by adding with() conditions. * handle() was called with a pre-set array, even if it does not care about what is in this array. Replaced with []. * Replaced a returnCallback() with a more simple returnValue(). * Inlined as well as extracted some code. * Fixed PHPDocs. Change-Id: Ie975283a564c2493842bdc7fec13f0d28f5122b8 --- M repo/includes/Hooks/InfoActionHookHandler.php M repo/tests/phpunit/includes/Hooks/InfoActionHookHandlerTest.php 2 files changed, 65 insertions(+), 83 deletions(-) git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/Wikibase refs/changes/94/330194/1 diff --git a/repo/includes/Hooks/InfoActionHookHandler.php b/repo/includes/Hooks/InfoActionHookHandler.php index 133c4b7..f3e5cae 100644 --- a/repo/includes/Hooks/InfoActionHookHandler.php +++ b/repo/includes/Hooks/InfoActionHookHandler.php @@ -6,7 +6,7 @@ use IContextSource; use SiteLookup; use Title; -use Wikibase\Store\Sql\SqlSubscriptionLookup; +use Wikibase\Store\SubscriptionLookup; use Wikibase\Store\EntityIdLookup; use Wikibase\Lib\Store\EntityNamespaceLookup; @@ -24,7 +24,7 @@ private $namespaceChecker; /** - * @var SqlSubscriptionLookup + * @var SubscriptionLookup */ private $subscriptionLookup; @@ -45,7 +45,7 @@ public function __construct( EntityNamespaceLookup $namespaceChecker, - SqlSubscriptionLookup $subscriptionLookup, + SubscriptionLookup $subscriptionLookup, SiteLookup $siteLookup, EntityIdLookup $entityIdLookup, IContextSource $context @@ -77,7 +77,7 @@ /** * @param Title $title * - * @return array + * @return string[] HTML */ private function getPageInfoRow( Title $title ) { $entity = $this->entityIdLookup->getEntityIdForTitle( $title ); @@ -93,7 +93,7 @@ * @param string[] $subscriptions * @param Title $title * - * @return string HTML[] + * @return string[] HTML */ private function formatSubscriptions( array $subscriptions, Title $title ) { $output = ''; diff --git a/repo/tests/phpunit/includes/Hooks/InfoActionHookHandlerTest.php b/repo/tests/phpunit/includes/Hooks/InfoActionHookHandlerTest.php index 847da7f..b52b600 100644 --- a/repo/tests/phpunit/includes/Hooks/InfoActionHookHandlerTest.php +++ b/repo/tests/phpunit/includes/Hooks/InfoActionHookHandlerTest.php @@ -5,15 +5,15 @@ use Html; use IContextSource; use RequestContext; -use FileBasedSiteLookup; +use SiteLookup; use Site; use Title; -use Wikibase\Client\Store\Sql\PagePropsEntityIdLookup; +use Wikibase\Store\EntityIdLookup; use Wikibase\Lib\Store\EntityNamespaceLookup; use Wikibase\DataModel\Entity\Item; use Wikibase\DataModel\Entity\ItemId; use Wikibase\Repo\Hooks\InfoActionHookHandler; -use Wikibase\Store\Sql\SqlSubscriptionLookup; +use Wikibase\Store\SubscriptionLookup; /** * @covers Wikibase\Repo\Hooks\InfoActionHookHandler @@ -24,17 +24,22 @@ * * @license GPL-2.0+ * @author Amir Sarabadani <ladsgr...@gmail.com> + * @author Thiemo Mättig */ class InfoActionHookHandlerTest extends \PHPUnit_Framework_TestCase { /** * @dataProvider handleProvider */ - public function testHandle( $expected, $context, $pageInfo, $enabled, $subscriptions, $message ) { - $hookHandler = $this->newHookHandler( $enabled, $subscriptions, $context ); - $pageInfo = $hookHandler->handle( $context, $pageInfo ); + public function testHandle( + array $expected, + IContextSource $context, + array $subscriptions + ) { + $hookHandler = $this->newHookHandler( $subscriptions, $context ); + $pageInfo = $hookHandler->handle( $context, [] ); - $this->assertEquals( $expected, $pageInfo, $message ); + $this->assertEquals( $expected, $pageInfo ); } public function handleProvider() { @@ -44,114 +49,92 @@ $elementElwiki = Html::element( 'a', [ 'href' => $url ], 'elwiki' ); $context = $this->getContext(); - $cases = []; - - $cases[] = [ - [ - 'header-properties' => [ + return [ + 'dewiki and enwiki' => [ + [ 'header-properties' => [ [ $context->msg( 'wikibase-pageinfo-subscription' )->escaped(), "<ul><li>$elementDewiki</li><li>$elementEnwiki</li></ul>", ], - ] + ] ], + $context, + [ 'dewiki', 'enwiki' ] ], - $context, [ 'header-properties' => [] ], true, [ 'dewiki', 'enwiki' ], - 'dewiki and enwiki' - ]; - - $cases[] = [ - [ 'header-properties' => [ + 'elwiki' => [ + [ 'header-properties' => [ [ $context->msg( 'wikibase-pageinfo-subscription' )->escaped(), "<ul><li>$elementElwiki</li></ul>", ], - ] + ] ], + $context, + [ 'elwiki' ] ], - $context, - [ 'header-properties' => [] ], - false, - [ 'elwiki' ], - 'elwiki' - ]; - - $cases[] = [ - [ - 'header-properties' => [ + 'no subscription' => [ + [ 'header-properties' => [ [ $context->msg( 'wikibase-pageinfo-subscription' )->escaped(), $context->msg( 'wikibase-pageinfo-subscription-none' )->escaped() ] - ] - ], - $context, [ 'header-properties' => [] ], true, false, - 'no subscription' + ] ], + $context, + [] + ] ]; - - return $cases; } /** - * @param bool $enabled - * @param ItemId $entityId + * @param string[] $subscriptions + * @param IContextSource $context * * @return InfoActionHookHandler */ - private function newHookHandler( $enabled, $subscriptions, $context ) { - $namespaceLookup = new EntityNamespaceLookup( [ Item::ENTITY_TYPE => NS_MAIN ] ); + private function newHookHandler( array $subscriptions, IContextSource $context ) { + $itemId = new ItemId( 'Q4' ); - $subLookup = $this->getMockBuilder( SqlSubscriptionLookup::class ) - ->disableOriginalConstructor() - ->getMock(); - - $subLookup->expects( $this->any() ) + $subLookup = $this->getMock( SubscriptionLookup::class ); + $subLookup->expects( $this->once() ) ->method( 'getSubscribers' ) + ->with( $itemId ) ->will( $this->returnValue( $subscriptions ) ); - $siteLookup = $this->getMockBuilder( FileBasedSiteLookup::class ) - ->disableOriginalConstructor() - ->setMethods( [ 'getSite' ] ) - ->getMock(); - - $siteLookup->expects( $this->any() ) - ->method( 'getSite' ) - ->will( $this->returnCallback( function () { - $site = new Site(); - $site->addInterwikiId( 'en' ); - $site->setLinkPath( 'https://en.wikipedia.org/wiki/$1' ); - return $site; - } ) ); - - $entityIdLookup = $this->getMockBuilder( PagePropsEntityIdLookup::class ) - ->disableOriginalConstructor() - ->setMethods( [ 'getEntityIdForTitle' ] ) - ->getMock(); - - $entityIdLookup->expects( $this->any() ) + $entityIdLookup = $this->getMock( EntityIdLookup::class ); + $entityIdLookup->expects( $this->once() ) ->method( 'getEntityIdForTitle' ) - ->will( $this->returnValue( new ItemId( 'Q4' ) ) ); + ->with( $context->getTitle() ) + ->will( $this->returnValue( $itemId ) ); - $entityIdLookup->expects( $this->any() ) - ->method( 'getEntityIdForTitle' ) - ->will( $this->returnValue( false ) ); - - $hookHandler = new InfoActionHookHandler( - $namespaceLookup, + return new InfoActionHookHandler( + new EntityNamespaceLookup( [ Item::ENTITY_TYPE => NS_MAIN ] ), $subLookup, - $siteLookup, + $this->newSiteLookup(), $entityIdLookup, $context ); + } - return $hookHandler; + /** + * @return SiteLookup + */ + private function newSiteLookup() { + $site = new Site(); + $site->addInterwikiId( 'en' ); + $site->setLinkPath( 'https://en.wikipedia.org/wiki/$1' ); + + $siteLookup = $this->getMock( SiteLookup::class ); + + $siteLookup->expects( $this->any() ) + ->method( 'getSite' ) + ->will( $this->returnValue( $site ) ); + + return $siteLookup; } /** * @return IContextSource */ private function getContext() { - $title = $this->getMockBuilder( Title::class ) - ->disableOriginalConstructor() - ->getMock(); + $title = $this->getMock( Title::class ); $title->expects( $this->any() ) ->method( 'exists' ) @@ -167,7 +150,6 @@ $context = new RequestContext(); $context->setTitle( $title ); - $context->setLanguage( 'en' ); return $context; -- To view, visit https://gerrit.wikimedia.org/r/330194 To unsubscribe, visit https://gerrit.wikimedia.org/r/settings Gerrit-MessageType: newchange Gerrit-Change-Id: Ie975283a564c2493842bdc7fec13f0d28f5122b8 Gerrit-PatchSet: 1 Gerrit-Project: mediawiki/extensions/Wikibase Gerrit-Branch: master Gerrit-Owner: Thiemo Mättig (WMDE) <thiemo.maet...@wikimedia.de> _______________________________________________ MediaWiki-commits mailing list MediaWiki-commits@lists.wikimedia.org https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits